jdk/src/java.rmi/share/classes/sun/rmi/transport/proxy/HttpReceiveSocket.java
changeset 37925 3560b89fb532
parent 37924 a78497edf9fb
parent 37916 627154540b60
child 37934 4c38661774bf
equal deleted inserted replaced
37924:a78497edf9fb 37925:3560b89fb532
     1 /*
       
     2  * Copyright (c) 1996, 2000, Oracle and/or its affiliates. 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package sun.rmi.transport.proxy;
       
    26 
       
    27 import java.io.*;
       
    28 import java.net.Socket;
       
    29 import java.net.InetAddress;
       
    30 
       
    31 /**
       
    32  * The HttpReceiveSocket class extends the WrappedSocket class
       
    33  * by removing the HTTP protocol packaging from the input stream and
       
    34  * formatting the output stream as an HTTP response.
       
    35  *
       
    36  * NOTES:
       
    37  *
       
    38  * The output stream must be explicitly closed for the output to be
       
    39  * sent, since the HttpResponseOutputStream needs to buffer the entire
       
    40  * transmission to be able to fill in the content-length field of
       
    41  * the HTTP header.  Closing this socket will do this.
       
    42  *
       
    43  * The constructor blocks until the HTTP protocol header
       
    44  * is received.  This could be fixed, but I don't think it should be a
       
    45  * problem because this object would not be created unless the
       
    46  * HttpAwareServerSocket has detected the beginning of the header
       
    47  * anyway, so the rest should be there.
       
    48  *
       
    49  * This socket can only be used to process one POST and reply to it.
       
    50  * Another message would be received on a newly accepted socket anyway.
       
    51  */
       
    52 public class HttpReceiveSocket extends WrappedSocket implements RMISocketInfo {
       
    53 
       
    54     /** true if the HTTP header has pushed through the output stream yet */
       
    55     private boolean headerSent = false;
       
    56 
       
    57     /**
       
    58      * Layer on top of a pre-existing Socket object, and use specified
       
    59      * input and output streams.
       
    60      * @param socket the pre-existing socket to use
       
    61      * @param in the InputStream to use for this socket (can be null)
       
    62      * @param out the OutputStream to use for this socket (can be null)
       
    63      */
       
    64     public HttpReceiveSocket(Socket socket, InputStream in, OutputStream out)
       
    65         throws IOException
       
    66     {
       
    67         super(socket, in, out);
       
    68 
       
    69         this.in = new HttpInputStream(in != null ? in :
       
    70                                                    socket.getInputStream());
       
    71         this.out = (out != null ? out :
       
    72                     socket.getOutputStream());
       
    73     }
       
    74 
       
    75     /**
       
    76      * Indicate that this socket is not reusable.
       
    77      */
       
    78     public boolean isReusable()
       
    79     {
       
    80         return false;
       
    81     }
       
    82 
       
    83     /**
       
    84      * Get the address to which this socket is connected.  "null" is always
       
    85      * returned (to indicate an unknown address) because the originating
       
    86      * host's IP address cannot be reliably determined: both because the
       
    87      * request probably went through a proxy server, and because if it was
       
    88      * delivered by a local forwarder (CGI script or servlet), we do NOT
       
    89      * want it to appear as if the call is coming from the local host (in
       
    90      * case the remote object makes access control decisions based on the
       
    91      * "client host" of a remote call; see bugid 4399040).
       
    92      */
       
    93     public InetAddress getInetAddress() {
       
    94         return null;
       
    95     }
       
    96 
       
    97     /**
       
    98      * Get an OutputStream for this socket.
       
    99      */
       
   100     public OutputStream getOutputStream() throws IOException
       
   101     {
       
   102         if (!headerSent) { // could this be done in constructor??
       
   103             DataOutputStream dos = new DataOutputStream(out);
       
   104             dos.writeBytes("HTTP/1.0 200 OK\r\n");
       
   105             dos.flush();
       
   106             headerSent = true;
       
   107             out = new HttpOutputStream(out);
       
   108         }
       
   109         return out;
       
   110     }
       
   111 
       
   112     /**
       
   113      * Close the socket.
       
   114      */
       
   115     public synchronized void close() throws IOException
       
   116     {
       
   117         getOutputStream().close(); // make sure response is sent
       
   118         socket.close();
       
   119     }
       
   120 
       
   121     /**
       
   122      * Return string representation of the socket.
       
   123      */
       
   124     public String toString()
       
   125     {
       
   126         return "HttpReceive" + socket.toString();
       
   127     }
       
   128 }