Search in sources :

Example 1 with DecodeRepresentation

use of org.restlet.engine.application.DecodeRepresentation in project camel by apache.

the class DefaultRestletBinding method populateExchangeFromRestletResponse.

public void populateExchangeFromRestletResponse(Exchange exchange, Response response) throws Exception {
    for (Map.Entry<String, Object> entry : response.getAttributes().entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (!headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) {
            exchange.getOut().setHeader(key, value);
            LOG.debug("Populate exchange from Restlet response header: {} value: {}", key, value);
        }
    }
    // set response code
    int responseCode = response.getStatus().getCode();
    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
    // set restlet response as header so end user have access to it if needed
    exchange.getOut().setHeader(RestletConstants.RESTLET_RESPONSE, response);
    if (response.getEntity() != null) {
        // get content type
        MediaType mediaType = response.getEntity().getMediaType();
        if (mediaType != null) {
            LOG.debug("Setting the Content-Type to be {}", mediaType.toString());
            exchange.getOut().setHeader(Exchange.CONTENT_TYPE, mediaType.toString());
        }
        if (streamRepresentation && response.getEntity() instanceof StreamRepresentation) {
            Representation representationDecoded = new DecodeRepresentation(response.getEntity());
            InputStream is = representationDecoded.getStream();
            exchange.getOut().setBody(is);
            if (autoCloseStream) {
                // ensure the input stream is closed when we are done routing
                exchange.addOnCompletion(new RestletOnCompletion(is));
            }
        } else if (response.getEntity() instanceof Representation) {
            Representation representationDecoded = new DecodeRepresentation(response.getEntity());
            exchange.getOut().setBody(representationDecoded.getText());
        } else {
            // get content text by default
            String text = response.getEntity().getText();
            LOG.debug("Populate exchange from Restlet response: {}", text);
            exchange.getOut().setBody(text);
        }
    }
    // preserve headers from in by copying any non existing headers
    // to avoid overriding existing headers with old values
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), false);
}
Also used : DecodeRepresentation(org.restlet.engine.application.DecodeRepresentation) InputStream(java.io.InputStream) MediaType(org.restlet.data.MediaType) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) InputRepresentation(org.restlet.representation.InputRepresentation) ByteArrayRepresentation(org.restlet.representation.ByteArrayRepresentation) FileRepresentation(org.restlet.representation.FileRepresentation) StreamRepresentation(org.restlet.representation.StreamRepresentation) Representation(org.restlet.representation.Representation) DecodeRepresentation(org.restlet.engine.application.DecodeRepresentation) Map(java.util.Map) StreamRepresentation(org.restlet.representation.StreamRepresentation)

Aggregations

InputStream (java.io.InputStream)1 Map (java.util.Map)1 MediaType (org.restlet.data.MediaType)1 DecodeRepresentation (org.restlet.engine.application.DecodeRepresentation)1 ByteArrayRepresentation (org.restlet.representation.ByteArrayRepresentation)1 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)1 FileRepresentation (org.restlet.representation.FileRepresentation)1 InputRepresentation (org.restlet.representation.InputRepresentation)1 Representation (org.restlet.representation.Representation)1 StreamRepresentation (org.restlet.representation.StreamRepresentation)1 StringRepresentation (org.restlet.representation.StringRepresentation)1