Search in sources :

Example 26 with Attachment

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

the class TestAttachmentOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Assert.assertEquals("check attachment count", 1, message.getAttachments().size());
    Attachment att = message.getAttachments().iterator().next();
    Assert.assertNotNull("Attachment is null", att);
    Assert.assertNotNull("Attachment content-type is null", att.getDataHandler().getDataSource().getContentType());
}
Also used : Attachment(org.apache.cxf.message.Attachment)

Example 27 with Attachment

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

the class DefaultCxfBindingTest method testPopupalteExchangeFromCxfRequest.

@Test
public void testPopupalteExchangeFromCxfRequest() {
    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);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    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"));
    headers.put("soapAction", Arrays.asList("urn:hello:world"));
    headers.put("myfruitheader", Arrays.asList("peach"));
    headers.put("mybrewheader", Arrays.asList("cappuccino", "espresso"));
    cxfMessage.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
    Set<Attachment> attachments = new HashSet<Attachment>();
    AttachmentImpl attachment = new AttachmentImpl("att-1", new DataHandler(new FileDataSource("pom.xml")));
    attachment.setHeader("attachment-header", "value 1");
    attachments.add(attachment);
    cxfMessage.setAttachments(attachments);
    cxfExchange.setInMessage(cxfMessage);
    cxfBinding.populateExchangeFromCxfRequest(cxfExchange, exchange);
    Map<String, Object> camelHeaders = exchange.getIn().getHeaders();
    assertNotNull(camelHeaders);
    assertEquals("urn:hello:world", camelHeaders.get("soapaction"));
    assertEquals("urn:hello:world", camelHeaders.get("SoapAction"));
    assertEquals("text/xml;charset=UTF-8", camelHeaders.get("content-type"));
    assertEquals("241", camelHeaders.get("content-length"));
    assertEquals("peach", camelHeaders.get("MyFruitHeader"));
    assertEquals(Arrays.asList("cappuccino", "espresso"), camelHeaders.get("MyBrewHeader"));
    Map<String, org.apache.camel.Attachment> camelAttachments = exchange.getIn().getAttachmentObjects();
    assertNotNull(camelAttachments);
    assertNotNull(camelAttachments.get("att-1"));
    assertEquals("value 1", camelAttachments.get("att-1").getHeader("attachment-header"));
}
Also used : 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 28 with Attachment

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

the class DefaultCxfBindingTest method testPopulateCxfRequestFromExchange.

@Test
public void testPopulateCxfRequestFromExchange() {
    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> requestContext = new HashMap<String, Object>();
    exchange.getIn().setHeader("soapAction", "urn:hello:world");
    exchange.getIn().setHeader("MyFruitHeader", "peach");
    exchange.getIn().setHeader("MyBrewHeader", Arrays.asList("cappuccino", "espresso"));
    exchange.getIn().addAttachment("att-1", new DataHandler(new FileDataSource("pom.xml")));
    exchange.getIn().getAttachmentObject("att-1").setHeader("attachment-header", "value 1");
    cxfBinding.populateCxfRequestFromExchange(cxfExchange, exchange, requestContext);
    // check the protocol headers
    Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) requestContext.get(Message.PROTOCOL_HEADERS));
    assertNotNull(headers);
    assertEquals(3, headers.size());
    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");
    verifyHeader(headers, "MyBrewHeader", Arrays.asList("cappuccino", "espresso"));
    Set<Attachment> attachments = CastUtils.cast((Set<?>) requestContext.get(CxfConstants.CAMEL_CXF_ATTACHMENTS));
    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) HashMap(java.util.HashMap) 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) FileDataSource(javax.activation.FileDataSource) List(java.util.List) Test(org.junit.Test)

Example 29 with Attachment

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

the class DefaultCxfBinding method populateCxfResponseFromExchange.

/**
     * This method is called by {@link CxfConsumer} to populate a CXF response exchange 
     * from a Camel exchange.
     */
public void populateCxfResponseFromExchange(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange) {
    if (cxfExchange.isOneWay()) {
        return;
    }
    // create response context
    Map<String, Object> responseContext = new HashMap<String, Object>();
    org.apache.camel.Message response;
    if (camelExchange.getPattern().isOutCapable()) {
        if (camelExchange.hasOut()) {
            response = camelExchange.getOut();
            LOG.trace("Get the response from the out message");
        } else {
            // Take the in message as a fall back
            response = camelExchange.getIn();
            LOG.trace("Get the response from the in message as a fallback");
        }
    } else {
        response = camelExchange.getIn();
        LOG.trace("Get the response from the in message");
    }
    // propagate response context
    Map<String, Object> camelHeaders = response.getHeaders();
    extractInvocationContextFromCamel(camelExchange, camelHeaders, responseContext, Client.RESPONSE_CONTEXT);
    propagateHeadersFromCamelToCxf(camelExchange, camelHeaders, cxfExchange, responseContext);
    // create out message
    Endpoint ep = cxfExchange.get(Endpoint.class);
    Message outMessage = ep.getBinding().createMessage();
    cxfExchange.setOutMessage(outMessage);
    DataFormat dataFormat = camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class);
    // make sure the "requestor role" property does not get propagated as we do switch role
    responseContext.remove(Message.REQUESTOR_ROLE);
    outMessage.putAll(responseContext);
    // Do we still need to put the response context back like this
    outMessage.put(Client.RESPONSE_CONTEXT, responseContext);
    LOG.trace("Set out response context = {}", responseContext);
    // set body
    Object outBody = DefaultCxfBinding.getBodyFromCamel(response, dataFormat);
    if (outBody != null) {
        if (dataFormat == DataFormat.PAYLOAD) {
            CxfPayload<?> payload = (CxfPayload<?>) outBody;
            outMessage.setContent(List.class, getResponsePayloadList(cxfExchange, payload.getBodySources()));
            outMessage.put(Header.HEADER_LIST, payload.getHeaders());
        } else {
            if (responseContext.get(Header.HEADER_LIST) != null) {
                outMessage.put(Header.HEADER_LIST, responseContext.get(Header.HEADER_LIST));
            }
            MessageContentsList resList = null;
            // Create a new MessageContentsList to avoid OOM from the HolderOutInterceptor
            if (outBody instanceof List) {
                resList = new MessageContentsList((List<?>) outBody);
            } else if (outBody.getClass().isArray()) {
                resList = new MessageContentsList((Object[]) outBody);
            } else {
                resList = new MessageContentsList(outBody);
            }
            if (resList != null) {
                outMessage.setContent(List.class, resList);
                LOG.trace("Set Out CXF message content = {}", resList);
            }
        }
    } else if (!cxfExchange.isOneWay() && cxfExchange.getInMessage() != null && MessageUtils.isTrue(cxfExchange.getInMessage().getContextualProperty("jaxws.provider.interpretNullAsOneway"))) {
        // treat this non-oneway call as oneway when the provider returns a null
        changeToOneway(cxfExchange);
        return;
    }
    // propagate attachments
    Set<Attachment> attachments = null;
    boolean isXop = Boolean.valueOf(camelExchange.getProperty(Message.MTOM_ENABLED, String.class));
    for (Map.Entry<String, org.apache.camel.Attachment> entry : camelExchange.getOut().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) {
        outMessage.setAttachments(attachments);
    }
    BindingOperationInfo boi = cxfExchange.get(BindingOperationInfo.class);
    if (boi != null) {
        cxfExchange.put(BindingMessageInfo.class, boi.getOutput());
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) HashMap(java.util.HashMap) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.cxf.message.Attachment) Endpoint(org.apache.cxf.endpoint.Endpoint) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ArrayList(java.util.ArrayList) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap)

Example 30 with Attachment

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

the class DefaultCxfBinding method populateExchangeFromCxfResponse.

/**
     * This method is called by {@link CxfProducer#process(Exchange)}.  It propagates 
     * information from CXF Exchange to Camel Exchange.  The CXF Exchange contains a 
     * request from a CXF server.
     */
public void populateExchangeFromCxfResponse(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange, Map<String, Object> responseContext) {
    Message cxfMessage = cxfExchange.getInMessage();
    // Need to check if the inMessage is set
    if (cxfMessage == null) {
        return;
    }
    LOG.trace("Populate exchange from CXF response message: {}", cxfMessage);
    // copy the InMessage header to OutMessage header
    camelExchange.getOut().getHeaders().putAll(camelExchange.getIn().getHeaders());
    // propagate body
    String encoding = (String) camelExchange.getProperty(Exchange.CHARSET_NAME);
    camelExchange.getOut().setBody(DefaultCxfBinding.getContentFromCxf(cxfMessage, camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class), encoding));
    // propagate response context
    if (responseContext != null && responseContext.size() > 0) {
        if (!headerFilterStrategy.applyFilterToExternalHeaders(Client.RESPONSE_CONTEXT, responseContext, camelExchange)) {
            camelExchange.getOut().setHeader(Client.RESPONSE_CONTEXT, responseContext);
            LOG.trace("Set header = {} value = {}", Client.RESPONSE_CONTEXT, responseContext);
        }
    }
    // propagate protocol headers
    propagateHeadersFromCxfToCamel(cxfMessage, camelExchange.getOut(), camelExchange);
    // propagate attachments
    if (cxfMessage.getAttachments() != null) {
        // propagate attachments
        for (Attachment attachment : cxfMessage.getAttachments()) {
            camelExchange.getOut().addAttachmentObject(attachment.getId(), createCamelAttachment(attachment));
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.cxf.message.Attachment)

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