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