Skip to content

Package: EnvelopeFactory$1$1

EnvelopeFactory$1$1

nameinstructionbranchcomplexitylinemethod
run()
M: 4 C: 4
50%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.messaging.saaj.soap;
12:
13: import com.sun.xml.messaging.saaj.LazyEnvelopeSource;
14: import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
15: import com.sun.xml.messaging.saaj.util.JAXMStreamSource;
16: import com.sun.xml.messaging.saaj.util.LogDomainConstants;
17: import com.sun.xml.messaging.saaj.util.ParserPool;
18: import com.sun.xml.messaging.saaj.util.RejectDoctypeSaxFilter;
19: import com.sun.xml.messaging.saaj.util.transform.EfficientStreamingTransformer;
20:
21: import org.xml.sax.InputSource;
22: import org.xml.sax.XMLReader;
23:
24: import javax.xml.parsers.SAXParser;
25: import jakarta.xml.soap.SOAPException;
26: import javax.xml.stream.XMLInputFactory;
27: import javax.xml.stream.XMLStreamException;
28: import javax.xml.stream.XMLStreamReader;
29: import javax.xml.transform.Source;
30: import javax.xml.transform.Transformer;
31: import javax.xml.transform.dom.DOMResult;
32: import javax.xml.transform.sax.SAXSource;
33: import javax.xml.transform.stax.StAXSource;
34: import javax.xml.transform.stream.StreamSource;
35:
36: import java.security.AccessController;
37: import java.security.PrivilegedAction;
38: import java.util.logging.Logger;
39:
40: /**
41: * EnvelopeFactory creates SOAP Envelope objects using different
42: * underlying implementations.
43: */
44: public class EnvelopeFactory {
45: private static final String SAX_PARSER_POOL_SIZE_PROP_NAME = "com.sun.xml.messaging.saaj.soap.saxParserPoolSize";
46: private static final int DEFAULT_SAX_PARSER_POOL_SIZE = 5;
47:
48: protected static final Logger
49: log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
50: "com.sun.xml.messaging.saaj.soap.LocalStrings");
51:
52: private static ContextClassloaderLocal<ParserPool> parserPool =
53: new ContextClassloaderLocal<ParserPool>() {
54: @Override
55: protected ParserPool initialValue() throws Exception {
56:         Integer poolSize = AccessController.doPrivileged(
57:                         new PrivilegedAction<Integer>() {
58:                                 @Override
59:                                 public Integer run() {
60:                                         try {
61:                                                 return Integer.getInteger(
62:                                                                 SAX_PARSER_POOL_SIZE_PROP_NAME,
63:                                                                 DEFAULT_SAX_PARSER_POOL_SIZE);
64:                                         } catch (SecurityException se) {
65:                                                 return DEFAULT_SAX_PARSER_POOL_SIZE;
66:                                         }
67:                                 }
68:                         });
69: return new ParserPool(poolSize);
70: }
71: };
72:
73: public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart)
74: throws SOAPException
75: {
76: if (src instanceof JAXMStreamSource) {
77: try {
78: if (!SOAPPartImpl.lazyContentLength) {
79: ((JAXMStreamSource) src).reset();
80: }
81: } catch (java.io.IOException ioe) {
82: log.severe("SAAJ0515.source.reset.exception");
83: throw new SOAPExceptionImpl(ioe);
84: }
85: }
86: if (src instanceof LazyEnvelopeSource) {
87: return lazy((LazyEnvelopeSource)src, soapPart);
88: }
89: if (soapPart.message.isLazySoapBodyParsing()) {
90: return parseEnvelopeStax(src, soapPart);
91: } else {
92: return parseEnvelopeSax(src, soapPart);
93: }
94: }
95:
96: private static Envelope lazy(LazyEnvelopeSource src, SOAPPartImpl soapPart) throws SOAPException {
97: try {
98:         StaxBridge staxBridge = new StaxLazySourceBridge(src, soapPart);
99:         staxBridge.bridgeEnvelopeAndHeaders();
100: Envelope env = (Envelope) soapPart.getEnvelope();
101: env.setStaxBridge(staxBridge);
102: return env;
103: } catch (XMLStreamException e) {
104: throw new SOAPException(e);
105: }
106: }
107:
108: static private XMLInputFactory xmlInputFactory = null;
109:
110: private static Envelope parseEnvelopeStax(Source src, SOAPPartImpl soapPart)
111: throws SOAPException {
112: XMLStreamReader streamReader = null;
113: if (src instanceof StAXSource) {
114: streamReader = ((StAXSource) src).getXMLStreamReader();
115: }
116: try {
117: if (streamReader == null) {
118: if (xmlInputFactory == null) xmlInputFactory = XMLInputFactory.newInstance();
119: streamReader = xmlInputFactory.createXMLStreamReader(src);
120: }
121: // SaajStaxWriter saajWriter = new SaajStaxWriter(soapPart.message, soapPart.document);
122: // XMLStreamReaderToXMLStreamWriter readerWriterBridge = new XMLStreamReaderToXMLStreamWriter(
123: // streamReader, saajWriter, soapPart.getSOAPNamespace());
124:
125: StaxBridge readerWriterBridge = new StaxReaderBridge(streamReader, soapPart);
126: //bridge will stop reading at body element, and parse upon request, so save it
127: //on the envelope
128: readerWriterBridge.bridgeEnvelopeAndHeaders();
129:
130: Envelope env = (Envelope) soapPart.getEnvelope();
131: env.setStaxBridge(readerWriterBridge);
132: return env;
133: } catch (Exception e) {
134: throw new SOAPException(e);
135: }
136: }
137: private static Envelope parseEnvelopeSax(Source src, SOAPPartImpl soapPart)
138: throws SOAPException {
139:         SAXParser saxParser = null;
140:          ParserPool underlyingParserPool = parserPool.get();
141:          try {
142:                 // Insert SAX filter to disallow Document Type Declarations since
143:                 // they are not legal in SOAP
144:
145:                 if (src instanceof StreamSource) {
146:                         try {
147:                                 saxParser = underlyingParserPool.get();
148:                         } catch (Exception e) {
149:                                 log.severe("SAAJ0601.util.newSAXParser.exception");
150:                                 throw new SOAPExceptionImpl(
151:                                                 "Couldn't get a SAX parser while constructing a envelope",
152:                                                 e);
153:                         }
154:                         InputSource is = SAXSource.sourceToInputSource(src);
155:                         if (is.getEncoding()== null && soapPart.getSourceCharsetEncoding() != null) {
156:                                 is.setEncoding(soapPart.getSourceCharsetEncoding());
157:                         }
158:                         XMLReader rejectFilter;
159:                         try {
160:                                 rejectFilter = new RejectDoctypeSaxFilter(saxParser);
161:                         } catch (Exception ex) {
162:                                 log.severe("SAAJ0510.soap.cannot.create.envelope");
163:                                 throw new SOAPExceptionImpl(
164:                                                 "Unable to create envelope from given source: ",
165:                                                 ex);
166:                         }
167:                         src = new SAXSource(rejectFilter, is);
168:                 }
169:
170:                 try {
171:                         Transformer transformer =
172:                                         EfficientStreamingTransformer.newTransformer();
173:                         DOMResult result = new DOMResult(soapPart);
174:                         transformer.transform(src, result);
175:
176:                         Envelope env = (Envelope) soapPart.getEnvelope();
177:                         return env;
178:                 } catch (Exception ex) {
179:                         if (ex instanceof SOAPVersionMismatchException) {
180:                                 throw (SOAPVersionMismatchException) ex;
181:                         }
182:                         log.severe("SAAJ0511.soap.cannot.create.envelope");
183:                         throw new SOAPExceptionImpl(
184:                                         "Unable to create envelope from given source: ",
185:                                         ex);
186:                 }
187:         } finally {
188:                 //no matter what condition occurs, always return the parser to the pool
189: if (saxParser != null) {
190: underlyingParserPool.returnParser(saxParser);
191: }
192: }
193: }
194: }