1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.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
32package org.apache.http.impl;
33
34import java.io.IOException;
35import java.net.InetAddress;
36import java.net.Socket;
37import java.net.SocketException;
38
39import org.apache.http.HttpInetConnection;
40import org.apache.http.impl.io.SocketInputBuffer;
41import org.apache.http.impl.io.SocketOutputBuffer;
42import org.apache.http.io.SessionInputBuffer;
43import org.apache.http.io.SessionOutputBuffer;
44import org.apache.http.params.HttpConnectionParams;
45import org.apache.http.params.HttpParams;
46
47/**
48 * Implementation of a client-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 */
57public class SocketHttpClientConnection
58        extends AbstractHttpClientConnection implements HttpInetConnection {
59
60    private volatile boolean open;
61    private Socket socket = null;
62
63    public SocketHttpClientConnection() {
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 createSessionInputBuffer(
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 createSessionOutputBuffer(
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(
94            final Socket socket,
95            final HttpParams params) throws IOException {
96        if (socket == null) {
97            throw new IllegalArgumentException("Socket may not be null");
98        }
99        if (params == null) {
100            throw new IllegalArgumentException("HTTP parameters may not be null");
101        }
102        this.socket = socket;
103
104        int buffersize = HttpConnectionParams.getSocketBufferSize(params);
105
106        init(
107                createSessionInputBuffer(socket, buffersize, params),
108                createSessionOutputBuffer(socket, buffersize, params),
109                params);
110
111        this.open = true;
112    }
113
114    public boolean isOpen() {
115        return this.open;
116    }
117
118    protected Socket getSocket() {
119        return this.socket;
120    }
121
122    public InetAddress getLocalAddress() {
123        if (this.socket != null) {
124            return this.socket.getLocalAddress();
125        } else {
126            return null;
127        }
128    }
129
130    public int getLocalPort() {
131        if (this.socket != null) {
132            return this.socket.getLocalPort();
133        } else {
134            return -1;
135        }
136    }
137
138    public InetAddress getRemoteAddress() {
139        if (this.socket != null) {
140            return this.socket.getInetAddress();
141        } else {
142            return null;
143        }
144    }
145
146    public int getRemotePort() {
147        if (this.socket != null) {
148            return this.socket.getPort();
149        } else {
150            return -1;
151        }
152    }
153
154    public void setSocketTimeout(int timeout) {
155        assertOpen();
156        if (this.socket != null) {
157            try {
158                this.socket.setSoTimeout(timeout);
159            } catch (SocketException ignore) {
160                // It is not quite clear from the Sun's documentation if there are any
161                // other legitimate cases for a socket exception to be thrown when setting
162                // SO_TIMEOUT besides the socket being already closed
163            }
164        }
165    }
166
167    public int getSocketTimeout() {
168        if (this.socket != null) {
169            try {
170                return this.socket.getSoTimeout();
171            } catch (SocketException ignore) {
172                return -1;
173            }
174        } else {
175            return -1;
176        }
177    }
178
179    public void shutdown() throws IOException {
180        this.open = false;
181        Socket tmpsocket = this.socket;
182        if (tmpsocket != null) {
183            tmpsocket.close();
184        }
185    }
186
187    public void close() throws IOException {
188        if (!this.open) {
189            return;
190        }
191        this.open = false;
192        doFlush();
193        try {
194            try {
195                this.socket.shutdownOutput();
196            } catch (IOException ignore) {
197            }
198            try {
199                this.socket.shutdownInput();
200            } catch (IOException ignore) {
201            }
202        } catch (UnsupportedOperationException ignore) {
203            // if one isn't supported, the other one isn't either
204        }
205        this.socket.close();
206    }
207
208}
209