use of org.eclipse.californium.proxy2.http.ContentTypedEntityConsumer in project californium by eclipse.
the class ProxyHttpClientResource method handleRequest.
@Override
public void handleRequest(final Exchange exchange) {
final Request incomingCoapRequest = exchange.getRequest();
URI destination;
try {
InetSocketAddress exposedInterface = translator.getExposedInterface(incomingCoapRequest);
destination = translator.getDestinationURI(incomingCoapRequest, exposedInterface);
} catch (TranslationException ex) {
LOGGER.debug("URI error.", ex);
Response response = new Response(Coap2CoapTranslator.STATUS_FIELD_MALFORMED);
response.setPayload(ex.getMessage());
exchange.sendResponse(response);
return;
}
final CacheKey cacheKey;
final CacheResource cache = getCache();
if (cache != null) {
cacheKey = new CacheKey(incomingCoapRequest.getCode(), destination, incomingCoapRequest.getOptions().getAccept(), incomingCoapRequest.getPayload());
Response response = cache.getResponse(cacheKey);
StatsResource statsResource = getStatsResource();
if (statsResource != null) {
statsResource.updateStatistics(destination, response != null);
}
if (response != null) {
LOGGER.info("Cache returned {}", response);
exchange.sendResponse(response);
return;
}
} else {
cacheKey = null;
}
ProxyRequestProducer httpRequest = null;
try {
// get the mapping to http for the incoming coap request
httpRequest = translator.getHttpRequest(destination, incomingCoapRequest);
LOGGER.debug("Outgoing http request: {}", httpRequest.getRequestLine());
} catch (InvalidFieldException e) {
LOGGER.debug("Problems during the http/coap translation: {}", e.getMessage());
exchange.sendResponse(new Response(Coap2CoapTranslator.STATUS_FIELD_MALFORMED));
return;
} catch (TranslationException e) {
LOGGER.debug("Problems during the http/coap translation: {}", e.getMessage());
exchange.sendResponse(new Response(Coap2CoapTranslator.STATUS_TRANSLATION_ERROR));
return;
}
if (accept) {
exchange.sendAccept();
}
asyncClient.execute(httpRequest, new BasicResponseConsumer<ContentTypedEntity>(new ContentTypedEntityConsumer()), new BasicHttpContext(), new FutureCallback<Message<HttpResponse, ContentTypedEntity>>() {
@Override
public void completed(Message<HttpResponse, ContentTypedEntity> result) {
StatusLine status = new StatusLine(result.getHead());
try {
long timestamp = ClockUtil.nanoRealtime();
LOGGER.debug("Incoming http response: {}", status);
// 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
Response coapResponse = translator.getCoapResponse(result, incomingCoapRequest);
coapResponse.setNanoTimestamp(timestamp);
if (cache != null) {
cache.cacheResponse(cacheKey, coapResponse);
}
exchange.sendResponse(coapResponse);
} catch (InvalidFieldException e) {
LOGGER.debug("Problems during the http/coap translation: {}", e.getMessage());
exchange.sendResponse(new Response(Coap2CoapTranslator.STATUS_FIELD_MALFORMED));
} catch (TranslationException e) {
LOGGER.debug("Problems during the http/coap translation: {}", e.getMessage());
exchange.sendResponse(new Response(Coap2CoapTranslator.STATUS_TRANSLATION_ERROR));
} catch (Throwable e) {
LOGGER.debug("Error during the http/coap translation: {}", e.getMessage(), e);
exchange.sendResponse(new Response(Coap2CoapTranslator.STATUS_FIELD_MALFORMED));
}
LOGGER.debug("Incoming http response: {} processed!", status);
}
@Override
public void failed(Exception ex) {
LOGGER.debug("Failed to get the http response: {}", ex.getMessage(), ex);
if (ex instanceof SocketTimeoutException) {
exchange.sendResponse(new Response(ResponseCode.GATEWAY_TIMEOUT));
} else {
exchange.sendResponse(new Response(ResponseCode.BAD_GATEWAY));
}
}
@Override
public void cancelled() {
LOGGER.debug("Request canceled");
exchange.sendResponse(new Response(ResponseCode.SERVICE_UNAVAILABLE));
}
});
}
Aggregations