KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > saaj > SOAPPartImpl


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.saaj;
17
18 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder;
19 import org.apache.axis2.transport.http.HTTPConstants;
20 import org.apache.axis2.util.SessionUtils;
21 import org.w3c.dom.*;
22
23 import javax.xml.soap.MimeHeaders JavaDoc;
24 import javax.xml.soap.SOAPEnvelope JavaDoc;
25 import javax.xml.soap.SOAPException JavaDoc;
26 import javax.xml.soap.SOAPPart JavaDoc;
27 import javax.xml.stream.XMLInputFactory;
28 import javax.xml.transform.Source JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * @author Ashutosh Shahi ashutosh.shahi@gmail.com
35  *
36  * TODO To change the template for this generated type comment go to
37  * Window - Preferences - Java - Code Style - Code Templates
38  */

39 public class SOAPPartImpl extends SOAPPart JavaDoc {
40
41     private SOAPMessageImpl msgObject;
42     private MimeHeaders mimeHeaders = new MimeHeaders();
43     private Object JavaDoc envelope;
44     /**
45      * default message encoding charset
46      */

47     private String JavaDoc currentEncoding = "UTF-8";
48     
49     public SOAPPartImpl(SOAPMessageImpl parent, Object JavaDoc initialContents, boolean isBodyStream) throws SOAPException JavaDoc{
50         
51         setMimeHeader(HTTPConstants.HEADER_CONTENT_ID , SessionUtils.generateSessionId());
52         setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE , "text/xml");
53         StAXSOAPModelBuilder stAXSOAPModelBuilder;
54         
55         msgObject = parent;
56         try{
57             if(initialContents instanceof SOAPEnvelope JavaDoc){
58                 ((SOAPEnvelopeImpl)initialContents).setOwnerDocument(this);
59                 envelope = initialContents;
60             } else if(initialContents instanceof InputStream JavaDoc){
61                 //XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader((InputStream)initialContents);
62
InputStreamReader JavaDoc inr = new InputStreamReader JavaDoc((InputStream JavaDoc)initialContents);
63                 stAXSOAPModelBuilder = new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(inr));
64                 org.apache.axis2.soap.SOAPEnvelope omEnv = stAXSOAPModelBuilder.getSOAPEnvelope();
65                 envelope = new SOAPEnvelopeImpl(omEnv);
66                 ((SOAPEnvelopeImpl)envelope).setOwnerDocument(this);
67             }
68         
69         }catch(Exception JavaDoc e){
70             throw new SOAPException JavaDoc(e);
71         }
72     }
73     
74     public SOAPMessageImpl getMessage(){
75         return msgObject;
76       }
77
78       /**
79        * Set the Message for this Part.
80        * Do not call this Directly. Called by Message.
81        *
82        * @param msg the <code>Message</code> for this part
83        */

84       public void setMessage (SOAPMessageImpl msg) {
85           this.msgObject= msg;
86       }
87     
88     /* (non-Javadoc)
89      * @see javax.xml.soap.SOAPPart#getEnvelope()
90      */

91     public SOAPEnvelope JavaDoc getEnvelope() throws SOAPException JavaDoc {
92         //if(envelope != null)
93
return (SOAPEnvelope JavaDoc)envelope;
94         
95     }
96
97     /**
98      * Removes all MIME headers that match the given name.
99      * @param header a <CODE>String</CODE> giving
100      * the name of the MIME header(s) to be removed
101      */

102     public void removeMimeHeader(String JavaDoc header) {
103         mimeHeaders.removeHeader(header);
104     }
105
106        /**
107      * Removes all the <CODE>MimeHeader</CODE> objects for this
108      * <CODE>SOAPEnvelope</CODE> object.
109      */

110     public void removeAllMimeHeaders() {
111         mimeHeaders.removeAllHeaders();
112     }
113
114     /**
115      * Gets all the values of the <CODE>MimeHeader</CODE> object
116      * in this <CODE>SOAPPart</CODE> object that is identified by
117      * the given <CODE>String</CODE>.
118      * @param name the name of the header; example:
119      * "Content-Type"
120      * @return a <CODE>String</CODE> array giving all the values for
121      * the specified header
122      * @see #setMimeHeader(java.lang.String, java.lang.String) setMimeHeader(java.lang.String, java.lang.String)
123      */

124     public String JavaDoc[] getMimeHeader(String JavaDoc name) {
125         return mimeHeaders.getHeader(name);
126     }
127
128     /**
129      * Changes the first header entry that matches the given
130      * header name so that its value is the given value, adding a
131      * new header with the given name and value if no existing
132      * header is a match. If there is a match, this method clears
133      * all existing values for the first header that matches and
134      * sets the given value instead. If more than one header has
135      * the given name, this method removes all of the matching
136      * headers after the first one.
137      *
138      * <P>Note that RFC822 headers can contain only US-ASCII
139      * characters.</P>
140      * @param name a <CODE>String</CODE> giving the
141      * header name for which to search
142      * @param value a <CODE>String</CODE> giving the
143      * value to be set. This value will be substituted for the
144      * current value(s) of the first header that is a match if
145      * there is one. If there is no match, this value will be
146      * the value for a new <CODE>MimeHeader</CODE> object.
147      * @ throws java.lang.IllegalArgumentException if
148      * there was a problem with the specified mime header name
149      * or value
150      * @see #getMimeHeader(java.lang.String) getMimeHeader(java.lang.String)
151      */

152     public void setMimeHeader(String JavaDoc name, String JavaDoc value) {
153         mimeHeaders.setHeader(name,value);
154     }
155
156     /**
157      * Add the specified MIME header, as per JAXM.
158      *
159      * @param header the header to add
160      * @param value the value of that header
161      */

162     public void addMimeHeader (String JavaDoc header, String JavaDoc value) {
163         mimeHeaders.addHeader(header, value);
164     }
165
166     /**
167      * Retrieves all the headers for this <CODE>SOAPPart</CODE>
168      * object as an iterator over the <CODE>MimeHeader</CODE>
169      * objects.
170      * @return an <CODE>Iterator</CODE> object with all of the Mime
171      * headers for this <CODE>SOAPPart</CODE> object
172      */

173     public Iterator JavaDoc getAllMimeHeaders() {
174         return mimeHeaders.getAllHeaders();
175     }
176
177     /**
178      * Get all headers that match.
179      *
180      * @param match an array of <code>String</code>s giving mime header names
181      * @return an <code>Iterator</code> over all values matching these headers
182      */

183     public java.util.Iterator JavaDoc getMatchingMimeHeaders( final String JavaDoc[] match){
184         return mimeHeaders.getMatchingHeaders(match);
185     }
186
187     /**
188      * Get all headers that do not match.
189      *
190      * @param match an array of <code>String</code>s giving mime header names
191      * @return an <code>Iterator</code> over all values not matching these
192      * headers
193      */

194     public java.util.Iterator JavaDoc getNonMatchingMimeHeaders( final String JavaDoc[] match){
195         return mimeHeaders.getNonMatchingHeaders(match);
196     }
197
198     /* (non-Javadoc)
199      * @see javax.xml.soap.SOAPPart#setContent(javax.xml.transform.Source)
200      */

201     public void setContent(Source JavaDoc source) throws SOAPException JavaDoc {
202         // TODO Auto-generated method stub
203

204     }
205
206     /* (non-Javadoc)
207      * @see javax.xml.soap.SOAPPart#getContent()
208      */

209     public Source JavaDoc getContent() throws SOAPException JavaDoc {
210         // TODO Auto-generated method stub
211
return null;
212     }
213
214     /**
215      * Implementation of org.w3c.Document
216      * Most of methods will be implemented using the delgate
217      * instance of SOAPDocumentImpl
218      * This is for two reasons:
219      * - possible change of message classes, by extenstion of xerces implementation
220      * - we cannot extends SOAPPart (multiple inheritance),
221      * since it is defined as Abstract class
222      * ***********************************************************
223      */

224
225     private Document document = new SOAPDocumentImpl(this);
226     /**
227      * @since SAAJ 1.2
228      */

229     public Document getSOAPDocument(){
230         if(document == null){
231             document = new SOAPDocumentImpl(this);
232         }
233         return document;
234     }
235
236     /**
237      * @return
238      */

239     public DocumentType getDoctype(){
240         return document.getDoctype();
241     }
242
243     /**
244      * @return
245      */

246     public DOMImplementation getImplementation(){
247         return document.getImplementation();
248     }
249
250     /**
251      * SOAPEnvelope is the Document Elements of this XML docuement
252      */

253     protected Document mDocument;
254
255     public Element getDocumentElement()
256     {
257         try{
258             return getEnvelope();
259         }catch(SOAPException JavaDoc se){
260             return null;
261         }
262     }
263
264     /**
265      *
266      * @param tagName
267      * @return
268      * @throws DOMException
269      */

270     public Element createElement(String JavaDoc tagName) throws DOMException {
271         return document.createElement(tagName);
272     }
273
274     public DocumentFragment createDocumentFragment() {
275         return document.createDocumentFragment();
276     }
277
278     public Text createTextNode(String JavaDoc data) {
279         return document.createTextNode(data);
280     }
281
282     public Comment createComment(String JavaDoc data){
283         return document.createComment(data);
284     }
285
286     public CDATASection createCDATASection(String JavaDoc data) throws DOMException {
287         return document.createCDATASection(data);
288     }
289
290     public ProcessingInstruction createProcessingInstruction(String JavaDoc target, String JavaDoc data)
291     throws DOMException {
292         return document.createProcessingInstruction(target,data);
293     }
294
295     public Attr createAttribute(String JavaDoc name)throws DOMException {
296         return document.createAttribute(name);
297     }
298
299     public EntityReference createEntityReference(String JavaDoc name) throws DOMException {
300         return document.createEntityReference(name);
301     }
302
303     public NodeList getElementsByTagName(String JavaDoc tagname) {
304         return document.getElementsByTagName(tagname);
305     }
306
307     public Node importNode(Node importedNode, boolean deep)
308     throws DOMException {
309         return document.importNode(importedNode, deep);
310     }
311
312     public Element createElementNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
313     throws DOMException {
314         return document.createElementNS(namespaceURI, qualifiedName);
315     }
316
317     public Attr createAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
318     throws DOMException {
319         return document.createAttributeNS(namespaceURI, qualifiedName);
320     }
321
322     public NodeList getElementsByTagNameNS(String JavaDoc namespaceURI, String JavaDoc localName) {
323         return document.getElementsByTagNameNS(namespaceURI,localName);
324     }
325
326     public Element getElementById(String JavaDoc elementId){
327         return document.getElementById(elementId);
328     }
329
330     /////////////////////////////////////////////////////////////
331

332     public String JavaDoc getEncoding()
333     {
334         return currentEncoding;
335     }
336
337     public void setEncoding(String JavaDoc s)
338     {
339         currentEncoding = s;
340     }
341
342     public boolean getStandalone()
343     {
344         throw new UnsupportedOperationException JavaDoc("Not yet implemented.71");
345     }
346
347
348     public void setStandalone(boolean flag)
349     {
350         throw new UnsupportedOperationException JavaDoc("Not yet implemented.72");
351     }
352
353     public boolean getStrictErrorChecking()
354     {
355         throw new UnsupportedOperationException JavaDoc("Not yet implemented.73");
356     }
357
358
359     public void setStrictErrorChecking(boolean flag)
360     {
361         throw new UnsupportedOperationException JavaDoc("Not yet implemented. 74");
362     }
363
364
365     public String JavaDoc getVersion()
366     {
367         throw new UnsupportedOperationException JavaDoc("Not yet implemented. 75");
368     }
369
370
371     public void setVersion(String JavaDoc s)
372     {
373         throw new UnsupportedOperationException JavaDoc("Not yet implemented.76");
374     }
375
376
377     public Node adoptNode(Node node)
378     throws DOMException
379     {
380         throw new UnsupportedOperationException JavaDoc("Not yet implemented.77");
381     }
382
383     /**
384      * Node Implementation
385      */

386
387     public String JavaDoc getNodeName(){
388         return document.getNodeName();
389     }
390
391     public String JavaDoc getNodeValue() throws DOMException {
392         return document.getNodeValue();
393     }
394
395     public void setNodeValue(String JavaDoc nodeValue) throws DOMException{
396         document.setNodeValue(nodeValue);
397     }
398
399     public short getNodeType() {
400         return document.getNodeType();
401     }
402
403     public Node getParentNode(){
404         return document.getParentNode();
405     }
406
407     public NodeList getChildNodes() {
408         return document.getChildNodes();
409     }
410
411     public Node getFirstChild() {
412         return document.getFirstChild();
413     }
414
415     public Node getLastChild(){
416         return document.getLastChild();
417     }
418
419     public Node getPreviousSibling(){
420         return document.getPreviousSibling();
421     }
422
423     public Node getNextSibling(){
424         return document.getNextSibling();
425     }
426
427     public NamedNodeMap getAttributes(){
428         return document.getAttributes();
429     }
430
431     public Document getOwnerDocument(){
432         return document.getOwnerDocument();
433     }
434
435     public Node insertBefore(Node newChild, Node refChild) throws DOMException {
436         return document.insertBefore(newChild, refChild);
437     }
438
439     public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
440         return document.replaceChild(newChild, oldChild);
441     }
442
443     public Node removeChild(Node oldChild) throws DOMException {
444         return document.removeChild(oldChild);
445     }
446
447     public Node appendChild(Node newChild) throws DOMException {
448         return document.appendChild(newChild);
449     }
450
451     public boolean hasChildNodes(){
452         return document.hasChildNodes();
453     }
454     public Node cloneNode(boolean deep) {
455         return document.cloneNode(deep);
456     }
457
458     public void normalize(){
459         document.normalize();
460     }
461
462     public boolean isSupported(String JavaDoc feature, String JavaDoc version){
463         return document.isSupported(feature, version);
464     }
465
466     public String JavaDoc getNamespaceURI() {
467         return document.getNamespaceURI();
468     }
469
470     public String JavaDoc getPrefix() {
471         return document.getPrefix();
472     }
473
474     public void setPrefix(String JavaDoc prefix) throws DOMException {
475         document.setPrefix(prefix);
476     }
477     public String JavaDoc getLocalName() {
478         return document.getLocalName();
479     }
480
481     public boolean hasAttributes(){
482         return document.hasAttributes();
483     }
484
485 }
486
Popular Tags