private void set(String uriString, String payload) { //System.out.println("payload\n" + payload); try { URI uri = new URI(uriString); CoapClient client = new CoapClient(uri); client.setEndpoint(endPoint); CoapResponse response = client.put(payload, MediaTypeRegistry.TEXT_PLAIN); if (response != null && response.isSuccess()) { //System.out.println("Yay"); } else { System.out.println("Sending payload to " + uriString + " failed!"); } client.shutdown(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
response = client.put("", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); response = client.put("", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); response = clientStimulus.put("CC21 at " + new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()), MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode()); response = clientStimulus.put("CC22 at " + new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()), MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode());
@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."); } }
@Test public void testAsyncPutIsNotAllowed() throws Exception { final CountDownLatch latch = new CountDownLatch(1); // Try a put and receive a METHOD_NOT_ALLOWED client.put(new TestHandler("Test 6") { @Override public void onLoad(CoapResponse response) { if (ResponseCode.METHOD_NOT_ALLOWED.equals(response.getCode())) { latch.countDown(); } } }, CONTENT_2, MediaTypeRegistry.TEXT_PLAIN); assertTrue(latch.await(1, TimeUnit.SECONDS)); }
@Override public Serializable doSend(Message message, MeasurementUnit measurementUnit) throws Exception { switch (method) { case GET: coapResponse = client.get(); break; case POST: coapResponse = client.post(message.getPayload().toString(), MediaTypeRegistry.TEXT_PLAIN); break; case PUT: coapResponse = client.put(message.getPayload().toString(), MediaTypeRegistry.TEXT_PLAIN); break; case DELETE: coapResponse = client.delete(); break; } return coapResponse.getResponseText(); }
case CoAPConstants.METHOD_PUT: byte[] bodyPut = exchange.getIn().getBody(byte[].class); response = client.put(bodyPut, mediaType); break; case CoAPConstants.METHOD_PING:
response = client.put(getLargeRequestPayload(), MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode()); System.out.println(response.getResponseText());
@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."); } }
ResponseCode code6 = client.put(CONTENT_4, MediaTypeRegistry.TEXT_PLAIN).getCode(); Assert.assertEquals(ResponseCode.METHOD_NOT_ALLOWED, code6);