Search in sources :

Example 11 with List

use of java.util.List in project camel by apache.

the class SimpleCxfRsBinding method bindHeadersFromSubresourceLocators.

/**
     * Transfers path parameters from the full path (including ancestor subresource locators) into Camel IN Message Headers.
     */
@SuppressWarnings("unchecked")
protected void bindHeadersFromSubresourceLocators(Exchange cxfExchange, org.apache.camel.Exchange camelExchange) {
    MultivaluedMap<String, String> pathParams = (MultivaluedMap<String, String>) cxfExchange.getInMessage().get(URITemplate.TEMPLATE_PARAMETERS);
    // return immediately if we have no path parameters
    if (pathParams == null || (pathParams.size() == 1 && pathParams.containsKey(URITemplate.FINAL_MATCH_GROUP))) {
        return;
    }
    Message m = camelExchange.getIn();
    for (Entry<String, List<String>> entry : pathParams.entrySet()) {
        // skip over the FINAL_MATCH_GROUP which stores the entire path
        if (URITemplate.FINAL_MATCH_GROUP.equals(entry.getKey())) {
            continue;
        }
        m.setHeader(entry.getKey(), entry.getValue().get(0));
    }
}
Also used : Message(org.apache.camel.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 12 with List

use of java.util.List in project camel by apache.

the class CxfConsumerWithTryCatchTest method createRouteBuilder.

// START SNIPPET: example
protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            from(SIMPLE_ENDPOINT_URI).choice().when(header(CxfConstants.OPERATION_NAME).isEqualTo(ECHO_OPERATION)).process(new Processor() {

                public void process(final Exchange exchange) {
                    Message in = exchange.getIn();
                    // Get the parameter list
                    List<?> parameter = in.getBody(List.class);
                    // Get the operation name
                    String operation = (String) in.getHeader(CxfConstants.OPERATION_NAME);
                    Object result = operation + " " + (String) parameter.get(0);
                    // Put the result back
                    exchange.getOut().setBody(result);
                }
            }).when(header(CxfConstants.OPERATION_NAME).isEqualTo(ECHO_BOOLEAN_OPERATION)).doTry().process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    throw new IllegalStateException();
                }
            }).doCatch(IllegalStateException.class).process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    Message in = exchange.getIn();
                    // Get the parameter list
                    List<?> parameter = in.getBody(List.class);
                    // Put the result back
                    exchange.getOut().setBody(parameter.get(0));
                }
            }).end();
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(org.apache.camel.Message) List(java.util.List)

Example 13 with List

use of java.util.List in project camel by apache.

the class DefaultCxfBindingTest method testPopupalteExchangeFromCxfResponse.

@Test
public void testPopupalteExchangeFromCxfResponse() {
    DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
    cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
    Exchange exchange = new DefaultExchange(context);
    org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
    exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
    Map<String, Object> responseContext = new HashMap<String, Object>();
    responseContext.put(org.apache.cxf.message.Message.RESPONSE_CODE, Integer.valueOf(200));
    Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
    headers.put("content-type", Arrays.asList("text/xml;charset=UTF-8"));
    headers.put("Content-Length", Arrays.asList("241"));
    responseContext.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    cxfExchange.setInMessage(cxfMessage);
    Set<Attachment> attachments = new HashSet<Attachment>();
    AttachmentImpl attachment = new AttachmentImpl("att-1", new DataHandler(new FileDataSource("pom.xml")));
    attachment.setHeader("additional-header", "value 1");
    attachments.add(attachment);
    cxfMessage.setAttachments(attachments);
    cxfBinding.populateExchangeFromCxfResponse(exchange, cxfExchange, responseContext);
    Map<String, Object> camelHeaders = exchange.getOut().getHeaders();
    assertNotNull(camelHeaders);
    assertEquals(responseContext, camelHeaders.get(Client.RESPONSE_CONTEXT));
    Map<String, org.apache.camel.Attachment> camelAttachments = exchange.getOut().getAttachmentObjects();
    assertNotNull(camelAttachments);
    assertNotNull(camelAttachments.get("att-1"));
    assertEquals("value 1", camelAttachments.get("att-1").getHeader("additional-header"));
}
Also used : HashMap(java.util.HashMap) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) FileDataSource(javax.activation.FileDataSource) List(java.util.List) HashSet(java.util.HashSet) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.cxf.message.Message) TreeMap(java.util.TreeMap) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 14 with List

use of java.util.List in project camel by apache.

the class CxfMixedModeRouterTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            errorHandler(noErrorHandler());
            from(routerEndpointURI).process(new Processor() {

                // convert request message
                public void process(Exchange exchange) throws Exception {
                    CxfPayload<?> message = exchange.getIn().getBody(CxfPayload.class);
                    List<String> params = new ArrayList<String>();
                    if (message != null) {
                        // convert CxfPayload to list of objects any way you like
                        Element element = new XmlConverter().toDOMElement(message.getBody().get(0));
                        params.add(element.getFirstChild().getTextContent());
                    }
                    // replace the body
                    exchange.getIn().setBody(params);
                // if you need to change the operation name
                //exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);      
                }
            }).to(serviceEndpointURI).process(new Processor() {

                // convert response to CxfPayload
                public void process(Exchange exchange) throws Exception {
                    List<?> list = exchange.getIn().getBody(List.class);
                    CxfPayload<SoapHeader> message = null;
                    if (list != null) {
                        // convert the list of objects to CxfPayload any way you like
                        String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<ns1:echoResponse xmlns:ns1=\"http://cxf.component.camel.apache.org/\">" + "<return xmlns=\"http://cxf.component.camel.apache.org/\">" + list.get(0) + "</return></ns1:echoResponse>";
                        List<Element> body = new ArrayList<Element>();
                        body.add(StaxUtils.read(new StringReader(s)).getDocumentElement());
                        message = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), body);
                    }
                    exchange.getIn().setBody(message);
                    // we probably should be smarter in detecting data format based on message body
                    // but for now we need to explicitly reset the mode (see CAMEL-3420)
                    exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
                }
            });
        }
    };
}
Also used : Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Exchange(org.apache.camel.Exchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with List

use of java.util.List in project camel by apache.

the class CxfPayLoadSoapHeaderTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            // START SNIPPET: payload
            from(getRouterEndpointURI()).process(new Processor() {

                @SuppressWarnings("unchecked")
                public void process(Exchange exchange) throws Exception {
                    CxfPayload<SoapHeader> payload = exchange.getIn().getBody(CxfPayload.class);
                    List<Source> elements = payload.getBodySources();
                    assertNotNull("We should get the elements here", elements);
                    assertEquals("Get the wrong elements size", 1, elements.size());
                    Element el = new XmlConverter().toDOMElement(elements.get(0));
                    elements.set(0, new DOMSource(el));
                    assertEquals("Get the wrong namespace URI", "http://camel.apache.org/pizza/types", el.getNamespaceURI());
                    List<SoapHeader> headers = payload.getHeaders();
                    assertNotNull("We should get the headers here", headers);
                    assertEquals("Get the wrong headers size", headers.size(), 1);
                    assertEquals("Get the wrong namespace URI", ((Element) (headers.get(0).getObject())).getNamespaceURI(), "http://camel.apache.org/pizza/types");
                    // alternatively you can also get the SOAP header via the camel header:
                    headers = exchange.getIn().getHeader(Header.HEADER_LIST, List.class);
                    assertNotNull("We should get the headers here", headers);
                    assertEquals("Get the wrong headers size", headers.size(), 1);
                    assertEquals("Get the wrong namespace URI", ((Element) (headers.get(0).getObject())).getNamespaceURI(), "http://camel.apache.org/pizza/types");
                }
            }).to(getServiceEndpointURI());
        // END SNIPPET: payload
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) List(java.util.List) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

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