private void assignClientUriIfEmpty(Request request) { // request.getUri() is a computed getter and never returns null so checking destination if (request.getDestination() == null) { request.setURI(uri); } }
/** * Sends a GET request and blocks until the response is available. * * @return the CoAP response */ public CoapResponse get() { return synchronous(newGet().setURI(uri)); }
/** * Sends a GET request and invokes the specified handler when a response * arrives. * * @param handler the Response handler */ public void get(CoapHandler handler) { asynchronous(newGet().setURI(uri), handler); }
/** * Sends a DELETE request and waits for the response. * * @return the CoAP response */ public CoapResponse delete() { return synchronous(newDelete().setURI(uri)); }
/** * Sends a DELETE request and invokes the specified handler when a response * arrives. * * @param handler the response handler */ public void delete(CoapHandler handler) { asynchronous(newDelete().setURI(uri), handler); }
/** * Sends an observe request and invokes the specified handler each time * a notification arrives. * * @param handler the Response handler * @return the CoAP observe relation */ public CoapObserveRelation observe(CoapHandler handler) { Request request = newGet().setURI(uri).setObserve(); return observe(request, handler); }
/** * Sends a PUT request with payload and required Content-Format and blocks * until the response is available. * * @param payload the payload * @param format the Content-Format * @return the CoAP response */ public CoapResponse put(byte[] payload, int format) { return synchronous(format(newPut().setURI(uri).setPayload(payload), format)); }
/** * Sends an observe request with the specified Accept option and invokes the * specified handler each time a notification arrives. * * @param handler the Response handler * @param accept the Accept option * @return the CoAP observe relation */ public CoapObserveRelation observe(CoapHandler handler, int accept) { Request request = newGet().setURI(uri).setObserve(); return observe(accept(request, accept), handler); }
/** * Sends a PUT request with the specified payload and the specified content * format and invokes the specified handler when a response arrives. * * @param handler the Response handler * @param payload the payload * @param format the Content-Format */ public void put(CoapHandler handler, String payload, int format) { asynchronous(format(newPut().setURI(uri).setPayload(payload), format), handler); }
/** * Sends a POST request with the specified payload and the specified content * format and invokes the specified handler when a response arrives. * * @param handler the Response handler * @param payload the payload * @param format the Content-Format */ public void post(CoapHandler handler, String payload, int format) { asynchronous(format(newPost().setURI(uri).setPayload(payload), format), handler); }
/** * Sends a PUT request with the specified payload and the specified content * format and invokes the specified handler when a response arrives. * * @param handler the Response handler * @param payload the payload * @param format the Content-Format */ public void put(CoapHandler handler, byte[] payload, int format) { asynchronous(format(newPut().setURI(uri).setPayload(payload), format), handler); }
public static Request createRequest(Code code, String path, LockstepEndpoint server) throws Exception { Request request = new Request(code); String uri = String.format("coap://%s:%d/%s", server.getAddress().getHostAddress(), server.getPort(), path); request.setURI(uri); return request; }
/** * Sends a PUT request with the If-None-Match option set and blocks until * the response is available. * * @param payload the payload string * @param format the Content-Format * @return the CoAP response */ public CoapResponse putIfNoneMatch(String payload, int format) { return synchronous(ifNoneMatch(format(newPut().setURI(uri).setPayload(payload), format))); }
/** * * @param handler the Response handler * @param payload the payload * @param format the Content-Format * @param etags the ETags for the If-Match option */ public void putIfMatch(CoapHandler handler, String payload, int format, byte[] ... etags) { asynchronous(ifMatch(format(newPut().setURI(uri).setPayload(payload), format), etags), handler); }
@Test public void testSetURISetsDestination() { InetSocketAddress dest = InetSocketAddress.createUnresolved("192.168.0.1", 12000); Request req = Request.newGet().setURI("coap://192.168.0.1:12000"); assertThat(req.getDestination().getHostAddress(), is(dest.getHostString())); assertThat(req.getDestinationPort(), is(dest.getPort())); }
@Test public void testSetURISetsDestinationPortBasedOnUriScheme() { Request req = Request.newGet().setURI("coap://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_PORT)); req = Request.newGet().setURI("coaps://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_SECURE_PORT)); req = Request.newGet().setURI("coap+tcp://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_PORT)); req = Request.newGet().setURI("coaps+tcp://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_SECURE_PORT)); }
@Test public void testSetURIDoesNotSetUriHostOptionForIp6Address() { // use www.google.com's IPv6 address Request req = Request.newGet().setURI("coap://[2a00:1450:4001:817::2003]"); assertNull(req.getOptions().getUriHost()); }
@Test public void testDiscoveryFiltering() { final String expectedTree = "</sensors/light>;if=\"sensor\";rt=\"light-lux\""; Request request = Request.newGet(); request.setURI("coap://localhost/.well-known/core?rt=light-lux"); DiscoveryResource discovery = new DiscoveryResource(root); String serialized = discovery.discoverTree(root, request.getOptions().getUriQuery()); System.out.println(serialized); Assert.assertEquals(expectedTree, serialized); }
@Test public void testDiscoveryMultiFiltering() { Request request = Request.newGet(); request.setURI("coap://localhost/.well-known/core?rt=light-lux&rt=temprature-cel"); Exchange exchange = new Exchange(request, Origin.REMOTE); exchange.setRequest(request); exchange.setEndpoint(new DummyEndpoint()); DiscoveryResource discovery = new DiscoveryResource(root); discovery.handleRequest(exchange); System.out.println(exchange.getResponse().getPayloadString()); Assert.assertEquals(ResponseCode.BAD_OPTION, exchange.getResponse().getCode()); }
@Test public void testAdvancedUsesUriFromRequest() throws Exception { String nonExistingUri = TestTools.getUri(serverEndpoint, "non-existing"); CoapClient client = new CoapClient(nonExistingUri).useExecutor(); Request request = new Request(Code.GET, Type.CON); String uri = TestTools.getUri(serverEndpoint, TARGET); request.setURI(uri); CoapResponse resp = client.advanced(request); Assert.assertEquals(Type.ACK, resp.advanced().getType()); Assert.assertEquals(CONTENT_1, resp.getResponseText()); }