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.grizzly;
038:
039: import com.sun.enterprise.web.portunif.ProtocolFinder;
040: import com.sun.enterprise.web.portunif.util.ProtocolInfo;
041: import com.sun.istack.NotNull;
042: import com.sun.xml.ws.transport.tcp.util.TCPConstants;
043: import java.io.IOException;
044: import java.nio.BufferUnderflowException;
045: import java.nio.ByteBuffer;
046: import java.nio.channels.SelectionKey;
047: import java.nio.channels.SocketChannel;
048:
049: /**
050: * A <code>ProtocolFinder</code> implementation that parse the available
051: * SocketChannel bytes looking for the PROTOCOL_ID bytes. An SOAP/TCP request will
052: * always start with: vnd.sun.ws.tcp
053: *
054: * This object shoudn't be called by several threads simultaneously.
055: *
056: * @author Jeanfrancois Arcand
057: * @author Alexey Stashok
058: */
059: public final class WSTCPProtocolFinder implements ProtocolFinder {
060:
061: public WSTCPProtocolFinder() {
062: }
063:
064: /**
065: * Try to find the protocol from the <code>SocketChannel</code> bytes.
066: *
067: * @param selectionKey The key from which the SocketChannel can be retrieved.
068: * @return ProtocolInfo The ProtocolInfo that contains the information about the
069: * current protocol.
070: */
071: public void find(@NotNull
072: final ProtocolInfo protocolInfo) throws IOException {
073: final SelectionKey key = protocolInfo.key;
074: final SocketChannel socketChannel = (SocketChannel) key
075: .channel();
076: final ByteBuffer byteBuffer = protocolInfo.byteBuffer;
077:
078: int loop = 0;
079: int count = -1;
080:
081: if (protocolInfo.bytesRead == 0) {
082: try {
083: while (socketChannel.isOpen()
084: && ((count = socketChannel.read(byteBuffer)) > -1)) {
085:
086: if (count == 0) {
087: loop++;
088: if (loop > 2) {
089: break;
090: }
091: continue;
092: }
093: }
094: } catch (IOException ex) {
095: ;
096: } finally {
097: if (count == -1) {
098: return;
099: }
100: protocolInfo.bytesRead = count;
101: }
102: }
103:
104: final int curPosition = byteBuffer.position();
105: final int curLimit = byteBuffer.limit();
106:
107: // Rule a - If read length < PROTOCOL_ID.length, return to the Selector.
108: if (curPosition < TCPConstants.PROTOCOL_SCHEMA.length()) {
109: return;
110: }
111:
112: byteBuffer.position(0);
113: byteBuffer.limit(curPosition);
114:
115: // Rule b - check protocol id
116: try {
117: final byte[] protocolBytes = new byte[TCPConstants.PROTOCOL_SCHEMA
118: .length()];
119: byteBuffer.get(protocolBytes);
120: final String incomeProtocolId = new String(protocolBytes);
121: if (TCPConstants.PROTOCOL_SCHEMA.equals(incomeProtocolId)) {
122: protocolInfo.protocol = TCPConstants.PROTOCOL_SCHEMA;
123: protocolInfo.byteBuffer = byteBuffer;
124: protocolInfo.socketChannel = (SocketChannel) key
125: .channel();
126: protocolInfo.isSecure = false;
127: }
128: } catch (BufferUnderflowException bue) {
129: } finally {
130: byteBuffer.limit(curLimit);
131: byteBuffer.position(curPosition);
132: }
133: }
134:
135: }
|