Search in sources :

Example 1 with DataHandler

use of javax.activation.DataHandler 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 2 with DataHandler

use of javax.activation.DataHandler in project camel by apache.

the class DefaultCxfBindingTest method testPopupalteCxfResponseFromExchange.

@Test
public void testPopupalteCxfResponseFromExchange() {
    DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
    cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
    Exchange exchange = new DefaultExchange(context, ExchangePattern.InOut);
    org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
    exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
    exchange.getOut().setHeader("soapAction", "urn:hello:world");
    exchange.getOut().setHeader("MyFruitHeader", "peach");
    exchange.getOut().addAttachment("att-1", new DataHandler(new FileDataSource("pom.xml")));
    exchange.getOut().getAttachmentObject("att-1").setHeader("attachment-header", "value 1");
    IMocksControl control = EasyMock.createNiceControl();
    Endpoint endpoint = control.createMock(Endpoint.class);
    Binding binding = control.createMock(Binding.class);
    EasyMock.expect(endpoint.getBinding()).andReturn(binding);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    EasyMock.expect(binding.createMessage()).andReturn(cxfMessage);
    cxfExchange.put(Endpoint.class, endpoint);
    control.replay();
    cxfBinding.populateCxfResponseFromExchange(exchange, cxfExchange);
    cxfMessage = cxfExchange.getOutMessage();
    assertNotNull(cxfMessage);
    Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) cxfMessage.get(Message.PROTOCOL_HEADERS));
    assertNotNull(headers);
    assertTrue(headers.size() == 2);
    verifyHeader(headers, "soapaction", "urn:hello:world");
    verifyHeader(headers, "SoapAction", "urn:hello:world");
    verifyHeader(headers, "SOAPAction", "urn:hello:world");
    verifyHeader(headers, "myfruitheader", "peach");
    verifyHeader(headers, "myFruitHeader", "peach");
    verifyHeader(headers, "MYFRUITHEADER", "peach");
    Collection<Attachment> attachments = cxfMessage.getAttachments();
    assertNotNull(attachments);
    assertNotNull(attachments.size() == 1);
    Attachment att = attachments.iterator().next();
    assertEquals("att-1", att.getId());
    assertEquals("value 1", att.getHeader("attachment-header"));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Binding(org.apache.cxf.binding.Binding) Message(org.apache.cxf.message.Message) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) IMocksControl(org.easymock.IMocksControl) Endpoint(org.apache.cxf.endpoint.Endpoint) FileDataSource(javax.activation.FileDataSource) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 3 with DataHandler

use of javax.activation.DataHandler in project camel by apache.

the class CxfRsConsumerSimpleBindingTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() {
            from(CXF_RS_ENDPOINT_URI).recipientList(simple("direct:${header.operationName}"));
            from("direct:getCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertNotNull(exchange.getIn().getHeader("id"));
                    long id = exchange.getIn().getHeader("id", Long.class);
                    if (id == 123) {
                        assertEquals("123", exchange.getIn().getHeader("id"));
                        assertEquals(MessageContentsList.class, exchange.getIn().getBody().getClass());
                        exchange.getOut().setBody(new Customer(123, "Raul"));
                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
                    } else if (id == 456) {
                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                    } else {
                        fail();
                    }
                }
            });
            from("direct:updateCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertEquals(123, c.getId());
                    assertNotNull(c);
                }
            });
            from("direct:newCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertNotNull(c);
                    assertEquals(123, c.getId());
                }
            });
            from("direct:listVipCustomers").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals(MessageContentsList.class, exchange.getIn().getBody().getClass());
                    assertEquals(0, exchange.getIn().getBody(MessageContentsList.class).size());
                    CustomerList response = new CustomerList();
                    List<Customer> list = new ArrayList<Customer>(2);
                    list.add(new Customer(123, "Raul"));
                    list.add(new Customer(456, "Raul2"));
                    response.setCustomers(list);
                    exchange.getOut().setBody(response);
                }
            });
            from("direct:updateVipCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertEquals(123, c.getId());
                    assertNotNull(c);
                }
            });
            from("direct:deleteVipCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                }
            });
            from("direct:uploadImageInputStream").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertEquals("image/jpeg", exchange.getIn().getHeader("Content-Type"));
                    assertTrue(InputStream.class.isAssignableFrom(exchange.getIn().getBody().getClass()));
                    InputStream is = exchange.getIn().getBody(InputStream.class);
                    is.close();
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:uploadImageDataHandler").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertEquals("image/jpeg", exchange.getIn().getHeader("Content-Type"));
                    assertTrue(DataHandler.class.isAssignableFrom(exchange.getIn().getBody().getClass()));
                    DataHandler dh = exchange.getIn().getBody(DataHandler.class);
                    assertEquals("image/jpeg", dh.getContentType());
                    dh.getInputStream().close();
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:multipartPostWithParametersAndPayload").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("abcd", exchange.getIn().getHeader("query"));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertNotNull(exchange.getIn().getAttachment("part1"));
                    assertNotNull(exchange.getIn().getAttachment("part2"));
                    assertNull(exchange.getIn().getHeader("part1"));
                    assertNull(exchange.getIn().getHeader("part2"));
                    assertEquals(Customer.class, exchange.getIn().getHeader("body").getClass());
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:multipartPostWithoutParameters").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertNotNull(exchange.getIn().getAttachment("part1"));
                    assertNotNull(exchange.getIn().getAttachment("part2"));
                    assertNull(exchange.getIn().getHeader("part1"));
                    assertNull(exchange.getIn().getHeader("part2"));
                    assertEquals(Customer.class, exchange.getIn().getHeader("body").getClass());
                    exchange.getOut().setBody(null);
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MessageContentsList(org.apache.cxf.message.MessageContentsList) Customer(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer) InputStream(java.io.InputStream) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) CustomerList(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList) DataHandler(javax.activation.DataHandler) CustomerList(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList)

Example 4 with DataHandler

use of javax.activation.DataHandler in project camel by apache.

the class CxfMtomDisabledProducerPayloadModeTest method testProducer.

@Override
public void testProducer() throws Exception {
    if (MtomTestHelper.isAwtHeadless(logger, null)) {
        return;
    }
    Exchange exchange = context.createProducerTemplate().send("direct:testEndpoint", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            List<Source> elements = new ArrayList<Source>();
            elements.add(new DOMSource(StaxUtils.read(new StringReader(MtomTestHelper.MTOM_DISABLED_REQ_MESSAGE)).getDocumentElement()));
            CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null);
            exchange.getIn().setBody(body);
            exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, "application/octet-stream")));
            exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.requestJpeg, "image/jpeg")));
        }
    });
    // process response - verify response attachments
    CxfPayload<?> out = exchange.getOut().getBody(CxfPayload.class);
    Assert.assertEquals(1, out.getBody().size());
    DataHandler dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_PHOTO_CID);
    Assert.assertEquals("application/octet-stream", dr.getContentType());
    MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
    dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_IMAGE_CID);
    Assert.assertEquals("image/jpeg", dr.getContentType());
    BufferedImage image = ImageIO.read(dr.getInputStream());
    Assert.assertEquals(560, image.getWidth());
    Assert.assertEquals(300, image.getHeight());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) AttachmentDataSource(org.apache.cxf.attachment.AttachmentDataSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) BufferedImage(java.awt.image.BufferedImage) Exchange(org.apache.camel.Exchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 5 with DataHandler

use of javax.activation.DataHandler in project camel by apache.

the class BeanMethodWithExchangeTest method testBeanWithAnnotationAndExchangeTest.

public void testBeanWithAnnotationAndExchangeTest() throws Exception {
    Exchange result = template.request("direct:start1", new Processor() {

        public void process(Exchange exchange) throws Exception {
            Message m = exchange.getIn();
            m.addAttachment("attachment", new DataHandler(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java")));
        }
    });
    assertTrue(result.getOut().getAttachmentObjects().containsKey("attachment2"));
    assertTrue(result.getOut().getAttachments().containsKey("attachment1"));
    assertEquals("attachmentValue1", result.getOut().getAttachmentObjects().get("attachment1").getHeader("attachmentHeader1"));
    assertFalse(result.getOut().getAttachments().containsKey("attachment"));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler)

Aggregations

DataHandler (javax.activation.DataHandler)180 Exchange (org.apache.camel.Exchange)39 MimeBodyPart (javax.mail.internet.MimeBodyPart)38 FileDataSource (javax.activation.FileDataSource)33 Test (org.junit.Test)33 DataSource (javax.activation.DataSource)32 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)25 MessagingException (javax.mail.MessagingException)25 MimeMultipart (javax.mail.internet.MimeMultipart)25 MimeMessage (javax.mail.internet.MimeMessage)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)17 Processor (org.apache.camel.Processor)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 File (java.io.File)13 PipedInputStream (java.io.PipedInputStream)13