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.DistributedPropertySet;
042: import com.sun.xml.ws.transport.tcp.io.Connection;
043: import com.sun.xml.ws.transport.tcp.io.DataInOutUtils;
044: import com.sun.xml.ws.transport.tcp.util.ChannelContext;
045: import com.sun.xml.ws.transport.tcp.util.FrameType;
046: import com.sun.xml.ws.transport.tcp.util.TCPConstants;
047: import com.sun.xml.ws.transport.tcp.util.WSTCPError;
048: import com.sun.xml.ws.transport.tcp.util.WSTCPException;
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.io.OutputStream;
052:
053: /**
054: * @author Alexey Stashok
055: */
056: public class TCPClientTransport extends DistributedPropertySet {
057: private ChannelContext channelContext;
058: private Connection connection;
059:
060: private InputStream inputStream;
061: private OutputStream outputStream;
062:
063: // Response status
064: private int status;
065: // Request/response content type
066: private String contentType;
067:
068: private WSTCPError error;
069:
070: public TCPClientTransport() {
071: }
072:
073: public TCPClientTransport(final @NotNull
074: ChannelContext channelContext) {
075: setup(channelContext);
076: }
077:
078: public void setup(final @Nullable
079: ChannelContext channelContext) {
080: this .channelContext = channelContext;
081: if (channelContext != null) {
082: this .connection = channelContext.getConnection();
083: }
084: }
085:
086: public int getStatus() {
087: return status;
088: }
089:
090: public void setStatus(final int status) {
091: this .status = status;
092: }
093:
094: /*
095: * Getting output stream.
096: * Making some stream preparation before
097: */
098: public @NotNull
099: OutputStream openOutputStream() throws WSTCPException {
100: connection.setChannelId(channelContext.getChannelId());
101: connection.setMessageId(FrameType.MESSAGE);
102: channelContext.setContentType(contentType);
103:
104: outputStream = connection.openOutputStream();
105: return outputStream;
106: }
107:
108: /*
109: * Getting input stream.
110: * Making some stream preparation before
111: */
112: public @NotNull
113: InputStream openInputStream() throws IOException, WSTCPException {
114: connection.prepareForReading();
115: inputStream = connection.openInputStream();
116: final int messageId = connection.getMessageId();
117: status = convertToReplyStatus(messageId);
118: if (FrameType.isFrameContainsParams(messageId)) {
119: contentType = channelContext.getContentType();
120: }
121:
122: if (status == TCPConstants.ERROR) {
123: error = parseErrorMessagePayload();
124: }
125:
126: return inputStream;
127: }
128:
129: public void send() throws IOException {
130: connection.flush();
131: }
132:
133: public void close() {
134: error = null;
135: // Perform some cleanings
136: }
137:
138: public void setContentType(final @NotNull
139: String contentType) {
140: this .contentType = contentType;
141: }
142:
143: public @Nullable
144: String getContentType() {
145: return contentType;
146: }
147:
148: public @Nullable
149: WSTCPError getError() {
150: return error;
151: }
152:
153: private @Nullable
154: WSTCPError parseErrorMessagePayload() throws IOException {
155: final int[] params = new int[3];
156: DataInOutUtils.readInts4(inputStream, params, 3);
157: final int errorCode = params[0];
158: final int errorSubCode = params[1];
159: final int errorDescriptionBufferLength = params[2];
160:
161: final byte[] errorDescriptionBuffer = new byte[errorDescriptionBufferLength];
162: DataInOutUtils.readFully(inputStream, errorDescriptionBuffer);
163:
164: String errorDescription = new String(errorDescriptionBuffer,
165: TCPConstants.UTF8);
166: return WSTCPError.createError(errorCode, errorSubCode,
167: errorDescription);
168: }
169:
170: private int convertToReplyStatus(final int messageId) {
171: if (messageId == FrameType.NULL) {
172: return TCPConstants.ONE_WAY;
173: } else if (messageId == FrameType.ERROR) {
174: return TCPConstants.ERROR;
175: }
176:
177: return TCPConstants.OK;
178: }
179:
180: @Property(TCPConstants.CHANNEL_CONTEXT)
181: public ChannelContext getConnectionContext() {
182: return channelContext;
183: }
184:
185: private static final PropertyMap model;
186: static {
187: model = parse(TCPClientTransport.class);
188: }
189:
190: public DistributedPropertySet.PropertyMap getPropertyMap() {
191: return model;
192: }
193: }
|