001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: EnvelopeFactory.java,v 1.3 2007/07/24 10:37:43 kumarjayanti Exp $
022: * $Revision: 1.3 $
023: * $Date: 2007/07/24 10:37:43 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj.soap;
062:
063: import java.util.logging.Logger;
064:
065: import javax.xml.parsers.SAXParser;
066: import javax.xml.soap.SOAPException;
067: import javax.xml.transform.Source;
068: import javax.xml.transform.Transformer;
069: import javax.xml.transform.dom.DOMResult;
070: import javax.xml.transform.sax.SAXSource;
071: import javax.xml.transform.stream.StreamSource;
072:
073: import org.xml.sax.InputSource;
074: import org.xml.sax.XMLReader;
075:
076: import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
077: import com.sun.xml.messaging.saaj.util.*;
078:
079: import com.sun.xml.messaging.saaj.util.transform.EfficientStreamingTransformer;
080:
081: /**
082: * EnvelopeFactory creates SOAP Envelope objects using different
083: * underlying implementations.
084: */
085: public class EnvelopeFactory {
086:
087: protected static Logger log = Logger.getLogger(
088: LogDomainConstants.SOAP_DOMAIN,
089: "com.sun.xml.messaging.saaj.soap.LocalStrings");
090:
091: private static ParserPool parserPool = new ParserPool(5);
092:
093: public static Envelope createEnvelope(Source src,
094: SOAPPartImpl soapPart) throws SOAPException {
095: // Insert SAX filter to disallow Document Type Declarations since
096: // they are not legal in SOAP
097: SAXParser saxParser = null;
098: if (src instanceof StreamSource) {
099: if (src instanceof JAXMStreamSource) {
100: try {
101: ((JAXMStreamSource) src).reset();
102: } catch (java.io.IOException ioe) {
103: log.severe("SAAJ0515.source.reset.exception");
104: throw new SOAPExceptionImpl(ioe);
105: }
106: }
107: try {
108: saxParser = parserPool.get();
109: } catch (Exception e) {
110: log.severe("SAAJ0601.util.newSAXParser.exception");
111: throw new SOAPExceptionImpl(
112: "Couldn't get a SAX parser while constructing a envelope",
113: e);
114: }
115: InputSource is = SAXSource.sourceToInputSource(src);
116: if (is.getEncoding() == null
117: && soapPart.getSourceCharsetEncoding() != null) {
118: is.setEncoding(soapPart.getSourceCharsetEncoding());
119: }
120: XMLReader rejectFilter;
121: try {
122: rejectFilter = new RejectDoctypeSaxFilter(saxParser);
123: } catch (Exception ex) {
124: log.severe("SAAJ0510.soap.cannot.create.envelope");
125: throw new SOAPExceptionImpl(
126: "Unable to create envelope from given source: ",
127: ex);
128: }
129: src = new SAXSource(rejectFilter, is);
130: }
131:
132: try {
133: Transformer transformer = EfficientStreamingTransformer
134: .newTransformer();
135: DOMResult result = new DOMResult(soapPart);
136: transformer.transform(src, result);
137:
138: Envelope env = (Envelope) soapPart.getEnvelope();
139: if (saxParser != null) {
140: parserPool.put(saxParser);
141: }
142: return env;
143: } catch (Exception ex) {
144: if (ex instanceof SOAPVersionMismatchException) {
145: throw (SOAPVersionMismatchException) ex;
146: }
147: log.severe("SAAJ0511.soap.cannot.create.envelope");
148: throw new SOAPExceptionImpl(
149: "Unable to create envelope from given source: ", ex);
150: }
151: }
152: }
|