@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (Exception e) { LOG.error("Exception while handling a request on the /bs resource", e); exchange.sendResponse(new Response(ResponseCode.INTERNAL_SERVER_ERROR)); } }
@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (Exception e) { LOG.error("Exception while handling a request on the /rd resource", e); // unexpected error, we should sent something like a INTERNAL_SERVER_ERROR. // but it would not be LWM2M compliant. so BAD_REQUEST for now... exchange.sendResponse(new Response(ResponseCode.BAD_REQUEST)); } }
/** * Send an error response on the given exchange. * * @param code * @param message * @param exchange */ protected void createAndSendResponse(ResponseCode code, String message, Exchange exchange) { Response response = new Response(code); response.setPayload(message); exchange.sendResponse(response); }
@Override public void handleRequest(Exchange exchange) { Response response = new Response(ResponseCode.CONTENT); response.setPayload("hello world"); exchange.sendResponse(response); }
/** * Send an error response on the given exchange. * * @param code * @param message * @param exchange */ protected void createAndSendResponse(ResponseCode code, String message, Exchange exchange) { Response response = new Response(code); response.setPayload(message); exchange.sendResponse(response); }
@Override public void handleRequest(Exchange exchange) { exchange.sendAccept(); Response response = forwardRequest(exchange.getRequest()); exchange.sendResponse(response); }
@Override public void handleRequest(Exchange exchange) { exchange.sendAccept(); Response response = forwardRequest(exchange.getRequest()); exchange.sendResponse(response); }
@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (InvalidRequestException e) { LOG.debug("InvalidRequestException while handling request({}) on the /rd resource", exchange.getRequest(), e); Response response = new Response(ResponseCode.BAD_REQUEST); response.setPayload(e.getMessage()); exchange.sendResponse(response); } catch (RuntimeException e) { LOG.error("Exception while handling request({}) on the /rd resource", exchange.getRequest(), e); exchange.sendResponse(new Response(ResponseCode.INTERNAL_SERVER_ERROR)); } }
@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (InvalidRequestException e) { if (LOG.isDebugEnabled()) { LOG.debug(String.format("InvalidRequestException while handling request(%s) on the %s resource", exchange.getRequest(), getURI()), e); } Response response = new Response(ResponseCode.BAD_REQUEST); response.setPayload(e.getMessage()); exchange.sendResponse(response); } catch (Exception e) { LOG.error(String.format("Exception while handling request(%s) on the %s resource", exchange.getRequest(), getURI()), e); exchange.sendResponse(new Response(ResponseCode.INTERNAL_SERVER_ERROR)); } }
/** * Remove all observe relations to CoAP clients and notify them that the * observe relation has been canceled. * * @param code * the error code why the relation was terminated * (e.g., 4.04 after deletion) */ public void clearAndNotifyObserveRelations(ResponseCode code) { /* * draft-ietf-core-observe-08, chapter 3.2 Notification states: * In the event that the resource changes in a way that would cause * a normal GET request at that time to return a non-2.xx response * (for example, when the resource is deleted), the server sends a * notification with a matching response code and removes the client * from the list of observers. * This method is called, when the resource is deleted. */ for (ObserveRelation relation:observeRelations) { relation.cancel(); relation.getExchange().sendResponse(new Response(code)); } }
/** * Remove all observe relations to CoAP clients and notify them that the * observe relation has been canceled. * * @param code * the error code why the relation was terminated * (e.g., 4.04 after deletion) */ public void clearAndNotifyObserveRelations(ResponseCode code) { /* * draft-ietf-core-observe-08, chapter 3.2 Notification states: * In the event that the resource changes in a way that would cause * a normal GET request at that time to return a non-2.xx response * (for example, when the resource is deleted), the server sends a * notification with a matching response code and removes the client * from the list of observers. * This method is called, when the resource is deleted. */ for (ObserveRelation relation:observeRelations) { relation.cancel(); relation.getExchange().sendResponse(new Response(code)); } }
@Override public void deliverRequest(Exchange exchange) { System.out.println("server received request"); exchange.sendAccept(); try { Thread.sleep(500); } catch (Exception e) {} Response response = new Response(ResponseCode.CONTENT); response.setConfirmable(false); response.setPayload(SERVER_RESPONSE); exchange.sendResponse(response); } @Override
@Override public void deliverRequest(final Exchange exchange) { final Request request = exchange.getRequest(); final List<String> path = request.getOptions().getUriPath(); final Code code = request.getCode(); final Resource resource = findResource(path, code); if (resource != null) { checkForObserveOption(exchange, resource); // Get the executor and let it process the request final Executor executor = resource.getExecutor(); if (executor != null) { executor.execute(new Runnable() { @Override public void run() { resource.handleRequest(exchange); } }); } else { resource.handleRequest(exchange); } } else { LOGGER.info("Did not find resource " + path.toString()); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND)); } }
@Override public void deliverRequest(final Exchange exchange) { final Request request = exchange.getRequest(); final List<String> path = request.getOptions().getUriPath(); final Code code = request.getCode(); final Resource resource = findResource(path, code); if (resource != null) { checkForObserveOption(exchange, resource); // Get the executor and let it process the request final Executor executor = resource.getExecutor(); if (executor != null) { executor.execute(new Runnable() { @Override public void run() { resource.handleRequest(exchange); } }); } else { resource.handleRequest(exchange); } } else { LOGGER.info("Did not find resource " + path.toString()); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND)); } }
@Override public void deliverRequest(final Exchange exchange) { Request request = exchange.getRequest(); List<String> path = request.getOptions().getUriPath(); final Resource resource = findResource(path); if (resource != null) { checkForObserveOption(exchange, resource); // Get the executor and let it process the request Executor executor = resource.getExecutor(); if (executor != null) { exchange.setCustomExecutor(); executor.execute(new Runnable() { public void run() { resource.handleRequest(exchange); } }); } else { resource.handleRequest(exchange); } } else { LOGGER.info("Did not find resource " + path.toString() + " requested by " + request.getSource()+":"+request.getSourcePort()); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND)); } }
/** * Respond with the specified response. * @param response the response */ public void respond(Response response) { if (response == null) throw new NullPointerException(); // set the response options configured through the CoapExchange API if (locationPath != null) response.getOptions().setLocationPath(locationPath); if (locationQuery != null) response.getOptions().setLocationQuery(locationQuery); if (maxAge != 60) response.getOptions().setMaxAge(maxAge); if (eTag != null) { response.getOptions().clearETags(); response.getOptions().addETag(eTag); } resource.checkObserveRelation(exchange, response); exchange.sendResponse(response); }
/** * Respond with the specified response. * @param response the response */ public void respond(Response response) { if (response == null) throw new NullPointerException(); // set the response options configured through the CoapExchange API if (locationPath != null) response.getOptions().setLocationPath(locationPath); if (locationQuery != null) response.getOptions().setLocationQuery(locationQuery); if (maxAge != 60) response.getOptions().setMaxAge(maxAge); if (eTag != null) { response.getOptions().clearETags(); response.getOptions().addETag(eTag); } resource.checkObserveRelation(exchange, response); exchange.sendResponse(response); }
LOGGER.log(Level.INFO, "Did not find resource {0} requested by {1}:{2}", new Object[]{path, request.getSource(), request.getSourcePort()}); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND));
exchange.sendResponse(response); return; } else {
LOGGER.log(Level.INFO, "Did not find resource {0} requested by {1}:{2}", new Object[]{request.getOptions().getUriPath(), request.getSource(), request.getSourcePort()}); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND));