001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.util;
027:
028: import com.sun.xml.internal.ws.encoding.soap.message.SOAPMsgCreateException;
029: import com.sun.xml.internal.ws.encoding.soap.message.SOAPMsgFactoryCreateException;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import javax.xml.soap.*;
033: import javax.xml.ws.soap.SOAPBinding;
034: import javax.xml.namespace.QName;
035:
036: import org.w3c.dom.*;
037: import org.w3c.dom.Node;
038:
039: /**
040: * $author: JAXWS Development Team
041: */
042:
043: /**
044: * Has utility methods to create SOAPMessage
045: */
046: public class SOAPUtil {
047:
048: private static final MessageFactory soap11messageFactory = createMessageFactory(SOAPConstants.SOAP_1_1_PROTOCOL);
049: private static final MessageFactory soap12messageFactory = createMessageFactory(SOAPConstants.SOAP_1_2_PROTOCOL);
050: private static final SOAPFactory soap11Factory = createSOAPFactory(SOAPConstants.SOAP_1_1_PROTOCOL);
051: private static final SOAPFactory soap12Factory = createSOAPFactory(SOAPConstants.SOAP_1_2_PROTOCOL);
052:
053: private static SOAPFactory createSOAPFactory(String soapProtocol) {
054: try {
055: return SOAPFactory.newInstance(soapProtocol);
056: } catch (SOAPException e) {
057: throw new SOAPMsgFactoryCreateException(
058: "soap.factory.create.err", new Object[] { e });
059: }
060: }
061:
062: /**
063: *
064: * @param bindingId
065: * @return
066: * returns SOAPFactor for SOAP 1.2 if bindingID equals SOAP1.2 HTTP binding else
067: * SOAPFactory for SOAP 1.1
068: */
069: public static SOAPFactory getSOAPFactory(String bindingId) {
070: if (bindingId.equals(SOAPBinding.SOAP12HTTP_BINDING)) {
071: return soap12Factory;
072: }
073: return soap11Factory;
074: }
075:
076: public static SOAPFault createSOAPFault(String bindingId) {
077: if (bindingId == null)
078: bindingId = SOAPBinding.SOAP11HTTP_BINDING;
079: try {
080: if (bindingId.equals(SOAPBinding.SOAP12HTTP_BINDING))
081: return soap12Factory.createFault();
082: return soap11Factory.createFault();
083: } catch (SOAPException e) {
084: throw new SOAPMsgFactoryCreateException(
085: "soap.fault.create.err", new Object[] { e });
086: }
087: }
088:
089: /**
090: * Creates SOAP 1.1 or SOAP 1.2 SOAPFault based on the bindingId
091: * @param msg
092: * @param code
093: * @param actor
094: * @param detail
095: * @return the created SOAPFault
096: */
097: public static SOAPFault createSOAPFault(String msg, QName code,
098: String actor, Detail detail, String bindingId) {
099: if (bindingId == null)
100: bindingId = SOAPBinding.SOAP11HTTP_BINDING;
101: try {
102: SOAPFault fault = null;
103: if (bindingId.equals(SOAPBinding.SOAP12HTTP_BINDING))
104: fault = soap12Factory.createFault(msg, code);
105: else if (bindingId.equals(SOAPBinding.SOAP11HTTP_BINDING))
106: fault = soap11Factory.createFault(msg, code);
107: if (actor != null)
108: fault.setFaultActor(actor);
109: if (detail != null) {
110: Node n = fault.getOwnerDocument().importNode(detail,
111: true);
112: fault.appendChild(n);
113: }
114: return fault;
115: } catch (SOAPException e) {
116: throw new SOAPMsgFactoryCreateException(
117: "soap.fault.create.err", new Object[] { e });
118: }
119: }
120:
121: public static SOAPMessage createMessage() {
122: return createMessage(SOAPBinding.SOAP11HTTP_BINDING);
123: }
124:
125: /**
126: *
127: * @param binding
128: * @return a <code>SOAPMessage</code> associated with <code>binding</code>
129: */
130: public static SOAPMessage createMessage(String binding) {
131: try {
132: return getMessageFactory(binding).createMessage();
133: } catch (SOAPException e) {
134: throw new SOAPMsgCreateException("soap.msg.create.err",
135: new Object[] { e });
136: }
137: }
138:
139: /**
140: *
141: * @param binding
142: * @param headers
143: * @param in
144: * @return <code>SOAPMessage</code> with <code>MimeHeaders</code> from an
145: * <code>InputStream</code> and binding.
146: * @throws IOException
147: */
148: public static SOAPMessage createMessage(MimeHeaders headers,
149: InputStream in, String binding) throws IOException {
150: try {
151: return getMessageFactory(binding)
152: .createMessage(headers, in);
153: } catch (SOAPException e) {
154: throw new SOAPMsgCreateException("soap.msg.create.err",
155: new Object[] { e });
156: }
157: }
158:
159: public static SOAPMessage createMessage(MimeHeaders headers,
160: InputStream in) throws IOException {
161: return createMessage(headers, in,
162: SOAPBinding.SOAP11HTTP_BINDING);
163: }
164:
165: public static MessageFactory getMessageFactory(String binding) {
166: if (binding.equals(SOAPBinding.SOAP11HTTP_BINDING)) {
167: return soap11messageFactory;
168: } else if (binding.equals(SOAPBinding.SOAP12HTTP_BINDING)) {
169: return soap12messageFactory;
170: }
171: return soap11messageFactory;
172: }
173:
174: private static MessageFactory createMessageFactory(String bindingId) {
175: try {
176: return MessageFactory.newInstance(bindingId);
177: } catch (SOAPException e) {
178: throw new SOAPMsgFactoryCreateException(
179: "soap.msg.factory.create.err", new Object[] { e });
180: }
181: }
182:
183: }
|