View Javadoc

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