aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.messaging.saaj.soap; aoqi@0: aoqi@0: import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; aoqi@0: import com.sun.xml.internal.messaging.saaj.util.JAXMStreamSource; aoqi@0: import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; aoqi@0: import com.sun.xml.internal.messaging.saaj.util.ParserPool; aoqi@0: import com.sun.xml.internal.messaging.saaj.util.RejectDoctypeSaxFilter; aoqi@0: import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer; aoqi@0: import org.xml.sax.InputSource; aoqi@0: import org.xml.sax.XMLReader; aoqi@0: aoqi@0: import javax.xml.parsers.SAXParser; aoqi@0: import javax.xml.soap.SOAPException; aoqi@0: import javax.xml.transform.Source; aoqi@0: import javax.xml.transform.Transformer; aoqi@0: import javax.xml.transform.dom.DOMResult; aoqi@0: import javax.xml.transform.sax.SAXSource; aoqi@0: import javax.xml.transform.stream.StreamSource; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: /** aoqi@0: * EnvelopeFactory creates SOAP Envelope objects using different aoqi@0: * underlying implementations. aoqi@0: */ aoqi@0: public class EnvelopeFactory { aoqi@0: aoqi@0: protected static final Logger aoqi@0: log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, aoqi@0: "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); aoqi@0: aoqi@0: private static ContextClassloaderLocal parserPool = aoqi@0: new ContextClassloaderLocal() { aoqi@0: @Override aoqi@0: protected ParserPool initialValue() throws Exception { aoqi@0: return new ParserPool(5); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: public static Envelope createEnvelope(Source src, SOAPPartImpl soapPart) aoqi@0: throws SOAPException { aoqi@0: // Insert SAX filter to disallow Document Type Declarations since aoqi@0: // they are not legal in SOAP aoqi@0: SAXParser saxParser = null; aoqi@0: if (src instanceof StreamSource) { aoqi@0: if (src instanceof JAXMStreamSource) { aoqi@0: try { aoqi@0: if (!SOAPPartImpl.lazyContentLength) { aoqi@0: ((JAXMStreamSource) src).reset(); aoqi@0: } aoqi@0: } catch (java.io.IOException ioe) { aoqi@0: log.severe("SAAJ0515.source.reset.exception"); aoqi@0: throw new SOAPExceptionImpl(ioe); aoqi@0: } aoqi@0: } aoqi@0: try { aoqi@0: saxParser = parserPool.get().get(); aoqi@0: } catch (Exception e) { aoqi@0: log.severe("SAAJ0601.util.newSAXParser.exception"); aoqi@0: throw new SOAPExceptionImpl( aoqi@0: "Couldn't get a SAX parser while constructing a envelope", aoqi@0: e); aoqi@0: } aoqi@0: InputSource is = SAXSource.sourceToInputSource(src); aoqi@0: if (is.getEncoding() == null && soapPart.getSourceCharsetEncoding() != null) { aoqi@0: is.setEncoding(soapPart.getSourceCharsetEncoding()); aoqi@0: } aoqi@0: XMLReader rejectFilter; aoqi@0: try { aoqi@0: rejectFilter = new RejectDoctypeSaxFilter(saxParser); aoqi@0: } catch (Exception ex) { aoqi@0: log.severe("SAAJ0510.soap.cannot.create.envelope"); aoqi@0: throw new SOAPExceptionImpl( aoqi@0: "Unable to create envelope from given source: ", aoqi@0: ex); aoqi@0: } aoqi@0: src = new SAXSource(rejectFilter, is); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: Transformer transformer = aoqi@0: EfficientStreamingTransformer.newTransformer(); aoqi@0: DOMResult result = new DOMResult(soapPart); aoqi@0: transformer.transform(src, result); aoqi@0: aoqi@0: Envelope env = (Envelope) soapPart.getEnvelope(); aoqi@0: return env; aoqi@0: } catch (Exception ex) { aoqi@0: if (ex instanceof SOAPVersionMismatchException) { aoqi@0: throw (SOAPVersionMismatchException) ex; aoqi@0: } aoqi@0: log.severe("SAAJ0511.soap.cannot.create.envelope"); aoqi@0: throw new SOAPExceptionImpl( aoqi@0: "Unable to create envelope from given source: ", aoqi@0: ex); aoqi@0: } finally { aoqi@0: if (saxParser != null) { aoqi@0: parserPool.get().returnParser(saxParser); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }