public OdlOnem2mCoapRequestPrimitive build() { onem2mRequest.optionsSet.addUriQuery(onem2mRequest.uriQueryString); // M3 onem2mRequest.optionsSet.addUriQuery(onem2mRequest.uriQueryString); onem2mRequest.coapRequest.setOptions(onem2mRequest.optionsSet); return (onem2mRequest); } }
/** * Sets the destination address and port and options from a given URI. * <p> * This method sets the <em>destination</em> to the IP address that the host part of the URI * has been resolved to and then delegates to the {@link #setOptions(URI)} method in order * to populate the request's options. * * @param uri The target URI. * @return This request for command chaining. * @throws NullPointerException if the URI is {@code null}. * @throws IllegalArgumentException if the URI contains a non-resolvable host name, an * unsupported scheme or a fragment. */ public Request setURI(final URI uri) { if (uri == null) { throw new NullPointerException("URI must not be null"); } final String host = uri.getHost() == null ? "localhost" : uri.getHost(); try { InetAddress destAddress = InetAddress.getByName(host); setDestination(destAddress); return setOptions(new URI(uri.getScheme(), null, host, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment())); } catch (UnknownHostException e) { throw new IllegalArgumentException("cannot resolve host name: " + host); } catch (URISyntaxException e) { // should not happen because we are creating the URI from an existing URI object LOGGER.log(Level.WARNING, "cannot set URI on request", e); throw new IllegalArgumentException(e); } }
/** * Sets the destination address and port and options from a given URI. * <p> * This method sets the <em>destination</em> to the IP address that the host part of the URI * has been resolved to and then delegates to the {@link #setOptions(URI)} method in order * to populate the request's options. * * @param uri The target URI. * @return This request for command chaining. * @throws NullPointerException if the URI is {@code null}. * @throws IllegalArgumentException if the URI contains a non-resolvable host name, an * unsupported scheme or a fragment. */ public Request setURI(final URI uri) { if (uri == null) { throw new NullPointerException("URI must not be null"); } final String host = uri.getHost() == null ? "localhost" : uri.getHost(); try { InetAddress destAddress = InetAddress.getByName(host); setDestination(destAddress); return setOptions(new URI(uri.getScheme(), null, host, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment())); } catch (UnknownHostException e) { throw new IllegalArgumentException("cannot resolve host name: " + host); } catch (URISyntaxException e) { // should not happen because we are creating the URI from an existing URI object LOGGER.log(Level.WARNING, "cannot set URI on request", e); throw new IllegalArgumentException(e); } }
@Test(expected = IllegalStateException.class) public void testSetOptionsFailsIfDestinationIsNotSet() { Request.newGet().setOptions(URI.create("coap://iot.eclipse.org")); }
@Test public void testSetOptionsSetsUriHostOption() { Request req = Request.newGet(); req.setDestination(InetAddress.getLoopbackAddress()); req.setOptions(URI.create("coap://iot.eclipse.org")); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_PORT)); assertThat(req.getOptions().getUriHost(), is("iot.eclipse.org")); }
request.setOptions(options); return request;
options.clearUriPath(); options.clearUriQuery(); outgoingRequest.setOptions(options);
options.clearUriPath(); options.clearUriQuery(); outgoingRequest.setOptions(options);
refresh.setOptions(request.getOptions());
refresh.setOptions(request.getOptions());
/** * Verifies that the URI examples from <a href="https://tools.ietf.org/html/rfc7252#section-6.3"> * RFC 7252, Section 6.3</a> result in the same option values. * @throws URISyntaxException */ @Test public void testSetOptionsCompliesWithRfcExample() throws URISyntaxException { String[] exampleUris = new String[]{ "coap://example.com:5683/~sensors/temp.xml", "coap://EXAMPLE.com/%7Esensors/temp.xml", "coap://EXAMPLE.com:/%7esensors/temp.xml" }; for (String uriString : exampleUris) { URI uri = new URI(uriString); Request req = Request.newGet(); // explicitly set destination address so that we do not rely on working DNS req.setDestination(InetAddress.getLoopbackAddress()); req.setOptions(uri); assertThat(req.getOptions().getUriHost(), is("example.com")); assertThat(req.getDestinationPort(), is(5683)); assertThat(req.getOptions().getUriPort(), is(nullValue())); assertThat(req.getOptions().getUriPathString(), is("~sensors/temp.xml")); } }
/** * Send request with option "cancel observe" (GET with Observe=1). */ private void sendCancelObserve() { Request request = this.request; Request cancel = Request.newGet(); cancel.setDestination(request.getDestination()); cancel.setDestinationPort(request.getDestinationPort()); // use same Token cancel.setToken(request.getToken()); // copy options, but set Observe to cancel cancel.setOptions(request.getOptions()); cancel.setObserveCancel(); // dispatch final response to the same message observers for (MessageObserver mo: request.getMessageObservers()) { cancel.addMessageObserver(mo); } endpoint.sendRequest(cancel); }
private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) { int num = status.getCurrentNum(); int szx = status.getCurrentSzx(); Request block = new Request(request.getCode()); // do not enforce CON, since NON could make sense over SMS or similar transports block.setType(request.getType()); block.setDestination(request.getDestination()); block.setDestinationPort(request.getDestinationPort()); // copy options block.setOptions(new OptionSet(request.getOptions())); // copy message observers so that a failing blockwise request also notifies observers registered with // the original request block.addMessageObservers(request.getMessageObservers()); int currentSize = 1 << (4 + szx); int from = num * currentSize; int to = Math.min((num + 1) * currentSize, request.getPayloadSize()); int length = to - from; byte[] blockPayload = new byte[length]; System.arraycopy(request.getPayload(), from, blockPayload, 0, length); block.setPayload(blockPayload); boolean m = (to < request.getPayloadSize()); block.getOptions().setBlock1(szx, m, num); status.setComplete(!m); return block; }
private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) { int num = status.getCurrentNum(); int szx = status.getCurrentSzx(); Request block = new Request(request.getCode()); // do not enforce CON, since NON could make sense over SMS or similar transports block.setType(request.getType()); block.setDestination(request.getDestination()); block.setDestinationPort(request.getDestinationPort()); // copy options block.setOptions(new OptionSet(request.getOptions())); // copy message observers so that a failing blockwise request also notifies observers registered with // the original request block.addMessageObservers(request.getMessageObservers()); int currentSize = 1 << (4 + szx); int from = num * currentSize; int to = Math.min((num + 1) * currentSize, request.getPayloadSize()); int length = to - from; byte[] blockPayload = new byte[length]; System.arraycopy(request.getPayload(), from, blockPayload, 0, length); block.setPayload(blockPayload); boolean m = (to < request.getPayloadSize()); block.getOptions().setBlock1(szx, m, num); status.setComplete(!m); return block; }
/** * Send request with option "cancel observe" (GET with Observe=1). */ private void sendCancelObserve() { Request request = this.request; Request cancel = Request.newGet(); cancel.setDestination(request.getDestination()); cancel.setDestinationPort(request.getDestinationPort()); // use same Token cancel.setToken(request.getToken()); // copy options, but set Observe to cancel cancel.setOptions(request.getOptions()); cancel.setObserveCancel(); // dispatch final response to the same message observers for (MessageObserver mo : request.getMessageObservers()) { cancel.addMessageObserver(mo); } endpoint.sendRequest(cancel); }
block.setOptions(new OptionSet(request.getOptions()));
block.setOptions(new OptionSet(request.getOptions()));