View Javadoc

1   /*
2    * $Id: XFireMessageAdapter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.soap.xfire;
12  
13  import org.mule.config.MuleProperties;
14  import org.mule.impl.ThreadSafeAccess;
15  import org.mule.providers.AbstractMessageAdapter;
16  import org.mule.providers.soap.MuleSoapHeaders;
17  import org.mule.transformers.simple.SerializableToByteArray;
18  import org.mule.umo.transformer.UMOTransformer;
19  
20  import java.util.Iterator;
21  
22  import javax.activation.DataHandler;
23  
24  import org.codehaus.xfire.MessageContext;
25  import org.codehaus.xfire.attachments.Attachment;
26  import org.codehaus.xfire.attachments.Attachments;
27  import org.codehaus.xfire.attachments.SimpleAttachment;
28  import org.jdom.Element;
29  import org.jdom.Namespace;
30  
31  /**
32   * <code>XFireMessageAdapter</code> wraps an XFire MessageContext, reading
33   * attachments and Mule headers.
34   */
35  public class XFireMessageAdapter extends AbstractMessageAdapter
36  {
37      /**
38       * Serial version
39       */
40      private static final long serialVersionUID = 419878758858206446L;
41  
42      private final Object payload;
43      private MessageContext messageContext;
44  
45      private UMOTransformer trans = new SerializableToByteArray();
46  
47      public XFireMessageAdapter(Object message)
48      {
49          this.payload = message;
50      }
51  
52      protected XFireMessageAdapter(XFireMessageAdapter template)
53      {
54          super(template);
55          payload = template.payload;
56          messageContext = template.messageContext;
57      }
58  
59      /**
60       * Converts the message implementation into a String representation
61       * 
62       * @param encoding The encoding to use when transforming the message (if
63       *            necessary). The parameter is used when converting from a byte array
64       * @return String representation of the message payload
65       * @throws Exception Implementation may throw an endpoint specific exception
66       */
67      public String getPayloadAsString(String encoding) throws Exception
68      {
69          return new String(getPayloadAsBytes(), encoding);
70      }
71  
72      /**
73       * Converts the payload implementation into a String representation
74       * 
75       * @return String representation of the payload
76       * @throws Exception Implemetation may throw an endpoint specific exception
77       */
78      public byte[] getPayloadAsBytes() throws Exception
79      {
80          return (byte[]) trans.transform(payload);
81      }
82  
83      /**
84       * @return the current payload
85       */
86      public Object getPayload()
87      {
88          return payload;
89      }
90  
91      public void addAttachment(String name, DataHandler dataHandler) throws Exception
92      {
93          messageContext.getInMessage().getAttachments().addPart(new SimpleAttachment(name, dataHandler));
94          super.addAttachment(name, dataHandler);
95      }
96  
97      public void removeAttachment(String name) throws Exception
98      {
99          throw new UnsupportedOperationException("XFIRE: removeAttachment");
100         // TODO unable to remove an attachment from XFire Attachments
101     }
102 
103     public MessageContext getMessageContext()
104     {
105         return messageContext;
106     }
107 
108     public void setMessageContext(MessageContext messageContext)
109     {
110         this.messageContext = messageContext;
111         initHeaders();
112         // TODO what is the expense of reading attachments??
113         initAttachments();
114     }
115 
116     protected void initHeaders()
117     {
118         if (messageContext.getInMessage() != null)
119         {
120             Element header = messageContext.getInMessage().getHeader();
121             if (header == null)
122             {
123                 return;
124             }
125 
126             Namespace ns = Namespace.getNamespace(MuleSoapHeaders.MULE_NAMESPACE,
127                 MuleSoapHeaders.MULE_10_ACTOR);
128             Element muleHeaders = header.getChild(MuleSoapHeaders.MULE_HEADER, ns);
129             if (muleHeaders != null)
130             {
131                 Element child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_ID_PROPERTY, ns);
132                 if (child != null)
133                 {
134                     setCorrelationId(child.getText());
135                 }
136                 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, ns);
137                 if (child != null)
138                 {
139                     setCorrelationGroupSize(Integer.valueOf(child.getText()).intValue());
140                 }
141                 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, ns);
142                 if (child != null)
143                 {
144                     setCorrelationSequence(Integer.valueOf(child.getText()).intValue());
145                 }
146                 child = muleHeaders.getChild(MuleProperties.MULE_REPLY_TO_PROPERTY, ns);
147                 if (child != null)
148                 {
149                     setReplyTo(child.getText());
150                 }
151             }
152         }
153 
154     }
155 
156     protected void initAttachments()
157     {
158         try
159         {
160             Attachments atts = this.messageContext.getInMessage().getAttachments();
161             if (atts != null)
162             {
163                 for (Iterator i = atts.getParts(); i.hasNext();)
164                 {
165                     Attachment att = ((Attachment) i.next());
166                     super.addAttachment(att.getId(), att.getDataHandler());
167                 }
168             }
169         }
170         catch (Exception e)
171         {
172             // this will not happen
173             logger.fatal("Failed to read attachments", e);
174         }
175     }
176 
177     public ThreadSafeAccess newThreadCopy()
178     {
179         return new XFireMessageAdapter(this);
180     }
181 
182 }