/** * Determines which CoAP request method to use based on the content of the target * request URI, the message body or value from the CamelCoapMethod header. * * @param exchange the exchange * @param client the CoAP client * @return the CoAP request method */ public static String getDefaultMethod(Exchange exchange, CoapClient client) { String method = exchange.getIn().getHeader(CoAPConstants.COAP_METHOD, String.class); if (method == null) { Object body = exchange.getIn().getBody(); if (body == null || client.getURI().contains("?")) { method = CoAPConstants.METHOD_GET; } else { method = CoAPConstants.METHOD_POST; } } return method; }
@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."); } }
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)); } }
private static void testBlockwise(final CoapClient client, final Mode mode) { LOGGER.log(Level.FINE, "Test blockwise POST to {0}", client.getURI()); currentRequestText = LONG_REQUEST; String currentResponseText = LONG_RESPONSE; resource.setResponse(currentResponseText, mode); CoapResponse response = client.post(currentRequestText, MediaTypeRegistry.TEXT_PLAIN); assertThatResponseContainsValue(response, currentResponseText); }
@Override public void deliverSystemCommand(IDeviceNestingContext nested, IDeviceAssignment assignment, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; getLogger().debug(parameters.toString()); getLogger().debug( "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) { getLogger().info("Response from delivering system command: " + response.getResponseText()); } else { getLogger().info("No response from delivering system command."); } }
@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."); } }