@Override public void sendRequest(Exchange exchange, Request request) { if (request.getToken() == null) request.setToken(createNewToken()); // if (exchange.getCurrentRequest().getToken() == null) // throw new NullPointerException("Sending request's token cannot be null, use byte[0] for // empty tokens"); super.sendRequest(exchange, request); }
/* * Sends a GET request to itself */ public static void selfTest() { try { Request request = Request.newGet(); request.setURI("localhost:5683/benchmark"); request.send(); Response response = request.waitForResponse(1000); System.out.println("received " + response); } catch (Exception e) { e.printStackTrace(); } }
@Override public void execute(Request request) throws IOException { if (request != null) { request.setURI(this.uri); // execute the request request.execute(); } }
/** * Updates the endpoint parameters from POST and PUT requests. * * @param request A POST or PUT request with a {?et,lt,con} URI Template query and a Link Format * payload. */ public boolean setParameters(Request request) { LinkAttribute attr; String newEndpointType = ""; int newLifeTime = NetworkConfig.getStandard().getInt("RD_DEFAULT_LIFETIME"); String newContext = ""; /* * get lifetime from option query - only for PUT request. */ List<String> query = request.getOptions().getURIQueries(); for (String q : query) { // FIXME Do not use Link attributes for URI template variables attr = LinkAttribute.parse(q); if (attr.getName().equals(LinkFormat.END_POINT_TYPE)) { newEndpointType = attr.getValue(); } if (attr.getName().equals(LinkFormat.LIFE_TIME)) { newLifeTime = attr.getIntValue(); if (newLifeTime < 60) { LOGGER.warning("Enforcing minimal RD lifetime of 60 seconds (was " + newLifeTime + ")"); newLifeTime = 60; } } if (attr.getName().equals(LinkFormat.CONTEXT)) { newContext = attr.getValue(); } } setEndpointType(newEndpointType); setLifeTime(newLifeTime); // TODO check with draft authors if update should be atomic if (newContext.equals("")) { context = "coap://" + request.getSource() + ":" + request.getSourcePort(); } else { Request checkRequest = Request.newGet(); try { checkRequest.setURI(context); } catch (Exception e) { LOGGER.warning(e.toString()); return false; } } return updateEndpointResources(request.getPayloadString()); }
@Override public void run() { LOGGER.info("Validating endpoint: " + getContext()); Request validationRequest = Request.newGet(); validationRequest.setURI(getContext() + "/.well-known/core"); if (etag != null) { validationRequest.getOptions().addETag(etag); } Response response = null; try { validationRequest.send(); response = validationRequest.waitForResponse(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (response == null) { delete(); } else if (response.getCode() == ResponseCode.VALID) { LOGGER.fine("Resources up-to-date: " + getContext()); } else if (response.getCode() == ResponseCode.CONTENT) { List<byte[]> etags = response.getOptions().getETags(); if (!etags.isEmpty()) { etag = etags.get(0); } updateEndpointResources(response.getPayloadString()); setLifeTime(lifeTime); LOGGER.fine("Updated Resources: " + getContext()); } }