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 server-side HTTP connection that can be bound to a |
michael@0 | 45 | * network Socket in order to receive and transmit data. |
michael@0 | 46 | * <p> |
michael@0 | 47 | * The following parameters can be used to customize the behavior of this |
michael@0 | 48 | * class: |
michael@0 | 49 | * <ul> |
michael@0 | 50 | * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li> |
michael@0 | 51 | * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li> |
michael@0 | 52 | * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li> |
michael@0 | 53 | * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li> |
michael@0 | 54 | * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li> |
michael@0 | 55 | * </ul> |
michael@0 | 56 | * |
michael@0 | 57 | * @since 4.0 |
michael@0 | 58 | */ |
michael@0 | 59 | public class SocketHttpServerConnection extends |
michael@0 | 60 | AbstractHttpServerConnection implements HttpInetConnection { |
michael@0 | 61 | |
michael@0 | 62 | private volatile boolean open; |
michael@0 | 63 | private volatile Socket socket = null; |
michael@0 | 64 | |
michael@0 | 65 | public SocketHttpServerConnection() { |
michael@0 | 66 | super(); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | protected void assertNotOpen() { |
michael@0 | 70 | if (this.open) { |
michael@0 | 71 | throw new IllegalStateException("Connection is already open"); |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | protected void assertOpen() { |
michael@0 | 76 | if (!this.open) { |
michael@0 | 77 | throw new IllegalStateException("Connection is not open"); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * @deprecated Use {@link #createSessionInputBuffer(Socket, int, HttpParams)} |
michael@0 | 83 | */ |
michael@0 | 84 | protected SessionInputBuffer createHttpDataReceiver( |
michael@0 | 85 | final Socket socket, |
michael@0 | 86 | int buffersize, |
michael@0 | 87 | final HttpParams params) throws IOException { |
michael@0 | 88 | return createSessionInputBuffer(socket, buffersize, params); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * @deprecated Use {@link #createSessionOutputBuffer(Socket, int, HttpParams)} |
michael@0 | 93 | */ |
michael@0 | 94 | protected SessionOutputBuffer createHttpDataTransmitter( |
michael@0 | 95 | final Socket socket, |
michael@0 | 96 | int buffersize, |
michael@0 | 97 | final HttpParams params) throws IOException { |
michael@0 | 98 | return createSessionOutputBuffer(socket, buffersize, params); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Creates an instance of {@link SocketInputBuffer} to be used for |
michael@0 | 103 | * receiving data from the given {@link Socket}. |
michael@0 | 104 | * <p> |
michael@0 | 105 | * This method can be overridden in a super class in order to provide |
michael@0 | 106 | * a custom implementation of {@link SessionInputBuffer} interface. |
michael@0 | 107 | * |
michael@0 | 108 | * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams) |
michael@0 | 109 | * |
michael@0 | 110 | * @param socket the socket. |
michael@0 | 111 | * @param buffersize the buffer size. |
michael@0 | 112 | * @param params HTTP parameters. |
michael@0 | 113 | * @return session input buffer. |
michael@0 | 114 | * @throws IOException in case of an I/O error. |
michael@0 | 115 | */ |
michael@0 | 116 | protected SessionInputBuffer createSessionInputBuffer( |
michael@0 | 117 | final Socket socket, |
michael@0 | 118 | int buffersize, |
michael@0 | 119 | final HttpParams params) throws IOException { |
michael@0 | 120 | return new SocketInputBuffer(socket, buffersize, params); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | /** |
michael@0 | 124 | * Creates an instance of {@link SessionOutputBuffer} to be used for |
michael@0 | 125 | * sending data to the given {@link Socket}. |
michael@0 | 126 | * <p> |
michael@0 | 127 | * This method can be overridden in a super class in order to provide |
michael@0 | 128 | * a custom implementation of {@link SocketOutputBuffer} interface. |
michael@0 | 129 | * |
michael@0 | 130 | * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams) |
michael@0 | 131 | * |
michael@0 | 132 | * @param socket the socket. |
michael@0 | 133 | * @param buffersize the buffer size. |
michael@0 | 134 | * @param params HTTP parameters. |
michael@0 | 135 | * @return session output buffer. |
michael@0 | 136 | * @throws IOException in case of an I/O error. |
michael@0 | 137 | */ |
michael@0 | 138 | protected SessionOutputBuffer createSessionOutputBuffer( |
michael@0 | 139 | final Socket socket, |
michael@0 | 140 | int buffersize, |
michael@0 | 141 | final HttpParams params) throws IOException { |
michael@0 | 142 | return new SocketOutputBuffer(socket, buffersize, params); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Binds this connection to the given {@link Socket}. This socket will be |
michael@0 | 147 | * used by the connection to send and receive data. |
michael@0 | 148 | * <p> |
michael@0 | 149 | * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)} |
michael@0 | 150 | * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods |
michael@0 | 151 | * to create session input / output buffers bound to this socket and then |
michael@0 | 152 | * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)} |
michael@0 | 153 | * method to pass references to those buffers to the underlying HTTP message |
michael@0 | 154 | * parser and formatter. |
michael@0 | 155 | * <p> |
michael@0 | 156 | * After this method's execution the connection status will be reported |
michael@0 | 157 | * as open and the {@link #isOpen()} will return <code>true</code>. |
michael@0 | 158 | * |
michael@0 | 159 | * @param socket the socket. |
michael@0 | 160 | * @param params HTTP parameters. |
michael@0 | 161 | * @throws IOException in case of an I/O error. |
michael@0 | 162 | */ |
michael@0 | 163 | protected void bind(final Socket socket, final HttpParams params) throws IOException { |
michael@0 | 164 | if (socket == null) { |
michael@0 | 165 | throw new IllegalArgumentException("Socket may not be null"); |
michael@0 | 166 | } |
michael@0 | 167 | if (params == null) { |
michael@0 | 168 | throw new IllegalArgumentException("HTTP parameters may not be null"); |
michael@0 | 169 | } |
michael@0 | 170 | this.socket = socket; |
michael@0 | 171 | |
michael@0 | 172 | int buffersize = HttpConnectionParams.getSocketBufferSize(params); |
michael@0 | 173 | |
michael@0 | 174 | init( |
michael@0 | 175 | createHttpDataReceiver(socket, buffersize, params), |
michael@0 | 176 | createHttpDataTransmitter(socket, buffersize, params), |
michael@0 | 177 | params); |
michael@0 | 178 | |
michael@0 | 179 | this.open = true; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | protected Socket getSocket() { |
michael@0 | 183 | return this.socket; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | public boolean isOpen() { |
michael@0 | 187 | return this.open; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | public InetAddress getLocalAddress() { |
michael@0 | 191 | if (this.socket != null) { |
michael@0 | 192 | return this.socket.getLocalAddress(); |
michael@0 | 193 | } else { |
michael@0 | 194 | return null; |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | public int getLocalPort() { |
michael@0 | 199 | if (this.socket != null) { |
michael@0 | 200 | return this.socket.getLocalPort(); |
michael@0 | 201 | } else { |
michael@0 | 202 | return -1; |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | public InetAddress getRemoteAddress() { |
michael@0 | 207 | if (this.socket != null) { |
michael@0 | 208 | return this.socket.getInetAddress(); |
michael@0 | 209 | } else { |
michael@0 | 210 | return null; |
michael@0 | 211 | } |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | public int getRemotePort() { |
michael@0 | 215 | if (this.socket != null) { |
michael@0 | 216 | return this.socket.getPort(); |
michael@0 | 217 | } else { |
michael@0 | 218 | return -1; |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | public void setSocketTimeout(int timeout) { |
michael@0 | 223 | assertOpen(); |
michael@0 | 224 | if (this.socket != null) { |
michael@0 | 225 | try { |
michael@0 | 226 | this.socket.setSoTimeout(timeout); |
michael@0 | 227 | } catch (SocketException ignore) { |
michael@0 | 228 | // It is not quite clear from the Sun's documentation if there are any |
michael@0 | 229 | // other legitimate cases for a socket exception to be thrown when setting |
michael@0 | 230 | // SO_TIMEOUT besides the socket being already closed |
michael@0 | 231 | } |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | public int getSocketTimeout() { |
michael@0 | 236 | if (this.socket != null) { |
michael@0 | 237 | try { |
michael@0 | 238 | return this.socket.getSoTimeout(); |
michael@0 | 239 | } catch (SocketException ignore) { |
michael@0 | 240 | return -1; |
michael@0 | 241 | } |
michael@0 | 242 | } else { |
michael@0 | 243 | return -1; |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | public void shutdown() throws IOException { |
michael@0 | 248 | this.open = false; |
michael@0 | 249 | Socket tmpsocket = this.socket; |
michael@0 | 250 | if (tmpsocket != null) { |
michael@0 | 251 | tmpsocket.close(); |
michael@0 | 252 | } |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | public void close() throws IOException { |
michael@0 | 256 | if (!this.open) { |
michael@0 | 257 | return; |
michael@0 | 258 | } |
michael@0 | 259 | this.open = false; |
michael@0 | 260 | this.open = false; |
michael@0 | 261 | Socket sock = this.socket; |
michael@0 | 262 | try { |
michael@0 | 263 | doFlush(); |
michael@0 | 264 | try { |
michael@0 | 265 | try { |
michael@0 | 266 | sock.shutdownOutput(); |
michael@0 | 267 | } catch (IOException ignore) { |
michael@0 | 268 | } |
michael@0 | 269 | try { |
michael@0 | 270 | sock.shutdownInput(); |
michael@0 | 271 | } catch (IOException ignore) { |
michael@0 | 272 | } |
michael@0 | 273 | } catch (UnsupportedOperationException ignore) { |
michael@0 | 274 | // if one isn't supported, the other one isn't either |
michael@0 | 275 | } |
michael@0 | 276 | } finally { |
michael@0 | 277 | sock.close(); |
michael@0 | 278 | } |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | } |