@Override public void onLoad(CoapResponse response) { String content = response.getResponseText(); System.out.println("-CO01----------"); System.out.println(content); }
@Override public void onLoad(CoapResponse response) { String content = response.getResponseText(); System.out.println("-CO02----------"); System.out.println(content); }
@Override public void onLoad(CoapResponse response) { String content = response.getResponseText(); System.out.println("-CO04----------"); System.out.println(content); }
@Override public void onLoad(CoapResponse response) { String content = response.getResponseText(); System.out.println("-CO13----------"); System.out.println(content); }
@Override public void onLoad(CoapResponse response) { String content = response.getResponseText(); System.out.println("-CO14----------"); System.out.println(content); }
@Override public void onLoad(CoapResponse response) { if (CONTENT_1.equals(response.getResponseText())) { latch.countDown(); } } });
@Override public void onLoad(CoapResponse response) { if (CONTENT_1.equals(response.getResponseText())) { latch.countDown(); } } }, CONTENT_2, MediaTypeRegistry.TEXT_PLAIN);
@Override public void onLoad(CoapResponse response) { if (CONTENT_1.toUpperCase().equals(response.getResponseText())) { latch.countDown(); } } }
private static void assertThatResponseContainsValue(CoapResponse response, String expectedValue) { assertThat(response, is(notNullValue())); LOGGER.log(Level.FINE, "Client received response [{0}]", response.getResponseText()); assertThat(response.getResponseText(), is(expectedValue)); }
@Override public void deliver(IDeviceNestingContext nested, IDeviceAssignment assignment, IDeviceCommandExecution execution, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; LOGGER.info(parameters.toString()); LOGGER.info("Delivering command as " + parameters.getMethod().toUpperCase() + " to " + client.getURI()); if ("put".equalsIgnoreCase(parameters.getMethod())) { response = client.put(encoded, MediaTypeRegistry.APPLICATION_JSON); } else { response = client.post(encoded, MediaTypeRegistry.APPLICATION_JSON); } if (response != null) { LOGGER.info("Response from delivering command: " + response.getResponseText()); } else { LOGGER.info("No response from delivering command."); } }
@Override public void deliverSystemCommand(IDeviceNestingContext nested, IDeviceAssignment assignment, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; LOGGER.info(parameters.toString()); LOGGER.info("Delivering system command as " + parameters.getMethod().toUpperCase() + " to " + client.getURI()); if ("put".equalsIgnoreCase(parameters.getMethod())) { response = client.put(encoded, MediaTypeRegistry.APPLICATION_JSON); } else { response = client.post(encoded, MediaTypeRegistry.APPLICATION_JSON); } if (response != null) { LOGGER.info("Response from delivering system command: " + response.getResponseText()); } else { LOGGER.info("No response from delivering system command."); } }
@Override public void onLoad(final CoapResponse response) { String responseDescription = String.format("onLoad(%d) '%s'", onLoadCounter.incrementAndGet(), response.getResponseText()); System.out.println(responseDescription); if (response.getResponseText().startsWith(CONTENT_1) && response.advanced().getOptions().hasObserve()) { try { assertAwait("assert missing", barrier, 2000, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { } } } });
/** * Handle a CoAP response. * * @param response * @throws Exception */ private static void handleResponse(CoapResponse response) throws Exception { if (response != null) { System.out.println(response.getCode()); System.out.println(response.getOptions()); System.out.println(response.getResponseText()); } else { throw new Exception("Response was null."); } } }
private static void getResourceAndAssertPayload(final CoapClient client, final String expectedPayload) { System.out.println(String.format("doing a blockwise GET on: %s", client.getURI())); long start = System.currentTimeMillis(); CoapResponse response = client.get(); long end = System.currentTimeMillis(); assertThat("Blockwise GET timed out after " + (end - start) + "ms", response, is(notNullValue())); System.out.println(String.format("Received %d bytes after %dms", response.getPayload().length, end - start)); assertThat("Did not receive expected resource body", response.getResponseText(), is(expectedPayload)); } }
@Override public void onLoad(CoapResponse response) { notifications.incrementAndGet(); String payload = response.getResponseText(); Assert.assertEquals(expected, payload); Assert.assertTrue(response.advanced().getOptions().hasObserve()); } @Override public void onError() {
public Set<WebLink> discover(String query) { Request discover = Request.newGet(); discover.setURI(uri); // for scheme and authority, but then remove path and query discover.getOptions().clearUriPath().clearUriQuery().setUriPath("/.well-known/core"); if (query!=null) { discover.getOptions().setUriQuery(query); } CoapResponse links = synchronous(discover); // if no response, return null (e.g., timeout) if (links == null) { return null; } // check if Link Format if (links.getOptions().getContentFormat()!=MediaTypeRegistry.APPLICATION_LINK_FORMAT) return Collections.emptySet(); // parse and return return LinkFormat.parse(links.getResponseText()); }
@Override public void deliver(IDeviceNestingContext nested, IDeviceAssignment assignment, IDeviceCommandExecution execution, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; getLogger().debug(parameters.toString()); getLogger().debug("Delivering command as " + parameters.getMethod().toUpperCase() + " to " + client.getURI()); if ("put".equalsIgnoreCase(parameters.getMethod())) { response = client.put(encoded, MediaTypeRegistry.APPLICATION_JSON); } else { response = client.post(encoded, MediaTypeRegistry.APPLICATION_JSON); } if (response != null) { getLogger().info("Response from delivering command: " + response.getResponseText()); } else { getLogger().info("No response from delivering command."); } }
public Set<WebLink> discover(String query) { Request discover = newGet(); discover.setURI(uri); // for scheme and authority, but then remove path and query discover.getOptions().clearUriPath().clearUriQuery().setUriPath("/.well-known/core"); if (query!=null) { discover.getOptions().setUriQuery(query); } CoapResponse links = synchronous(discover); // if no response, return null (e.g., timeout) if (links == null) { return null; } // check if Link Format if (links.getOptions().getContentFormat()!=MediaTypeRegistry.APPLICATION_LINK_FORMAT) return Collections.emptySet(); // parse and return return LinkFormat.parse(links.getResponseText()); }
@Test public void testAdvancedUsesTypeFromRequest() throws Exception { String uri = TestTools.getUri(serverEndpoint, TARGET); CoapClient client = new CoapClient(uri).useExecutor(); // Set NONs but expecting CONs as specified in request client.useNONs(); Request request = new Request(Code.GET, Type.CON); CoapResponse resp = client.advanced(request); Assert.assertEquals(Type.ACK, resp.advanced().getType()); Assert.assertEquals(CONTENT_1, resp.getResponseText()); }
@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()); }