Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
1 /*
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.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.messaging.saaj.soap;
28 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
29 import com.sun.xml.internal.messaging.saaj.util.JAXMStreamSource;
30 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
31 import com.sun.xml.internal.messaging.saaj.util.ParserPool;
32 import com.sun.xml.internal.messaging.saaj.util.RejectDoctypeSaxFilter;
33 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.XMLReader;
37 import javax.xml.parsers.SAXParser;
38 import javax.xml.soap.SOAPException;
39 import javax.xml.transform.Source;
40 import javax.xml.transform.Transformer;
41 import javax.xml.transform.dom.DOMResult;
42 import javax.xml.transform.sax.SAXSource;
43 import javax.xml.transform.stream.StreamSource;
44 import java.util.logging.Logger;
46 /**
47 * EnvelopeFactory creates SOAP Envelope objects using different
48 * underlying implementations.
49 */
50 public class EnvelopeFactory {
52 protected static final Logger
53 log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
54 "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
56 private static ContextClassloaderLocal<ParserPool> parserPool =
57 new ContextClassloaderLocal<ParserPool>() {
58 @Override
59 protected ParserPool initialValue() throws Exception {
60 return new ParserPool(5);
61 }
62 };
64 public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart)
65 throws SOAPException {
66 // Insert SAX filter to disallow Document Type Declarations since
67 // they are not legal in SOAP
68 SAXParser saxParser = null;
69 if (src instanceof StreamSource) {
70 if (src instanceof JAXMStreamSource) {
71 try {
72 if (!SOAPPartImpl.lazyContentLength) {
73 ((JAXMStreamSource) src).reset();
74 }
75 } catch (java.io.IOException ioe) {
76 log.severe("SAAJ0515.source.reset.exception");
77 throw new SOAPExceptionImpl(ioe);
78 }
79 }
80 try {
81 saxParser = parserPool.get().get();
82 } catch (Exception e) {
83 log.severe("SAAJ0601.util.newSAXParser.exception");
84 throw new SOAPExceptionImpl(
85 "Couldn't get a SAX parser while constructing a envelope",
86 e);
87 }
88 InputSource is = SAXSource.sourceToInputSource(src);
89 if (is.getEncoding() == null && soapPart.getSourceCharsetEncoding() != null) {
90 is.setEncoding(soapPart.getSourceCharsetEncoding());
91 }
92 XMLReader rejectFilter;
93 try {
94 rejectFilter = new RejectDoctypeSaxFilter(saxParser);
95 } catch (Exception ex) {
96 log.severe("SAAJ0510.soap.cannot.create.envelope");
97 throw new SOAPExceptionImpl(
98 "Unable to create envelope from given source: ",
99 ex);
100 }
101 src = new SAXSource(rejectFilter, is);
102 }
104 try {
105 Transformer transformer =
106 EfficientStreamingTransformer.newTransformer();
107 DOMResult result = new DOMResult(soapPart);
108 transformer.transform(src, result);
110 Envelope env = (Envelope) soapPart.getEnvelope();
111 return env;
112 } catch (Exception ex) {
113 if (ex instanceof SOAPVersionMismatchException) {
114 throw (SOAPVersionMismatchException) ex;
115 }
116 log.severe("SAAJ0511.soap.cannot.create.envelope");
117 throw new SOAPExceptionImpl(
118 "Unable to create envelope from given source: ",
119 ex);
120 } finally {
121 if (saxParser != null) {
122 parserPool.get().returnParser(saxParser);
123 }
124 }
125 }
126 }