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.server;
038:
039: import com.sun.xml.ws.api.server.WSEndpoint;
040: import com.sun.xml.ws.transport.tcp.io.Connection;
041: import com.sun.xml.ws.transport.tcp.io.DataInOutUtils;
042: import com.sun.xml.ws.transport.tcp.util.ChannelContext;
043: import com.sun.xml.ws.transport.tcp.util.FrameType;
044: import com.sun.xml.ws.transport.tcp.util.TCPConstants;
045: import com.sun.xml.ws.api.message.Packet;
046: import com.sun.xml.ws.api.server.WebServiceContextDelegate;
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: import java.security.Principal;
053: import com.sun.istack.NotNull;
054:
055: /**
056: * @author Alexey Stashok
057: */
058: public class TCPConnectionImpl implements WebServiceContextDelegate {
059: private final ChannelContext channelContext;
060: private final Connection connection;
061:
062: private String contentType;
063: private int replyStatus;
064:
065: private InputStream inputStream;
066: private OutputStream outputStream;
067:
068: private boolean isHeaderSerialized;
069:
070: public TCPConnectionImpl(final ChannelContext channelContext) {
071: this .channelContext = channelContext;
072: this .connection = channelContext.getConnection();
073: }
074:
075: public InputStream openInput() throws IOException, WSTCPException {
076: inputStream = connection.openInputStream();
077: contentType = channelContext.getContentType();
078: return inputStream;
079: }
080:
081: public OutputStream openOutput() throws WSTCPException {
082: try {
083: setMessageHeaders();
084: } catch (IOException ex) {
085: }
086:
087: outputStream = connection.openOutputStream();
088: return outputStream;
089: }
090:
091: public int getStatus() {
092: return replyStatus;
093: }
094:
095: public void setStatus(final int statusCode) {
096: replyStatus = statusCode;
097: }
098:
099: public String getContentType() {
100: return contentType;
101: }
102:
103: public void setContentType(final String contentType) {
104: this .contentType = contentType;
105: }
106:
107: public void flush() throws IOException, WSTCPException {
108: if (outputStream == null) {
109: setMessageHeaders();
110: outputStream = connection.openOutputStream();
111: }
112:
113: connection.flush();
114: }
115:
116: public void close() {
117: }
118:
119: // Not supported
120: public Principal getUserPrincipal(final Packet request) {
121: return null;
122: }
123:
124: // Not supported
125: public boolean isUserInRole(final Packet request, final String role) {
126: return false;
127: }
128:
129: public @NotNull
130: String getEPRAddress(@NotNull
131: final Packet request, @NotNull
132: final WSEndpoint endpoint) {
133: return channelContext.getTargetWSURI().toString();
134: }
135:
136: public String getWSDLAddress(@NotNull
137: final Packet request, @NotNull
138: final WSEndpoint endpoint) {
139: return null;
140: }
141:
142: public void sendErrorMessage(WSTCPError message)
143: throws IOException, WSTCPException {
144: setStatus(TCPConstants.ERROR);
145: OutputStream output = openOutput();
146: String description = message.getDescription();
147: DataInOutUtils.writeInts4(output, message.getCode(), message
148: .getSubCode(), description.length());
149: output.write(description.getBytes(TCPConstants.UTF8));
150: flush();
151: }
152:
153: private void setMessageHeaders() throws IOException, WSTCPException {
154: if (!isHeaderSerialized) {
155: isHeaderSerialized = true;
156:
157: final int messageId = getMessageId();
158: connection.setMessageId(messageId);
159: if (FrameType.isFrameContainsParams(messageId)) {
160: channelContext.setContentType(contentType);
161: }
162: }
163: }
164:
165: private int getMessageId() {
166: if (getStatus() == TCPConstants.ONE_WAY) {
167: return FrameType.NULL;
168: } else if (getStatus() != TCPConstants.OK) {
169: return FrameType.ERROR;
170: }
171:
172: return FrameType.MESSAGE;
173: }
174:
175: public ChannelContext getChannelContext() {
176: return channelContext;
177: }
178: }
|