KickJava   Java API By Example, From Geeks To Geeks.

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


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: SOAPPartImpl.java,v 1.1.2.2 2005/04/21 22:35:19 tdiesler Exp $
10

11 import org.jboss.axis.transport.http.HTTPConstants;
12 import org.jboss.axis.utils.Messages;
13 import org.jboss.logging.Logger;
14 import org.w3c.dom.Attr JavaDoc;
15 import org.w3c.dom.CDATASection JavaDoc;
16 import org.w3c.dom.Comment JavaDoc;
17 import org.w3c.dom.DOMException JavaDoc;
18 import org.w3c.dom.DOMImplementation JavaDoc;
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.DocumentFragment JavaDoc;
21 import org.w3c.dom.DocumentType JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.EntityReference JavaDoc;
24 import org.w3c.dom.NamedNodeMap JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.w3c.dom.ProcessingInstruction JavaDoc;
28 import org.w3c.dom.Text JavaDoc;
29 import org.w3c.dom.DOMConfiguration JavaDoc;
30 import org.w3c.dom.UserDataHandler JavaDoc;
31
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import javax.xml.soap.MimeHeaders JavaDoc;
35 import javax.xml.soap.SOAPEnvelope JavaDoc;
36 import javax.xml.soap.SOAPException JavaDoc;
37 import javax.xml.soap.SOAPMessage JavaDoc;
38 import javax.xml.transform.Source JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 /**
43  * An implemenation of the abstract SOAPPart.
44  * <p/>
45  * This class should not expose functionality that is not part of
46  * {@link javax.xml.soap.SOAPPart}. Client code should use <code>SOAPPart</code> whenever possible.
47  *
48  * @author Thomas Diesler (thomas.diesler@jboss.org)
49  * @since 31-May-2004
50  */

51 public class SOAPPartImpl extends javax.xml.soap.SOAPPart JavaDoc
52 {
53
54    private static Logger log = Logger.getLogger(SOAPPartImpl.class.getName());
55
56    private SOAPMessage JavaDoc soapMessage;
57    private MimeHeaders JavaDoc mimeHeaders;
58    private SOAPEnvelope JavaDoc soapEnvelope;
59
60    private Document JavaDoc document;
61
62    // The contentSource
63
private Source JavaDoc contentSource;
64
65    public SOAPPartImpl()
66    {
67    }
68
69    public SOAPPartImpl(SOAPMessage JavaDoc soapMessage, InputStream JavaDoc inStream, MimeHeaders JavaDoc headers)
70    {
71       this.soapMessage = soapMessage;
72
73       mimeHeaders = new MimeHeadersImpl(headers);
74       if (headers == null)
75       {
76          mimeHeaders = new MimeHeadersImpl();
77          mimeHeaders.addHeader(HTTPConstants.HEADER_CONTENT_TYPE, "text/xml");
78       }
79
80       try
81       {
82          DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
83          factory.setValidating(false);
84          factory.setNamespaceAware(true);
85          DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
86          document = builder.parse(inStream);
87          document.getDocumentElement();
88       }
89       catch (Exception JavaDoc e)
90       {
91          e.printStackTrace();
92       }
93    }
94
95    /**
96     * Add the specified MIME header, as per JAXM.
97     *
98     * @param header the header to add
99     * @param value the value of that header
100     */

101    public void addMimeHeader(String JavaDoc header, String JavaDoc value)
102    {
103       mimeHeaders.addHeader(header, value);
104    }
105
106    /**
107     * Content location.
108     *
109     * @return the content location
110     */

111    public String JavaDoc getContentLocation()
112    {
113       return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION);
114    }
115
116    /**
117     * Set content location.
118     *
119     * @param loc the content location
120     */

121    public void setContentLocation(String JavaDoc loc)
122    {
123       setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION, loc);
124    }
125
126    /**
127     * Sets Content-Id of this part.
128     * already defined.
129     *
130     * @param newCid new Content-Id
131     */

132    public void setContentId(String JavaDoc newCid)
133    {
134       setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, newCid);
135    }
136
137    /**
138     * Content ID.
139     *
140     * @return the content ID
141     */

142    public String JavaDoc getContentId()
143    {
144       return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_ID);
145    }
146
147    /**
148     * Get all headers that match.
149     *
150     * @param match an array of <code>String</code>s giving mime header names
151     * @return an <code>Iterator</code> over all values matching these headers
152     */

153    public Iterator JavaDoc getMatchingMimeHeaders(final String JavaDoc[] match)
154    {
155       return mimeHeaders.getMatchingHeaders(match);
156    }
157
158    /**
159     * Get all headers that do not match.
160     *
161     * @param match an array of <code>String</code>s giving mime header names
162     * @return an <code>Iterator</code> over all values not matching these
163     * headers
164     */

165    public Iterator JavaDoc getNonMatchingMimeHeaders(final String JavaDoc[] match)
166    {
167       return mimeHeaders.getNonMatchingHeaders(match);
168    }
169
170    /**
171     * Sets the content of the SOAPEnvelope object with the data from the given Source object.
172     * This Source must contain a valid SOAP document.
173     *
174     * @param source the {@link javax.xml.transform.Source} object with the data to be set
175     * @throws SOAPException if the implementation cannot convert the specified Source object
176     */

177    public void setContent(Source JavaDoc source) throws SOAPException JavaDoc
178    {
179
180       if (source == null)
181          throw new SOAPException JavaDoc(Messages.getMessage("illegalArgumentException00"));
182
183       //[todo-tdi] this is the place for the actual parsing, maybe
184

185       this.contentSource = source;
186       /*
187       InputSource in = org.jboss.axis.utils.XMLUtils.sourceToInputSource(contentSource);
188       InputStream is = in.getByteStream();
189       if(is != null) {
190           setCurrentMessage(is, FORM_INPUTSTREAM);
191       } else {
192           Reader r = in.getCharacterStream();
193           if(r == null) {
194               throw new SOAPException(Messages.getMessage("noCharacterOrByteStream"));
195           }
196           BufferedReader br = new BufferedReader(r);
197           String line = null;
198           StringBuffer sb = new StringBuffer();
199           try {
200               while((line = br.readLine()) != null) {
201                   sb.append(line);
202               }
203           } catch (IOException e) {
204               throw new SOAPException(Messages.getMessage("couldNotReadFromCharStream"), e);
205           }
206           setCurrentMessage(sb.toString(), FORM_STRING);
207       }
208       */

209    }
210
211    /**
212     * Returns the content of the SOAPEnvelope as a JAXP <CODE>Source</CODE> object.
213     *
214     * @return the content as a <CODE> javax.xml.transform.Source</CODE> object
215     * @throws javax.xml.soap.SOAPException if the implementation cannot convert the specified <CODE>Source</CODE> object
216     * @see #setContent(javax.xml.transform.Source) setContent(javax.xml.transform.Source)
217     */

218    public Source JavaDoc getContent() throws SOAPException JavaDoc
219    {
220       /*
221       if(contentSource == null) {
222           switch(currentForm) {
223           case FORM_STRING:
224               String s = (String)currentMessage;
225               contentSource = new StreamSource(new StringReader(s));
226               break;
227           case FORM_INPUTSTREAM:
228               contentSource = new StreamSource((InputStream)currentMessage);
229               break;
230           case FORM_SOAPENVELOPE:
231               SOAPEnvelopeImpl se = (SOAPEnvelopeImpl)currentMessage;
232               try {
233                   contentSource = new DOMSource(se.getAsDocument());
234               } catch (Exception e) {
235                   throw new SOAPException(Messages.getMessage("errorGetDocFromSOAPEnvelope"), e);
236               }
237               break;
238           case FORM_BYTES:
239               byte[] bytes = (byte[])currentMessage;
240               contentSource = new StreamSource(new ByteArrayInputStream(bytes));
241               break;
242               case FORM_BODYINSTREAM:
243               contentSource = new StreamSource((InputStream)currentMessage);
244               break;
245           }
246       }
247       */

248       return contentSource;
249    }
250
251    /**
252     * Retrieves all the headers for this <CODE>SOAPPart</CODE>
253     * object as an iterator over the <CODE>MimeHeader</CODE>
254     * objects.
255     *
256     * @return an <CODE>Iterator</CODE> object with all of the Mime
257     * headers for this <CODE>SOAPPart</CODE> object
258     */

259    public Iterator JavaDoc getAllMimeHeaders()
260    {
261       return mimeHeaders.getAllHeaders();
262    }
263
264    /**
265     * Changes the first header entry that matches the given
266     * header name so that its value is the given value, adding a
267     * new header with the given name and value if no existing
268     * header is a match. If there is a match, this method clears
269     * all existing values for the first header that matches and
270     * sets the given value instead. If more than one header has
271     * the given name, this method removes all of the matching
272     * headers after the first one.
273     * <p/>
274     * <P>Note that RFC822 headers can contain only US-ASCII
275     * characters.</P>
276     *
277     * @param name a <CODE>String</CODE> giving the
278     * header name for which to search
279     * @param value a <CODE>String</CODE> giving the
280     * value to be set. This value will be substituted for the
281     * current value(s) of the first header that is a match if
282     * there is one. If there is no match, this value will be
283     * the value for a new <CODE>MimeHeader</CODE> object.
284     * @ throws java.lang.IllegalArgumentException if
285     * there was a problem with the specified mime header name
286     * or value
287     * @see #getMimeHeader(String) getMimeHeader(java.lang.String)
288     */

289    public void setMimeHeader(String JavaDoc name, String JavaDoc value)
290    {
291       mimeHeaders.setHeader(name, value);
292    }
293
294    /**
295     * Gets all the values of the <CODE>MimeHeader</CODE> object
296     * in this <CODE>SOAPPart</CODE> object that is identified by
297     * the given <CODE>String</CODE>.
298     *
299     * @param name the name of the header; example:
300     * "Content-Type"
301     * @return a <CODE>String</CODE> array giving all the values for
302     * the specified header
303     * @see #setMimeHeader(String, String) setMimeHeader(java.lang.String, java.lang.String)
304     */

305    public String JavaDoc[] getMimeHeader(String JavaDoc name)
306    {
307       return mimeHeaders.getHeader(name);
308    }
309
310    /**
311     * Removes all the <CODE>MimeHeader</CODE> objects for this
312     * <CODE>SOAPEnvelope</CODE> object.
313     */

314    public void removeAllMimeHeaders()
315    {
316       mimeHeaders.removeAllHeaders();
317    }
318
319    /**
320     * Removes all MIME headers that match the given name.
321     *
322     * @param header a <CODE>String</CODE> giving
323     * the name of the MIME header(s) to be removed
324     */

325    public void removeMimeHeader(String JavaDoc header)
326    {
327       mimeHeaders.removeHeader(header);
328    }
329
330    /**
331     * Gets the <CODE>SOAPEnvelope</CODE> object associated with
332     * this <CODE>SOAPPart</CODE> object. Once the SOAP envelope is
333     * obtained, it can be used to get its contents.
334     *
335     * @return the <CODE>SOAPEnvelope</CODE> object for this <CODE>
336     * SOAPPart</CODE> object
337     * @throws javax.xml.soap.SOAPException if there is a SOAP error
338     */

339    public SOAPEnvelope JavaDoc getEnvelope() throws SOAPException JavaDoc
340    {
341       return soapEnvelope;
342    }
343
344    /**
345     * Get the specified MIME header.
346     *
347     * @param header the name of a MIME header
348     * @return the value of the first header named <code>header</code>
349     */

350    private String JavaDoc getFirstMimeHeader(String JavaDoc header)
351    {
352       String JavaDoc[] values = mimeHeaders.getHeader(header);
353       if (values != null && values.length > 0)
354          return values[0];
355       return null;
356    }
357
358    // org.w3c.dom.Document ******************************************************************************************
359

360    public DocumentType JavaDoc getDoctype()
361    {
362       return document.getDoctype();
363    }
364
365    public DOMImplementation JavaDoc getImplementation()
366    {
367       return document.getImplementation();
368    }
369
370    public Element JavaDoc getDocumentElement()
371    {
372       return document.getDocumentElement();
373    }
374
375    public Element JavaDoc createElement(String JavaDoc tagName) throws DOMException JavaDoc
376    {
377       return document.createElement(tagName);
378    }
379
380    public DocumentFragment JavaDoc createDocumentFragment()
381    {
382       return document.createDocumentFragment();
383    }
384
385    public Text JavaDoc createTextNode(String JavaDoc data)
386    {
387       return document.createTextNode(data);
388    }
389
390    public Comment JavaDoc createComment(String JavaDoc data)
391    {
392       return document.createComment(data);
393    }
394
395    public CDATASection JavaDoc createCDATASection(String JavaDoc data) throws DOMException JavaDoc
396    {
397       return document.createCDATASection(data);
398    }
399
400    public ProcessingInstruction JavaDoc createProcessingInstruction(String JavaDoc target, String JavaDoc data) throws DOMException JavaDoc
401    {
402       return document.createProcessingInstruction(target, data);
403    }
404
405    public Attr JavaDoc createAttribute(String JavaDoc name) throws DOMException JavaDoc
406    {
407       return document.createAttribute(name);
408    }
409
410    public EntityReference JavaDoc createEntityReference(String JavaDoc name) throws DOMException JavaDoc
411    {
412       return document.createEntityReference(name);
413    }
414
415    public NodeList JavaDoc getElementsByTagName(String JavaDoc tagname)
416    {
417       return document.getElementsByTagName(tagname);
418    }
419
420    public Node JavaDoc importNode(Node JavaDoc importedNode, boolean deep) throws DOMException JavaDoc
421    {
422       return document.importNode(importedNode, deep);
423    }
424
425    public Element JavaDoc createElementNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
426            throws DOMException JavaDoc
427    {
428       return document.createElementNS(namespaceURI, qualifiedName);
429    }
430
431    public Attr JavaDoc createAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
432            throws DOMException JavaDoc
433    {
434       return document.createAttributeNS(namespaceURI, qualifiedName);
435    }
436
437    public NodeList JavaDoc getElementsByTagNameNS(String JavaDoc namespaceURI, String JavaDoc localName)
438    {
439       return document.getElementsByTagNameNS(namespaceURI, localName);
440    }
441
442    public Element JavaDoc getElementById(String JavaDoc elementId)
443    {
444       return document.getElementById(elementId);
445    }
446
447    // org.w3c.dom.Node *******************************************************************************************
448

449    public String JavaDoc getNodeName()
450    {
451       return document.getNodeName();
452    }
453
454    public String JavaDoc getNodeValue() throws DOMException JavaDoc
455    {
456       return document.getNodeValue();
457    }
458
459    public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc
460    {
461       document.setNodeValue(nodeValue);
462    }
463
464    public short getNodeType()
465    {
466       return document.getNodeType();
467    }
468
469    public Node JavaDoc getParentNode()
470    {
471       return document.getParentNode();
472    }
473
474    public NodeList JavaDoc getChildNodes()
475    {
476       return document.getChildNodes();
477    }
478
479    public Node JavaDoc getFirstChild()
480    {
481       return document.getFirstChild();
482    }
483
484    public Node JavaDoc getLastChild()
485    {
486       return document.getLastChild();
487    }
488
489    public Node JavaDoc getPreviousSibling()
490    {
491       return document.getPreviousSibling();
492    }
493
494    public Node JavaDoc getNextSibling()
495    {
496       return document.getNextSibling();
497    }
498
499    public NamedNodeMap JavaDoc getAttributes()
500    {
501       return document.getAttributes();
502    }
503
504    public Document JavaDoc getOwnerDocument()
505    {
506       return document.getOwnerDocument();
507    }
508
509    public Node JavaDoc insertBefore(Node JavaDoc newChild, Node JavaDoc refChild) throws DOMException JavaDoc
510    {
511       return document.insertBefore(newChild, refChild);
512    }
513
514    public Node JavaDoc replaceChild(Node JavaDoc newChild, Node JavaDoc oldChild) throws DOMException JavaDoc
515    {
516       return document.replaceChild(newChild, oldChild);
517    }
518
519    public Node JavaDoc removeChild(Node JavaDoc oldChild) throws DOMException JavaDoc
520    {
521       return document.removeChild(oldChild);
522    }
523
524    public Node JavaDoc appendChild(Node JavaDoc newChild) throws DOMException JavaDoc
525    {
526       return document.appendChild(newChild);
527    }
528
529    public boolean hasChildNodes()
530    {
531       return document.hasChildNodes();
532    }
533
534    public Node JavaDoc cloneNode(boolean deep)
535    {
536       return document.cloneNode(deep);
537    }
538
539    public void normalize()
540    {
541       document.normalize();
542    }
543
544    public boolean isSupported(String JavaDoc feature, String JavaDoc version)
545    {
546       return document.isSupported(feature, version);
547    }
548
549    public String JavaDoc getNamespaceURI()
550    {
551       return document.getNamespaceURI();
552    }
553
554    public String JavaDoc getPrefix()
555    {
556       return document.getPrefix();
557    }
558
559    public void setPrefix(String JavaDoc prefix) throws DOMException JavaDoc
560    {
561       document.setPrefix(prefix);
562    }
563
564    public String JavaDoc getLocalName()
565    {
566       return document.getLocalName();
567    }
568
569    public boolean hasAttributes()
570    {
571       return document.hasAttributes();
572    }
573
574    // DOM3-API start ***************************************************************************************************
575
public String JavaDoc getInputEncoding()
576    {
577       return null;
578    }
579
580    public String JavaDoc getXmlEncoding()
581    {
582       return null;
583    }
584
585    public boolean getXmlStandalone()
586    {
587       return false;
588    }
589
590    public void setXmlStandalone(boolean xmlStandalone) throws DOMException JavaDoc
591    {
592
593    }
594
595    public String JavaDoc getXmlVersion()
596    {
597       return null;
598    }
599
600    public void setXmlVersion(String JavaDoc xmlVersion) throws DOMException JavaDoc
601    {
602
603    }
604
605    public boolean getStrictErrorChecking()
606    {
607       return false;
608    }
609
610    public void setStrictErrorChecking(boolean strictErrorChecking)
611    {
612
613    }
614
615    public String JavaDoc getDocumentURI()
616    {
617       return null;
618    }
619
620    public void setDocumentURI(String JavaDoc documentURI)
621    {
622
623    }
624
625    public Node JavaDoc adoptNode(Node JavaDoc source) throws DOMException JavaDoc
626    {
627       return null;
628    }
629
630    public DOMConfiguration JavaDoc getDomConfig()
631    {
632       return null;
633    }
634
635    public void normalizeDocument()
636    {
637
638    }
639
640    public Node JavaDoc renameNode(Node JavaDoc n, String JavaDoc namespaceURI, String JavaDoc qualifiedName) throws DOMException JavaDoc
641    {
642       return null;
643    }
644    public String JavaDoc getBaseURI()
645    {
646       return null;
647    }
648
649    public short compareDocumentPosition(Node JavaDoc other) throws DOMException JavaDoc
650    {
651       return 0;
652    }
653
654    public String JavaDoc getTextContent() throws DOMException JavaDoc
655    {
656       return null;
657    }
658
659    public void setTextContent(String JavaDoc textContent) throws DOMException JavaDoc
660    {
661
662    }
663
664    public boolean isSameNode(Node JavaDoc other)
665    {
666       return false;
667    }
668
669    public String JavaDoc lookupPrefix(String JavaDoc namespaceURI)
670    {
671       return null;
672    }
673
674    public boolean isDefaultNamespace(String JavaDoc namespaceURI)
675    {
676       return false;
677    }
678
679    public String JavaDoc lookupNamespaceURI(String JavaDoc prefix)
680    {
681       return null;
682    }
683
684    public boolean isEqualNode(Node JavaDoc arg)
685    {
686       return false;
687    }
688
689    public Object JavaDoc getFeature(String JavaDoc feature, String JavaDoc version)
690    {
691       return null;
692    }
693
694    public Object JavaDoc setUserData(String JavaDoc key, Object JavaDoc data, UserDataHandler JavaDoc handler)
695    {
696       return null;
697    }
698
699    public Object JavaDoc getUserData(String JavaDoc key)
700    {
701       return null;
702    }
703    // DOM3-API end *****************************************************************************************************
704
}
705
Popular Tags