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.protocol; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.ConnectionReuseStrategy; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.HttpException; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.HttpRequest; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.HttpResponseFactory; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.HttpServerConnection; |
michael@0 | 40 | import ch.boye.httpclientandroidlib.HttpStatus; |
michael@0 | 41 | import ch.boye.httpclientandroidlib.HttpVersion; |
michael@0 | 42 | import ch.boye.httpclientandroidlib.MethodNotSupportedException; |
michael@0 | 43 | import ch.boye.httpclientandroidlib.ProtocolException; |
michael@0 | 44 | import ch.boye.httpclientandroidlib.ProtocolVersion; |
michael@0 | 45 | import ch.boye.httpclientandroidlib.UnsupportedHttpVersionException; |
michael@0 | 46 | import ch.boye.httpclientandroidlib.entity.ByteArrayEntity; |
michael@0 | 47 | import ch.boye.httpclientandroidlib.params.HttpParams; |
michael@0 | 48 | import ch.boye.httpclientandroidlib.params.DefaultedHttpParams; |
michael@0 | 49 | import ch.boye.httpclientandroidlib.util.EncodingUtils; |
michael@0 | 50 | import ch.boye.httpclientandroidlib.util.EntityUtils; |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * HttpService is a server side HTTP protocol handler based in the blocking |
michael@0 | 54 | * I/O model that implements the essential requirements of the HTTP protocol |
michael@0 | 55 | * for the server side message processing as described by RFC 2616. |
michael@0 | 56 | * <br> |
michael@0 | 57 | * HttpService relies on {@link HttpProcessor} to generate mandatory protocol |
michael@0 | 58 | * headers for all outgoing messages and apply common, cross-cutting message |
michael@0 | 59 | * transformations to all incoming and outgoing messages, whereas individual |
michael@0 | 60 | * {@link HttpRequestHandler}s are expected to take care of application specific |
michael@0 | 61 | * content generation and processing. |
michael@0 | 62 | * <br> |
michael@0 | 63 | * HttpService relies on {@link HttpRequestHandler} to resolve matching request |
michael@0 | 64 | * handler for a particular request URI of an incoming HTTP request. |
michael@0 | 65 | * <br> |
michael@0 | 66 | * HttpService can use optional {@link HttpExpectationVerifier} to ensure that |
michael@0 | 67 | * incoming requests meet server's expectations. |
michael@0 | 68 | * |
michael@0 | 69 | * @since 4.0 |
michael@0 | 70 | */ |
michael@0 | 71 | public class HttpService { |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * TODO: make all variables final in the next major version |
michael@0 | 75 | */ |
michael@0 | 76 | private volatile HttpParams params = null; |
michael@0 | 77 | private volatile HttpProcessor processor = null; |
michael@0 | 78 | private volatile HttpRequestHandlerResolver handlerResolver = null; |
michael@0 | 79 | private volatile ConnectionReuseStrategy connStrategy = null; |
michael@0 | 80 | private volatile HttpResponseFactory responseFactory = null; |
michael@0 | 81 | private volatile HttpExpectationVerifier expectationVerifier = null; |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Create a new HTTP service. |
michael@0 | 85 | * |
michael@0 | 86 | * @param processor the processor to use on requests and responses |
michael@0 | 87 | * @param connStrategy the connection reuse strategy |
michael@0 | 88 | * @param responseFactory the response factory |
michael@0 | 89 | * @param handlerResolver the handler resolver. May be null. |
michael@0 | 90 | * @param expectationVerifier the expectation verifier. May be null. |
michael@0 | 91 | * @param params the HTTP parameters |
michael@0 | 92 | * |
michael@0 | 93 | * @since 4.1 |
michael@0 | 94 | */ |
michael@0 | 95 | public HttpService( |
michael@0 | 96 | final HttpProcessor processor, |
michael@0 | 97 | final ConnectionReuseStrategy connStrategy, |
michael@0 | 98 | final HttpResponseFactory responseFactory, |
michael@0 | 99 | final HttpRequestHandlerResolver handlerResolver, |
michael@0 | 100 | final HttpExpectationVerifier expectationVerifier, |
michael@0 | 101 | final HttpParams params) { |
michael@0 | 102 | super(); |
michael@0 | 103 | if (processor == null) { |
michael@0 | 104 | throw new IllegalArgumentException("HTTP processor may not be null"); |
michael@0 | 105 | } |
michael@0 | 106 | if (connStrategy == null) { |
michael@0 | 107 | throw new IllegalArgumentException("Connection reuse strategy may not be null"); |
michael@0 | 108 | } |
michael@0 | 109 | if (responseFactory == null) { |
michael@0 | 110 | throw new IllegalArgumentException("Response factory may not be null"); |
michael@0 | 111 | } |
michael@0 | 112 | if (params == null) { |
michael@0 | 113 | throw new IllegalArgumentException("HTTP parameters may not be null"); |
michael@0 | 114 | } |
michael@0 | 115 | this.processor = processor; |
michael@0 | 116 | this.connStrategy = connStrategy; |
michael@0 | 117 | this.responseFactory = responseFactory; |
michael@0 | 118 | this.handlerResolver = handlerResolver; |
michael@0 | 119 | this.expectationVerifier = expectationVerifier; |
michael@0 | 120 | this.params = params; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | /** |
michael@0 | 124 | * Create a new HTTP service. |
michael@0 | 125 | * |
michael@0 | 126 | * @param processor the processor to use on requests and responses |
michael@0 | 127 | * @param connStrategy the connection reuse strategy |
michael@0 | 128 | * @param responseFactory the response factory |
michael@0 | 129 | * @param handlerResolver the handler resolver. May be null. |
michael@0 | 130 | * @param params the HTTP parameters |
michael@0 | 131 | * |
michael@0 | 132 | * @since 4.1 |
michael@0 | 133 | */ |
michael@0 | 134 | public HttpService( |
michael@0 | 135 | final HttpProcessor processor, |
michael@0 | 136 | final ConnectionReuseStrategy connStrategy, |
michael@0 | 137 | final HttpResponseFactory responseFactory, |
michael@0 | 138 | final HttpRequestHandlerResolver handlerResolver, |
michael@0 | 139 | final HttpParams params) { |
michael@0 | 140 | this(processor, connStrategy, responseFactory, handlerResolver, null, params); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | /** |
michael@0 | 144 | * Create a new HTTP service. |
michael@0 | 145 | * |
michael@0 | 146 | * @param proc the processor to use on requests and responses |
michael@0 | 147 | * @param connStrategy the connection reuse strategy |
michael@0 | 148 | * @param responseFactory the response factory |
michael@0 | 149 | * |
michael@0 | 150 | * @deprecated use {@link HttpService#HttpService(HttpProcessor, |
michael@0 | 151 | * ConnectionReuseStrategy, HttpResponseFactory, HttpRequestHandlerResolver, HttpParams)} |
michael@0 | 152 | */ |
michael@0 | 153 | public HttpService( |
michael@0 | 154 | final HttpProcessor proc, |
michael@0 | 155 | final ConnectionReuseStrategy connStrategy, |
michael@0 | 156 | final HttpResponseFactory responseFactory) { |
michael@0 | 157 | super(); |
michael@0 | 158 | setHttpProcessor(proc); |
michael@0 | 159 | setConnReuseStrategy(connStrategy); |
michael@0 | 160 | setResponseFactory(responseFactory); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * @deprecated set {@link HttpProcessor} using constructor |
michael@0 | 165 | */ |
michael@0 | 166 | public void setHttpProcessor(final HttpProcessor processor) { |
michael@0 | 167 | if (processor == null) { |
michael@0 | 168 | throw new IllegalArgumentException("HTTP processor may not be null"); |
michael@0 | 169 | } |
michael@0 | 170 | this.processor = processor; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | /** |
michael@0 | 174 | * @deprecated set {@link ConnectionReuseStrategy} using constructor |
michael@0 | 175 | */ |
michael@0 | 176 | public void setConnReuseStrategy(final ConnectionReuseStrategy connStrategy) { |
michael@0 | 177 | if (connStrategy == null) { |
michael@0 | 178 | throw new IllegalArgumentException("Connection reuse strategy may not be null"); |
michael@0 | 179 | } |
michael@0 | 180 | this.connStrategy = connStrategy; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | /** |
michael@0 | 184 | * @deprecated set {@link HttpResponseFactory} using constructor |
michael@0 | 185 | */ |
michael@0 | 186 | public void setResponseFactory(final HttpResponseFactory responseFactory) { |
michael@0 | 187 | if (responseFactory == null) { |
michael@0 | 188 | throw new IllegalArgumentException("Response factory may not be null"); |
michael@0 | 189 | } |
michael@0 | 190 | this.responseFactory = responseFactory; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * @deprecated set {@link HttpResponseFactory} using constructor |
michael@0 | 195 | */ |
michael@0 | 196 | public void setParams(final HttpParams params) { |
michael@0 | 197 | this.params = params; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /** |
michael@0 | 201 | * @deprecated set {@link HttpRequestHandlerResolver} using constructor |
michael@0 | 202 | */ |
michael@0 | 203 | public void setHandlerResolver(final HttpRequestHandlerResolver handlerResolver) { |
michael@0 | 204 | this.handlerResolver = handlerResolver; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | /** |
michael@0 | 208 | * @deprecated set {@link HttpExpectationVerifier} using constructor |
michael@0 | 209 | */ |
michael@0 | 210 | public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) { |
michael@0 | 211 | this.expectationVerifier = expectationVerifier; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | public HttpParams getParams() { |
michael@0 | 215 | return this.params; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | /** |
michael@0 | 219 | * Handles receives one HTTP request over the given connection within the |
michael@0 | 220 | * given execution context and sends a response back to the client. |
michael@0 | 221 | * |
michael@0 | 222 | * @param conn the active connection to the client |
michael@0 | 223 | * @param context the actual execution context. |
michael@0 | 224 | * @throws IOException in case of an I/O error. |
michael@0 | 225 | * @throws HttpException in case of HTTP protocol violation or a processing |
michael@0 | 226 | * problem. |
michael@0 | 227 | */ |
michael@0 | 228 | public void handleRequest( |
michael@0 | 229 | final HttpServerConnection conn, |
michael@0 | 230 | final HttpContext context) throws IOException, HttpException { |
michael@0 | 231 | |
michael@0 | 232 | context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); |
michael@0 | 233 | |
michael@0 | 234 | HttpResponse response = null; |
michael@0 | 235 | |
michael@0 | 236 | try { |
michael@0 | 237 | |
michael@0 | 238 | HttpRequest request = conn.receiveRequestHeader(); |
michael@0 | 239 | request.setParams( |
michael@0 | 240 | new DefaultedHttpParams(request.getParams(), this.params)); |
michael@0 | 241 | |
michael@0 | 242 | ProtocolVersion ver = |
michael@0 | 243 | request.getRequestLine().getProtocolVersion(); |
michael@0 | 244 | if (!ver.lessEquals(HttpVersion.HTTP_1_1)) { |
michael@0 | 245 | // Downgrade protocol version if greater than HTTP/1.1 |
michael@0 | 246 | ver = HttpVersion.HTTP_1_1; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | if (request instanceof HttpEntityEnclosingRequest) { |
michael@0 | 250 | |
michael@0 | 251 | if (((HttpEntityEnclosingRequest) request).expectContinue()) { |
michael@0 | 252 | response = this.responseFactory.newHttpResponse(ver, |
michael@0 | 253 | HttpStatus.SC_CONTINUE, context); |
michael@0 | 254 | response.setParams( |
michael@0 | 255 | new DefaultedHttpParams(response.getParams(), this.params)); |
michael@0 | 256 | |
michael@0 | 257 | if (this.expectationVerifier != null) { |
michael@0 | 258 | try { |
michael@0 | 259 | this.expectationVerifier.verify(request, response, context); |
michael@0 | 260 | } catch (HttpException ex) { |
michael@0 | 261 | response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, |
michael@0 | 262 | HttpStatus.SC_INTERNAL_SERVER_ERROR, context); |
michael@0 | 263 | response.setParams( |
michael@0 | 264 | new DefaultedHttpParams(response.getParams(), this.params)); |
michael@0 | 265 | handleException(ex, response); |
michael@0 | 266 | } |
michael@0 | 267 | } |
michael@0 | 268 | if (response.getStatusLine().getStatusCode() < 200) { |
michael@0 | 269 | // Send 1xx response indicating the server expections |
michael@0 | 270 | // have been met |
michael@0 | 271 | conn.sendResponseHeader(response); |
michael@0 | 272 | conn.flush(); |
michael@0 | 273 | response = null; |
michael@0 | 274 | conn.receiveRequestEntity((HttpEntityEnclosingRequest) request); |
michael@0 | 275 | } |
michael@0 | 276 | } else { |
michael@0 | 277 | conn.receiveRequestEntity((HttpEntityEnclosingRequest) request); |
michael@0 | 278 | } |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | if (response == null) { |
michael@0 | 282 | response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context); |
michael@0 | 283 | response.setParams( |
michael@0 | 284 | new DefaultedHttpParams(response.getParams(), this.params)); |
michael@0 | 285 | |
michael@0 | 286 | context.setAttribute(ExecutionContext.HTTP_REQUEST, request); |
michael@0 | 287 | context.setAttribute(ExecutionContext.HTTP_RESPONSE, response); |
michael@0 | 288 | |
michael@0 | 289 | this.processor.process(request, context); |
michael@0 | 290 | doService(request, response, context); |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | // Make sure the request content is fully consumed |
michael@0 | 294 | if (request instanceof HttpEntityEnclosingRequest) { |
michael@0 | 295 | HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity(); |
michael@0 | 296 | EntityUtils.consume(entity); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | } catch (HttpException ex) { |
michael@0 | 300 | response = this.responseFactory.newHttpResponse |
michael@0 | 301 | (HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, |
michael@0 | 302 | context); |
michael@0 | 303 | response.setParams( |
michael@0 | 304 | new DefaultedHttpParams(response.getParams(), this.params)); |
michael@0 | 305 | handleException(ex, response); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | this.processor.process(response, context); |
michael@0 | 309 | conn.sendResponseHeader(response); |
michael@0 | 310 | conn.sendResponseEntity(response); |
michael@0 | 311 | conn.flush(); |
michael@0 | 312 | |
michael@0 | 313 | if (!this.connStrategy.keepAlive(response, context)) { |
michael@0 | 314 | conn.close(); |
michael@0 | 315 | } |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | /** |
michael@0 | 319 | * Handles the given exception and generates an HTTP response to be sent |
michael@0 | 320 | * back to the client to inform about the exceptional condition encountered |
michael@0 | 321 | * in the course of the request processing. |
michael@0 | 322 | * |
michael@0 | 323 | * @param ex the exception. |
michael@0 | 324 | * @param response the HTTP response. |
michael@0 | 325 | */ |
michael@0 | 326 | protected void handleException(final HttpException ex, final HttpResponse response) { |
michael@0 | 327 | if (ex instanceof MethodNotSupportedException) { |
michael@0 | 328 | response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED); |
michael@0 | 329 | } else if (ex instanceof UnsupportedHttpVersionException) { |
michael@0 | 330 | response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED); |
michael@0 | 331 | } else if (ex instanceof ProtocolException) { |
michael@0 | 332 | response.setStatusCode(HttpStatus.SC_BAD_REQUEST); |
michael@0 | 333 | } else { |
michael@0 | 334 | response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); |
michael@0 | 335 | } |
michael@0 | 336 | byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage()); |
michael@0 | 337 | ByteArrayEntity entity = new ByteArrayEntity(msg); |
michael@0 | 338 | entity.setContentType("text/plain; charset=US-ASCII"); |
michael@0 | 339 | response.setEntity(entity); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | /** |
michael@0 | 343 | * The default implementation of this method attempts to resolve an |
michael@0 | 344 | * {@link HttpRequestHandler} for the request URI of the given request |
michael@0 | 345 | * and, if found, executes its |
michael@0 | 346 | * {@link HttpRequestHandler#handle(HttpRequest, HttpResponse, HttpContext)} |
michael@0 | 347 | * method. |
michael@0 | 348 | * <p> |
michael@0 | 349 | * Super-classes can override this method in order to provide a custom |
michael@0 | 350 | * implementation of the request processing logic. |
michael@0 | 351 | * |
michael@0 | 352 | * @param request the HTTP request. |
michael@0 | 353 | * @param response the HTTP response. |
michael@0 | 354 | * @param context the execution context. |
michael@0 | 355 | * @throws IOException in case of an I/O error. |
michael@0 | 356 | * @throws HttpException in case of HTTP protocol violation or a processing |
michael@0 | 357 | * problem. |
michael@0 | 358 | */ |
michael@0 | 359 | protected void doService( |
michael@0 | 360 | final HttpRequest request, |
michael@0 | 361 | final HttpResponse response, |
michael@0 | 362 | final HttpContext context) throws HttpException, IOException { |
michael@0 | 363 | HttpRequestHandler handler = null; |
michael@0 | 364 | if (this.handlerResolver != null) { |
michael@0 | 365 | String requestURI = request.getRequestLine().getUri(); |
michael@0 | 366 | handler = this.handlerResolver.lookup(requestURI); |
michael@0 | 367 | } |
michael@0 | 368 | if (handler != null) { |
michael@0 | 369 | handler.handle(request, response, context); |
michael@0 | 370 | } else { |
michael@0 | 371 | response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED); |
michael@0 | 372 | } |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | } |