jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/EnvelopeFactory.java
changeset 33547 e4c76ac38b12
parent 28326 2b9860c0d68a
equal deleted inserted replaced
33390:d131f4b8433a 33547:e4c76ac38b12
    30 import com.sun.xml.internal.messaging.saaj.util.JAXMStreamSource;
    30 import com.sun.xml.internal.messaging.saaj.util.JAXMStreamSource;
    31 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
    31 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
    32 import com.sun.xml.internal.messaging.saaj.util.ParserPool;
    32 import com.sun.xml.internal.messaging.saaj.util.ParserPool;
    33 import com.sun.xml.internal.messaging.saaj.util.RejectDoctypeSaxFilter;
    33 import com.sun.xml.internal.messaging.saaj.util.RejectDoctypeSaxFilter;
    34 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
    34 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
       
    35 
    35 import org.xml.sax.InputSource;
    36 import org.xml.sax.InputSource;
    36 import org.xml.sax.XMLReader;
    37 import org.xml.sax.XMLReader;
    37 
    38 
    38 import javax.xml.parsers.SAXParser;
    39 import javax.xml.parsers.SAXParser;
    39 import javax.xml.soap.SOAPException;
    40 import javax.xml.soap.SOAPException;
    44 import javax.xml.transform.Transformer;
    45 import javax.xml.transform.Transformer;
    45 import javax.xml.transform.dom.DOMResult;
    46 import javax.xml.transform.dom.DOMResult;
    46 import javax.xml.transform.sax.SAXSource;
    47 import javax.xml.transform.sax.SAXSource;
    47 import javax.xml.transform.stax.StAXSource;
    48 import javax.xml.transform.stax.StAXSource;
    48 import javax.xml.transform.stream.StreamSource;
    49 import javax.xml.transform.stream.StreamSource;
       
    50 
       
    51 import java.security.AccessController;
       
    52 import java.security.PrivilegedAction;
    49 import java.util.logging.Logger;
    53 import java.util.logging.Logger;
    50 
    54 
    51 /**
    55 /**
    52  * EnvelopeFactory creates SOAP Envelope objects using different
    56  * EnvelopeFactory creates SOAP Envelope objects using different
    53  * underlying implementations.
    57  * underlying implementations.
    54  */
    58  */
    55 public class EnvelopeFactory {
    59 public class EnvelopeFactory {
       
    60     private static final String SAX_PARSER_POOL_SIZE_PROP_NAME = "com.sun.xml.internal.messaging.saaj.soap.saxParserPoolSize";
       
    61     private static final int DEFAULT_SAX_PARSER_POOL_SIZE = 5;
    56 
    62 
    57     protected static final Logger
    63     protected static final Logger
    58         log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
    64         log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
    59         "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
    65         "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
    60 
    66 
    61     private static ContextClassloaderLocal<ParserPool> parserPool =
    67     private static ContextClassloaderLocal<ParserPool> parserPool =
    62             new ContextClassloaderLocal<ParserPool>() {
    68             new ContextClassloaderLocal<ParserPool>() {
    63                 @Override
    69                 @Override
    64                 protected ParserPool initialValue() throws Exception {
    70                 protected ParserPool initialValue() throws Exception {
    65                     return new ParserPool(5);
    71                         Integer poolSize = AccessController.doPrivileged(
    66                 }
    72                                         new PrivilegedAction<Integer>() {
    67             };
    73                                                 @Override
       
    74                                                 public Integer run() {
       
    75                                                         try {
       
    76                                                                 return Integer.getInteger(
       
    77                                                                                 SAX_PARSER_POOL_SIZE_PROP_NAME,
       
    78                                                                                 DEFAULT_SAX_PARSER_POOL_SIZE);
       
    79                                                         } catch (SecurityException se) {
       
    80                                                                 return DEFAULT_SAX_PARSER_POOL_SIZE;
       
    81                                                         }
       
    82                                                 }
       
    83                                         });
       
    84                     return new ParserPool(poolSize);
       
    85                 }
       
    86     };
    68 
    87 
    69     public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart)
    88     public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart)
    70         throws SOAPException
    89         throws SOAPException
    71     {
    90     {
    72             if (src instanceof JAXMStreamSource) {
    91             if (src instanceof JAXMStreamSource) {
   130             throw new SOAPException(e);
   149             throw new SOAPException(e);
   131         }
   150         }
   132     }
   151     }
   133     private static Envelope parseEnvelopeSax(Source src, SOAPPartImpl soapPart)
   152     private static Envelope parseEnvelopeSax(Source src, SOAPPartImpl soapPart)
   134             throws SOAPException {
   153             throws SOAPException {
   135         // Insert SAX filter to disallow Document Type Declarations since
       
   136         // they are not legal in SOAP
       
   137         SAXParser saxParser = null;
   154         SAXParser saxParser = null;
   138         if (src instanceof StreamSource) {
       
   139             try {
       
   140                 saxParser = parserPool.get().get();
       
   141             } catch (Exception e) {
       
   142                 log.severe("SAAJ0601.util.newSAXParser.exception");
       
   143                 throw new SOAPExceptionImpl(
       
   144                     "Couldn't get a SAX parser while constructing a envelope",
       
   145                     e);
       
   146             }
       
   147             InputSource is = SAXSource.sourceToInputSource(src);
       
   148             if (is.getEncoding()== null && soapPart.getSourceCharsetEncoding() != null) {
       
   149                 is.setEncoding(soapPart.getSourceCharsetEncoding());
       
   150             }
       
   151             XMLReader rejectFilter;
       
   152             try {
       
   153                 rejectFilter = new RejectDoctypeSaxFilter(saxParser);
       
   154             } catch (Exception ex) {
       
   155                 log.severe("SAAJ0510.soap.cannot.create.envelope");
       
   156                 throw new SOAPExceptionImpl(
       
   157                     "Unable to create envelope from given source: ",
       
   158                     ex);
       
   159             }
       
   160             src = new SAXSource(rejectFilter, is);
       
   161         }
       
   162 
       
   163         try {
   155         try {
   164             Transformer transformer =
   156                 // Insert SAX filter to disallow Document Type Declarations since
   165                 EfficientStreamingTransformer.newTransformer();
   157                 // they are not legal in SOAP
   166             DOMResult result = new DOMResult(soapPart);
   158 
   167             transformer.transform(src, result);
   159                 if (src instanceof StreamSource) {
   168 
   160                         try {
   169             Envelope env = (Envelope) soapPart.getEnvelope();
   161                                 saxParser = parserPool.get().get();
   170             return env;
   162                         } catch (Exception e) {
   171         } catch (Exception ex) {
   163                                 log.severe("SAAJ0601.util.newSAXParser.exception");
   172             if (ex instanceof SOAPVersionMismatchException) {
   164                                 throw new SOAPExceptionImpl(
   173                 throw (SOAPVersionMismatchException) ex;
   165                                                 "Couldn't get a SAX parser while constructing a envelope",
   174             }
   166                                                 e);
   175             log.severe("SAAJ0511.soap.cannot.create.envelope");
   167                         }
   176             throw new SOAPExceptionImpl(
   168                         InputSource is = SAXSource.sourceToInputSource(src);
   177                 "Unable to create envelope from given source: ",
   169                         if (is.getEncoding()== null && soapPart.getSourceCharsetEncoding() != null) {
   178                 ex);
   170                                 is.setEncoding(soapPart.getSourceCharsetEncoding());
       
   171                         }
       
   172                         XMLReader rejectFilter;
       
   173                         try {
       
   174                                 rejectFilter = new RejectDoctypeSaxFilter(saxParser);
       
   175                         } catch (Exception ex) {
       
   176                                 log.severe("SAAJ0510.soap.cannot.create.envelope");
       
   177                                 throw new SOAPExceptionImpl(
       
   178                                                 "Unable to create envelope from given source: ",
       
   179                                                 ex);
       
   180                         }
       
   181                         src = new SAXSource(rejectFilter, is);
       
   182                 }
       
   183 
       
   184                 try {
       
   185                         Transformer transformer =
       
   186                                         EfficientStreamingTransformer.newTransformer();
       
   187                         DOMResult result = new DOMResult(soapPart);
       
   188                         transformer.transform(src, result);
       
   189 
       
   190                         Envelope env = (Envelope) soapPart.getEnvelope();
       
   191                         return env;
       
   192                 } catch (Exception ex) {
       
   193                         if (ex instanceof SOAPVersionMismatchException) {
       
   194                                 throw (SOAPVersionMismatchException) ex;
       
   195                         }
       
   196                         log.severe("SAAJ0511.soap.cannot.create.envelope");
       
   197                         throw new SOAPExceptionImpl(
       
   198                                         "Unable to create envelope from given source: ",
       
   199                                         ex);
       
   200                 }
   179         } finally {
   201         } finally {
       
   202                 //no matter what condition occurs, always return the parser to the pool
   180             if (saxParser != null) {
   203             if (saxParser != null) {
   181                 parserPool.get().returnParser(saxParser);
   204                 parserPool.get().returnParser(saxParser);
   182             }
   205             }
   183         }
   206         }
   184     }
   207     }