use of java.util.List in project camel by apache.
the class GroupedExchangeRoundtripTest method sendGrouped.
/**
* Tests that a grouped exhcange is successfully received
*
* @throws Exception
*/
@Test
public void sendGrouped() throws Exception {
Exchange exchange1 = new DefaultExchange(context);
Exchange exchange2 = new DefaultExchange(context);
String body1 = "Group 1.1 : " + exchange1.getExchangeId();
String body2 = "Group 1.2 : " + exchange2.getExchangeId();
receiveResult.expectedMessageCount(2);
receiveResult.expectedBodiesReceivedInAnyOrder(body1, body2);
exchange1.getIn().setBody(body1);
exchange2.getIn().setBody(body2);
producer.send(exchange1);
producer.send(exchange2);
receiveResult.assertIsSatisfied(3000);
// Send result section
List<Exchange> results = sendResult.getExchanges();
assertEquals("Received exchanges", 1, results.size());
List exchangeGrouped = (List) results.get(0).getProperty(Exchange.GROUPED_EXCHANGE);
assertEquals("Received messages within the exchange", 2, exchangeGrouped.size());
}
use of java.util.List in project camel by apache.
the class HttpProducer method populateResponse.
protected void populateResponse(Exchange exchange, HttpRequestBase httpRequest, HttpResponse httpResponse, Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException, ClassNotFoundException {
// We just make the out message is not create when extractResponseBody throws exception
Object response = extractResponseBody(httpRequest, httpResponse, exchange, getEndpoint().isIgnoreResponseBody());
Message answer = exchange.getOut();
answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
if (httpResponse.getStatusLine() != null) {
answer.setHeader(Exchange.HTTP_RESPONSE_TEXT, httpResponse.getStatusLine().getReasonPhrase());
}
answer.setBody(response);
// propagate HTTP response headers
Header[] headers = httpResponse.getAllHeaders();
Map<String, List<String>> m = new HashMap<String, List<String>>();
for (Header header : headers) {
String name = header.getName();
String value = header.getValue();
m.put(name, Collections.singletonList(value));
if (name.toLowerCase().equals("content-type")) {
name = Exchange.CONTENT_TYPE;
exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.getCharsetNameFromContentType(value));
}
// use http helper to extract parameter value as it may contain multiple values
Object extracted = HttpHelper.extractHttpParameterValue(value);
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, extracted, exchange)) {
HttpHelper.appendHeader(answer.getHeaders(), name, extracted);
}
}
// handle cookies
if (getEndpoint().getCookieHandler() != null) {
getEndpoint().getCookieHandler().storeCookies(exchange, httpRequest.getURI(), m);
}
// filter the http protocol headers
if (getEndpoint().isCopyHeaders()) {
MessageHelper.copyHeaders(exchange.getIn(), answer, httpProtocolHeaderFilterStrategy, false);
}
}
use of java.util.List in project camel by apache.
the class HttpProducer method populateHttpOperationFailedException.
protected Exception populateHttpOperationFailedException(Exchange exchange, HttpRequestBase httpRequest, HttpResponse httpResponse, int responseCode) throws IOException, ClassNotFoundException {
Exception answer;
String uri = httpRequest.getURI().toString();
String statusText = httpResponse.getStatusLine() != null ? httpResponse.getStatusLine().getReasonPhrase() : null;
Map<String, String> headers = extractResponseHeaders(httpResponse.getAllHeaders());
// handle cookies
if (getEndpoint().getCookieHandler() != null) {
Map<String, List<String>> m = new HashMap<String, List<String>>();
for (Entry<String, String> e : headers.entrySet()) {
m.put(e.getKey(), Collections.singletonList(e.getValue()));
}
getEndpoint().getCookieHandler().storeCookies(exchange, httpRequest.getURI(), m);
}
Object responseBody = extractResponseBody(httpRequest, httpResponse, exchange, getEndpoint().isIgnoreResponseBody());
if (transferException && responseBody != null && responseBody instanceof Exception) {
// if the response was a serialized exception then use that
return (Exception) responseBody;
}
// make a defensive copy of the response body in the exception so its detached from the cache
String copy = null;
if (responseBody != null) {
copy = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, responseBody);
}
Header locationHeader = httpResponse.getFirstHeader("location");
if (locationHeader != null && (responseCode >= 300 && responseCode < 400)) {
answer = new HttpOperationFailedException(uri, responseCode, statusText, locationHeader.getValue(), headers, copy);
} else {
answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
}
return answer;
}
use of java.util.List in project camel by apache.
the class HttpProducerTwoHeadersWithSameKeyTest method testTwoHeadersWithSameKeyHeader.
@Test
public void testTwoHeadersWithSameKeyHeader() throws Exception {
Exchange out = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/myapp", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(null);
exchange.getIn().setHeader("from", "me");
List<String> list = new ArrayList<String>();
list.add("foo");
list.add("bar");
exchange.getIn().setHeader("to", list);
}
});
assertNotNull(out);
assertFalse("Should not fail", out.isFailed());
assertEquals("OK", out.getOut().getBody(String.class));
assertEquals("yes", out.getOut().getHeader("bar"));
List<?> foo = out.getOut().getHeader("foo", List.class);
assertNotNull(foo);
assertEquals(2, foo.size());
assertEquals("123", foo.get(0));
assertEquals("456", foo.get(1));
}
use of java.util.List in project camel by apache.
the class SpringJacksonMarshalUnmarshalListTest method testUnmarshalListPojo.
@Test
public void testUnmarshalListPojo() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:reversePojo");
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(List.class);
String json = "[{\"name\":\"Camel\"}, {\"name\":\"World\"}]";
template.sendBody("direct:backPojo", json);
assertMockEndpointsSatisfied();
List list = mock.getReceivedExchanges().get(0).getIn().getBody(List.class);
assertNotNull(list);
assertEquals(2, list.size());
TestPojo pojo = (TestPojo) list.get(0);
assertEquals("Camel", pojo.getName());
pojo = (TestPojo) list.get(1);
assertEquals("World", pojo.getName());
}
Aggregations