Search in sources :

Example 1 with Attachment

use of org.apache.cxf.message.Attachment in project camel by apache.

the class DefaultCxfBinding method populateCxfRequestFromExchange.

// CxfBinding Methods
// -------------------------------------------------------------------------
/**
     * This method is called by {@link CxfProducer#process(Exchange)}. It populates
     * the CXF exchange and invocation context (i.e. request/response) contexts, it 
     * but does not create and populate a CXF message as a ClientImpl's invoke method
     * will create a new CXF Message.  That method will put all properties from the 
     * CXF exchange and request context to the CXF message.
     */
public void populateCxfRequestFromExchange(org.apache.cxf.message.Exchange cxfExchange, Exchange camelExchange, Map<String, Object> requestContext) {
    // propagate request context
    Map<String, Object> camelHeaders = camelExchange.getIn().getHeaders();
    extractInvocationContextFromCamel(camelExchange, camelHeaders, requestContext, Client.REQUEST_CONTEXT);
    // propagate headers
    propagateHeadersFromCamelToCxf(camelExchange, camelHeaders, cxfExchange, requestContext);
    String overrideAddress = camelExchange.getIn().getHeader(Exchange.DESTINATION_OVERRIDE_URL, String.class);
    if (overrideAddress != null) {
        LOG.trace("Client address is overridden by header '{}' to value '{}'", Exchange.DESTINATION_OVERRIDE_URL, overrideAddress);
        requestContext.put(Message.ENDPOINT_ADDRESS, overrideAddress);
    }
    // propagate attachments
    Set<Attachment> attachments = null;
    boolean isXop = Boolean.valueOf(camelExchange.getProperty(Message.MTOM_ENABLED, String.class));
    DataFormat dataFormat = camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class);
    // already has the attachment information
    if (!DataFormat.CXF_MESSAGE.equals(dataFormat)) {
        for (Map.Entry<String, org.apache.camel.Attachment> entry : camelExchange.getIn().getAttachmentObjects().entrySet()) {
            if (attachments == null) {
                attachments = new HashSet<Attachment>();
            }
            AttachmentImpl attachment = new AttachmentImpl(entry.getKey());
            org.apache.camel.Attachment camelAttachment = entry.getValue();
            attachment.setDataHandler(camelAttachment.getDataHandler());
            for (String name : camelAttachment.getHeaderNames()) {
                attachment.setHeader(name, camelAttachment.getHeader(name));
            }
            attachment.setXOP(isXop);
            attachments.add(attachment);
        }
    }
    if (attachments != null) {
        requestContext.put(CxfConstants.CAMEL_CXF_ATTACHMENTS, attachments);
    }
}
Also used : DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.cxf.message.Attachment) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap)

Example 2 with Attachment

use of org.apache.cxf.message.Attachment 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 3 with Attachment

use of org.apache.cxf.message.Attachment 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 4 with Attachment

use of org.apache.cxf.message.Attachment in project tomee by apache.

the class LazyAttachmentCollection method loadAll.

private void loadAll() {
    try {
        Attachment a = deserializer.readNext();
        int count = 0;
        while (a != null) {
            attachments.add(a);
            count++;
            if (count > maxAttachmentCount) {
                throw new IOException("The message contains more attachments than are permitted");
            }
            a = deserializer.readNext();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Attachment(org.apache.cxf.message.Attachment) IOException(java.io.IOException)

Example 5 with Attachment

use of org.apache.cxf.message.Attachment in project tomee by apache.

the class AttachmentSerializer method writeAttachments.

/**
 * Write the end of the body boundary and any attachments included.
 * @throws IOException
 */
public void writeAttachments() throws IOException {
    if (message.getAttachments() != null) {
        for (Attachment a : message.getAttachments()) {
            StringWriter writer = new StringWriter();
            writer.write("\r\n--");
            writer.write(bodyBoundary);
            final Map<String, List<String>> headers;
            Iterator<String> it = a.getHeaderNames();
            if (it.hasNext()) {
                headers = new LinkedHashMap<>();
                while (it.hasNext()) {
                    String key = it.next();
                    headers.put(key, Collections.singletonList(a.getHeader(key)));
                }
            } else {
                headers = Collections.emptyMap();
            }
            DataHandler handler = a.getDataHandler();
            handler.setCommandMap(AttachmentUtil.getCommandMap());
            writeHeaders(handler.getContentType(), a.getId(), headers, writer);
            out.write(writer.getBuffer().toString().getBytes(encoding));
            if ("base64".equals(contentTransferEncoding)) {
                try (InputStream inputStream = handler.getInputStream()) {
                    encodeBase64(inputStream, out, IOUtils.DEFAULT_BUFFER_SIZE);
                }
            } else {
                handler.writeTo(out);
            }
        }
    }
    StringWriter writer = new StringWriter();
    writer.write("\r\n--");
    writer.write(bodyBoundary);
    writer.write("--");
    out.write(writer.getBuffer().toString().getBytes(encoding));
    out.flush();
}
Also used : StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) Attachment(org.apache.cxf.message.Attachment) List(java.util.List) DataHandler(javax.activation.DataHandler)

Aggregations

Attachment (org.apache.cxf.message.Attachment)51 DataHandler (javax.activation.DataHandler)18 Test (org.junit.Test)18 InputStream (java.io.InputStream)14 Message (org.apache.cxf.message.Message)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 MessageImpl (org.apache.cxf.message.MessageImpl)11 HashMap (java.util.HashMap)10 List (java.util.List)9 PushbackInputStream (java.io.PushbackInputStream)7 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)6 Fault (org.apache.cxf.interceptor.Fault)6 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)6 Map (java.util.Map)5 OutputStream (java.io.OutputStream)4 TreeMap (java.util.TreeMap)4