Search in sources :

Example 56 with List

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());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) List(java.util.List) Test(org.junit.Test)

Example 57 with List

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);
    }
}
Also used : Message(org.apache.camel.Message) Header(org.apache.http.Header) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList)

Example 58 with List

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;
}
Also used : Header(org.apache.http.Header) HashMap(java.util.HashMap) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) List(java.util.List) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 59 with List

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));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) Test(org.junit.Test)

Example 60 with List

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());
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) List(java.util.List) Test(org.junit.Test)

Aggregations

List (java.util.List)19204 ArrayList (java.util.ArrayList)12470 Test (org.junit.Test)4025 HashMap (java.util.HashMap)3622 Map (java.util.Map)3242 IOException (java.io.IOException)1670 Iterator (java.util.Iterator)1563 LinkedList (java.util.LinkedList)1336 HashSet (java.util.HashSet)1189 Set (java.util.Set)1151 File (java.io.File)921 ImmutableList (com.google.common.collect.ImmutableList)826 Collectors (java.util.stream.Collectors)784 LinkedHashMap (java.util.LinkedHashMap)540 Test (org.testng.annotations.Test)527 Session (org.hibernate.Session)521 Collection (java.util.Collection)496 Collections (java.util.Collections)474 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)471 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)453