J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package sun.rmi.transport.proxy; |
| 26 | |
| 27 | import java.io.BufferedInputStream; |
| 28 | import java.io.IOException; |
| 29 | import java.net.ServerSocket; |
| 30 | import java.net.Socket; |
| 31 | import sun.rmi.runtime.Log; |
| 32 | |
| 33 | /** |
| 34 | * The HttpAwareServerSocket class extends the java.net.ServerSocket |
| 35 | * class. It behaves like a ServerSocket, except that if |
| 36 | * the first four bytes of an accepted socket are the letters "POST", |
| 37 | * then it returns an HttpReceiveSocket instead of a java.net.Socket. |
| 38 | * This means that the accept method blocks until four bytes have been |
| 39 | * read from the new socket's input stream. |
| 40 | */ |
| 41 | class HttpAwareServerSocket extends ServerSocket { |
| 42 | |
| 43 | /** |
| 44 | * Create a server socket on a specified port. |
| 45 | * @param port the port |
| 46 | * @exception IOException IO error when opening the socket. |
| 47 | */ |
| 48 | public HttpAwareServerSocket(int port) throws IOException |
| 49 | { |
| 50 | super(port); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create a server socket, bind it to the specified local port |
| 55 | * and listen to it. You can connect to an annonymous port by |
| 56 | * specifying the port number to be 0. <i>backlog</i> specifies |
| 57 | * how many connection requests the system will queue up while waiting |
| 58 | * for the ServerSocket to execute accept(). |
| 59 | * @param port the specified port |
| 60 | * @param backlog the number of queued connect requests pending accept |
| 61 | */ |
| 62 | public HttpAwareServerSocket(int port, int backlog) throws IOException |
| 63 | { |
| 64 | super(port, backlog); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Accept a connection. This method will block until the connection |
| 69 | * is made and four bytes can be read from the input stream. |
| 70 | * If the first four bytes are "POST", then an HttpReceiveSocket is |
| 71 | * returned, which will handle the HTTP protocol wrapping. |
| 72 | * Otherwise, a WrappedSocket is returned. The input stream will be |
| 73 | * reset to the beginning of the transmission. |
| 74 | * In either case, a BufferedInputStream will already be on top of |
| 75 | * the underlying socket's input stream. |
| 76 | * @exception IOException IO error when waiting for the connection. |
| 77 | */ |
| 78 | public Socket accept() throws IOException |
| 79 | { |
| 80 | Socket socket = super.accept(); |
| 81 | BufferedInputStream in = |
| 82 | new BufferedInputStream(socket.getInputStream()); |
| 83 | |
| 84 | RMIMasterSocketFactory.proxyLog.log(Log.BRIEF, |
| 85 | "socket accepted (checking for POST)"); |
| 86 | |
| 87 | in.mark(4); |
| 88 | boolean isHttp = (in.read() == 'P') && |
| 89 | (in.read() == 'O') && |
| 90 | (in.read() == 'S') && |
| 91 | (in.read() == 'T'); |
| 92 | in.reset(); |
| 93 | |
| 94 | if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.BRIEF)) { |
| 95 | RMIMasterSocketFactory.proxyLog.log(Log.BRIEF, |
| 96 | (isHttp ? "POST found, HTTP socket returned" : |
| 97 | "POST not found, direct socket returned")); |
| 98 | } |
| 99 | |
| 100 | if (isHttp) |
| 101 | return new HttpReceiveSocket(socket, in, null); |
| 102 | else |
| 103 | return new WrappedSocket(socket, in, null); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Return the implementation address and implementation port of |
| 108 | * the HttpAwareServerSocket as a String. |
| 109 | */ |
| 110 | public String toString() |
| 111 | { |
| 112 | return "HttpAware" + super.toString(); |
| 113 | } |
| 114 | } |