final Request incomingCoapRequest = request;
// check the invariant: the request must have the proxy-uri set
if (!incomingCoapRequest.getOptions().hasProxyURI()) {
LOGGER.warning("Proxy-uri option not set.");
return new Response(ResponseCode.BAD_OPTION);
}
// remove the fake uri-path // TODO: why? still necessary in new Cf?
incomingCoapRequest.getOptions().clearURIPaths();; // HACK
// get the proxy-uri set in the incoming coap request
URI proxyUri;
try {
String proxyUriString = URLDecoder.decode(
incomingCoapRequest.getOptions().getProxyURI(), "UTF-8");
proxyUri = new URI(proxyUriString);
} catch (UnsupportedEncodingException e) {
LOGGER.warning("Proxy-uri option malformed: " + e.getMessage());
return new Response(CoapTranslator.STATUS_FIELD_MALFORMED);
} catch (URISyntaxException e) {
LOGGER.warning("Proxy-uri option malformed: " + e.getMessage());
return new Response(CoapTranslator.STATUS_FIELD_MALFORMED);
}
// get the requested host, if the port is not specified, the constructor
// sets it to -1
HttpHost httpHost = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme());
HttpRequest httpRequest = null;
try {
// get the mapping to http for the incoming coap request
httpRequest = HttpTranslator.getHttpRequest(incomingCoapRequest);
LOGGER.finer("Outgoing http request: " + httpRequest.getRequestLine());
} catch (InvalidFieldException e) {
LOGGER.warning("Problems during the http/coap translation: " + e.getMessage());
return new Response(CoapTranslator.STATUS_FIELD_MALFORMED);
} catch (TranslationException e) {
LOGGER.warning("Problems during the http/coap translation: " + e.getMessage());
return new Response(CoapTranslator.STATUS_TRANSLATION_ERROR);
}
ResponseHandler<Response> httpResponseHandler = new ResponseHandler<Response>() {
@Override
public Response handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
long timestamp = System.nanoTime();
LOGGER.finer("Incoming http response: " + httpResponse.getStatusLine());
// the entity of the response, if non repeatable, could be
// consumed only one time, so do not debug it!
// System.out.println(EntityUtils.toString(httpResponse.getEntity()));
// translate the received http response in a coap response
try {
Response coapResponse = HttpTranslator.getCoapResponse(httpResponse, incomingCoapRequest);
coapResponse.setTimestamp(timestamp);
return coapResponse;
} catch (InvalidFieldException e) {
LOGGER.warning("Problems during the http/coap translation: " + e.getMessage());
return new Response(CoapTranslator.STATUS_FIELD_MALFORMED);
} catch (TranslationException e) {
LOGGER.warning("Problems during the http/coap translation: " + e.getMessage());
return new Response(CoapTranslator.STATUS_TRANSLATION_ERROR);
}
}
};
// accept the request sending a separate response to avoid the timeout
// in the requesting client
LOGGER.finer("Acknowledge message sent");
Response coapResponse = null;
try {
// execute the request
coapResponse = HTTP_CLIENT.execute(httpHost, httpRequest, httpResponseHandler, null);
} catch (IOException e) {
LOGGER.warning("Failed to get the http response: " + e.getMessage());
return new Response(ResponseCode.INTERNAL_SERVER_ERROR);
}
return coapResponse;
}