KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > axis > AxisMessageAdapter


1 /*
2  * $Id: AxisMessageAdapter.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.axis;
12
13 import java.util.Iterator JavaDoc;
14
15 import javax.activation.DataHandler JavaDoc;
16 import javax.xml.soap.SOAPException JavaDoc;
17 import javax.xml.soap.SOAPMessage JavaDoc;
18
19 import org.apache.axis.MessageContext;
20 import org.apache.axis.attachments.AttachmentPart;
21 import org.apache.commons.lang.StringUtils;
22 import org.mule.config.i18n.Message;
23 import org.mule.providers.AbstractMessageAdapter;
24 import org.mule.providers.soap.MuleSoapHeaders;
25 import org.mule.transformers.simple.SerializableToByteArray;
26 import org.mule.umo.MessagingException;
27 import org.mule.umo.transformer.UMOTransformer;
28
29 /**
30  * <code>AxisMessageAdapter</code> wraps a soap message. The payload of the adapter
31  * is the raw message received from the transport, but you also have access to the
32  * SOAPMessage object by using <code>adapter.getSOAPMessage()</code>
33  */

34 public class AxisMessageAdapter extends AbstractMessageAdapter
35 {
36     /**
37      * Serial version
38      */

39     private static final long serialVersionUID = -923205879581370143L;
40
41     private final Object JavaDoc payload;
42     private final SOAPMessage JavaDoc message;
43     private UMOTransformer trans = new SerializableToByteArray();
44
45     public AxisMessageAdapter(Object JavaDoc message) throws MessagingException
46     {
47         this.payload = message;
48         try
49         {
50             MessageContext ctx = MessageContext.getCurrentContext();
51
52             if (ctx != null)
53             {
54                 MuleSoapHeaders header = new MuleSoapHeaders(ctx.getMessage().getSOAPPart().getEnvelope()
55                     .getHeader());
56
57                 if (StringUtils.isNotBlank(header.getReplyTo()))
58                 {
59                     setReplyTo(header.getReplyTo());
60                 }
61
62                 if (StringUtils.isNotBlank(header.getCorrelationGroup()))
63                 {
64                     setCorrelationGroupSize(Integer.parseInt(header.getCorrelationGroup()));
65                 }
66                 if (StringUtils.isNotBlank(header.getCorrelationSequence()))
67                 {
68                     setCorrelationSequence(Integer.parseInt(header.getCorrelationSequence()));
69                 }
70                 if (StringUtils.isNotBlank(header.getCorrelationId()))
71                 {
72                     setCorrelationId(header.getCorrelationId());
73                 }
74
75                 this.message = ctx.getMessage();
76                 int x = 1;
77                 try
78                 {
79                     for (Iterator JavaDoc i = this.message.getAttachments(); i.hasNext(); x++)
80                     {
81                         super.addAttachment(String.valueOf(x), ((AttachmentPart)i.next())
82                             .getActivationDataHandler());
83                     }
84                 }
85                 catch (Exception JavaDoc e)
86                 {
87                     // this will not happen
88
logger.fatal("Failed to read attachments", e);
89                 }
90             }
91             else
92             {
93                 this.message = null;
94             }
95         }
96         catch (SOAPException JavaDoc e)
97         {
98             throw new MessagingException(new Message JavaDoc("soap", 5), message, e);
99         }
100     }
101
102     /**
103      * Converts the message implementation into a String representation
104      *
105      * @param encoding The encoding to use when transforming the message (if
106      * necessary). The parameter is used when converting from a byte array
107      * @return String representation of the message payload
108      * @throws Exception Implementation may throw an endpoint specific exception
109      */

110     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
111     {
112         return new String JavaDoc(getPayloadAsBytes(), encoding);
113     }
114
115     /**
116      * Converts the payload implementation into a String representation
117      *
118      * @return String representation of the payload
119      * @throws Exception Implemetation may throw an endpoint specific exception
120      */

121     public byte[] getPayloadAsBytes() throws Exception JavaDoc
122     {
123         return (byte[])trans.transform(payload);
124     }
125
126     /**
127      * @return the current payload
128      */

129     public Object JavaDoc getPayload()
130     {
131         return payload;
132     }
133
134     public SOAPMessage JavaDoc getSoapMessage()
135     {
136         return message;
137     }
138
139     public void addAttachment(String JavaDoc name, DataHandler JavaDoc dataHandler) throws Exception JavaDoc
140     {
141         message.addAttachmentPart(new AttachmentPart(dataHandler));
142         super.addAttachment(name, dataHandler);
143     }
144
145     public void removeAttachment(String JavaDoc name) throws Exception JavaDoc
146     {
147         if ("all".equalsIgnoreCase(name))
148         {
149             message.removeAllAttachments();
150             attachments.clear();
151         }
152         else
153         {
154             throw new SOAPException JavaDoc(new Message JavaDoc("soap", 6).toString());
155         }
156     }
157 }
158
Popular Tags