jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/EnvelopeFactory.java
changeset 23782 953bfc3fbe31
parent 22679 d785acd84a14
child 23961 439846965790
equal deleted inserted replaced
23403:85dbdc227c5e 23782:953bfc3fbe31
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27 
    27 
    28 import java.util.logging.Logger;
    28 import java.util.logging.Logger;
    29 
    29 
    30 import javax.xml.parsers.SAXParser;
    30 import javax.xml.parsers.SAXParser;
    31 import javax.xml.soap.SOAPException;
    31 import javax.xml.soap.SOAPException;
       
    32 import javax.xml.stream.XMLInputFactory;
       
    33 import javax.xml.stream.XMLStreamException;
       
    34 import javax.xml.stream.XMLStreamReader;
    32 import javax.xml.transform.Source;
    35 import javax.xml.transform.Source;
    33 import javax.xml.transform.Transformer;
    36 import javax.xml.transform.Transformer;
    34 import javax.xml.transform.dom.DOMResult;
    37 import javax.xml.transform.dom.DOMResult;
    35 import javax.xml.transform.sax.SAXSource;
    38 import javax.xml.transform.sax.SAXSource;
       
    39 import javax.xml.transform.stax.StAXSource;
    36 import javax.xml.transform.stream.StreamSource;
    40 import javax.xml.transform.stream.StreamSource;
    37 
    41 
    38 import org.xml.sax.InputSource;
    42 import org.xml.sax.InputSource;
    39 import org.xml.sax.XMLReader;
    43 import org.xml.sax.XMLReader;
    40 
    44 
       
    45 import com.sun.xml.internal.messaging.saaj.LazyEnvelopeSource;
    41 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
    46 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
    42 import com.sun.xml.internal.messaging.saaj.util.*;
    47 import com.sun.xml.internal.messaging.saaj.util.*;
    43 
       
    44 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
    48 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
    45 
    49 
    46 /**
    50 /**
    47  * EnvelopeFactory creates SOAP Envelope objects using different
    51  * EnvelopeFactory creates SOAP Envelope objects using different
    48  * underlying implementations.
    52  * underlying implementations.
    56     private static ParserPool parserPool = new ParserPool(5);
    60     private static ParserPool parserPool = new ParserPool(5);
    57 
    61 
    58     public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart)
    62     public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart)
    59         throws SOAPException
    63         throws SOAPException
    60     {
    64     {
       
    65         if (src instanceof JAXMStreamSource) {
       
    66             try {
       
    67                 if (!SOAPPartImpl.lazyContentLength) {
       
    68                     ((JAXMStreamSource) src).reset();
       
    69                 }
       
    70             } catch (java.io.IOException ioe) {
       
    71                 log.severe("SAAJ0515.source.reset.exception");
       
    72                 throw new SOAPExceptionImpl(ioe);
       
    73             }
       
    74         }
       
    75         if (src instanceof LazyEnvelopeSource) {
       
    76           return lazy((LazyEnvelopeSource)src, soapPart);
       
    77       }
       
    78         if (soapPart.message.isLazySoapBodyParsing()) {
       
    79             return parseEnvelopeStax(src, soapPart);
       
    80         } else {
       
    81             return parseEnvelopeSax(src, soapPart);
       
    82         }
       
    83     }
       
    84 
       
    85     private static Envelope lazy(LazyEnvelopeSource src, SOAPPartImpl soapPart) throws SOAPException {
       
    86         try {
       
    87                 StaxBridge staxBridge = new StaxLazySourceBridge(src, soapPart);
       
    88                 staxBridge.bridgeEnvelopeAndHeaders();
       
    89             Envelope env = (Envelope) soapPart.getEnvelope();
       
    90             env.setStaxBridge(staxBridge);
       
    91             return env;
       
    92         } catch (XMLStreamException e) {
       
    93             throw new SOAPException(e);
       
    94         }
       
    95     }
       
    96 
       
    97     static private XMLInputFactory xmlInputFactory = null;
       
    98 
       
    99     private static Envelope parseEnvelopeStax(Source src, SOAPPartImpl soapPart)
       
   100             throws SOAPException {
       
   101         XMLStreamReader streamReader = null;
       
   102         if (src instanceof StAXSource) {
       
   103            streamReader = ((StAXSource) src).getXMLStreamReader();
       
   104         }
       
   105         try {
       
   106             if (streamReader == null) {
       
   107                 if (xmlInputFactory == null) xmlInputFactory = XMLInputFactory.newInstance();
       
   108                 streamReader = xmlInputFactory.createXMLStreamReader(src);
       
   109             }
       
   110 //            SaajStaxWriter saajWriter = new SaajStaxWriter(soapPart.message, soapPart.document);
       
   111 //            XMLStreamReaderToXMLStreamWriter readerWriterBridge = new XMLStreamReaderToXMLStreamWriter(
       
   112 //                    streamReader, saajWriter, soapPart.getSOAPNamespace());
       
   113 
       
   114             StaxBridge readerWriterBridge = new StaxReaderBridge(streamReader, soapPart);
       
   115             //bridge will stop reading at body element, and parse upon request, so save it
       
   116             //on the envelope
       
   117             readerWriterBridge.bridgeEnvelopeAndHeaders();
       
   118 
       
   119             Envelope env = (Envelope) soapPart.getEnvelope();
       
   120             env.setStaxBridge(readerWriterBridge);
       
   121             return env;
       
   122         } catch (Exception e) {
       
   123             throw new SOAPException(e);
       
   124         }
       
   125     }
       
   126     private static Envelope parseEnvelopeSax(Source src, SOAPPartImpl soapPart)
       
   127             throws SOAPException {
    61         // Insert SAX filter to disallow Document Type Declarations since
   128         // Insert SAX filter to disallow Document Type Declarations since
    62         // they are not legal in SOAP
   129         // they are not legal in SOAP
    63         SAXParser saxParser = null;
   130         SAXParser saxParser = null;
    64         if (src instanceof StreamSource) {
   131         if (src instanceof StreamSource) {
    65             if (src instanceof JAXMStreamSource) {
       
    66                 try {
       
    67                     if (!SOAPPartImpl.lazyContentLength) {
       
    68                         ((JAXMStreamSource) src).reset();
       
    69                     }
       
    70                 } catch (java.io.IOException ioe) {
       
    71                     log.severe("SAAJ0515.source.reset.exception");
       
    72                     throw new SOAPExceptionImpl(ioe);
       
    73                 }
       
    74             }
       
    75             try {
   132             try {
    76                 saxParser = parserPool.get();
   133                 saxParser = parserPool.get();
    77             } catch (Exception e) {
   134             } catch (Exception e) {
    78                 log.severe("SAAJ0601.util.newSAXParser.exception");
   135                 log.severe("SAAJ0601.util.newSAXParser.exception");
    79                 throw new SOAPExceptionImpl(
   136                 throw new SOAPExceptionImpl(