Source code: org/apache/http/impl/AbstractHttpConnection.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/AbstractHttpConnection.java $
3 * $Revision: 410884 $
4 * $Date: 2006-06-01 18:35:03 +0200 (Thu, 01 Jun 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.impl;
31
32 import java.io.IOException;
33 import java.net.Socket;
34
35 import org.apache.http.HttpConnection;
36 import org.apache.http.impl.io.SocketHttpDataReceiver;
37 import org.apache.http.impl.io.SocketHttpDataTransmitter;
38 import org.apache.http.io.HttpDataReceiver;
39 import org.apache.http.io.HttpDataReceiverFactory;
40 import org.apache.http.io.HttpDataTransmitter;
41 import org.apache.http.io.HttpDataTransmitterFactory;
42 import org.apache.http.params.HttpConnectionParams;
43 import org.apache.http.params.HttpParams;
44
45 /**
46 * Abstract base class for HTTP connections on the client and server side.
47 *
48 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
49 *
50 * @version $Revision: 410884 $
51 *
52 * @since 4.0
53 */
54 abstract class AbstractHttpConnection implements HttpConnection {
55
56 /*
57 * I/O operations may not be performed if this flag is set to false
58 * All methods must call #assertOpen() to ensure the connection
59 * is open prior to performing any I/O
60 */
61 protected volatile boolean open;
62 protected Socket socket = null;
63 protected HttpDataTransmitter datatransmitter = null;
64 protected HttpDataReceiver datareceiver = null;
65
66 /*
67 * Dependent interfaces
68 */
69 private HttpDataTransmitterFactory trxfactory = null;
70 private HttpDataReceiverFactory rcvfactory = null;
71
72 protected AbstractHttpConnection() {
73 super();
74 }
75
76 public void setReceiverFactory(final HttpDataReceiverFactory rcvfactory) {
77 if (rcvfactory == null) {
78 throw new IllegalArgumentException("Factory may not be null");
79 }
80 this.rcvfactory = rcvfactory;
81 }
82
83 public void setTransmitterFactory(final HttpDataTransmitterFactory trxfactory) {
84 if (trxfactory == null) {
85 throw new IllegalArgumentException("Factory may not be null");
86 }
87 this.trxfactory = trxfactory;
88 }
89
90 protected void assertNotOpen() {
91 if (this.open) {
92 throw new IllegalStateException("Connection is already open");
93 }
94 }
95
96 protected void assertOpen() {
97 if (!this.open) {
98 throw new IllegalStateException("Connection is not open");
99 }
100 }
101
102 protected void bind(final Socket socket, final HttpParams params) throws IOException {
103 if (socket == null) {
104 throw new IllegalArgumentException("Socket may not be null");
105 }
106 if (params == null) {
107 throw new IllegalArgumentException("HTTP parameters may not be null");
108 }
109 socket.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
110 socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
111
112 int linger = HttpConnectionParams.getLinger(params);
113 if (linger >= 0) {
114 socket.setSoLinger(linger > 0, linger);
115 }
116
117 int buffersize = HttpConnectionParams.getSocketBufferSize(params);
118 assertNotOpen();
119 this.open = true;
120 this.socket = socket;
121 if (this.trxfactory != null) {
122 this.datatransmitter = this.trxfactory.create(this.socket);
123 } else {
124 this.datatransmitter = new SocketHttpDataTransmitter(this.socket, buffersize);
125 }
126 if (this.rcvfactory != null) {
127 this.datareceiver = this.rcvfactory.create(this.socket);
128 } else {
129 this.datareceiver = new SocketHttpDataReceiver(this.socket, buffersize);
130 }
131 this.datatransmitter.reset(params);
132 this.datareceiver.reset(params);
133 }
134
135 public boolean isOpen() {
136 return this.open;
137 }
138
139 public void shutdown() throws IOException {
140 this.open = false;
141 Socket tmpsocket = this.socket;
142 if (tmpsocket != null) {
143 tmpsocket.close();
144 }
145 }
146
147 public void close() throws IOException {
148 if (!this.open) {
149 return;
150 }
151 this.open = false;
152 this.datatransmitter.flush();
153 try {
154 this.socket.shutdownOutput();
155 } catch (IOException ignore) {
156 }
157 try {
158 this.socket.shutdownInput();
159 } catch (IOException ignore) {
160 }
161 this.socket.close();
162 }
163
164 public boolean isStale() {
165 assertOpen();
166 try {
167 this.datareceiver.isDataAvailable(1);
168 return false;
169 } catch (IOException ex) {
170 return true;
171 }
172 }
173
174 }