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.servicechannel;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.xml.ws.api.SOAPVersion;
041: import com.sun.xml.ws.api.pipe.Codec;
042: import com.sun.xml.ws.transport.tcp.server.ServerConnectionSession;
043: import com.sun.xml.ws.transport.tcp.util.BindingUtils;
044: import com.sun.xml.ws.transport.tcp.util.ChannelContext;
045: import com.sun.xml.ws.transport.tcp.util.ChannelSettings;
046: import com.sun.xml.ws.transport.tcp.util.ConnectionSession;
047: import com.sun.xml.ws.transport.tcp.util.TCPConstants;
048: import com.sun.xml.ws.transport.tcp.util.WSTCPURI;
049: import com.sun.xml.ws.transport.tcp.resources.MessagesMessages;
050: import com.sun.xml.ws.transport.tcp.server.TCPAdapter;
051: import com.sun.xml.ws.transport.tcp.server.WSTCPAdapterRegistry;
052: import java.util.List;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055: import javax.annotation.Resource;
056: import javax.jws.WebParam;
057: import javax.jws.WebResult;
058: import javax.jws.WebService;
059: import javax.xml.ws.Holder;
060: import javax.xml.ws.WebServiceContext;
061: import javax.xml.ws.handler.MessageContext;
062:
063: /**
064: * @author Alexey Stashok
065: */
066:
067: @SuppressWarnings({"unchecked"})
068: @WebService
069: public class ServiceChannelWSImpl {
070: private static final Logger logger = Logger
071: .getLogger(com.sun.xml.ws.transport.tcp.util.TCPConstants.LoggingDomain
072: + ".server");
073:
074: @Resource
075: private WebServiceContext wsContext;
076:
077: public void initiateSession() throws ServiceChannelException {
078: final ChannelContext serviceChannelContext = getChannelContext();
079: final ConnectionSession connectionSession = serviceChannelContext
080: .getConnectionSession();
081: logger.log(Level.FINE, MessagesMessages
082: .WSTCP_1140_SOAPTCP_SESSION_OPEN(connectionSession
083: .hashCode()));
084: }
085:
086: @WebResult(name="channelId")
087: public int openChannel(
088: @WebParam(name="targetWSURI",mode=WebParam.Mode.IN)
089: String targetWSURI,
090: @WebParam(name="negotiatedMimeTypes",mode=WebParam.Mode.INOUT)
091: Holder<List<String>> negotiatedMimeTypes,
092: @WebParam(name="negotiatedParams",mode=WebParam.Mode.INOUT)
093: Holder<List<String>> negotiatedParams)
094: throws ServiceChannelException {
095: final ChannelContext serviceChannelContext = getChannelContext();
096: final ServerConnectionSession connectionSession = (ServerConnectionSession) serviceChannelContext
097: .getConnectionSession();
098:
099: final WSTCPAdapterRegistry adapterRegistry = getAdapterRegistry();
100:
101: final WSTCPURI tcpURI = WSTCPURI.parse(targetWSURI);
102: final TCPAdapter adapter = adapterRegistry.getTarget(tcpURI);
103: if (adapter == null)
104: throw new ServiceChannelException(
105: ServiceChannelErrorCode.UNKNOWN_ENDPOINT_ADDRESS,
106: MessagesMessages
107: .WSTCP_0034_WS_ENDPOINT_NOT_FOUND(targetWSURI));
108:
109: final BindingUtils.NegotiatedBindingContent serviceSupportedContent = BindingUtils
110: .getNegotiatedContentTypesAndParams(adapter
111: .getEndpoint().getBinding());
112:
113: negotiatedMimeTypes.value
114: .retainAll(serviceSupportedContent.negotiatedMimeTypes);
115: if (negotiatedMimeTypes.value.size() == 0) {
116: throw new ServiceChannelException(
117: ServiceChannelErrorCode.CONTENT_NEGOTIATION_FAILED,
118: MessagesMessages
119: .WSTCP_0033_CONTENT_NEGOTIATION_FAILED(
120: targetWSURI,
121: serviceSupportedContent.negotiatedMimeTypes));
122: }
123:
124: negotiatedParams.value
125: .retainAll(serviceSupportedContent.negotiatedParams);
126:
127: int channelId = connectionSession.getNextAvailChannelId();
128: ChannelSettings channelSettings = new ChannelSettings(
129: negotiatedMimeTypes.value, negotiatedParams.value,
130: channelId, adapter.getEndpoint().getServiceName(),
131: tcpURI);
132: final ChannelContext openedChannelContext = new ChannelContext(
133: connectionSession, channelSettings);
134: final SOAPVersion soapVersion = adapter.getEndpoint()
135: .getBinding().getSOAPVersion();
136: final Codec defaultCodec = adapter.getEndpoint().createCodec();
137: ChannelContext.configureCodec(openedChannelContext,
138: soapVersion, defaultCodec);
139:
140: connectionSession.registerChannel(openedChannelContext);
141:
142: if (logger.isLoggable(Level.FINE)) {
143: logger.log(Level.FINE, MessagesMessages
144: .WSTCP_1141_SOAPTCP_CHANNEL_OPEN(connectionSession
145: .hashCode(), openedChannelContext
146: .getChannelId(), targetWSURI));
147: }
148: return channelId;
149: }
150:
151: public void closeChannel(
152: @WebParam(name="channelId",mode=WebParam.Mode.IN)
153: int channelId) throws ServiceChannelException {
154: final ChannelContext serviceChannelContext = getChannelContext();
155: final ServerConnectionSession connectionSession = (ServerConnectionSession) serviceChannelContext
156: .getConnectionSession();
157:
158: if (connectionSession
159: .findWSServiceContextByChannelId(channelId) != null) {
160: if (logger.isLoggable(Level.FINE)) {
161: logger.log(Level.FINE,
162: MessagesMessages
163: .WSTCP_1142_SOAPTCP_CHANNEL_CLOSE(
164: connectionSession.hashCode(),
165: channelId));
166: }
167: connectionSession.deregisterChannel(channelId);
168: } else {
169: if (logger.isLoggable(Level.WARNING)) {
170: logger.log(Level.WARNING, MessagesMessages
171: .WSTCP_0035_UNKNOWN_CHANNEL_UD("Session: "
172: + connectionSession.hashCode()
173: + " Channel-id: " + channelId));
174: }
175: throw new ServiceChannelException(
176: ServiceChannelErrorCode.UNKNOWN_CHANNEL_ID,
177: MessagesMessages
178: .WSTCP_0035_UNKNOWN_CHANNEL_UD(channelId));
179: }
180: }
181:
182: private @NotNull
183: ChannelContext getChannelContext() {
184: final MessageContext messageContext = wsContext
185: .getMessageContext();
186: return (ChannelContext) messageContext
187: .get(TCPConstants.CHANNEL_CONTEXT);
188: }
189:
190: private @NotNull
191: WSTCPAdapterRegistry getAdapterRegistry() {
192: final MessageContext messageContext = wsContext
193: .getMessageContext();
194: return (WSTCPAdapterRegistry) messageContext
195: .get(TCPConstants.ADAPTER_REGISTRY);
196: }
197: }
|