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.WSBinding;
043: import com.sun.xml.ws.api.addressing.AddressingVersion;
044: import com.sun.xml.ws.api.addressing.WSEndpointReference;
045: import com.sun.xml.ws.api.message.Header;
046: import com.sun.xml.ws.api.model.wsdl.WSDLBoundOperation;
047: import com.sun.xml.ws.api.model.wsdl.WSDLPort;
048: import static com.sun.xml.ws.tx.common.Constants.COORDINATION_CONTEXT;
049: import static com.sun.xml.ws.tx.common.Constants.WSCOOR_SOAP_NSURI;
050: import com.sun.xml.ws.tx.coordinator.CoordinationContextBase;
051: import com.sun.xml.ws.tx.coordinator.CoordinationContextInterface;
052:
053: import javax.xml.bind.JAXBException;
054: import javax.xml.bind.Unmarshaller;
055: import java.util.logging.Level;
056:
057: /**
058: * WS-TX view of a Message.
059: *
060: * @author jf39279
061: */
062: public class Message {
063:
064: static final private TxLogger logger = TxLogger
065: .getLogger(Message.class);
066:
067: /**
068: * The JAX-WS Message wrapped by this instance.
069: */
070: final private com.sun.xml.ws.api.message.Message coreMessage;
071: final private com.sun.xml.ws.api.message.HeaderList hdrList;
072: private SOAPVersion SOAP_VERSION;
073: private AddressingVersion ADDRESSING_VERSION;
074:
075: /* Caches of representations of CoordinationContext */
076: private Header ccHdr = null;
077: static final private int NOT_FOUND = -1;
078: private int ccHdrIndex = NOT_FOUND;
079:
080: private CoordinationContextInterface cc = null;
081:
082: /**
083: * Public ctor takes wrapped JAX-WS message as its argument.
084: *
085: * @param message core message
086: */
087: public Message(@NotNull
088: final com.sun.xml.ws.api.message.Message message,
089: final WSBinding wsBinding) {
090: this .coreMessage = message;
091: this .hdrList = (message == null ? null : message.getHeaders());
092: SOAP_VERSION = (wsBinding == null ? null : wsBinding
093: .getSOAPVersion());
094: ADDRESSING_VERSION = (wsBinding == null ? null : wsBinding
095: .getAddressingVersion());
096: }
097:
098: /**
099: * Public ctor takes wrapped JAX-WS message as its argument.
100: *
101: * @param message core message
102: */
103: public Message(@NotNull
104: final com.sun.xml.ws.api.message.Message message) {
105: this (message, null);
106: }
107:
108: /**
109: * Get the CoordinationContext Header Element from the underlying
110: * JAX-WS message's HeaderList. Only understand the header iff CoordinationContext is
111: * for coordinationType.
112: *
113: * @return the coordination context in this message
114: */
115: @NotNull
116: public com.sun.xml.ws.api.message.Header getCoordCtxHeader() {
117: if (ccHdr == null) {
118: if (hdrList != null) {
119: ccHdr = hdrList.get(WSCOOR_SOAP_NSURI,
120: COORDINATION_CONTEXT, false);
121:
122: /*
123: * Note: include when supporting OASIS WS-TX
124: * don't check for it since it will be a must understand soap header,
125: * must understand header processing should flag it is not processed.
126: if (ccHdr == null) {
127: hdrList.get(Constants.WSAT_OASIS_NSURI, COORDINATION_CONTEXT, true);
128: ....
129: }
130: */
131:
132: }
133: }
134: return ccHdr;
135: }
136:
137: /**
138: * Get the CoordinationContext Header Element from the underlying
139: * JAX-WS message's HeaderList. Only understand the header iff CoordinationContext is
140: * for coordinationType.
141: *
142: * @param namespace namespace
143: * @param localName local name
144: * @return index of coordination context in header list or null if not found
145: */
146: @Nullable
147: public com.sun.xml.ws.api.message.Header getCoordCtxHeader(@NotNull
148: final String namespace, @NotNull
149: final String localName) {
150: if (ccHdr == null && coreMessage != null) {
151: ccHdrIndex = NOT_FOUND;
152: final int len = hdrList.size();
153: for (int i = 0; i < len; i++) {
154: final Header h = hdrList.get(i);
155: if (h.getLocalPart().equals(localName)
156: && h.getNamespaceURI().equals(namespace)) {
157: ccHdrIndex = i;
158: ccHdr = h;
159: break;
160: }
161: }
162: }
163: return ccHdr;
164: }
165:
166: /**
167: * @param unmarshaller jaxb unmarshaller
168: * @return the coordination context
169: */
170: @Nullable
171: public CoordinationContextInterface getCoordinationContext(@NotNull
172: final Unmarshaller unmarshaller) throws JAXBException {
173: if (cc == null) {
174: final Header ccHdr = getCoordCtxHeader(WSCOOR_SOAP_NSURI,
175: COORDINATION_CONTEXT);
176: if (ccHdr != null) {
177: try {
178: cc = CoordinationContextBase
179: .createCoordinationContext(ccHdr
180: .readAsJAXB(unmarshaller));
181: } catch (JAXBException e) {
182: logger.warning("getCoordinationContext",
183: LocalizationMessages
184: .CANNOT_UNMARSHAL_CONTEXT_2000(e
185: .getLocalizedMessage()));
186: throw e;
187: }
188: }
189: }
190: return cc;
191: }
192:
193: /**
194: * Denote that CoordinationContext SOAP Header was processed and considered understood.
195: */
196: public void setCoordCtxUnderstood() {
197: if (ccHdr != null && ccHdrIndex != NOT_FOUND) {
198: coreMessage.getHeaders().understood(ccHdrIndex);
199: }
200: }
201:
202: /**
203: * Get the wsdl bound operation for the specified port
204: *
205: * @param port port
206: * @return the wsdl operation or null if not found
207: */
208: @Nullable
209: public WSDLBoundOperation getOperation(@NotNull
210: final WSDLPort port) {
211: return coreMessage.getOperation(port);
212: }
213:
214: /**
215: * @return the ws-addressing MessageId for this message
216: */
217: public String getMessageID() {
218: String result = null;
219: if (hdrList != null && ADDRESSING_VERSION != null
220: && SOAP_VERSION != null) {
221: result = hdrList.getMessageID(ADDRESSING_VERSION,
222: SOAP_VERSION);
223: }
224: return result;
225: }
226:
227: /**
228: * @return the ws-addressing To for this message
229: */
230: public String getTo() {
231: String result = null;
232: if (hdrList != null && ADDRESSING_VERSION != null
233: && SOAP_VERSION != null) {
234: result = hdrList.getTo(ADDRESSING_VERSION, SOAP_VERSION);
235: }
236: return result;
237: }
238:
239: /**
240: * @return the ws-addressing Action for this message
241: */
242: public String getAction() {
243: String result = null;
244: if (hdrList != null && ADDRESSING_VERSION != null
245: && SOAP_VERSION != null) {
246: result = hdrList
247: .getAction(ADDRESSING_VERSION, SOAP_VERSION);
248: }
249: return result;
250: }
251:
252: /**
253: * @return the ws-addressing FaultTo for this message
254: */
255: public WSEndpointReference getFaultTo() {
256: WSEndpointReference result = null;
257: if (hdrList != null && ADDRESSING_VERSION != null
258: && SOAP_VERSION != null) {
259: result = hdrList.getFaultTo(ADDRESSING_VERSION,
260: SOAP_VERSION);
261: }
262: return result;
263: }
264:
265: /**
266: * @return the ws-addressing ReplyTo for this message
267: */
268: public WSEndpointReference getReplyTo() {
269: WSEndpointReference result = null;
270: if (hdrList != null && ADDRESSING_VERSION != null
271: && SOAP_VERSION != null) {
272: result = hdrList.getReplyTo(ADDRESSING_VERSION,
273: SOAP_VERSION);
274: }
275: return result;
276: }
277: }
|