use of org.apache.camel.StringSource in project camel by apache.
the class DefaultRestletBinding method populateRestletResponseFromExchange.
public void populateRestletResponseFromExchange(Exchange exchange, Response response) throws Exception {
Message out;
if (exchange.isFailed()) {
// 500 for internal server error which can be overridden by response code in header
response.setStatus(Status.valueOf(500));
Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
if (msg.isFault()) {
out = msg;
} else {
// print exception as message and stacktrace
Exception t = exchange.getException();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
response.setEntity(sw.toString(), MediaType.TEXT_PLAIN);
return;
}
} else {
out = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
}
// get content type
MediaType mediaType = out.getHeader(Exchange.CONTENT_TYPE, MediaType.class);
if (mediaType == null) {
Object body = out.getBody();
mediaType = MediaType.TEXT_PLAIN;
if (body instanceof String) {
mediaType = MediaType.TEXT_PLAIN;
} else if (body instanceof StringSource || body instanceof DOMSource) {
mediaType = MediaType.TEXT_XML;
}
}
// get response code
Integer responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
if (responseCode != null) {
response.setStatus(Status.valueOf(responseCode));
}
// set response body according to the message body
Object body = out.getBody();
if (body instanceof WrappedFile) {
// grab body from generic file holder
GenericFile<?> gf = (GenericFile<?>) body;
body = gf.getBody();
}
if (body == null) {
// empty response
response.setEntity("", MediaType.TEXT_PLAIN);
} else if (body instanceof Response) {
// its already a restlet response, so dont do anything
LOG.debug("Using existing Restlet Response from exchange body: {}", body);
} else if (body instanceof Representation) {
response.setEntity(out.getBody(Representation.class));
} else if (body instanceof InputStream) {
response.setEntity(new InputRepresentation(out.getBody(InputStream.class), mediaType));
} else if (body instanceof File) {
response.setEntity(new FileRepresentation(out.getBody(File.class), mediaType));
} else if (body instanceof byte[]) {
byte[] bytes = out.getBody(byte[].class);
response.setEntity(new ByteArrayRepresentation(bytes, mediaType, bytes.length));
} else {
// fallback and use string
String text = out.getBody(String.class);
response.setEntity(text, mediaType);
}
LOG.debug("Populate Restlet response from exchange body: {}", body);
if (exchange.getProperty(Exchange.CHARSET_NAME) != null) {
CharacterSet cs = CharacterSet.valueOf(exchange.getProperty(Exchange.CHARSET_NAME, String.class));
response.getEntity().setCharacterSet(cs);
}
// set headers at the end, as the entity must be set first
// NOTE: setting HTTP headers on restlet is cumbersome and its API is "weird" and has some flaws
// so we need to headers two times, and the 2nd time we add the non-internal headers once more
Series<Header> series = new Series<Header>(Header.class);
for (Map.Entry<String, Object> entry : out.getHeaders().entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (!headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
boolean added = setResponseHeader(exchange, response, key, value);
if (!added) {
// we only want non internal headers
if (!key.startsWith("Camel") && !key.startsWith("org.restlet")) {
String text = exchange.getContext().getTypeConverter().tryConvertTo(String.class, exchange, value);
if (text != null) {
series.add(key, text);
}
}
}
}
}
// set HTTP headers so we return these in the response
if (!series.isEmpty()) {
response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, series);
}
}
use of org.apache.camel.StringSource in project camel by apache.
the class ProducerLocalRouteTest method consumeStockQuoteWebserviceWithCamelStringSourceInput.
@Test
public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception {
Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote));
assertNotNull(result);
assertTrue(result instanceof Source);
}
use of org.apache.camel.StringSource in project camel by apache.
the class ProducerRemoteRouteTest method consumeStockQuoteWebserviceWithCamelStringSourceInput.
@Test(timeout = 5000)
public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception {
Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote));
assertNotNull(result);
assertTrue(result instanceof Source);
}
use of org.apache.camel.StringSource in project camel by apache.
the class JmsXMLRouteTest method testLondonWithStringSourceAsBytes.
@Test
public void testLondonWithStringSourceAsBytes() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:london");
mock.expectedMessageCount(1);
mock.message(0).body(String.class).contains("James");
Source source = new StringSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<person user=\"james\">\n" + " <firstName>James</firstName>\n" + " <lastName>Strachan</lastName>\n" + " <city>London</city>\n" + "</person>");
assertNotNull(source);
template.sendBody("direct:bytes", source);
assertMockEndpointsSatisfied();
}
use of org.apache.camel.StringSource in project camel by apache.
the class JmsXMLRouteTest method testLondonWithStringSourceAsDefault.
@Test
public void testLondonWithStringSourceAsDefault() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:london");
mock.expectedMessageCount(1);
mock.message(0).body(String.class).contains("James");
Source source = new StringSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<person user=\"james\">\n" + " <firstName>James</firstName>\n" + " <lastName>Strachan</lastName>\n" + " <city>London</city>\n" + "</person>");
assertNotNull(source);
template.sendBody("direct:default", source);
assertMockEndpointsSatisfied();
}
Aggregations