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);
}
Aggregations