Home | History | Annotate | Download | only in impl
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java $
      3  * $Revision: 561083 $
      4  * $Date: 2007-07-30 11:31:17 -0700 (Mon, 30 Jul 2007) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl;
     33 
     34 import java.io.IOException;
     35 import java.net.InetAddress;
     36 import java.net.Socket;
     37 import java.net.SocketException;
     38 
     39 import org.apache.http.HttpInetConnection;
     40 import org.apache.http.impl.io.SocketInputBuffer;
     41 import org.apache.http.impl.io.SocketOutputBuffer;
     42 import org.apache.http.io.SessionInputBuffer;
     43 import org.apache.http.io.SessionOutputBuffer;
     44 import org.apache.http.params.HttpConnectionParams;
     45 import org.apache.http.params.HttpParams;
     46 
     47 /**
     48  * Implementation of a server-side HTTP connection that can be bound to a
     49  * network Socket in order to receive and transmit data.
     50  *
     51  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     52  *
     53  * @version $Revision: 561083 $
     54  *
     55  * @since 4.0
     56  */
     57 public class SocketHttpServerConnection extends
     58         AbstractHttpServerConnection implements HttpInetConnection {
     59 
     60     private volatile boolean open;
     61     private Socket socket = null;
     62 
     63     public SocketHttpServerConnection() {
     64         super();
     65     }
     66 
     67     protected void assertNotOpen() {
     68         if (this.open) {
     69             throw new IllegalStateException("Connection is already open");
     70         }
     71     }
     72 
     73     protected void assertOpen() {
     74         if (!this.open) {
     75             throw new IllegalStateException("Connection is not open");
     76         }
     77     }
     78 
     79     protected SessionInputBuffer createHttpDataReceiver(
     80             final Socket socket,
     81             int buffersize,
     82             final HttpParams params) throws IOException {
     83         return new SocketInputBuffer(socket, buffersize, params);
     84     }
     85 
     86     protected SessionOutputBuffer createHttpDataTransmitter(
     87             final Socket socket,
     88             int buffersize,
     89             final HttpParams params) throws IOException {
     90         return new SocketOutputBuffer(socket, buffersize, params);
     91     }
     92 
     93     protected void bind(final Socket socket, final HttpParams params) throws IOException {
     94         if (socket == null) {
     95             throw new IllegalArgumentException("Socket may not be null");
     96         }
     97         if (params == null) {
     98             throw new IllegalArgumentException("HTTP parameters may not be null");
     99         }
    100         this.socket = socket;
    101 
    102         int buffersize = HttpConnectionParams.getSocketBufferSize(params);
    103 
    104         init(
    105                 createHttpDataReceiver(socket, buffersize, params),
    106                 createHttpDataTransmitter(socket, buffersize, params),
    107                 params);
    108 
    109         this.open = true;
    110     }
    111 
    112     protected Socket getSocket() {
    113         return this.socket;
    114     }
    115 
    116     public boolean isOpen() {
    117         return this.open;
    118     }
    119 
    120     public InetAddress getLocalAddress() {
    121         if (this.socket != null) {
    122             return this.socket.getLocalAddress();
    123         } else {
    124             return null;
    125         }
    126     }
    127 
    128     public int getLocalPort() {
    129         if (this.socket != null) {
    130             return this.socket.getLocalPort();
    131         } else {
    132             return -1;
    133         }
    134     }
    135 
    136     public InetAddress getRemoteAddress() {
    137         if (this.socket != null) {
    138             return this.socket.getInetAddress();
    139         } else {
    140             return null;
    141         }
    142     }
    143 
    144     public int getRemotePort() {
    145         if (this.socket != null) {
    146             return this.socket.getPort();
    147         } else {
    148             return -1;
    149         }
    150     }
    151 
    152     public void setSocketTimeout(int timeout) {
    153         assertOpen();
    154         if (this.socket != null) {
    155             try {
    156                 this.socket.setSoTimeout(timeout);
    157             } catch (SocketException ignore) {
    158                 // It is not quite clear from the Sun's documentation if there are any
    159                 // other legitimate cases for a socket exception to be thrown when setting
    160                 // SO_TIMEOUT besides the socket being already closed
    161             }
    162         }
    163     }
    164 
    165     public int getSocketTimeout() {
    166         if (this.socket != null) {
    167             try {
    168                 return this.socket.getSoTimeout();
    169             } catch (SocketException ignore) {
    170                 return -1;
    171             }
    172         } else {
    173             return -1;
    174         }
    175     }
    176 
    177     public void shutdown() throws IOException {
    178         this.open = false;
    179         Socket tmpsocket = this.socket;
    180         if (tmpsocket != null) {
    181             tmpsocket.close();
    182         }
    183     }
    184 
    185     public void close() throws IOException {
    186         if (!this.open) {
    187             return;
    188         }
    189         this.open = false;
    190         doFlush();
    191         try {
    192             this.socket.shutdownOutput();
    193         } catch (IOException ignore) {
    194         }
    195         try {
    196             this.socket.shutdownInput();
    197         } catch (IOException ignore) {
    198         }
    199         this.socket.close();
    200     }
    201 
    202 }
    203