001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.tx.common;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.ws.api.SOAPVersion;
042: import com.sun.xml.ws.api.WSService;
043: import com.sun.xml.ws.api.addressing.AddressingVersion;
044: import com.sun.xml.ws.api.addressing.OneWayFeature;
045: import com.sun.xml.ws.api.addressing.WSEndpointReference;
046: import com.sun.xml.ws.api.message.HeaderList;
047: import com.sun.xml.ws.developer.JAXWSProperties;
048: import com.sun.xml.ws.developer.MemberSubmissionAddressingFeature;
049:
050: import javax.xml.namespace.QName;
051: import javax.xml.soap.SOAPConstants;
052: import javax.xml.soap.SOAPException;
053: import javax.xml.soap.SOAPFactory;
054: import javax.xml.soap.SOAPFault;
055: import javax.xml.transform.Source;
056: import javax.xml.transform.dom.DOMSource;
057: import javax.xml.ws.BindingProvider;
058: import javax.xml.ws.Dispatch;
059: import javax.xml.ws.EndpointReference;
060: import javax.xml.ws.Service;
061: import javax.xml.ws.WebServiceContext;
062: import javax.xml.ws.WebServiceException;
063: import javax.xml.ws.WebServiceFeature;
064: import javax.xml.ws.handler.MessageContext;
065: import javax.xml.ws.soap.SOAPBinding;
066: import java.util.Locale;
067:
068: /**
069: * WS-Addressing helper methods.
070: *
071: * @author Ryan.Shoemaker@Sun.COM
072: */
073: public class WsaHelper {
074:
075: static HeaderList getInboundHeaderList(@NotNull
076: WebServiceContext wsContext) {
077: MessageContext msgContext = wsContext.getMessageContext();
078: return (HeaderList) msgContext
079: .get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
080: }
081:
082: public static String getMsgID(@NotNull
083: WebServiceContext wsContext) {
084: HeaderList headers = getInboundHeaderList(wsContext);
085: return headers.getMessageID(AddressingVersion.MEMBER,
086: SOAPVersion.SOAP_11);
087: }
088:
089: public static EndpointReference getReplyTo(@NotNull
090: WebServiceContext wsContext) {
091: HeaderList headers = getInboundHeaderList(wsContext);
092: return (headers.getReplyTo(AddressingVersion.MEMBER,
093: SOAPVersion.SOAP_11)).toSpec();
094: }
095:
096: public static WSEndpointReference getFaultTo(@NotNull
097: WebServiceContext wsContext) {
098: HeaderList headers = getInboundHeaderList(wsContext);
099: return headers.getFaultTo(AddressingVersion.MEMBER,
100: SOAPVersion.SOAP_11);
101: }
102:
103: /**
104: * Create a SOAPFault from the specified information.
105: *
106: * @param soapVer soap version
107: * @param fault fault enum
108: * @param message message
109: * @return the new SOAPFault
110: */
111: @NotNull
112: static SOAPFault createFault(@NotNull
113: final SOAPVersion soapVer, @NotNull
114: final TxFault fault, @NotNull
115: final String message) {
116: try {
117: final SOAPFactory soapFactory = soapVer.saajSoapFactory;
118: final SOAPFault soapFault = soapFactory.createFault();
119:
120: if (soapVer == SOAPVersion.SOAP_11) {
121: soapFault.setFaultCode(fault.subcode);
122: soapFault.setFaultString(fault.reason + ": " + message,
123: Locale.ENGLISH);
124: } else { // SOAP 1.2
125: soapFault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT);
126: soapFault.appendFaultSubcode(fault.subcode);
127: soapFault.setFaultString(fault.reason + ": " + message,
128: Locale.ENGLISH);
129: }
130: return soapFault;
131: } catch (SOAPException e) {
132: throw new WebServiceException(e);
133: }
134: }
135:
136: /**
137: * Dispatch a fault, adding any necessary headers to 'fault' in the process.
138: *
139: * @param faultTo
140: * @param replyTo
141: * @param soapVer
142: * @param fault
143: * @param message
144: * @param msgID
145: */
146: public static void sendFault(@Nullable
147: final WSEndpointReference faultTo, @NotNull
148: final EndpointReference replyTo, @NotNull
149: final SOAPVersion soapVer, @NotNull
150: final TxFault fault, @NotNull
151: final String message, @NotNull
152: final String msgID) {
153:
154: final WSEndpointReference to = faultTo != null ? faultTo
155: : new WSEndpointReference(replyTo);
156:
157: final WSService s = WSService.create();
158: final QName port = new QName("foo", "bar");
159: s
160: .addPort(port, SOAPBinding.SOAP11HTTP_BINDING, to
161: .getAddress());
162:
163: // one-way feature
164: final OneWayFeature owf = new OneWayFeature();
165: owf.setRelatesToID(msgID);
166: // member submission addressing feature
167: final WebServiceFeature af = new MemberSubmissionAddressingFeature(
168: true);
169:
170: final Dispatch<Source> d = s.createDispatch(port, to,
171: Source.class, Service.Mode.PAYLOAD, owf, af);
172: d.getRequestContext().put(
173: BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
174: d.getRequestContext().put(
175: BindingProvider.SOAPACTION_URI_PROPERTY,
176: fault.actionURI);
177:
178: d.invokeOneWay(new DOMSource(createFault(soapVer, fault,
179: message)));
180: }
181:
182: public static void sendFault(@NotNull
183: WebServiceContext wsContext, @NotNull
184: final SOAPVersion soapVer, @NotNull
185: final TxFault fault, @NotNull
186: final String message) {
187:
188: String msgID = WsaHelper.getMsgID(wsContext);
189: EndpointReference replyTo = WsaHelper.getReplyTo(wsContext);
190: WSEndpointReference faultTo = WsaHelper.getFaultTo(wsContext);
191:
192: sendFault(faultTo, replyTo, soapVer, fault, message, msgID);
193: }
194:
195: }
|