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.transport.tcp.client;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.ws.api.WSBinding;
042: import com.sun.xml.ws.api.WSService;
043: import com.sun.xml.ws.api.pipe.ClientPipeAssemblerContext;
044: import com.sun.xml.ws.api.pipe.Codec;
045: import com.sun.xml.ws.client.ClientTransportException;
046: import com.sun.xml.ws.transport.tcp.util.ChannelContext;
047: import com.sun.xml.ws.transport.tcp.util.VersionMismatchException;
048: import com.sun.xml.ws.transport.tcp.util.WSTCPException;
049: import com.sun.xml.ws.transport.tcp.util.WSTCPURI;
050: import com.sun.xml.ws.transport.tcp.servicechannel.ServiceChannelException;
051: import java.io.IOException;
052: import java.net.URI;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055: import com.sun.xml.ws.api.message.Packet;
056: import com.sun.xml.ws.api.pipe.ContentType;
057: import com.sun.xml.ws.api.pipe.Pipe;
058: import com.sun.xml.ws.api.pipe.PipeCloner;
059: import com.sun.xml.ws.transport.tcp.util.TCPConstants;
060: import com.sun.xml.ws.transport.tcp.resources.MessagesMessages;
061: import java.io.InputStream;
062: import javax.xml.ws.BindingProvider;
063: import javax.xml.ws.WebServiceException;
064:
065: /**
066: * @author Alexey Stashok
067: */
068: public class TCPTransportPipe implements Pipe {
069: private static final Logger logger = Logger
070: .getLogger(com.sun.xml.ws.transport.tcp.util.TCPConstants.LoggingDomain
071: + ".client");
072:
073: protected TCPClientTransport clientTransport = new TCPClientTransport();
074:
075: final protected Codec defaultCodec;
076: final protected WSBinding wsBinding;
077: final protected WSService wsService;
078:
079: public TCPTransportPipe(final ClientPipeAssemblerContext context) {
080: this (context.getService(), context.getBinding(), context
081: .getCodec());
082: }
083:
084: protected TCPTransportPipe(final WSService wsService,
085: final WSBinding wsBinding, final Codec defaultCodec) {
086: this .wsService = wsService;
087: this .wsBinding = wsBinding;
088: this .defaultCodec = defaultCodec;
089: }
090:
091: protected TCPTransportPipe(final TCPTransportPipe that,
092: final PipeCloner cloner) {
093: this (that.wsService, that.wsBinding, that.defaultCodec.copy());
094: cloner.add(that, this );
095: }
096:
097: public void preDestroy() {
098: if (clientTransport != null) {
099: WSConnectionManager.getInstance().closeChannel(
100: clientTransport.getConnectionContext());
101: }
102: }
103:
104: public Pipe copy(final PipeCloner cloner) {
105: return new TCPTransportPipe(this , cloner);
106: }
107:
108: public Packet process(final Packet packet) {
109: if (logger.isLoggable(Level.FINE)) {
110: logger
111: .log(
112: Level.FINE,
113: MessagesMessages
114: .WSTCP_1010_TCP_TP_PROCESS_ENTER(packet.endpointAddress));
115: }
116: ChannelContext channelContext = null;
117: WebServiceException failure = null;
118: final WSConnectionManager wsConnectionManager = WSConnectionManager
119: .getInstance();
120:
121: int retryNum = 0;
122: do {
123: try {
124: setupClientTransport(wsConnectionManager,
125: packet.endpointAddress.getURI());
126: channelContext = clientTransport.getConnectionContext();
127:
128: wsConnectionManager.lockConnection(channelContext
129: .getConnectionSession());
130:
131: // Taking Codec from ChannelContext
132: final Codec codec = channelContext.getCodec();
133: final ContentType ct = codec
134: .getStaticContentType(packet);
135: clientTransport.setContentType(ct.getContentType());
136: /* write transport SOAPAction header if required
137: * in HTTP this param is sent as HTTP header, in SOAP/TCP
138: * it is part of content-type (similar to SOAP 1.2) */
139: writeTransportSOAPActionHeaderIfRequired(
140: channelContext, ct, packet);
141:
142: if (logger.isLoggable(Level.FINE)) {
143: logger.log(Level.FINE, MessagesMessages
144: .WSTCP_1013_TCP_TP_PROCESS_ENCODE(ct
145: .getContentType()));
146: }
147: codec
148: .encode(packet, clientTransport
149: .openOutputStream());
150:
151: if (logger.isLoggable(Level.FINE)) {
152: logger.log(Level.FINE, MessagesMessages
153: .WSTCP_1014_TCP_TP_PROCESS_SEND());
154: }
155: clientTransport.send();
156:
157: if (logger.isLoggable(Level.FINE)) {
158: logger
159: .log(
160: Level.FINE,
161: MessagesMessages
162: .WSTCP_1015_TCP_TP_PROCESS_OPEN_PREPARE_READING());
163: }
164: final InputStream replyInputStream = clientTransport
165: .openInputStream();
166:
167: if (logger.isLoggable(Level.FINE)) {
168: logger
169: .log(
170: Level.FINE,
171: MessagesMessages
172: .WSTCP_1016_TCP_TP_PROCESS_OPEN_PROCESS_READING(
173: clientTransport
174: .getStatus(),
175: clientTransport
176: .getContentType()));
177: }
178: if (clientTransport.getStatus() != TCPConstants.ERROR) {
179: final Packet reply = packet
180: .createClientResponse(null);
181: if (clientTransport.getStatus() != TCPConstants.ONE_WAY
182: && !Boolean.FALSE
183: .equals(packet.expectReply)) {
184: final String contentTypeStr = clientTransport
185: .getContentType();
186: codec.decode(replyInputStream, contentTypeStr,
187: reply);
188: }
189: return reply;
190: } else {
191: logger
192: .log(
193: Level.SEVERE,
194: MessagesMessages
195: .WSTCP_0016_ERROR_WS_EXECUTION_ON_SERVER(clientTransport
196: .getError()));
197: throw new WSTCPException(clientTransport.getError());
198: }
199: } catch (ClientTransportException e) {
200: abortSession(channelContext);
201: failure = e;
202: } catch (WSTCPException e) {
203: if (e.getError().isCritical()) {
204: abortSession(channelContext);
205: } else {
206: releaseSession(channelContext);
207: }
208: failure = new WebServiceException(MessagesMessages
209: .WSTCP_0016_ERROR_WS_EXECUTION_ON_SERVER(e
210: .getError()), e);
211: } catch (IOException e) {
212: abortSession(channelContext);
213: failure = new WebServiceException(MessagesMessages
214: .WSTCP_0017_ERROR_WS_EXECUTION_ON_CLIENT(), e);
215: } catch (ServiceChannelException e) {
216: releaseSession(channelContext);
217: retryNum = TCPConstants.CLIENT_MAX_FAIL_TRIES + 1;
218: failure = new WebServiceException(MessagesMessages
219: .WSTCP_0016_ERROR_WS_EXECUTION_ON_SERVER(e
220: .getFaultInfo().getErrorCode()
221: + ":" + e.getMessage()), e);
222: } catch (Exception e) {
223: abortSession(channelContext);
224: retryNum = TCPConstants.CLIENT_MAX_FAIL_TRIES + 1;
225: failure = new WebServiceException(MessagesMessages
226: .WSTCP_0017_ERROR_WS_EXECUTION_ON_CLIENT(), e);
227: }
228:
229: if (logger.isLoggable(Level.FINE) && canRetry(retryNum + 1)) {
230: logger.log(Level.FINE, MessagesMessages
231: .WSTCP_0012_SEND_RETRY(retryNum), failure);
232: }
233: } while (canRetry(++retryNum));
234:
235: assert failure != null;
236: logger.log(Level.SEVERE, MessagesMessages
237: .WSTCP_0001_MESSAGE_PROCESS_FAILED(), failure);
238: throw failure;
239: }
240:
241: protected void writeTransportSOAPActionHeaderIfRequired(
242: ChannelContext channelContext, ContentType ct, Packet packet) {
243: String soapActionTransportHeader = getSOAPAction(ct
244: .getSOAPActionHeader(), packet);
245: if (soapActionTransportHeader != null) {
246: try {
247: int transportSoapActionParamId = channelContext
248: .encodeParam(TCPConstants.TRANSPORT_SOAP_ACTION_PROPERTY);
249: channelContext.getConnection().setContentProperty(
250: transportSoapActionParamId,
251: soapActionTransportHeader);
252: } catch (WSTCPException ex) {
253: logger.log(Level.WARNING, MessagesMessages
254: .WSTCP_0032_UNEXPECTED_TRANSPORT_SOAP_ACTION(),
255: ex);
256: }
257: }
258: }
259:
260: protected void abortSession(final ChannelContext channelContext) {
261: if (channelContext != null) {
262: WSConnectionManager.getInstance().abortConnection(
263: channelContext.getConnectionSession());
264: }
265: }
266:
267: protected void releaseSession(final ChannelContext channelContext) {
268: if (channelContext != null) {
269: WSConnectionManager.getInstance().freeConnection(
270: channelContext.getConnectionSession());
271: }
272: }
273:
274: private @NotNull
275: void setupClientTransport(@NotNull
276: final WSConnectionManager wsConnectionManager, @NotNull
277: final URI uri) throws InterruptedException, IOException,
278: ServiceChannelException, VersionMismatchException {
279:
280: final WSTCPURI tcpURI = WSTCPURI.parse(uri);
281: if (tcpURI == null)
282: throw new WebServiceException(MessagesMessages
283: .WSTCP_0005_INVALID_EP_URL(uri.toString()));
284: final ChannelContext channelContext = wsConnectionManager
285: .openChannel(tcpURI, wsService, wsBinding, defaultCodec);
286: clientTransport.setup(channelContext);
287: }
288:
289: /**
290: * get SOAPAction header if the soapAction parameter is non-null or BindingProvider properties set.
291: * BindingProvider properties take precedence.
292: */
293: private @Nullable
294: String getSOAPAction(String soapAction, Packet packet) {
295: Boolean useAction = (Boolean) packet.invocationProperties
296: .get(BindingProvider.SOAPACTION_USE_PROPERTY);
297: String sAction = null;
298: boolean use = (useAction != null) ? useAction.booleanValue()
299: : false;
300:
301: if (use) {
302: //TODO check if it needs to be quoted
303: sAction = packet.soapAction;
304: }
305: //request Property soapAction overrides wsdl
306: if (sAction != null) {
307: return sAction;
308: } else {
309: return soapAction;
310: }
311: }
312:
313: private static boolean canRetry(int retryNum) {
314: return retryNum <= TCPConstants.CLIENT_MAX_FAIL_TRIES;
315: }
316: }
|