public void handleRequest(final Request request) {
// if (Bench_Help.DO_LOG)
LOGGER.info("ProxyEndpoint handles request "+request);
Exchange exchange = new Exchange(request, Origin.REMOTE) {
@Override public void sendResponse(Response response) {
// Redirect the response to the HttpStack instead of a normal
// CoAP endpoint.
// TODO: When we change endpoint to be an interface, we can
// redirect the responses a little more elegantly.
try {
request.setResponse(response);
responseProduced(request, response);
httpStack.doSendResponse(request, response);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Exception while responding to Http request", e);
}
}
};
exchange.setRequest(request);
Response response = null;
// ignore the request if it is reset or acknowledge
// check if the proxy-uri is defined
if (request.getType() != Type.RST && request.getType() != Type.ACK
&& request.getOptions().hasProxyURI()) {
// get the response from the cache
response = cacheResource.getResponse(request);
// if (Bench_Help.DO_LOG)
LOGGER.info("Cache returned "+response);
// update statistics
statsResource.updateStatistics(request, response != null);
}
// check if the response is present in the cache
if (response != null) {
// link the retrieved response with the request to set the
// parameters request-specific (i.e., token, id, etc)
exchange.sendResponse(response);
return;
} else {
// edit the request to be correctly forwarded if the proxy-uri is
// set
if (request.getOptions().hasProxyURI()) {
try {
manageProxyUriRequest(request);
// if (Bench_Help.DO_LOG)
LOGGER.info("after manageProxyUriRequest: "+request);
} catch (URISyntaxException e) {
LOGGER.warning(String.format("Proxy-uri malformed: %s", request.getOptions().getProxyURI()));
exchange.sendResponse(new Response(ResponseCode.BAD_OPTION));
}
}
// handle the request as usual
proxyCoapResolver.forwardRequest(exchange);