Debian Codegraph @

src/org/apache/axis/SOAPPart.java

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright 2001-2004 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
17 package org.apache.axis ;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.encoding.DeserializationContext;
21 import org.apache.axis.encoding.SerializationContext;
22 import org.apache.axis.message.InputStreamBody;
23 import org.apache.axis.message.MimeHeaders;
24 import org.apache.axis.message.SOAPDocumentImpl;
25 import org.apache.axis.message.SOAPEnvelope;
26 import org.apache.axis.message.SOAPHeaderElement;
27 import org.apache.axis.transport.http.HTTPConstants;
28 import org.apache.axis.utils.ByteArray;
29 import org.apache.axis.utils.Messages;
30 import org.apache.axis.utils.SessionUtils;
31 import org.apache.axis.utils.XMLUtils;
32 import org.apache.axis.handlers.HandlerChainImpl;
33 import org.apache.commons.logging.Log;
34 import org.w3c.dom.Attr;
35 import org.w3c.dom.CDATASection;
36 import org.w3c.dom.Comment;
•37 import org.w3c.dom.DOMConfiguration;
38 import org.w3c.dom.DOMException;
39 import org.w3c.dom.DOMImplementation;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.DocumentFragment;
42 import org.w3c.dom.DocumentType;
43 import org.w3c.dom.Element;
44 import org.w3c.dom.EntityReference;
45 import org.w3c.dom.NamedNodeMap;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48 import org.w3c.dom.ProcessingInstruction;
49 import org.w3c.dom.Text;
•50 import org.w3c.dom.UserDataHandler;
51 import org.xml.sax.InputSource;
52 import org.xml.sax.SAXException;
53
54 import javax.xml.soap.SOAPException;
55 import javax.xml.soap.SOAPMessage;
56 import javax.xml.transform.Source;
57 import javax.xml.transform.dom.DOMSource;
58 import javax.xml.transform.stream.StreamSource;
59 import java.io.BufferedOutputStream;
60 import java.io.BufferedReader;
61 import java.io.BufferedWriter;
62 import java.io.ByteArrayInputStream;
63 import java.io.ByteArrayOutputStream;
64 import java.io.IOException;
65 import java.io.InputStream;
66 import java.io.OutputStreamWriter;
67 import java.io.PrintWriter;
68 import java.io.Reader;
69 import java.io.StringReader;
70 import java.io.StringWriter;
71 import java.io.UnsupportedEncodingException;
72 import java.io.Writer;
73 import java.util.Iterator;
74 import java.util.Vector;
75
76 /**
77 * The SOAPPart provides access to the root part of the Message which
78 * contains the envelope.
79 * <p>
80 * SOAPPart implements Part, providing common MIME operations.
81 * <p>
82 * SOAPPart also allows access to its envelope,
83 * as a string, byte[], InputStream, or SOAPEnvelope. (This functionality
84 * used to be in Message, and has been moved here more or less verbatim
85 * pending further cleanup.)
86 *
87 * @author Rob Jellinghaus ([email protected])
88 * @author Doug Davis ([email protected])
89 * @author Glen Daniels ([email protected])
90 * @author Heejune Ahn ([email protected])
91 */
92 public class SOAPPart extends javax.xml.soap.SOAPPart implements Part
93 {
94 protected static Log log =
95 LogFactory.getLog(SOAPPart.class.getName());
96
97 public static final int FORM_STRING = 1;
98 public static final int FORM_INPUTSTREAM = 2;
99 public static final int FORM_SOAPENVELOPE = 3;
100 public static final int FORM_BYTES = 4;
101 public static final int FORM_BODYINSTREAM = 5;
102 public static final int FORM_FAULT = 6;
103 public static final int FORM_OPTIMIZED = 7;
104 private int currentForm;
105
106 /**
107 * property used to set SOAPEnvelope as default form
108 */
109 public static final String ALLOW_FORM_OPTIMIZATION = "axis.form.optimization";
110
111
112 //private Hashtable headers = new Hashtable();
113 private MimeHeaders mimeHeaders = new MimeHeaders();
114
115 private static final String[] formNames =
116 { "", "FORM_STRING", "FORM_INPUTSTREAM", "FORM_SOAPENVELOPE",
117 "FORM_BYTES", "FORM_BODYINSTREAM", "FORM_FAULT", "FORM_OPTIMIZED" };
118
119 /**
120 * The current representation of the SOAP contents of this part.
121 * May be a String, byte[], InputStream, or SOAPEnvelope, depending
122 * on whatever was last asked for. (ack)
123 * <p>
124 * currentForm must have the corresponding value.
125 * <p>
126 * As someone once said: "Just a placeholder until we figure out what the actual Message
127 * object is."
128 */
129 private Object currentMessage ;
130
131 /**
132 * default message encoding charset
133 */
134 private String currentEncoding = "UTF-8";
135
136 // These two fields are used for caching in getAsString and getAsBytes
137 private String currentMessageAsString = null;
138 private byte[] currentMessageAsBytes = null;
139 private org.apache.axis.message.SOAPEnvelope currentMessageAsEnvelope= null;
140
141 /**
142 * Message object this part is tied to. Used for serialization settings.
143 */
144 private Message msgObject;
145
146 /** Field contentSource. */
147 private Source contentSource = null;
148
149 /**
150 * The original message. Again, may be String, byte[], InputStream,
151 * or SOAPEnvelope.
152 */
153 // private Object originalMessage ; //free up reference this is not in use.
154
155 /**
156 * Create a new SOAPPart.
157 * <p>
158 * Do not call this directly! Should only be called by Message.
159 *
160 * @param parent the parent <code>Message</code>
161 * @param initialContents the initial contens <code>Object</code>
162 * @param isBodyStream if the body is in a stream
163 */
164 public SOAPPart(Message parent, Object initialContents, boolean isBodyStream) {
165
166 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID , SessionUtils.generateSessionId());
167 setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE , "text/xml");
168
169 msgObject=parent;
170 // originalMessage = initialContents;
171 int form = FORM_STRING;
172 if (initialContents instanceof SOAPEnvelope) {
173 form = FORM_SOAPENVELOPE;
174 ((SOAPEnvelope)initialContents).setOwnerDocument(this);
175 } else if (initialContents instanceof InputStream) {
176 form = isBodyStream ? FORM_BODYINSTREAM : FORM_INPUTSTREAM;
177 } else if (initialContents instanceof byte[]) {
178 form = FORM_BYTES;
179 } else if (initialContents instanceof AxisFault) {
180 form = FORM_FAULT;
181 }
182
183 if (log.isDebugEnabled()) {
184 log.debug("Enter: SOAPPart ctor(" + formNames[form] + ")");
185 }
186
187 setCurrentMessage(initialContents, form);
188
189 if (log.isDebugEnabled()) {
190 log.debug("Exit: SOAPPart ctor()");
191 }
192 }
193
194
195 /**
196 * Get the <code>Message</code> for this <code>Part</code>.
197 *
198 * @return the <code>Message</code> for this <code>Part</code>
199 */
200 public Message getMessage(){
201 return msgObject;
202 }
203
204 /**
205 * Set the Message for this Part.
206 * Do not call this Directly. Called by Message.
207 *
208 * @param msg the <code>Message</code> for this part
209 */
210 public void setMessage (Message msg) {
211 this.msgObject= msg;
212 }
213
214 /**
215 * Content type is always "text/xml" for SOAPParts.
216 *
217 * @return the content type
218 */
219 public String getContentType() {
220 return "text/xml";
221 }
222
223 /**
224 * Get the content length for this SOAPPart.
225 * This will force buffering of the SOAPPart, but it will
226 * also cache the byte[] form of the SOAPPart.
227 *
228 * @return the content length in bytes
229 */
230 public long getContentLength() throws AxisFault {
231 saveChanges();
232 if (currentForm == FORM_OPTIMIZED) {
233 return ((ByteArray) currentMessage).size();
234 } else if (currentForm == FORM_BYTES) {
235 return ((byte[]) currentMessage).length;
236 }
237 byte[] bytes = this.getAsBytes();
238 return bytes.length;
239 }
240
241 /**
242 * This set the SOAP Envelope for this part.
243 * <p>
244 * Note: It breaks the chicken/egg created.
245 * I need a message to create an attachment...
246 * From the attachment I should be able to get a reference...
247 * I now want to edit elements in the envelope in order to
248 * place the attachment reference to it.
249 * How do I now update the SOAP envelope with what I've changed?
250 *
251 * @param env the <code>SOAPEnvelope</CODE> for this <code>SOAPPart</code>
252 */
253
254 public void setSOAPEnvelope(org.apache.axis.message.SOAPEnvelope env){
255 setCurrentMessage(env, FORM_SOAPENVELOPE) ;
256 }
257
258 /**
259 * Write the contents to the specified stream.
260 *
261 * @param os the <code>java.io.OutputStream</code> to write to
262 */
263 public void writeTo(java.io.OutputStream os) throws IOException {
264 if ( currentForm == FORM_BYTES ) {
265 os.write((byte[])currentMessage);
266 } else if ( currentForm == FORM_OPTIMIZED ) {
267 ((ByteArray) currentMessage).writeTo(os);
268 } else {
269 Writer writer = new OutputStreamWriter(os, currentEncoding);
270 writer = new BufferedWriter(new PrintWriter(writer));
271 writeTo(writer);
272 writer.flush();
273 }
274 }
275
276 /**
277 * Write the contents to the specified writer.
278 *
279 * @param writer the <code>Writer</code> to write to
280 */
281 public void writeTo(Writer writer) throws IOException {
282 boolean inclXmlDecl = false;
283
284 if (msgObject.getMessageContext() != null) { // if we have message context (JAX-RPC), write xml decl always.
285 inclXmlDecl = true;
286 } else { // if we have no message context (SAAJ), write xml decl according to property.
287 try {
288 String xmlDecl = (String)msgObject.getProperty(SOAPMessage.WRITE_XML_DECLARATION);
289 if (xmlDecl != null && xmlDecl.equals("true")) {
290 inclXmlDecl = true;
291 }
292 } catch (SOAPException e) {
293 throw new IOException(e.getMessage());
294 }
295 }
296
297 if ( currentForm == FORM_FAULT ) {
298 AxisFault env = (AxisFault)currentMessage;
299 try {
300 SerializationContext serContext = new SerializationContext(writer, getMessage().getMessageContext());
301 serContext.setSendDecl(inclXmlDecl);
302 serContext.setEncoding(currentEncoding);
303 env.output(serContext);
304 } catch (Exception e) {
305 log.error(Messages.getMessage("exception00"), e);
306 throw env;
307 }
308 return;
309 }
310
311 if ( currentForm == FORM_SOAPENVELOPE ) {
312 SOAPEnvelope env = (SOAPEnvelope)currentMessage;
313 try {
314 SerializationContext serContext = new SerializationContext(writer, getMessage().getMessageContext());
315 serContext.setSendDecl(inclXmlDecl);
316 serContext.setEncoding(currentEncoding);
317 env.output(serContext);
318 } catch (Exception e) {
319 throw AxisFault.makeFault(e);
320 }
321 return;
322 }
323
324 String xml = this.getAsString();
325 if(inclXmlDecl){
326 if(!xml.startsWith("<?xml")){
327 writer.write("<?xml version=\"1.0\" encoding=\"");
328 writer.write(currentEncoding);
329 writer.write("\"?>");
330 }
331 }
332 writer.write(xml);
333 }
334
335 /**
336 * Get the current message, in whatever form it happens to be right now.
337 * Will return a String, byte[], InputStream, or SOAPEnvelope, depending
338 * on circumstances.
339 * <p>
340 * The method name is historical.
341 * TODO: rename this for clarity; should be more like getContents.
342 *
343 * @return the current content
344 */
345 public Object getCurrentMessage() {
346 return currentMessage;
347 }
348
349 /**
350 * Set the current message
351 * @param currMsg
352 * @param form
353 */
354 public void setCurrentMessage(Object currMsg, int form) {
355 currentMessageAsString = null; //Get rid of any cached stuff this is new.
356 currentMessageAsBytes = null;
357 currentMessageAsEnvelope= null;
358 setCurrentForm(currMsg, form);
359 }
360 /**
361 * Set the current contents of this Part.
362 * The method name is historical.
363 * TODO: rename this for clarity to something more like setContents???
364 *
365 * @param currMsg the new content of this part
366 * @param form the form of the message
367 */
368 private void setCurrentForm(Object currMsg, int form) {
369 if (log.isDebugEnabled()) {
370 String msgStr;
371 if (currMsg instanceof String) {
372 msgStr = (String)currMsg;
373 } else {
374 msgStr = currMsg.getClass().getName();
375 }
376 log.debug(Messages.getMessage("setMsgForm", formNames[form],
377 "" + msgStr));
378 }
379
380 // only change form if allowed
381 if (isFormOptimizationAllowed()) {
382 currentMessage = currMsg;
383 currentForm = form;
384 if (currentForm == FORM_SOAPENVELOPE) {
385 currentMessageAsEnvelope = (org.apache.axis.message.SOAPEnvelope) currMsg;
386 }
387 }
388 }
389
390 /**
391 * check if the allow optimization flag is on
392 * @return form optimization flag
393 */
394 private boolean isFormOptimizationAllowed() {
395 boolean allowFormOptimization = true;
396 Message msg = getMessage();
397 if (msg != null) {
398 MessageContext ctx = msg.getMessageContext();
399 if (ctx != null) {
400 Boolean propFormOptimization = (Boolean)ctx.getProperty(ALLOW_FORM_OPTIMIZATION);
401 if (propFormOptimization != null) {
402 allowFormOptimization = propFormOptimization.booleanValue();
403 }
404 }
405 }
406 return allowFormOptimization;
407 }
408
409 public int getCurrentForm() {
410 return currentForm;
411 }
412
413 /**
414 * Get the contents of this Part (not the headers!), as a byte
415 * array. This will force buffering of the message.
416 *
417 * @return an array of bytes containing a byte representation of this Part
418 * @throws AxisFault if this Part can't be serialized to the byte array
419 */
420 public byte[] getAsBytes() throws AxisFault {
421 log.debug("Enter: SOAPPart::getAsBytes");
422 if ( currentForm == FORM_OPTIMIZED ) {
423 log.debug("Exit: SOAPPart::getAsBytes");
424 try {
425 return ((ByteArray) currentMessage).toByteArray();
426 } catch (IOException e) {
427 throw AxisFault.makeFault(e);
428 }
429 }
430 if ( currentForm == FORM_BYTES ) {
431 log.debug("Exit: SOAPPart::getAsBytes");
432 return (byte[])currentMessage;
433 }
434
435 if ( currentForm == FORM_BODYINSTREAM ) {
436 try {
437 getAsSOAPEnvelope();
438 } catch (Exception e) {
439 log.fatal(Messages.getMessage("makeEnvFail00"), e);
440 log.debug("Exit: SOAPPart::getAsBytes");
441 return null;
442 }
443 }
444
445 if ( currentForm == FORM_INPUTSTREAM ) {
446 // Assumes we don't need a content length
447 try {
448 InputStream inp = null;
449 byte[] buf = null;
450 try{
451 inp = (InputStream) currentMessage ;
452 ByteArrayOutputStream baos = new ByteArrayOutputStream();
453 buf = new byte[4096];
454 int len ;
455 while ( (len = inp.read(buf,0,4096)) != -1 )
456 baos.write( buf, 0, len );
457 buf = baos.toByteArray();
458 }finally{
459 if(inp != null &&
460 currentMessage instanceof org.apache.axis.transport.http.SocketInputStream )
461 inp.close();
462 }
463 setCurrentForm( buf, FORM_BYTES );
464 log.debug("Exit: SOAPPart::getAsBytes");
465 return (byte[])currentMessage;
466 }
467 catch( Exception e ) {
468 log.error(Messages.getMessage("exception00"), e);
469 }
470 log.debug("Exit: SOAPPart::getAsBytes");
471 return null;
472 }
473
474 if ( currentForm == FORM_SOAPENVELOPE ||
475 currentForm == FORM_FAULT ){
476 currentEncoding = XMLUtils.getEncoding(msgObject, null);
477 ByteArrayOutputStream baos = new ByteArrayOutputStream();
478 BufferedOutputStream os = new BufferedOutputStream(baos);
479 try {
480 this.writeTo(os);
481 os.flush();
482 } catch (Exception e) {
483 throw AxisFault.makeFault(e);
484 }
485 setCurrentForm(baos.toByteArray(), FORM_BYTES);
486 if (log.isDebugEnabled()) {
487 log.debug("Exit: SOAPPart::getAsBytes(): " + currentMessage);
488 }
489 return (byte[]) currentMessage;
490 }
491
492 if ( currentForm == FORM_STRING ) {
493 // If the current message was already converted from
494 // a byte[] to String, return the byte[] representation
495 // (this is done to avoid unnecessary conversions)
496 if (currentMessage == currentMessageAsString &&
497 currentMessageAsBytes != null) {
498 if (log.isDebugEnabled()) {
499 log.debug("Exit: SOAPPart::getAsBytes()");
500 }
501 return currentMessageAsBytes;
502 }
503 // Save this message in case it is requested later in getAsString
504 currentMessageAsString = (String) currentMessage;
505 try{
506 currentEncoding = XMLUtils.getEncoding(msgObject, null);
507 setCurrentForm( ((String)currentMessage).getBytes(currentEncoding),
508 FORM_BYTES );
509 }catch(UnsupportedEncodingException ue){
510 setCurrentForm( ((String)currentMessage).getBytes(),
511 FORM_BYTES );
512 }
513 currentMessageAsBytes = (byte[]) currentMessage;
514
515 log.debug("Exit: SOAPPart::getAsBytes");
516 return (byte[])currentMessage;
517 }
518
519 log.error(Messages.getMessage("cantConvert00", ""+currentForm));
520
521 log.debug("Exit: SOAPPart::getAsBytes");
522 return null;
523 }
524
525 public void saveChanges() throws AxisFault {
526 log.debug("Enter: SOAPPart::saveChanges");
527 if ( currentForm == FORM_SOAPENVELOPE ||
528 currentForm == FORM_FAULT ){
529 currentEncoding = XMLUtils.getEncoding(msgObject, null);
530 ByteArray array = new ByteArray();
531 try {
532 writeTo(array);
533 array.flush();
534 } catch (Exception e) {
535 throw AxisFault.makeFault(e);
536 }
537 setCurrentForm( array, FORM_OPTIMIZED );
538 if (log.isDebugEnabled()) {
539 log.debug("Exit: SOAPPart::saveChanges(): " + currentMessage);
540 }
541 }
542 }
543 /**
544 * Get the contents of this Part (not the headers!), as a String.
545 * This will force buffering of the message.
546 *
547 * @return a <code>String</code> containing the content of this message
548 * @throws AxisFault if there is an error serializing this part
549 */
550 public String getAsString() throws AxisFault {
551 log.debug("Enter: SOAPPart::getAsString");
552 if ( currentForm == FORM_STRING ) {
553 if (log.isDebugEnabled()) {
554 log.debug("Exit: SOAPPart::getAsString(): " + currentMessage);
555 }
556 return (String)currentMessage;
557 }
558
559 if ( currentForm == FORM_INPUTSTREAM ||
560 currentForm == FORM_BODYINSTREAM ) {
561 getAsBytes();
562 // Fall thru to "Bytes"
563 }
564
565 if ( currentForm == FORM_OPTIMIZED) {
566 try {
567 currentMessageAsBytes =
568 ((ByteArray) currentMessage).toByteArray();
569 } catch (IOException e) {
570 throw AxisFault.makeFault(e);
571 }
572
573 try{
574 setCurrentForm(new String((byte[])currentMessageAsBytes,
575 currentEncoding),
576 FORM_STRING);
577 }catch(UnsupportedEncodingException ue){
578 setCurrentForm( new String((byte[]) currentMessageAsBytes),
579 FORM_STRING );
580 }
581 if (log.isDebugEnabled()) {
582 log.debug("Exit: SOAPPart::getAsString(): " + currentMessage);
583 }
584 return (String)currentMessage;
585 }
586
587 if ( currentForm == FORM_BYTES ) {
588 // If the current message was already converted from
589 // a String to byte[], return the String representation
590 // (this is done to avoid unnecessary conversions)
591 if (currentMessage == currentMessageAsBytes &&
592 currentMessageAsString != null) {
593 if (log.isDebugEnabled()) {
594 log.debug("Exit: SOAPPart::getAsString(): " + currentMessageAsString);
595 }
596 return currentMessageAsString;
597 }
598
599 // Save this message in case it is requested later in getAsBytes
600 currentMessageAsBytes = (byte[]) currentMessage;
601 try{
602 setCurrentForm(new String((byte[])currentMessage,
603 currentEncoding),
604 FORM_STRING);
605 }catch(UnsupportedEncodingException ue){
606 setCurrentForm( new String((byte[]) currentMessage),
607 FORM_STRING );
608 }
609 currentMessageAsString = (String) currentMessage;
610 if (log.isDebugEnabled()) {
611 log.debug("Exit: SOAPPart::getAsString(): " + currentMessage);
612 }
613 return (String)currentMessage;
614 }
615
616 if ( currentForm == FORM_FAULT ) {
617 StringWriter writer = new StringWriter();
618 try {
619 this.writeTo(writer);
620 } catch (Exception e) {
621 log.error(Messages.getMessage("exception00"), e);
622 return null;
623 }
624 setCurrentForm(writer.getBuffer().toString(), FORM_STRING);
625 if (log.isDebugEnabled()) {
626 log.debug("Exit: SOAPPart::getAsString(): " + currentMessage);
627 }
628 return (String)currentMessage;
629 }
630
631 if ( currentForm == FORM_SOAPENVELOPE ) {
632 StringWriter writer = new StringWriter();
633 try {
634 this.writeTo(writer);
635 } catch (Exception e) {
636 throw AxisFault.makeFault(e);
637 }
638 setCurrentForm(writer.getBuffer().toString(), FORM_STRING);
639 if (log.isDebugEnabled()) {
640 log.debug("Exit: SOAPPart::getAsString(): " + currentMessage);
641 }
642 return (String)currentMessage;
643 }
644
645 log.error( Messages.getMessage("cantConvert01", ""+currentForm));
646
647 log.debug("Exit: SOAPPart::getAsString()");
648 return null;
649 }
650
651 /**
652 * Get the contents of this Part (not the MIME headers!), as a
653 * SOAPEnvelope. This will force a complete parse of the
654 * message.
655 *
656 * @return a <code>SOAPEnvelope</code> containing the message content
657 * @throws AxisFault if the envelope could not be constructed
658 */
659 public SOAPEnvelope getAsSOAPEnvelope()
660 throws AxisFault
661 {
662 if (log.isDebugEnabled()) {
663 log.debug("Enter: SOAPPart::getAsSOAPEnvelope()");
664 log.debug(Messages.getMessage("currForm", formNames[currentForm]));
665 }
666 if ( currentForm == FORM_SOAPENVELOPE )
667 return (SOAPEnvelope)currentMessage;
668
669
670 if (currentForm == FORM_BODYINSTREAM) {
671 InputStreamBody bodyEl =
672 new InputStreamBody((InputStream)currentMessage);
673 SOAPEnvelope env = new SOAPEnvelope();
674 env.setOwnerDocument(this);
675 env.addBodyElement(bodyEl);
676 setCurrentForm(env, FORM_SOAPENVELOPE);
677 return env;
678 }
679
680 InputSource is;
681
682 if ( currentForm == FORM_INPUTSTREAM ) {
683 is = new InputSource( (InputStream) currentMessage );
684 String encoding = XMLUtils.getEncoding(msgObject, null, null);
685 if (encoding != null) {
686 currentEncoding = encoding;
687 is.setEncoding(currentEncoding);
688 }
689 } else {
690 is = new InputSource(new StringReader(getAsString()));
691 }
692 DeserializationContext dser = new DeserializationContext(is,
693 getMessage().getMessageContext(),
694 getMessage().getMessageType());
695 dser.getEnvelope().setOwnerDocument(this);
696 // This may throw a SAXException
697 try {
698 dser.parse();
699 } catch (SAXException e) {
700 Exception real = e.getException();
701 if (real == null)
702 real = e;
703 throw AxisFault.makeFault(real);
704 }
705
706 SOAPEnvelope nse= dser.getEnvelope();
707 if(currentMessageAsEnvelope != null){
708 //Need to synchronize back processed header info.
709 Vector newHeaders= nse.getHeaders();
710 Vector oldHeaders= currentMessageAsEnvelope.getHeaders();
711 if( null != newHeaders && null != oldHeaders){
712 Iterator ohi= oldHeaders.iterator();
713 Iterator nhi= newHeaders.iterator();
714 while( ohi.hasNext() && nhi.hasNext()){
715 SOAPHeaderElement nhe= (SOAPHeaderElement)nhi.next();
716 SOAPHeaderElement ohe= (SOAPHeaderElement)ohi.next();
717
718 if(ohe.isProcessed()) nhe.setProcessed(true);
719 }
720 }
721
722 }
723
724 setCurrentForm(nse, FORM_SOAPENVELOPE);
725
726 log.debug("Exit: SOAPPart::getAsSOAPEnvelope");
727 SOAPEnvelope env = (SOAPEnvelope)currentMessage;
728 env.setOwnerDocument(this);
729 return env;
730 }
731
732 /**
733 * Add the specified MIME header, as per JAXM.
734 *
735 * @param header the header to add
736 * @param value the value of that header
737 */
738 public void addMimeHeader (String header, String value) {
739 mimeHeaders.addHeader(header, value);
740 }
741
742 /**
743 * Get the specified MIME header.
744 *
745 * @param header the name of a MIME header
746 * @return the value of the first header named <code>header</code>
747 */
748 private String getFirstMimeHeader (String header) {
749 String[] values = mimeHeaders.getHeader(header);
750 if(values != null && values.length>0)
751 return values[0];
752 return null;
753 }
754
755 /**
756 * Total size in bytes (of all content and headers, as encoded).
757 public abstract int getSize();
758 */
759
760 /**
761 * Content location.
762 *
763 * @return the content location
764 */
765 public String getContentLocation() {
766 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION);
767 }
768
769 /**
770 * Set content location.
771 *
772 * @param loc the content location
773 */
774 public void setContentLocation(String loc) {
775 setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION, loc);
776 }
777
778 /**
779 * Sets Content-Id of this part.
780 * already defined.
781 * @param newCid new Content-Id
782 */
783 public void setContentId(String newCid){
784 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID,newCid);
785 }
786
787 /**
788 * Content ID.
789 *
790 * @return the content ID
791 */
792 public String getContentId() {
793 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_ID);
794 }
795 /**
796 * Content ID.
797 *
798 * @return the contentId reference value that should be used directly
799 * as an href in a SOAP element to reference this attachment.
800 * <B>Not part of JAX-RPC, JAX-M, SAAJ, etc. </B>
801 */
802 public String getContentIdRef() {
803 return org.apache.axis.attachments.Attachments.CIDprefix +
804 getContentId();
805 }
806
807
808 /**
809 * Get all headers that match.
810 *
811 * @param match an array of <code>String</code>s giving mime header names
812 * @return an <code>Iterator</code> over all values matching these headers
813 */
814 public java.util.Iterator getMatchingMimeHeaders( final String[] match){
815 return mimeHeaders.getMatchingHeaders(match);
816 }
817
818 /**
819 * Get all headers that do not match.
820 *
821 * @param match an array of <code>String</code>s giving mime header names
822 * @return an <code>Iterator</code> over all values not matching these
823 * headers
824 */
825 public java.util.Iterator getNonMatchingMimeHeaders( final String[] match){
826 return mimeHeaders.getNonMatchingHeaders(match);
827 }
828
829 /**
830 * Sets the content of the <CODE>SOAPEnvelope</CODE> object
831 * with the data from the given <CODE>Source</CODE> object.
832 * @param source javax.xml.transform.Source</CODE> object with the data to
833 * be set
834 * @throws SOAPException if there is a problem in
835 * setting the source
836 * @see #getContent() getContent()
837 */
838 public void setContent(Source source) throws SOAPException {
839 if(source == null)
840 throw new SOAPException(Messages.getMessage("illegalArgumentException00"));
841
842 // override the checks in HandlerChainImpl for JAXRPCHandler kludge
843 MessageContext ctx = getMessage().getMessageContext();
844 if (ctx != null) {
845 ctx.setProperty(org.apache.axis.SOAPPart.ALLOW_FORM_OPTIMIZATION,
846 Boolean.TRUE);
847 }
848
849 contentSource = source;
850 InputSource in = org.apache.axis.utils.XMLUtils.sourceToInputSource(contentSource);
851 InputStream is = in.getByteStream();
852 if(is != null) {
853 setCurrentMessage(is, FORM_INPUTSTREAM);
854 } else {
855 Reader r = in.getCharacterStream();
856 if(r == null) {
857 throw new SOAPException(Messages.getMessage("noCharacterOrByteStream"));
858 }
859 BufferedReader br = new BufferedReader(r);
860 String line = null;
861 StringBuffer sb = new StringBuffer();
862 try {
863 while((line = br.readLine()) != null) {
864 sb.append(line);
865 }
866 } catch (IOException e) {
867 throw new SOAPException(Messages.getMessage("couldNotReadFromCharStream"), e);
868 }
869 setCurrentMessage(sb.toString(), FORM_STRING);
870 }
871 }
872
873 /**
874 * Returns the content of the SOAPEnvelope as a JAXP <CODE>
875 * Source</CODE> object.
876 * @return the content as a <CODE>
877 * javax.xml.transform.Source</CODE> object
878 * @throws SOAPException if the implementation cannot
879 * convert the specified <CODE>Source</CODE> object
880 * @see #setContent(javax.xml.transform.Source) setContent(javax.xml.transform.Source)
881 */
882 public Source getContent() throws SOAPException {
883 if(contentSource == null) {
884 switch(currentForm) {
885 case FORM_STRING:
886 String s = (String) currentMessage;
887 contentSource = new StreamSource(new StringReader(s));
888 break;
889 case FORM_INPUTSTREAM:
890 contentSource =
891 new StreamSource((InputStream) currentMessage);
892 break;
893 case FORM_SOAPENVELOPE:
894 SOAPEnvelope se = (SOAPEnvelope) currentMessage;
895 try {
896 contentSource = new DOMSource(se.getAsDocument());
897 } catch (Exception e) {
898 throw new SOAPException(Messages.getMessage("errorGetDocFromSOAPEnvelope"),
899 e);
900 }
901 break;
902 case FORM_OPTIMIZED:
903 try {
904 ByteArrayInputStream baos = new ByteArrayInputStream(((ByteArray) currentMessage).toByteArray());
905 contentSource = new StreamSource(baos);
906 } catch (IOException e) {
907 throw new SOAPException(Messages.getMessage("errorGetDocFromSOAPEnvelope"),
908 e);
909 }
910 break;
911 case FORM_BYTES:
912 byte[] bytes = (byte[]) currentMessage;
913 contentSource =
914 new StreamSource(new ByteArrayInputStream(bytes));
915 break;
916 case FORM_BODYINSTREAM:
917 contentSource =
918 new StreamSource((InputStream) currentMessage);
919 break;
920 }
921 }
922 return contentSource;
923 }
924
925 /**
926 * Retrieves all the headers for this <CODE>SOAPPart</CODE>
927 * object as an iterator over the <CODE>MimeHeader</CODE>
928 * objects.
929 * @return an <CODE>Iterator</CODE> object with all of the Mime
930 * headers for this <CODE>SOAPPart</CODE> object
931 */
932 public Iterator getAllMimeHeaders() {
933 return mimeHeaders.getAllHeaders();
934 }
935
936 /**
937 * Changes the first header entry that matches the given
938 * header name so that its value is the given value, adding a
939 * new header with the given name and value if no existing
940 * header is a match. If there is a match, this method clears
941 * all existing values for the first header that matches and
942 * sets the given value instead. If more than one header has
943 * the given name, this method removes all of the matching
944 * headers after the first one.
945 *
946 * <P>Note that RFC822 headers can contain only US-ASCII
947 * characters.</P>
948 * @param name a <CODE>String</CODE> giving the
949 * header name for which to search
950 * @param value a <CODE>String</CODE> giving the
951 * value to be set. This value will be substituted for the
952 * current value(s) of the first header that is a match if
953 * there is one. If there is no match, this value will be
954 * the value for a new <CODE>MimeHeader</CODE> object.
955 * @ throws java.lang.IllegalArgumentException if
956 * there was a problem with the specified mime header name
957 * or value
958 * @see #getMimeHeader(java.lang.String) getMimeHeader(java.lang.String)
959 */
960 public void setMimeHeader(String name, String value) {
961 mimeHeaders.setHeader(name,value);
962 }
963
964 /**
965 * Gets all the values of the <CODE>MimeHeader</CODE> object
966 * in this <CODE>SOAPPart</CODE> object that is identified by
967 * the given <CODE>String</CODE>.
968 * @param name the name of the header; example:
969 * "Content-Type"
970 * @return a <CODE>String</CODE> array giving all the values for
971 * the specified header
972 * @see #setMimeHeader(java.lang.String, java.lang.String) setMimeHeader(java.lang.String, java.lang.String)
973 */
974 public String[] getMimeHeader(String name) {
975 return mimeHeaders.getHeader(name);
976 }
977
978 /**
979 * Removes all the <CODE>MimeHeader</CODE> objects for this
980 * <CODE>SOAPEnvelope</CODE> object.
981 */
982 public void removeAllMimeHeaders() {
983 mimeHeaders.removeAllHeaders();
984 }
985
986 /**
987 * Removes all MIME headers that match the given name.
988 * @param header a <CODE>String</CODE> giving
989 * the name of the MIME header(s) to be removed
990 */
991 public void removeMimeHeader(String header) {
992 mimeHeaders.removeHeader(header);
993 }
994
995 /**
996 * Gets the <CODE>SOAPEnvelope</CODE> object associated with
997 * this <CODE>SOAPPart</CODE> object. Once the SOAP envelope is
998 * obtained, it can be used to get its contents.
999 * @return the <CODE>SOAPEnvelope</CODE> object for this <CODE>
1000 * SOAPPart</CODE> object
1001 * @throws SOAPException if there is a SOAP error
1002 */
1003 public javax.xml.soap.SOAPEnvelope getEnvelope() throws SOAPException {
1004 try {
1005 return getAsSOAPEnvelope();
1006 } catch (AxisFault af) {
1007 throw new SOAPException(af);
1008 }
1009 }
1010
1011 /**
1012 * Implementation of org.w3c.Document
1013 * Most of methods will be implemented using the delgate
1014 * instance of SOAPDocumentImpl
1015 * This is for two reasons:
1016 * - possible change of message classes, by extenstion of xerces implementation
1017 * - we cannot extends SOAPPart (multiple inheritance),
1018 * since it is defined as Abstract class
1019 * ***********************************************************
1020 */
1021
1022 private Document document = new SOAPDocumentImpl(this);
1023 /**
1024 * @since SAAJ 1.2
1025 */
1026 public Document getSOAPDocument(){
1027 if(document == null){
1028 document = new SOAPDocumentImpl(this);
1029 }
1030 return document;
1031 }
1032
1033 /**
1034 * @return
1035 */
1036 public DocumentType getDoctype(){
1037 return document.getDoctype();
1038 }
1039
1040 /**
1041 * @return
1042 */
1043 public DOMImplementation getImplementation(){
1044 return document.getImplementation();
1045 }
1046
1047 /**
1048 * SOAPEnvelope is the Document Elements of this XML docuement
1049 */
1050 protected Document mDocument;
1051
1052 public Element getDocumentElement()
1053 {
1054 try{
1055 return getEnvelope();
1056 }catch(SOAPException se){
1057 return null;
1058 }
1059 }
1060
1061 /**
1062 *
1063 * @param tagName
1064 * @return
1065 * @throws DOMException
1066 */
1067 public Element createElement(String tagName) throws DOMException {
1068 return document.createElement(tagName);
1069 }
1070
1071 public DocumentFragment createDocumentFragment() {
1072 return document.createDocumentFragment();
1073 }
1074
1075 public Text createTextNode(String data) {
1076 return document.createTextNode(data);
1077 }
1078
1079 public Comment createComment(String data){
1080 return document.createComment(data);
1081 }
1082
1083 public CDATASection createCDATASection(String data) throws DOMException {
1084 return document.createCDATASection(data);
1085 }
1086
1087 public ProcessingInstruction createProcessingInstruction(String target, String data)
1088 throws DOMException {
1089 return document.createProcessingInstruction(target,data);
1090 }
1091
1092 public Attr createAttribute(String name)throws DOMException {
1093 return document.createAttribute(name);
1094 }
1095
1096 public EntityReference createEntityReference(String name) throws DOMException {
1097 return document.createEntityReference(name);
1098 }
1099
1100 public NodeList getElementsByTagName(String tagname) {
1101 return document.getElementsByTagName(tagname);
1102 }
1103
1104 public Node importNode(Node importedNode, boolean deep)
1105 throws DOMException {
1106 return document.importNode(importedNode, deep);
1107 }
1108
1109 public Element createElementNS(String namespaceURI, String qualifiedName)
1110 throws DOMException {
1111 return document.createElementNS(namespaceURI, qualifiedName);
1112 }
1113
1114 public Attr createAttributeNS(String namespaceURI, String qualifiedName)
1115 throws DOMException {
1116 return document.createAttributeNS(namespaceURI, qualifiedName);
1117 }
1118
1119 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
1120 return document.getElementsByTagNameNS(namespaceURI,localName);
1121 }
1122
1123 public Element getElementById(String elementId){
1124 return document.getElementById(elementId);
1125 }
1126
1127 /////////////////////////////////////////////////////////////
1128
1129 public String getEncoding()
1130 {
1131 return currentEncoding;
1132 }
1133
1134 public void setEncoding(String s)
1135 {
1136 currentEncoding = s;
1137 }
1138
1139 public boolean getStandalone()
1140 {
1141 throw new UnsupportedOperationException("Not yet implemented.71");
1142 }
1143
1144
1145 public void setStandalone(boolean flag)
1146 {
1147 throw new UnsupportedOperationException("Not yet implemented.72");
1148 }
1149
1150 public boolean getStrictErrorChecking()
1151 {
1152 throw new UnsupportedOperationException("Not yet implemented.73");
1153 }
1154
1155
1156 public void setStrictErrorChecking(boolean flag)
1157 {
1158 throw new UnsupportedOperationException("Not yet implemented. 74");
1159 }
1160
1161
1162 public String getVersion()
1163 {
1164 throw new UnsupportedOperationException("Not yet implemented. 75");
1165 }
1166
1167
1168 public void setVersion(String s)
1169 {
1170 throw new UnsupportedOperationException("Not yet implemented.76");
1171 }
1172
1173
1174 public Node adoptNode(Node node)
1175 throws DOMException
1176 {
1177 throw new UnsupportedOperationException("Not yet implemented.77");
1178 }
1179
1180 /**
1181 * Node Implementation
1182 */
1183
1184 public String getNodeName(){
1185 return document.getNodeName();
1186 }
1187
1188 public String getNodeValue() throws DOMException {
1189 return document.getNodeValue();
1190 }
1191
1192 public void setNodeValue(String nodeValue) throws DOMException{
1193 document.setNodeValue(nodeValue);
1194 }
1195
1196 public short getNodeType() {
1197 return document.getNodeType();
1198 }
1199
1200 public Node getParentNode(){
1201 return document.getParentNode();
1202 }
1203
1204 public NodeList getChildNodes() {
1205 return document.getChildNodes();
1206 }
1207
1208 public Node getFirstChild() {
1209 return document.getFirstChild();
1210 }
1211
1212 public Node getLastChild(){
1213 return document.getLastChild();
1214 }
1215
1216 public Node getPreviousSibling(){
1217 return document.getPreviousSibling();
1218 }
1219
1220 public Node getNextSibling(){
1221 return document.getNextSibling();
1222 }
1223
1224 public NamedNodeMap getAttributes(){
1225 return document.getAttributes();
1226 }
1227
1228 public Document getOwnerDocument(){
1229 return document.getOwnerDocument();
1230 }
1231
1232 public Node insertBefore(Node newChild, Node refChild) throws DOMException {
1233 return document.insertBefore(newChild, refChild);
1234 }
1235
1236 public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
1237 return document.replaceChild(newChild, oldChild);
1238 }
1239
1240 public Node removeChild(Node oldChild) throws DOMException {
1241 return document.removeChild(oldChild);
1242 }
1243
1244 public Node appendChild(Node newChild) throws DOMException {
1245 return document.appendChild(newChild);
1246 }
1247
1248 public boolean hasChildNodes(){
1249 return document.hasChildNodes();
1250 }
1251 public Node cloneNode(boolean deep) {
1252 return document.cloneNode(deep);
1253 }
1254
1255 public void normalize(){
1256 document.normalize();
1257 }
1258
1259 public boolean isSupported(String feature, String version){
1260 return document.isSupported(feature, version);
1261 }
1262
1263 public String getNamespaceURI() {
1264 return document.getNamespaceURI();
1265 }
1266
1267 public String getPrefix() {
1268 return document.getPrefix();
1269 }
1270
1271 public void setPrefix(String prefix) throws DOMException {
1272 document.setPrefix(prefix);
1273 }
1274 public String getLocalName() {
1275 return document.getLocalName();
1276 }
1277
1278 public boolean hasAttributes(){
1279 return document.hasAttributes();
1280 }
1281
1282 public boolean isBodyStream() {
1283 return (currentForm == SOAPPart.FORM_INPUTSTREAM || currentForm == SOAPPart.FORM_BODYINSTREAM);
1284 }
•1285
1286 /**
1287 * DOM Level 3 stubs
1288 */
1289
1290 public String getBaseURI() {
1291 throw new RuntimeException("not implemented");
1292 }
1293
1294 public boolean getXmlStandalone() {
1295 throw new RuntimeException("not implemented");
1296 }
1297
1298 public Object setUserData(
1299 String key, Object data, UserDataHandler handler) {
1300 throw new RuntimeException("not implemented");
1301 }
1302
1303 public void setDocumentURI(String documentURI) {
1304 throw new RuntimeException("not implemented");
1305 }
1306
1307 public void setXmlStandalone(boolean xmlStandalone) {
1308 throw new RuntimeException("not implemented");
1309 }
1310
1311 public Node renameNode(Node n, String namespaceURI, String qualifiedName) {
1312 throw new RuntimeException("not implemented");
1313 }
1314
1315 public String getInputEncoding() {
1316 throw new RuntimeException("not implemented");
1317 }
1318
1319 public String getXmlEncoding() {
1320 throw new RuntimeException("not implemented");
1321 }
1322
1323 public void setTextContent(String textContent) {
1324 throw new RuntimeException("not implemented");
1325 }
1326
1327 public boolean isSameNode(Node other) {
1328 throw new RuntimeException("not implemented");
1329 }
1330
1331 public String getDocumentURI() {
1332 throw new RuntimeException("not implemented");
1333 }
1334
1335 public Object getFeature(String feature, String version) {
1336 throw new RuntimeException("not implemented");
1337 }
1338
1339 public boolean isDefaultNamespace(String namespaceURI) {
1340 throw new RuntimeException("not implemented");
1341 }
1342
1343 public String lookupPrefix(String namespaceURI) {
1344 throw new RuntimeException("not implemented");
1345 }
1346
1347 public boolean isEqualNode(Node arg) {
1348 throw new RuntimeException("not implemented");
1349 }
1350
1351 public void setXmlVersion(String xmlVersion) {
1352 throw new RuntimeException("not implemented");
1353 }
1354
1355 public String getXmlVersion() {
1356 throw new RuntimeException("not implemented");
1357 }
1358
1359 public Object getUserData(String key) {
1360 throw new RuntimeException("not implemented");
1361 }
1362
1363 public String getTextContent() {
1364 throw new RuntimeException("not implemented");
1365 }
1366
1367 public void normalizeDocument() {
1368 throw new RuntimeException("not implemented");
1369 }
1370
1371 public String lookupNamespaceURI(String prefix) {
1372 throw new RuntimeException("not implemented");
1373 }
1374
1375 public DOMConfiguration getDomConfig() {
1376 throw new RuntimeException("not implemented");
1377 }
1378
1379 public short compareDocumentPosition(Node other) {
1380 throw new RuntimeException("not implemented");
1381 }
1382 }
1383