1 /* |
1 /* |
2 * Copyright (c) 1997, 2012, 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 |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.xml.internal.messaging.saaj.soap; |
26 package com.sun.xml.internal.messaging.saaj.soap; |
27 |
27 |
28 import java.util.logging.Logger; |
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; |
29 |
36 |
30 import javax.xml.parsers.SAXParser; |
37 import javax.xml.parsers.SAXParser; |
31 import javax.xml.soap.SOAPException; |
38 import javax.xml.soap.SOAPException; |
32 import javax.xml.transform.Source; |
39 import javax.xml.transform.Source; |
33 import javax.xml.transform.Transformer; |
40 import javax.xml.transform.Transformer; |
34 import javax.xml.transform.dom.DOMResult; |
41 import javax.xml.transform.dom.DOMResult; |
35 import javax.xml.transform.sax.SAXSource; |
42 import javax.xml.transform.sax.SAXSource; |
36 import javax.xml.transform.stream.StreamSource; |
43 import javax.xml.transform.stream.StreamSource; |
37 |
44 import java.util.logging.Logger; |
38 import org.xml.sax.InputSource; |
|
39 import org.xml.sax.XMLReader; |
|
40 |
|
41 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
|
42 import com.sun.xml.internal.messaging.saaj.util.*; |
|
43 |
|
44 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer; |
|
45 |
45 |
46 /** |
46 /** |
47 * EnvelopeFactory creates SOAP Envelope objects using different |
47 * EnvelopeFactory creates SOAP Envelope objects using different |
48 * underlying implementations. |
48 * underlying implementations. |
49 */ |
49 */ |
50 public class EnvelopeFactory { |
50 public class EnvelopeFactory { |
51 |
51 |
52 protected static final Logger |
52 protected static final Logger |
53 log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, |
53 log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, |
54 "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); |
54 "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); |
55 |
55 |
56 private static ParserPool parserPool = new ParserPool(5); |
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 }; |
57 |
63 |
58 public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart) |
64 public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart) |
59 throws SOAPException |
65 throws SOAPException { |
60 { |
|
61 // Insert SAX filter to disallow Document Type Declarations since |
66 // Insert SAX filter to disallow Document Type Declarations since |
62 // they are not legal in SOAP |
67 // they are not legal in SOAP |
63 SAXParser saxParser = null; |
68 SAXParser saxParser = null; |
64 if (src instanceof StreamSource) { |
69 if (src instanceof StreamSource) { |
65 if (src instanceof JAXMStreamSource) { |
70 if (src instanceof JAXMStreamSource) { |
71 log.severe("SAAJ0515.source.reset.exception"); |
76 log.severe("SAAJ0515.source.reset.exception"); |
72 throw new SOAPExceptionImpl(ioe); |
77 throw new SOAPExceptionImpl(ioe); |
73 } |
78 } |
74 } |
79 } |
75 try { |
80 try { |
76 saxParser = parserPool.get(); |
81 saxParser = parserPool.get().get(); |
77 } catch (Exception e) { |
82 } catch (Exception e) { |
78 log.severe("SAAJ0601.util.newSAXParser.exception"); |
83 log.severe("SAAJ0601.util.newSAXParser.exception"); |
79 throw new SOAPExceptionImpl( |
84 throw new SOAPExceptionImpl( |
80 "Couldn't get a SAX parser while constructing a envelope", |
85 "Couldn't get a SAX parser while constructing a envelope", |
81 e); |
86 e); |
82 } |
87 } |
83 InputSource is = SAXSource.sourceToInputSource(src); |
88 InputSource is = SAXSource.sourceToInputSource(src); |
84 if (is.getEncoding()== null && soapPart.getSourceCharsetEncoding() != null) { |
89 if (is.getEncoding() == null && soapPart.getSourceCharsetEncoding() != null) { |
85 is.setEncoding(soapPart.getSourceCharsetEncoding()); |
90 is.setEncoding(soapPart.getSourceCharsetEncoding()); |
86 } |
91 } |
87 XMLReader rejectFilter; |
92 XMLReader rejectFilter; |
88 try { |
93 try { |
89 rejectFilter = new RejectDoctypeSaxFilter(saxParser); |
94 rejectFilter = new RejectDoctypeSaxFilter(saxParser); |
90 } catch (Exception ex) { |
95 } catch (Exception ex) { |
91 log.severe("SAAJ0510.soap.cannot.create.envelope"); |
96 log.severe("SAAJ0510.soap.cannot.create.envelope"); |
92 throw new SOAPExceptionImpl( |
97 throw new SOAPExceptionImpl( |
93 "Unable to create envelope from given source: ", |
98 "Unable to create envelope from given source: ", |
94 ex); |
99 ex); |
95 } |
100 } |
96 src = new SAXSource(rejectFilter, is); |
101 src = new SAXSource(rejectFilter, is); |
97 } |
102 } |
98 |
103 |
99 try { |
104 try { |
100 Transformer transformer = |
105 Transformer transformer = |
101 EfficientStreamingTransformer.newTransformer(); |
106 EfficientStreamingTransformer.newTransformer(); |
102 DOMResult result = new DOMResult(soapPart); |
107 DOMResult result = new DOMResult(soapPart); |
103 transformer.transform(src, result); |
108 transformer.transform(src, result); |
104 |
109 |
105 Envelope env = (Envelope) soapPart.getEnvelope(); |
110 Envelope env = (Envelope) soapPart.getEnvelope(); |
106 return env; |
111 return env; |
108 if (ex instanceof SOAPVersionMismatchException) { |
113 if (ex instanceof SOAPVersionMismatchException) { |
109 throw (SOAPVersionMismatchException) ex; |
114 throw (SOAPVersionMismatchException) ex; |
110 } |
115 } |
111 log.severe("SAAJ0511.soap.cannot.create.envelope"); |
116 log.severe("SAAJ0511.soap.cannot.create.envelope"); |
112 throw new SOAPExceptionImpl( |
117 throw new SOAPExceptionImpl( |
113 "Unable to create envelope from given source: ", |
118 "Unable to create envelope from given source: ", |
114 ex); |
119 ex); |
115 } finally { |
120 } finally { |
116 if (saxParser != null) { |
121 if (saxParser != null) { |
117 parserPool.returnParser(saxParser); |
122 parserPool.get().returnParser(saxParser); |
118 } |
123 } |
119 } |
124 } |
120 } |
125 } |
121 } |
126 } |