View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.soap.axis;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.module.cxf.MuleSoapHeaders;
12  import org.mule.transport.AbstractMuleMessageFactory;
13  import org.mule.util.StringUtils;
14  
15  import java.util.Iterator;
16  
17  import javax.xml.soap.SOAPMessage;
18  
19  import org.apache.axis.MessageContext;
20  import org.apache.axis.attachments.AttachmentPart;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  public class AxisMuleMessageFactory extends AbstractMuleMessageFactory
25  {
26      private static Log log = LogFactory.getLog(AxisMuleMessageFactory.class);
27      
28      public AxisMuleMessageFactory(MuleContext context)
29      {
30          super(context);
31      }
32  
33      @Override
34      protected Class<?>[] getSupportedTransportMessageTypes()
35      {
36          return new Class[] { Object.class };
37      }
38  
39      @Override
40      protected Object extractPayload(Object transportMessage, String encoding) throws Exception
41      {
42          return transportMessage;
43      }
44  
45      @Override
46      protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
47      {
48          MessageContext ctx = MessageContext.getCurrentContext();
49          if (ctx != null)
50          {
51              MuleSoapHeaders header = new MuleSoapHeaders(
52                  ctx.getMessage().getSOAPPart().getEnvelope().getHeader());
53  
54              if (StringUtils.isNotBlank(header.getReplyTo()))
55              {
56                  message.setReplyTo(header.getReplyTo());
57              }
58              if (StringUtils.isNotBlank(header.getCorrelationGroup()))
59              {
60                  message.setCorrelationGroupSize(Integer.parseInt(header.getCorrelationGroup()));
61              }
62              if (StringUtils.isNotBlank(header.getCorrelationSequence()))
63              {
64                  message.setCorrelationSequence(Integer.parseInt(header.getCorrelationSequence()));
65              }
66              if (StringUtils.isNotBlank(header.getCorrelationId()))
67              {
68                  message.setCorrelationId(header.getCorrelationId());
69              }
70          }
71      }
72  
73      @Override
74      protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception
75      {
76          MessageContext ctx = MessageContext.getCurrentContext();
77          if (ctx == null)
78          {
79              return;
80          }
81          
82          try
83          {
84              SOAPMessage soapMessage = ctx.getMessage();
85              int x = 1;
86              for (Iterator<?> i = soapMessage.getAttachments(); i.hasNext(); x++)
87              {
88                  String name = String.valueOf(x);
89                  AttachmentPart attachmentPart = (AttachmentPart)i.next();
90                  message.addOutboundAttachment(name, attachmentPart.getActivationDataHandler());
91              }
92          }
93          catch (Exception e)
94          {
95              // this will not happen
96              log.fatal("Failed to read attachments", e);
97          }
98      }
99  }