Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
28 package ch.boye.httpclientandroidlib.impl;
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.net.Socket;
33 import java.net.SocketException;
35 import ch.boye.httpclientandroidlib.HttpInetConnection;
36 import ch.boye.httpclientandroidlib.impl.io.SocketInputBuffer;
37 import ch.boye.httpclientandroidlib.impl.io.SocketOutputBuffer;
38 import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
39 import ch.boye.httpclientandroidlib.io.SessionOutputBuffer;
40 import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
41 import ch.boye.httpclientandroidlib.params.HttpParams;
43 /**
44 * Implementation of a client-side HTTP connection that can be bound to an
45 * arbitrary {@link Socket} for receiving data from and transmitting data to
46 * a remote server.
47 * <p>
48 * The following parameters can be used to customize the behavior of this
49 * class:
50 * <ul>
51 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
52 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
53 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
54 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
55 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
56 * </ul>
57 *
58 * @since 4.0
59 */
60 public class SocketHttpClientConnection
61 extends AbstractHttpClientConnection implements HttpInetConnection {
63 private volatile boolean open;
64 private volatile Socket socket = null;
66 public SocketHttpClientConnection() {
67 super();
68 }
70 protected void assertNotOpen() {
71 if (this.open) {
72 throw new IllegalStateException("Connection is already open");
73 }
74 }
76 protected void assertOpen() {
77 if (!this.open) {
78 throw new IllegalStateException("Connection is not open");
79 }
80 }
82 /**
83 * Creates an instance of {@link SocketInputBuffer} to be used for
84 * receiving data from the given {@link Socket}.
85 * <p>
86 * This method can be overridden in a super class in order to provide
87 * a custom implementation of {@link SessionInputBuffer} interface.
88 *
89 * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams)
90 *
91 * @param socket the socket.
92 * @param buffersize the buffer size.
93 * @param params HTTP parameters.
94 * @return session input buffer.
95 * @throws IOException in case of an I/O error.
96 */
97 protected SessionInputBuffer createSessionInputBuffer(
98 final Socket socket,
99 int buffersize,
100 final HttpParams params) throws IOException {
101 return new SocketInputBuffer(socket, buffersize, params);
102 }
104 /**
105 * Creates an instance of {@link SessionOutputBuffer} to be used for
106 * sending data to the given {@link Socket}.
107 * <p>
108 * This method can be overridden in a super class in order to provide
109 * a custom implementation of {@link SocketOutputBuffer} interface.
110 *
111 * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams)
112 *
113 * @param socket the socket.
114 * @param buffersize the buffer size.
115 * @param params HTTP parameters.
116 * @return session output buffer.
117 * @throws IOException in case of an I/O error.
118 */
119 protected SessionOutputBuffer createSessionOutputBuffer(
120 final Socket socket,
121 int buffersize,
122 final HttpParams params) throws IOException {
123 return new SocketOutputBuffer(socket, buffersize, params);
124 }
126 /**
127 * Binds this connection to the given {@link Socket}. This socket will be
128 * used by the connection to send and receive data.
129 * <p>
130 * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)}
131 * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods
132 * to create session input / output buffers bound to this socket and then
133 * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)}
134 * method to pass references to those buffers to the underlying HTTP message
135 * parser and formatter.
136 * <p>
137 * After this method's execution the connection status will be reported
138 * as open and the {@link #isOpen()} will return <code>true</code>.
139 *
140 * @param socket the socket.
141 * @param params HTTP parameters.
142 * @throws IOException in case of an I/O error.
143 */
144 protected void bind(
145 final Socket socket,
146 final HttpParams params) throws IOException {
147 if (socket == null) {
148 throw new IllegalArgumentException("Socket may not be null");
149 }
150 if (params == null) {
151 throw new IllegalArgumentException("HTTP parameters may not be null");
152 }
153 this.socket = socket;
155 int buffersize = HttpConnectionParams.getSocketBufferSize(params);
157 init(
158 createSessionInputBuffer(socket, buffersize, params),
159 createSessionOutputBuffer(socket, buffersize, params),
160 params);
162 this.open = true;
163 }
165 public boolean isOpen() {
166 return this.open;
167 }
169 protected Socket getSocket() {
170 return this.socket;
171 }
173 public InetAddress getLocalAddress() {
174 if (this.socket != null) {
175 return this.socket.getLocalAddress();
176 } else {
177 return null;
178 }
179 }
181 public int getLocalPort() {
182 if (this.socket != null) {
183 return this.socket.getLocalPort();
184 } else {
185 return -1;
186 }
187 }
189 public InetAddress getRemoteAddress() {
190 if (this.socket != null) {
191 return this.socket.getInetAddress();
192 } else {
193 return null;
194 }
195 }
197 public int getRemotePort() {
198 if (this.socket != null) {
199 return this.socket.getPort();
200 } else {
201 return -1;
202 }
203 }
205 public void setSocketTimeout(int timeout) {
206 assertOpen();
207 if (this.socket != null) {
208 try {
209 this.socket.setSoTimeout(timeout);
210 } catch (SocketException ignore) {
211 // It is not quite clear from the Sun's documentation if there are any
212 // other legitimate cases for a socket exception to be thrown when setting
213 // SO_TIMEOUT besides the socket being already closed
214 }
215 }
216 }
218 public int getSocketTimeout() {
219 if (this.socket != null) {
220 try {
221 return this.socket.getSoTimeout();
222 } catch (SocketException ignore) {
223 return -1;
224 }
225 } else {
226 return -1;
227 }
228 }
230 public void shutdown() throws IOException {
231 this.open = false;
232 Socket tmpsocket = this.socket;
233 if (tmpsocket != null) {
234 tmpsocket.close();
235 }
236 }
238 public void close() throws IOException {
239 if (!this.open) {
240 return;
241 }
242 this.open = false;
243 Socket sock = this.socket;
244 try {
245 doFlush();
246 try {
247 try {
248 sock.shutdownOutput();
249 } catch (IOException ignore) {
250 }
251 try {
252 sock.shutdownInput();
253 } catch (IOException ignore) {
254 }
255 } catch (UnsupportedOperationException ignore) {
256 // if one isn't supported, the other one isn't either
257 }
258 } finally {
259 sock.close();
260 }
261 }
263 }