KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > xfire > XFireMessageAdapter


1 /*
2  * $Id: XFireMessageAdapter.java 3982 2006-11-22 14:28:01Z lajos $
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 MuleSource MPL
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 java.util.Iterator JavaDoc;
14
15 import javax.activation.DataHandler JavaDoc;
16
17 import org.codehaus.xfire.MessageContext;
18 import org.codehaus.xfire.attachments.Attachment;
19 import org.codehaus.xfire.attachments.Attachments;
20 import org.codehaus.xfire.attachments.SimpleAttachment;
21 import org.jdom.Element;
22 import org.jdom.Namespace;
23 import org.mule.config.MuleProperties;
24 import org.mule.providers.AbstractMessageAdapter;
25 import org.mule.providers.soap.MuleSoapHeaders;
26 import org.mule.transformers.simple.SerializableToByteArray;
27 import org.mule.umo.transformer.UMOTransformer;
28
29 /**
30  * <code>XFireMessageAdapter</code> wraps an XFire MessageContext, reading
31  * attachments and Mule headers.
32  */

33 public class XFireMessageAdapter extends AbstractMessageAdapter
34 {
35     /**
36      * Serial version
37      */

38     private static final long serialVersionUID = 419878758858206446L;
39
40     private final Object JavaDoc payload;
41     private MessageContext messageContext;
42
43     private UMOTransformer trans = new SerializableToByteArray();
44
45     public XFireMessageAdapter(Object JavaDoc message)
46     {
47         this.payload = message;
48     }
49
50     /**
51      * Converts the message implementation into a String representation
52      *
53      * @param encoding The encoding to use when transforming the message (if
54      * necessary). The parameter is used when converting from a byte array
55      * @return String representation of the message payload
56      * @throws Exception Implementation may throw an endpoint specific exception
57      */

58     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
59     {
60         return new String JavaDoc(getPayloadAsBytes(), encoding);
61     }
62
63     /**
64      * Converts the payload implementation into a String representation
65      *
66      * @return String representation of the payload
67      * @throws Exception Implemetation may throw an endpoint specific exception
68      */

69     public byte[] getPayloadAsBytes() throws Exception JavaDoc
70     {
71         return (byte[])trans.transform(payload);
72     }
73
74     /**
75      * @return the current payload
76      */

77     public Object JavaDoc getPayload()
78     {
79         return payload;
80     }
81
82     public void addAttachment(String JavaDoc name, DataHandler JavaDoc dataHandler) throws Exception JavaDoc
83     {
84         messageContext.getInMessage().getAttachments().addPart(new SimpleAttachment(name, dataHandler));
85         super.addAttachment(name, dataHandler);
86     }
87
88     public void removeAttachment(String JavaDoc name) throws Exception JavaDoc
89     {
90         throw new UnsupportedOperationException JavaDoc("XFIRE: removeAttachment");
91         // TODO unable to remove an attachment from XFire Attachments
92
}
93
94     public MessageContext getMessageContext()
95     {
96         return messageContext;
97     }
98
99     public void setMessageContext(MessageContext messageContext)
100     {
101         this.messageContext = messageContext;
102         initHeaders();
103         // TODO what is the expense of reading attachments??
104
initAttachments();
105     }
106
107     protected void initHeaders()
108     {
109         if (messageContext.getInMessage() != null)
110         {
111             Element header = messageContext.getInMessage().getHeader();
112             if (header == null) return;
113
114             Namespace ns = Namespace.getNamespace(MuleSoapHeaders.MULE_NAMESPACE,
115                 MuleSoapHeaders.MULE_10_ACTOR);
116             Element muleHeaders = header.getChild(MuleSoapHeaders.MULE_HEADER, ns);
117             if (muleHeaders != null)
118             {
119                 Element child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_ID_PROPERTY, ns);
120                 if (child != null)
121                 {
122                     setCorrelationId(child.getText());
123                 }
124                 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, ns);
125                 if (child != null)
126                 {
127                     setCorrelationGroupSize(Integer.valueOf(child.getText()).intValue());
128                 }
129                 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, ns);
130                 if (child != null)
131                 {
132                     setCorrelationSequence(Integer.valueOf(child.getText()).intValue());
133                 }
134                 child = muleHeaders.getChild(MuleProperties.MULE_REPLY_TO_PROPERTY, ns);
135                 if (child != null)
136                 {
137                     setReplyTo(child.getText());
138                 }
139             }
140         }
141
142     }
143
144     protected void initAttachments()
145     {
146         try
147         {
148             Attachments atts = this.messageContext.getInMessage().getAttachments();
149             if (atts != null)
150             {
151                 for (Iterator JavaDoc i = atts.getParts(); i.hasNext();)
152                 {
153                     Attachment att = ((Attachment)i.next());
154                     super.addAttachment(att.getId(), att.getDataHandler());
155                 }
156             }
157         }
158         catch (Exception JavaDoc e)
159         {
160             // this will not happen
161
logger.fatal("Failed to read attachments", e);
162         }
163     }
164
165 }
166
Popular Tags