KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > SOAPMessageImpl


1 /**
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.axis.message;
8
9 // $Id: SOAPMessageImpl.java,v 1.1.2.1 2005/03/02 14:29:31 tdiesler Exp $
10

11 import org.jboss.axis.AxisFault;
12 import org.jboss.axis.attachments.AttachmentSupport;
13 import org.jboss.axis.attachments.Attachments;
14 import org.jboss.axis.transport.http.HTTPConstants;
15 import org.jboss.axis.utils.Messages;
16 import org.jboss.logging.Logger;
17
18 import javax.xml.soap.AttachmentPart JavaDoc;
19 import javax.xml.soap.MimeHeaders JavaDoc;
20 import javax.xml.soap.SOAPBody JavaDoc;
21 import javax.xml.soap.SOAPException JavaDoc;
22 import javax.xml.soap.SOAPHeader JavaDoc;
23 import javax.xml.soap.SOAPMessage JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * An implemenation of the abstract SOAPMessage.
32  * <p/>
33  * This class should not expose functionality that is not part of
34  * {@link SOAPMessage}. Client code should use <code>SOAPMessage</code> whenever possible.
35  *
36  * @author Thomas Diesler (thomas.diesler@jboss.org)
37  * @since 31-May-2004
38  */

39 public class SOAPMessageImpl extends SOAPMessage JavaDoc
40 {
41    /**
42     * The <code>Log</code> that this class uses for logging all messages.
43     */

44    private static Logger log = Logger.getLogger(SOAPMessageAxisImpl.class.getName());
45
46    // This Message's SOAPPart.
47
protected javax.xml.soap.SOAPPart JavaDoc mSOAPPart;
48    // This Message's Attachments object, which manages the attachments contained in this Message.
49
protected Attachments mAttachments;
50    // The MimeHeaders
51
protected MimeHeadersImpl headers;
52
53    private Hashtable JavaDoc mProps = new Hashtable JavaDoc();
54    private boolean saveRequired = true;
55
56    protected SOAPMessageImpl()
57    {
58    }
59
60    /**
61     * Writes this <CODE>SOAPMessage</CODE> object to the given
62     * output stream. The externalization format is as defined by
63     * the SOAP 1.1 with Attachments specification.
64     * <p/>
65     * <P>If there are no attachments, just an XML stream is
66     * written out. For those messages that have attachments,
67     * <CODE>writeTo</CODE> writes a MIME-encoded byte stream.</P>
68     *
69     * @param os the <CODE>OutputStream</CODE>
70     * object to which this <CODE>SOAPMessage</CODE> object will
71     * be written
72     * @throws javax.xml.soap.SOAPException if there was a problem in
73     * externalizing this SOAP message
74     * @throws java.io.IOException if an I/O error
75     * occurs
76     */

77    public void writeTo(OutputStream JavaDoc os) throws SOAPException JavaDoc, IOException JavaDoc
78    {
79       //Do it the old fashion way.
80
/*
81         if (mAttachments == null || 0 == mAttachments.getAttachmentCount()) {
82             try {
83                 String charEncoding = (String)getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
84                 if(charEncoding == null){
85                     charEncoding = "UTF-8";
86                 }
87                 Writer writer = new OutputStreamWriter(os,charEncoding);
88                 writer = new BufferedWriter(writer);
89
90                 // write the xml declaration header
91                 String incXMLDecl = (String)getProperty(SOAPMessage.WRITE_XML_DECLARATION);
92                 if(incXMLDecl == null){
93                     incXMLDecl = "false";
94                 }
95                 if(incXMLDecl.equalsIgnoreCase("true")){
96                     writer.write("<?xml version=\"1.0\" encoding=\"" + charEncoding +"\"?>");
97                 }
98                 mSOAPPart.writeTo(writer);
99                 writer.flush();
100             } catch (IOException e) {
101                 log.error(Messages.getMessage("javaIOException00"), e);
102             }
103         } else {
104             try {
105                 mAttachments.writeContentToStream(os);
106             } catch (Exception e) {
107                 log.error(Messages.getMessage("exception00"), e);
108             }
109         }
110 */

111    }
112
113    public SOAPBody JavaDoc getSOAPBody() throws SOAPException JavaDoc
114    {
115       return mSOAPPart.getEnvelope().getBody();
116    }
117
118    public SOAPHeader JavaDoc getSOAPHeader() throws SOAPException JavaDoc
119    {
120       return mSOAPPart.getEnvelope().getHeader();
121    }
122
123    public void setProperty(String JavaDoc property, Object JavaDoc value) throws SOAPException JavaDoc
124    {
125       mProps.put(property, value);
126    }
127
128    public Object JavaDoc getProperty(String JavaDoc property) throws SOAPException JavaDoc
129    {
130       return mProps.get(property);
131    }
132
133    /**
134     * Retrieves a description of this <CODE>SOAPMessage</CODE>
135     * object's content.
136     *
137     * @return a <CODE>String</CODE> describing the content of this
138     * message or <CODE>null</CODE> if no description has been
139     * set
140     * @see #setContentDescription(String) setContentDescription(java.lang.String)
141     */

142    public String JavaDoc getContentDescription()
143    {
144       String JavaDoc values[] = headers.getHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION);
145       if (values != null && values.length > 0)
146          return values[0];
147       return null;
148    }
149
150    /**
151     * Sets the description of this <CODE>SOAPMessage</CODE>
152     * object's content with the given description.
153     *
154     * @param description a <CODE>String</CODE>
155     * describing the content of this message
156     * @see #getContentDescription() getContentDescription()
157     */

158    public void setContentDescription(String JavaDoc description)
159    {
160       headers.setHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION, description);
161    }
162
163    /**
164     * Updates this <CODE>SOAPMessage</CODE> object with all the
165     * changes that have been made to it. This method is called
166     * automatically when a message is sent or written to by the
167     * methods <CODE>ProviderConnection.send</CODE>, <CODE>
168     * SOAPConnection.call</CODE>, or <CODE>
169     * SOAPMessage.writeTo</CODE>. However, if changes are made to
170     * a message that was received or to one that has already been
171     * sent, the method <CODE>saveChanges</CODE> needs to be
172     * called explicitly in order to save the changes. The method
173     * <CODE>saveChanges</CODE> also generates any changes that
174     * can be read back (for example, a MessageId in profiles that
175     * support a message id). All MIME headers in a message that
176     * is created for sending purposes are guaranteed to have
177     * valid values only after <CODE>saveChanges</CODE> has been
178     * called.
179     * <p/>
180     * <P>In addition, this method marks the point at which the
181     * data from all constituent <CODE>AttachmentPart</CODE>
182     * objects are pulled into the message.</P>
183     *
184     * @throws javax.xml.soap.SOAPException if there
185     * was a problem saving changes to this message.
186     */

187    public void saveChanges() throws SOAPException JavaDoc
188    {
189 /*
190         if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) {
191             try {
192                 headers.setHeader("Content-Type",mAttachments.getContentType());
193             } catch (AxisFault af){
194                 log.error(Messages.getMessage("exception00"), af);
195             }
196         }
197         saveRequired = false;
198         try {
199             // Fix for Bug 16418 - Start from scratch
200             getSOAPPartAsString();
201         } catch (AxisFault axisFault) {
202             log.error(Messages.getMessage("exception00"), axisFault);
203         }
204 */

205    }
206
207    /**
208     * Indicates whether this <CODE>SOAPMessage</CODE> object
209     * has had the method <CODE>saveChanges</CODE> called on
210     * it.
211     *
212     * @return <CODE>true</CODE> if <CODE>saveChanges</CODE> has
213     * been called on this message at least once; <CODE>
214     * false</CODE> otherwise.
215     */

216    public boolean saveRequired()
217    {
218       return saveRequired;
219    }
220
221    /**
222     * Returns all the transport-specific MIME headers for this
223     * <CODE>SOAPMessage</CODE> object in a transport-independent
224     * fashion.
225     *
226     * @return a <CODE>MimeHeaders</CODE> object containing the
227     * <CODE>MimeHeader</CODE> objects
228     */

229    public MimeHeaders JavaDoc getMimeHeaders()
230    {
231       return headers;
232    }
233
234    public javax.xml.soap.SOAPPart JavaDoc getSOAPPart()
235    {
236       return mSOAPPart;
237    }
238
239    /**
240     * Removes all <CODE>AttachmentPart</CODE> objects that have
241     * been added to this <CODE>SOAPMessage</CODE> object.
242     * <p/>
243     * <P>This method does not touch the SOAP part.</P>
244     */

245    public void removeAllAttachments()
246    {
247       mAttachments.removeAllAttachments();
248    }
249
250    /**
251     * Gets a count of the number of attachments in this
252     * message. This count does not include the SOAP part.
253     *
254     * @return the number of <CODE>AttachmentPart</CODE> objects
255     * that are part of this <CODE>SOAPMessage</CODE>
256     * object
257     */

258    public int countAttachments()
259    {
260       return mAttachments == null ? 0 : mAttachments.getAttachmentCount();
261    }
262
263    /**
264     * Retrieves all the <CODE>AttachmentPart</CODE> objects
265     * that are part of this <CODE>SOAPMessage</CODE> object.
266     *
267     * @return an iterator over all the attachments in this
268     * message
269     */

270    public Iterator JavaDoc getAttachments()
271    {
272       try
273       {
274          if (mAttachments != null && 0 != mAttachments.getAttachmentCount())
275          {
276             return mAttachments.getAttachments().iterator();
277          }
278       }
279       catch (AxisFault af)
280       {
281          log.error(Messages.getMessage("exception00"), af);
282       }
283       return Collections.EMPTY_LIST.iterator();
284    }
285
286    /**
287     * Retrieves all the <CODE>AttachmentPart</CODE> objects
288     * that have header entries that match the specified headers.
289     * Note that a returned attachment could have headers in
290     * addition to those specified.
291     *
292     * @param headers a <CODE>MimeHeaders</CODE>
293     * object containing the MIME headers for which to
294     * search
295     * @return an iterator over all attachments that have a header
296     * that matches one of the given headers
297     */

298    public Iterator JavaDoc getAttachments(javax.xml.soap.MimeHeaders JavaDoc headers)
299    {
300       return mAttachments.getAttachments(headers);
301    }
302
303    /**
304     * Adds the given <CODE>AttachmentPart</CODE> object to this
305     * <CODE>SOAPMessage</CODE> object. An <CODE>
306     * AttachmentPart</CODE> object must be created before it can be
307     * added to a message.
308     *
309     * @param attachmentpart an <CODE>
310     * AttachmentPart</CODE> object that is to become part of
311     * this <CODE>SOAPMessage</CODE> object
312     * @throws IllegalArgumentException
313     */

314    public void addAttachmentPart(AttachmentPart JavaDoc attachmentpart)
315    {
316       try
317       {
318          mAttachments.addAttachmentPart((org.jboss.axis.Part)attachmentpart);
319       }
320       catch (AxisFault af)
321       {
322          log.error(Messages.getMessage("exception00"), af);
323       }
324    }
325
326    /**
327     * Creates a new empty <CODE>AttachmentPart</CODE> object.
328     * Note that the method <CODE>addAttachmentPart</CODE> must be
329     * called with this new <CODE>AttachmentPart</CODE> object as
330     * the parameter in order for it to become an attachment to this
331     * <CODE>SOAPMessage</CODE> object.
332     *
333     * @return a new <CODE>AttachmentPart</CODE> object that can be
334     * populated and added to this <CODE>SOAPMessage</CODE>
335     * object
336     */

337    public AttachmentPart JavaDoc createAttachmentPart()
338    {
339       if (!AttachmentSupport.isAttachmentSupportEnabled())
340       {
341          throw new RuntimeException JavaDoc(Messages.getMessage("noAttachments"));
342       }
343
344       try
345       {
346          return (AttachmentPart JavaDoc)mAttachments.createAttachmentPart();
347       }
348       catch (AxisFault af)
349       {
350          log.error(Messages.getMessage("exception00"), af);
351       }
352       return null;
353    }
354 }
355
Popular Tags