CoapClient client = new CoapClient(uri + "/large"); CoapResponse response; client.useEarlyNegotiation(64); response = client.get(); System.out.println(response.getCode()); System.out.println(response.getResponseText()); client.useLateNegotiation(); response = client.get(); System.out.println(response.getCode()); System.out.println(response.getResponseText()); client.setURI(uri + "/large-update"); response = client.put(getLargeRequestPayload(), MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode()); System.out.println(response.getResponseText()); client.setURI(uri + "/large-create"); response = client.post(getLargeRequestPayload(), MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode() + " - " + response.getOptions().getLocationString()); System.out.println(response.getResponseText()); client.setURI(uri + response.getOptions().getLocationString());
CoapClient client = new CoapClient(uri + "/obs"); CoapObserveRelation relation1 = client.observe( new CoapHandler() { @Override public void onLoad(CoapResponse response) { client.setURI(uri + "/obs-non").useNONs(); CoapObserveRelation relation2 = client.observe( new CoapHandler() { @Override public void onLoad(CoapResponse response) { client.setURI(uri + "/obs").useCONs(); CoapObserveRelation relation4 = client.observeAndWait( new CoapHandler() { @Override public void onLoad(CoapResponse response) { try { Thread.sleep(6*1000); } catch (InterruptedException e) { } System.out.println("---------------\nReboot Server"); CoapClient clientStimulus = new CoapClient(uri + "/obs-reset"); clientStimulus.post("sesame", MediaTypeRegistry.TEXT_PLAIN); try { Thread.sleep((timeout+6)*1000); } catch (InterruptedException e) { } relation4.proactiveCancel(); try { Thread.sleep(2*1000); } catch (InterruptedException e) { } client.setURI(uri + "/obs-large"); CoapObserveRelation relation13 = client.observe(
@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."); } }
public OdlOnem2mCoapClient(String uri) { coapClient = new CoapClient(uri); coapClient.useCONs(); } public CoapResponse sendRequest(OdlOnem2mCoapRequestPrimitive onem2mRequest) {
@Override public void doInit(Properties messageAttributes) throws PerfCakeException { client = new CoapClient(safeGetTarget(messageAttributes)); switch (requestType) { case CONFIRMABLE: client.useCONs(); break; case NON_CONFIRMABLE: client.useNONs(); break; } }
/** * 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); }
CoapClient client = new CoapClient(uri + "/test"); response = client.get(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); response = client.delete(); 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 = client.post("non-empty", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); client.useNONs(); response = client.get(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); response = client.delete(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); response = client.put("", MediaTypeRegistry.TEXT_PLAIN);
CoapClient client = new CoapClient(uri).useExecutor(); String resp1 = client.get().getResponseText(); Assert.assertEquals(CONTENT_1, resp1); String resp2 = client.get().getResponseText(); Assert.assertEquals(CONTENT_1, resp2); String resp3 = client.post(CONTENT_2, MediaTypeRegistry.TEXT_PLAIN).getResponseText(); Assert.assertEquals(CONTENT_1, resp3); String resp4 = client.get().getResponseText(); Assert.assertEquals(CONTENT_2, resp4); CoapObserveRelation obs1 = client.observeAndWait( new CoapHandler() { @Override public void onLoad(CoapResponse response) { String resp5 = client.post(CONTENT_3, MediaTypeRegistry.TEXT_PLAIN).getResponseText(); Assert.assertEquals(CONTENT_2, resp5); ResponseCode code6 = client.put(CONTENT_4, MediaTypeRegistry.TEXT_PLAIN).getCode(); Assert.assertEquals(ResponseCode.METHOD_NOT_ALLOWED, code6); String resp7 = client.post(CONTENT_4, MediaTypeRegistry.TEXT_PLAIN).getResponseText(); Assert.assertEquals(CONTENT_3, resp7); .path(TARGET).query(QUERY_UPPER_CASE).create().get().getResponseText(); Assert.assertEquals(CONTENT_4.toUpperCase(), resp8);
CoapClient client = new CoapClient(new URI(uriString)); client.setEndpoint(endPoint); CoapHandler handler = new CoapHandler() { CoapObserveRelation relation = client.observe(handler); watching.add(relation);
@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(); }
CoapClient client = new CoapClient (uri); CoapResponse response = client.get ();
/** * Test case for CoapClient.observeAndWait(Request request, CoapHandler * handler) exception handling. * * @throws Exception */ @Test(expected = IllegalArgumentException.class) public void testObserveAndWaitExceptionHandling() throws Exception { CoapClient client = new CoapClient(uriX); Request request = Request.newGet().setURI(uriX); @SuppressWarnings("unused") CoapObserveRelation rel = client.observeAndWait(request, new CoapHandler() { @Override public void onLoad(CoapResponse response) { } @Override public void onError() { } }); }
public void test() { CoapResponse response = null; try { URI uri = new URI(SERVER_URI); CoapClient client = new CoapClient(uri); client.setEndpoint(new CoapEndpoint(dtlsConnector, NetworkConfig.getStandard())); response = client.get(); } catch (URISyntaxException e) { System.err.println("Invalid URI: " + e.getMessage()); System.exit(-1); } if (response != null) { System.out.println(response.getCode()); System.out.println(response.getOptions()); System.out.println(response.getResponseText()); System.out.println("\nADVANCED\n"); System.out.println(Utils.prettyPrint(response)); } else { System.out.println("No response received."); } }
/** * Create a CoAP client for the given URL. * * @param relativeUrl * @return */ private static CoapClient createClientFor(String relativeUrl) { return new CoapClient("coap://" + COAP_SERVER + ":" + COAP_PORT + "/" + relativeUrl); }
public void run(){ client = new CoapClient(serverURI); while(isActive()){ if (System.currentTimeMillis() / 1000 > nextExpectedMessage){ client.observe(this); } try{ Thread.sleep(60000); }catch(Exception ex){} } }
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(); } }
CoapClient client = new CoapClient(uri); client.setEndpoint(endPoint); CoapResponse response = client.get(); if (response == null) { System.out.println("Connection to Gateway timed out, please check ip address or increase the ACK_TIMEOUT in the Californium.properties file"); client.shutdown(); } catch (URISyntaxException e) { CoapClient client = new CoapClient(uri); client.setEndpoint(endPoint); CoapResponse response = client.get(); if (response == null) { System.out.println("Connection to Gateway timed out, please check ip address or increase the ACK_TIMEOUT in the Californium.properties file"); client.shutdown(); } catch (URISyntaxException e) { CoapClient client = new CoapClient(sceneURI); client.setEndpoint(endPoint); CoapResponse responseRooms = client.get(); if (responseRooms == null) { System.out.println("Connection to Gateway timed out, please check ip address or increase the ACK_TIMEOUT in the Californium.properties file"); client = new CoapClient(moodUri); client.setEndpoint(endPoint); CoapResponse responseMoods = client.get(); if (responseMoods == null) { System.out.println("Connection to Gateway timed out, please check ip address or increase the ACK_TIMEOUT in the Californium.properties file");
CoapClient client = new CoapClient(uri); Set<WebLink> links; links = client.discover(); for (WebLink link : links) { System.out.println(link); links = client.discover("rt=Type1"); for (WebLink link : links) { System.out.println(link); links = client.discover("rt=*"); for (WebLink link : links) { System.out.println(link); links = client.discover("rt=Type2"); for (WebLink link : links) { System.out.println(link); links = client.discover("if=If*"); for (WebLink link : links) { System.out.println(link); links = client.discover("sz=*"); for (WebLink link : links) { System.out.println(link); links = client.discover("href=/link1"); for (WebLink link : links) {
/** * Creates a {@link CoapClient} that uses the same executor as this resource * and one of the endpoints that this resource belongs to. If no executor is * defined by this resource or any parent, the client will not have an * executor (it still works). If this resource is not yet added to a server * or the server has no endpoints, the client has no specific endpoint and * will use Californium's default endpoint. * * @return the CoAP client */ public CoapClient createClient() { CoapClient client = new CoapClient(); client.setExecutor(getExecutor()); List<Endpoint> endpoints = getEndpoints(); if (!endpoints.isEmpty()) client.setEndpoint(endpoints.get(0)); return client; }
@Test public void testSynchronousPing() throws Exception { final AtomicBoolean sent = new AtomicBoolean(); CoapEndpoint clientEndpoint = new CoapEndpoint(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); clientEndpoint.addInterceptor(new MessageInterceptorAdapter() { @Override public void sendRequest(Request request) { sent.set(true); } }); String uri = TestTools.getUri(serverEndpoint, TARGET); CoapClient client = new CoapClient(uri).useExecutor(); client.setEndpoint(clientEndpoint); // Check that we get the right content when calling get() boolean ping = client.ping(); Assert.assertTrue(ping); Assert.assertTrue("Ping not sent using provided endpoint", sent.get()); }