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