Search in sources :

Example 1 with ProxyRequestProducer

use of org.eclipse.californium.proxy2.http.ProxyRequestProducer 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));
        }
    });
}
Also used : ContentTypedEntity(org.eclipse.californium.proxy2.http.ContentTypedEntity) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) ProxyRequestProducer(org.eclipse.californium.proxy2.http.ProxyRequestProducer) BasicHttpContext(org.apache.hc.core5.http.protocol.BasicHttpContext) Request(org.eclipse.californium.core.coap.Request) HttpResponse(org.apache.hc.core5.http.HttpResponse) TranslationException(org.eclipse.californium.proxy2.TranslationException) URI(java.net.URI) InvalidFieldException(org.eclipse.californium.proxy2.InvalidFieldException) SocketTimeoutException(java.net.SocketTimeoutException) TranslationException(org.eclipse.californium.proxy2.TranslationException) Response(org.eclipse.californium.core.coap.Response) HttpResponse(org.apache.hc.core5.http.HttpResponse) ContentTypedEntityConsumer(org.eclipse.californium.proxy2.http.ContentTypedEntityConsumer) StatusLine(org.apache.hc.core5.http.message.StatusLine) SocketTimeoutException(java.net.SocketTimeoutException) InvalidFieldException(org.eclipse.californium.proxy2.InvalidFieldException)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URI (java.net.URI)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 Message (org.apache.hc.core5.http.Message)1 StatusLine (org.apache.hc.core5.http.message.StatusLine)1 BasicHttpContext (org.apache.hc.core5.http.protocol.BasicHttpContext)1 Request (org.eclipse.californium.core.coap.Request)1 Response (org.eclipse.californium.core.coap.Response)1 InvalidFieldException (org.eclipse.californium.proxy2.InvalidFieldException)1 TranslationException (org.eclipse.californium.proxy2.TranslationException)1 ContentTypedEntity (org.eclipse.californium.proxy2.http.ContentTypedEntity)1 ContentTypedEntityConsumer (org.eclipse.californium.proxy2.http.ContentTypedEntityConsumer)1 ProxyRequestProducer (org.eclipse.californium.proxy2.http.ProxyRequestProducer)1