001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java $
003: * $Revision: 561083 $
004: * $Date: 2007-07-30 20:31:17 +0200 (Mon, 30 Jul 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl;
033:
034: import java.io.IOException;
035: import java.net.InetAddress;
036: import java.net.Socket;
037: import java.net.SocketException;
038:
039: import org.apache.http.HttpInetConnection;
040: import org.apache.http.impl.io.SocketInputBuffer;
041: import org.apache.http.impl.io.SocketOutputBuffer;
042: import org.apache.http.io.SessionInputBuffer;
043: import org.apache.http.io.SessionOutputBuffer;
044: import org.apache.http.params.HttpConnectionParams;
045: import org.apache.http.params.HttpParams;
046:
047: /**
048: * Implementation of a client-side HTTP connection that can be bound to a
049: * network Socket in order to receive and transmit data.
050: *
051: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
052: *
053: * @version $Revision: 561083 $
054: *
055: * @since 4.0
056: */
057: public class SocketHttpClientConnection extends
058: AbstractHttpClientConnection implements HttpInetConnection {
059:
060: private volatile boolean open;
061: private Socket socket = null;
062:
063: public SocketHttpClientConnection() {
064: super ();
065: }
066:
067: protected void assertNotOpen() {
068: if (this .open) {
069: throw new IllegalStateException(
070: "Connection is already open");
071: }
072: }
073:
074: protected void assertOpen() {
075: if (!this .open) {
076: throw new IllegalStateException("Connection is not open");
077: }
078: }
079:
080: protected SessionInputBuffer createSessionInputBuffer(
081: final Socket socket, int buffersize, final HttpParams params)
082: throws IOException {
083: return new SocketInputBuffer(socket, buffersize, params);
084: }
085:
086: protected SessionOutputBuffer createSessionOutputBuffer(
087: final Socket socket, int buffersize, final HttpParams params)
088: throws IOException {
089: return new SocketOutputBuffer(socket, buffersize, params);
090: }
091:
092: protected void bind(final Socket socket, final HttpParams params)
093: throws IOException {
094: if (socket == null) {
095: throw new IllegalArgumentException("Socket may not be null");
096: }
097: if (params == null) {
098: throw new IllegalArgumentException(
099: "HTTP parameters may not be null");
100: }
101: this .socket = socket;
102:
103: int buffersize = HttpConnectionParams
104: .getSocketBufferSize(params);
105:
106: init(createSessionInputBuffer(socket, buffersize, params),
107: createSessionOutputBuffer(socket, buffersize, params),
108: params);
109:
110: this .open = true;
111: }
112:
113: public boolean isOpen() {
114: return this .open;
115: }
116:
117: protected Socket getSocket() {
118: return this .socket;
119: }
120:
121: public InetAddress getLocalAddress() {
122: if (this .socket != null) {
123: return this .socket.getLocalAddress();
124: } else {
125: return null;
126: }
127: }
128:
129: public int getLocalPort() {
130: if (this .socket != null) {
131: return this .socket.getLocalPort();
132: } else {
133: return -1;
134: }
135: }
136:
137: public InetAddress getRemoteAddress() {
138: if (this .socket != null) {
139: return this .socket.getInetAddress();
140: } else {
141: return null;
142: }
143: }
144:
145: public int getRemotePort() {
146: if (this .socket != null) {
147: return this .socket.getPort();
148: } else {
149: return -1;
150: }
151: }
152:
153: public void setSocketTimeout(int timeout) {
154: assertOpen();
155: if (this .socket != null) {
156: try {
157: this .socket.setSoTimeout(timeout);
158: } catch (SocketException ignore) {
159: // It is not quite clear from the Sun's documentation if there are any
160: // other legitimate cases for a socket exception to be thrown when setting
161: // SO_TIMEOUT besides the socket being already closed
162: }
163: }
164: }
165:
166: public int getSocketTimeout() {
167: if (this .socket != null) {
168: try {
169: return this .socket.getSoTimeout();
170: } catch (SocketException ignore) {
171: return -1;
172: }
173: } else {
174: return -1;
175: }
176: }
177:
178: public void shutdown() throws IOException {
179: this .open = false;
180: Socket tmpsocket = this .socket;
181: if (tmpsocket != null) {
182: tmpsocket.close();
183: }
184: }
185:
186: public void close() throws IOException {
187: if (!this .open) {
188: return;
189: }
190: this .open = false;
191: doFlush();
192: try {
193: try {
194: this .socket.shutdownOutput();
195: } catch (IOException ignore) {
196: }
197: try {
198: this .socket.shutdownInput();
199: } catch (IOException ignore) {
200: }
201: } catch (UnsupportedOperationException ignore) {
202: // if one isn't supported, the other one isn't either
203: }
204: this.socket.close();
205: }
206:
207: }
|