View Javadoc

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   */
27  
28  package org.apache.http.nio.protocol;
29  
30  import java.io.IOException;
31  import java.net.SocketTimeoutException;
32  import java.util.Queue;
33  import java.util.concurrent.ConcurrentLinkedQueue;
34  import java.util.concurrent.atomic.AtomicBoolean;
35  
36  import org.apache.http.ConnectionReuseStrategy;
37  import org.apache.http.ExceptionLogger;
38  import org.apache.http.HttpEntity;
39  import org.apache.http.HttpEntityEnclosingRequest;
40  import org.apache.http.HttpException;
41  import org.apache.http.HttpRequest;
42  import org.apache.http.HttpResponse;
43  import org.apache.http.HttpResponseFactory;
44  import org.apache.http.HttpStatus;
45  import org.apache.http.HttpVersion;
46  import org.apache.http.MethodNotSupportedException;
47  import org.apache.http.ProtocolException;
48  import org.apache.http.UnsupportedHttpVersionException;
49  import org.apache.http.annotation.Immutable;
50  import org.apache.http.concurrent.Cancellable;
51  import org.apache.http.entity.ContentType;
52  import org.apache.http.impl.DefaultConnectionReuseStrategy;
53  import org.apache.http.impl.DefaultHttpResponseFactory;
54  import org.apache.http.nio.ContentDecoder;
55  import org.apache.http.nio.ContentEncoder;
56  import org.apache.http.nio.NHttpConnection;
57  import org.apache.http.nio.NHttpServerConnection;
58  import org.apache.http.nio.NHttpServerEventHandler;
59  import org.apache.http.nio.entity.NStringEntity;
60  import org.apache.http.nio.reactor.SessionBufferStatus;
61  import org.apache.http.params.HttpParams;
62  import org.apache.http.protocol.BasicHttpContext;
63  import org.apache.http.protocol.HttpContext;
64  import org.apache.http.protocol.HttpCoreContext;
65  import org.apache.http.protocol.HttpProcessor;
66  import org.apache.http.util.Args;
67  import org.apache.http.util.Asserts;
68  
69  /**
70   * {@code HttpAsyncService} is a fully asynchronous HTTP server side protocol
71   * handler based on the non-blocking (NIO) I/O model.
72   * {@code HttpAsyncServerProtocolHandler} translates individual events fired
73   * through the {@link NHttpServerEventHandler} interface into logically related
74   * HTTP message exchanges.
75   * <p>
76   * Upon receiving an incoming request {@code HttpAsyncService} verifies
77   * the message for compliance with the server expectations using
78   * {@link HttpAsyncExpectationVerifier}, if provided, and then
79   * {@link HttpAsyncRequestHandlerMapper} is used to map the request
80   * to a particular {@link HttpAsyncRequestHandler} intended to handle
81   * the request with the given URI. The protocol handler uses the selected
82   * {@link HttpAsyncRequestHandler} instance to process the incoming request
83   * and to generate an outgoing response.
84   * <p>
85   * {@code HttpAsyncService} relies on {@link HttpProcessor} to generate
86   * mandatory protocol headers for all outgoing messages and apply common,
87   * cross-cutting message transformations to all incoming and outgoing messages,
88   * whereas individual {@link HttpAsyncRequestHandler}s are expected
89   * to implement application specific content generation and processing.
90   * <p>
91   * Individual {@link HttpAsyncRequestHandler}s do not have to submit a response
92   * immediately. They can defer transmission of an HTTP response back to
93   * the client without blocking the I/O thread by delegating the process of
94   * request handling to another service or a worker thread. HTTP response can
95   * be submitted as a later a later point of time once response content becomes
96   * available.
97   *
98   * @since 4.2
99   */
100 @SuppressWarnings("deprecation")
101 @Immutable // provided injected dependencies are immutable
102 public class HttpAsyncService implements NHttpServerEventHandler {
103 
104     static final String HTTP_EXCHANGE_STATE = "http.nio.http-exchange-state";
105 
106     private final HttpProcessor httpProcessor;
107     private final ConnectionReuseStrategy connStrategy;
108     private final HttpResponseFactory responseFactory;
109     private final HttpAsyncRequestHandlerMapper handlerMapper;
110     private final HttpAsyncExpectationVerifier expectationVerifier;
111     private final ExceptionLogger exceptionLogger;
112 
113     /**
114      * Creates new instance of {@code HttpAsyncServerProtocolHandler}.
115      *
116      * @param httpProcessor HTTP protocol processor (required).
117      * @param connStrategy Connection re-use strategy (required).
118      * @param responseFactory HTTP response factory (required).
119      * @param handlerResolver Request handler resolver.
120      * @param expectationVerifier Request expectation verifier (optional).
121      * @param params HTTP parameters (required).
122      *
123      * @deprecated (4.3) use {@link HttpAsyncService#HttpAsyncService(HttpProcessor,
124      *  ConnectionReuseStrategy, HttpResponseFactory, HttpAsyncRequestHandlerMapper,
125      *    HttpAsyncExpectationVerifier)}
126      */
127     @Deprecated
128     public HttpAsyncService(
129             final HttpProcessor httpProcessor,
130             final ConnectionReuseStrategy connStrategy,
131             final HttpResponseFactory responseFactory,
132             final HttpAsyncRequestHandlerResolver handlerResolver,
133             final HttpAsyncExpectationVerifier expectationVerifier,
134             final HttpParams params) {
135         this(httpProcessor,
136              connStrategy,
137              responseFactory,
138              new HttpAsyncRequestHandlerResolverAdapter(handlerResolver),
139              expectationVerifier);
140     }
141 
142     /**
143      * Creates new instance of {@code HttpAsyncServerProtocolHandler}.
144      *
145      * @param httpProcessor HTTP protocol processor (required).
146      * @param connStrategy Connection re-use strategy (required).
147      * @param handlerResolver Request handler resolver.
148      * @param params HTTP parameters (required).
149      *
150      * @deprecated (4.3) use {@link HttpAsyncService#HttpAsyncService(HttpProcessor,
151      *   ConnectionReuseStrategy, HttpResponseFactory, HttpAsyncRequestHandlerMapper,
152      *   HttpAsyncExpectationVerifier)}
153      */
154     @Deprecated
155     public HttpAsyncService(
156             final HttpProcessor httpProcessor,
157             final ConnectionReuseStrategy connStrategy,
158             final HttpAsyncRequestHandlerResolver handlerResolver,
159             final HttpParams params) {
160         this(httpProcessor,
161              connStrategy,
162              DefaultHttpResponseFactory.INSTANCE,
163              new HttpAsyncRequestHandlerResolverAdapter(handlerResolver),
164              null);
165     }
166 
167     /**
168      * Creates new instance of {@code HttpAsyncServerProtocolHandler}.
169      *
170      * @param httpProcessor HTTP protocol processor.
171      * @param connStrategy Connection re-use strategy. If {@code null}
172      *   {@link DefaultConnectionReuseStrategy#INSTANCE} will be used.
173      * @param responseFactory HTTP response factory. If {@code null}
174      *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
175      * @param handlerMapper Request handler mapper.
176      * @param expectationVerifier Request expectation verifier. May be {@code null}.
177      *
178      * @since 4.3
179      */
180     public HttpAsyncService(
181             final HttpProcessor httpProcessor,
182             final ConnectionReuseStrategy connStrategy,
183             final HttpResponseFactory responseFactory,
184             final HttpAsyncRequestHandlerMapper handlerMapper,
185             final HttpAsyncExpectationVerifier expectationVerifier) {
186         this(httpProcessor, connStrategy, responseFactory, handlerMapper, expectationVerifier, null);
187     }
188 
189     /**
190      * Creates new instance of {@code HttpAsyncServerProtocolHandler}.
191      *
192      * @param httpProcessor HTTP protocol processor.
193      * @param connStrategy Connection re-use strategy. If {@code null}
194      *   {@link DefaultConnectionReuseStrategy#INSTANCE} will be used.
195      * @param responseFactory HTTP response factory. If {@code null}
196      *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
197      * @param handlerMapper Request handler mapper.
198      * @param expectationVerifier Request expectation verifier. May be {@code null}.
199      * @param exceptionLogger Exception logger. If {@code null}
200      *   {@link ExceptionLogger#NO_OP} will be used. Please note that the exception
201      *   logger will be only used to log I/O exception thrown while closing
202      *   {@link java.io.Closeable} objects (such as {@link org.apache.http.HttpConnection}).
203      *
204      * @since 4.4
205      */
206     public HttpAsyncService(
207             final HttpProcessor httpProcessor,
208             final ConnectionReuseStrategy connStrategy,
209             final HttpResponseFactory responseFactory,
210             final HttpAsyncRequestHandlerMapper handlerMapper,
211             final HttpAsyncExpectationVerifier expectationVerifier,
212             final ExceptionLogger exceptionLogger) {
213         super();
214         this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
215         this.connStrategy = connStrategy != null ? connStrategy :
216                 DefaultConnectionReuseStrategy.INSTANCE;
217         this.responseFactory = responseFactory != null ? responseFactory :
218                 DefaultHttpResponseFactory.INSTANCE;
219         this.handlerMapper = handlerMapper;
220         this.expectationVerifier = expectationVerifier;
221         this.exceptionLogger = exceptionLogger != null ? exceptionLogger : ExceptionLogger.NO_OP;
222     }
223 
224     /**
225      * Creates new instance of {@code HttpAsyncServerProtocolHandler}.
226      *
227      * @param httpProcessor HTTP protocol processor.
228      * @param handlerMapper Request handler mapper.
229      *
230      * @since 4.3
231      */
232     public HttpAsyncService(
233             final HttpProcessor httpProcessor,
234             final HttpAsyncRequestHandlerMapper handlerMapper) {
235         this(httpProcessor, null, null, handlerMapper, null);
236     }
237 
238     /**
239      * Creates new instance of {@code HttpAsyncServerProtocolHandler}.
240      *
241      * @param httpProcessor HTTP protocol processor.
242      * @param handlerMapper Request handler mapper.
243      * @param exceptionLogger Exception logger. If {@code null}
244      *   {@link ExceptionLogger#NO_OP} will be used. Please note that the exception
245      *   logger will be only used to log I/O exception thrown while closing
246      *   {@link java.io.Closeable} objects (such as {@link org.apache.http.HttpConnection}).
247      *
248      * @since 4.4
249      */
250     public HttpAsyncService(
251             final HttpProcessor httpProcessor,
252             final HttpAsyncRequestHandlerMapper handlerMapper,
253             final ExceptionLogger exceptionLogger) {
254         this(httpProcessor, null, null, handlerMapper, null, exceptionLogger);
255     }
256 
257     @Override
258     public void connected(final NHttpServerConnection conn) {
259         final State state = new State();
260         conn.getContext().setAttribute(HTTP_EXCHANGE_STATE, state);
261     }
262 
263     @Override
264     public void closed(final NHttpServerConnection conn) {
265         final State state = (State) conn.getContext().removeAttribute(HTTP_EXCHANGE_STATE);
266         if (state != null) {
267             state.setTerminated();
268             closeHandlers(state);
269             final Cancellable cancellable = state.getCancellable();
270             if (cancellable != null) {
271                 cancellable.cancel();
272             }
273         }
274     }
275 
276     @Override
277     public void exception(
278             final NHttpServerConnection conn, final Exception cause) {
279         final State state = getState(conn);
280         if (state == null) {
281             shutdownConnection(conn);
282             log(cause);
283             return;
284         }
285         state.setTerminated();
286         closeHandlers(state, cause);
287         final Cancellable cancellable = state.getCancellable();
288         if (cancellable != null) {
289             cancellable.cancel();
290         }
291         final Queue<PipelineEntry> pipeline = state.getPipeline();
292         if (!pipeline.isEmpty()
293                 || conn.isResponseSubmitted()
294                 || state.getResponseState().compareTo(MessageState.INIT) > 0) {
295             // There is not much that we can do if a response
296             // has already been submitted or pipelining is being used.
297             shutdownConnection(conn);
298         } else {
299             try {
300                 final Incoming incoming = state.getIncoming();
301                 final HttpRequest request = incoming != null ? incoming.getRequest() : null;
302                 final HttpContext context = incoming != null ? incoming.getContext() : new BasicHttpContext();
303                 final HttpAsyncResponseProducer responseProducer = handleException(cause, context);
304                 final HttpResponse response = responseProducer.generateResponse();
305                 final Outgoing outgoing = new Outgoing(request, response, responseProducer, context);
306                 state.setResponseState(MessageState.INIT);
307                 state.setOutgoing(outgoing);
308                 commitFinalResponse(conn, state);
309             } catch (final Exception ex) {
310                 shutdownConnection(conn);
311                 closeHandlers(state);
312                 if (ex instanceof RuntimeException) {
313                     throw (RuntimeException) ex;
314                 } else {
315                     log(ex);
316                 }
317             }
318         }
319     }
320 
321     @Override
322     public void requestReceived(
323             final NHttpServerConnection conn) throws IOException, HttpException {
324         final State state = getState(conn);
325         Asserts.notNull(state, "Connection state");
326         Asserts.check(state.getRequestState() == MessageState.READY,
327                 "Unexpected request state %s", state.getRequestState());
328 
329         final HttpRequest request = conn.getHttpRequest();
330         final HttpContext context = new BasicHttpContext();
331 
332         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
333         context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
334         this.httpProcessor.process(request, context);
335 
336         final HttpAsyncRequestHandler<Object> requestHandler = getRequestHandler(request);
337         final HttpAsyncRequestConsumer<Object> consumer = requestHandler.processRequest(request, context);
338         consumer.requestReceived(request);
339 
340         final Incoming incoming = new Incoming(request, requestHandler, consumer, context);
341         state.setIncoming(incoming);
342 
343         if (request instanceof HttpEntityEnclosingRequest) {
344 
345             // If 100-continue is expected make sure
346             // there is no pending response data, no pipelined requests or buffered input
347             if (((HttpEntityEnclosingRequest) request).expectContinue()
348                         && state.getResponseState() == MessageState.READY
349                         && state.getPipeline().isEmpty()
350                         && !(conn instanceof SessionBufferStatus && ((SessionBufferStatus) conn).hasBufferedInput())) {
351 
352                 state.setRequestState(MessageState.ACK_EXPECTED);
353                 final HttpResponse ack = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
354                         HttpStatus.SC_CONTINUE, context);
355                 if (this.expectationVerifier != null) {
356                     conn.suspendInput();
357                     conn.suspendOutput();
358                     final HttpAsyncExchange httpAsyncExchange = new HttpAsyncExchangeImpl(
359                             request, ack, state, conn, context);
360                     this.expectationVerifier.verify(httpAsyncExchange, context);
361                 } else {
362                     conn.submitResponse(ack);
363                     state.setRequestState(MessageState.BODY_STREAM);
364                 }
365             } else {
366                 state.setRequestState(MessageState.BODY_STREAM);
367             }
368         } else {
369             // No request content is expected. Process request right away
370             completeRequest(incoming, conn, state);
371         }
372     }
373 
374     @Override
375     public void inputReady(
376             final NHttpServerConnection conn,
377             final ContentDecoder decoder) throws IOException, HttpException {
378         final State state = getState(conn);
379         Asserts.notNull(state, "Connection state");
380         Asserts.check(state.getRequestState() == MessageState.BODY_STREAM,
381                 "Unexpected request state %s", state.getRequestState());
382 
383         final Incoming incoming = state.getIncoming();
384         Asserts.notNull(incoming, "Incoming request");
385         final HttpAsyncRequestConsumer<?> consumer = incoming.getConsumer();
386         consumer.consumeContent(decoder, conn);
387         if (decoder.isCompleted()) {
388             completeRequest(incoming, conn, state);
389         }
390     }
391 
392     @Override
393     public void responseReady(
394             final NHttpServerConnection conn) throws IOException, HttpException {
395         final State state = getState(conn);
396         Asserts.notNull(state, "Connection state");
397         Asserts.check(state.getResponseState() == MessageState.READY ||
398                         state.getResponseState() == MessageState.INIT,
399                 "Unexpected response state %s", state.getResponseState());
400 
401         if (state.getRequestState() == MessageState.ACK_EXPECTED) {
402             final Outgoing outgoing = state.getOutgoing();
403             Asserts.notNull(outgoing, "Outgoing response");
404 
405             final HttpResponse response = outgoing.getResponse();
406             final int status = response.getStatusLine().getStatusCode();
407             if (status == 100) {
408                 final HttpContext context = outgoing.getContext();
409                 final HttpAsyncResponseProducer responseProducer = outgoing.getProducer();
410                 try {
411                     // Make sure 100 response has no entity
412                     response.setEntity(null);
413                     conn.requestInput();
414                     state.setRequestState(MessageState.BODY_STREAM);
415                     state.setOutgoing(null);
416                     conn.submitResponse(response);
417                     responseProducer.responseCompleted(context);
418                 } finally {
419                     responseProducer.close();
420                 }
421             } else if (status >= 400) {
422                 conn.resetInput();
423                 state.setRequestState(MessageState.READY);
424                 commitFinalResponse(conn, state);
425             } else {
426                 throw new HttpException("Invalid response: " + response.getStatusLine());
427             }
428         } else {
429             if (state.getResponseState() == MessageState.READY) {
430                 final Queue<PipelineEntry> pipeline = state.getPipeline();
431                 final PipelineEntry pipelineEntry = pipeline.poll();
432                 if (pipelineEntry == null) {
433                     conn.suspendOutput();
434                     return;
435                 }
436                 state.setResponseState(MessageState.INIT);
437                 final Object result = pipelineEntry.getResult();
438                 final HttpRequest request = pipelineEntry.getRequest();
439                 final HttpContext context = pipelineEntry.getContext();
440                 if (result != null) {
441                     final HttpResponse response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
442                             HttpStatus.SC_OK, context);
443                     final HttpAsyncExchangeImpl httpExchange = new HttpAsyncExchangeImpl(
444                             request, response, state, conn, context);
445                     final HttpAsyncRequestHandler<Object> handler = pipelineEntry.getHandler();
446                     conn.suspendOutput();
447                     try {
448                         handler.handle(result, httpExchange, context);
449                     } catch (RuntimeException ex) {
450                         throw ex;
451                     } catch (Exception ex) {
452                         pipeline.add(new PipelineEntry(
453                             request,
454                             null,
455                             ex,
456                             handler,
457                             context));
458                         state.setResponseState(MessageState.READY);
459                         responseReady(conn);
460                         return;
461                     }
462                 } else {
463                     final Exception exception = pipelineEntry.getException();
464                     final HttpAsyncResponseProducer responseProducer = handleException(
465                             exception != null ? exception : new HttpException("Internal error processing request"),
466                             context);
467                     final HttpResponse error = responseProducer.generateResponse();
468                     state.setOutgoing(new Outgoing(request, error, responseProducer, context));
469                 }
470             }
471             if (state.getResponseState() == MessageState.INIT) {
472                 final Outgoing outgoing;
473                 synchronized (state) {
474                     outgoing = state.getOutgoing();
475                     if (outgoing == null) {
476                         conn.suspendOutput();
477                         return;
478                     }
479                 }
480                 final HttpResponse response = outgoing.getResponse();
481                 final int status = response.getStatusLine().getStatusCode();
482                 if (status >= 200) {
483                     commitFinalResponse(conn, state);
484                 } else {
485                     throw new HttpException("Invalid response: " + response.getStatusLine());
486                 }
487             }
488         }
489     }
490 
491     @Override
492     public void outputReady(
493             final NHttpServerConnection conn,
494             final ContentEncoder encoder) throws HttpException, IOException {
495         final State state = getState(conn);
496         Asserts.notNull(state, "Connection state");
497         Asserts.check(state.getResponseState() == MessageState.BODY_STREAM,
498                 "Unexpected response state %s", state.getResponseState());
499 
500         final Outgoing outgoing = state.getOutgoing();
501         Asserts.notNull(outgoing, "Outgoing response");
502         final HttpAsyncResponseProducer responseProducer = outgoing.getProducer();
503 
504         responseProducer.produceContent(encoder, conn);
505 
506         if (encoder.isCompleted()) {
507             completeResponse(outgoing, conn, state);
508         }
509     }
510 
511     @Override
512     public void endOfInput(final NHttpServerConnection conn) throws IOException {
513         // Closing connection in an orderly manner and
514         // waiting for output buffer to get flushed.
515         // Do not want to wait indefinitely, though, in case
516         // the opposite end is not reading
517         if (conn.getSocketTimeout() <= 0) {
518             conn.setSocketTimeout(1000);
519         }
520         conn.close();
521     }
522 
523     @Override
524     public void timeout(final NHttpServerConnection conn) throws IOException {
525         final State state = getState(conn);
526         if (state != null) {
527             closeHandlers(state, new SocketTimeoutException());
528         }
529         if (conn.getStatus() == NHttpConnection.ACTIVE) {
530             conn.close();
531             if (conn.getStatus() == NHttpConnection.CLOSING) {
532                 // Give the connection some grace time to
533                 // close itself nicely
534                 conn.setSocketTimeout(250);
535             }
536         } else {
537             conn.shutdown();
538         }
539     }
540 
541     private State getState(final NHttpConnection conn) {
542         return (State) conn.getContext().getAttribute(HTTP_EXCHANGE_STATE);
543     }
544 
545     /**
546      * This method can be used to log I/O exception thrown while closing
547      * {@link java.io.Closeable} objects (such as
548      * {@link org.apache.http.HttpConnection}).
549      *
550      * @param ex I/O exception thrown by {@link java.io.Closeable#close()}
551      */
552     protected void log(final Exception ex) {
553         this.exceptionLogger.log(ex);
554     }
555 
556     private void closeConnection(final NHttpConnection conn) {
557         try {
558             conn.close();
559         } catch (final IOException ex) {
560             log(ex);
561         }
562     }
563 
564     private void shutdownConnection(final NHttpConnection conn) {
565         try {
566             conn.shutdown();
567         } catch (final IOException ex) {
568             log(ex);
569         }
570     }
571 
572     private void closeHandlers(final State state, final Exception ex) {
573         final HttpAsyncRequestConsumer<Object> consumer =
574                 state.getIncoming() != null ? state.getIncoming().getConsumer() : null;
575         if (consumer != null) {
576             try {
577                 consumer.failed(ex);
578             } finally {
579                 try {
580                     consumer.close();
581                 } catch (final IOException ioex) {
582                     log(ioex);
583                 }
584             }
585         }
586         final HttpAsyncResponseProducer producer =
587                 state.getOutgoing() != null ? state.getOutgoing().getProducer() : null;
588         if (producer != null) {
589             try {
590                 producer.failed(ex);
591             } finally {
592                 try {
593                     producer.close();
594                 } catch (final IOException ioex) {
595                     log(ioex);
596                 }
597             }
598         }
599     }
600 
601     private void closeHandlers(final State state) {
602         final HttpAsyncRequestConsumer<Object> consumer =
603                 state.getIncoming() != null ? state.getIncoming().getConsumer() : null;
604         if (consumer != null) {
605             try {
606                 consumer.close();
607             } catch (final IOException ioex) {
608                 log(ioex);
609             }
610         }
611         final HttpAsyncResponseProducer producer =
612                 state.getOutgoing() != null ? state.getOutgoing().getProducer() : null;
613         if (producer != null) {
614             try {
615                 producer.close();
616             } catch (final IOException ioex) {
617                 log(ioex);
618             }
619         }
620     }
621 
622     protected HttpAsyncResponseProducer handleException(
623             final Exception ex, final HttpContext context) {
624         final int code;
625         if (ex instanceof MethodNotSupportedException) {
626             code = HttpStatus.SC_NOT_IMPLEMENTED;
627         } else if (ex instanceof UnsupportedHttpVersionException) {
628             code = HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED;
629         } else if (ex instanceof ProtocolException) {
630             code = HttpStatus.SC_BAD_REQUEST;
631         } else {
632             code = HttpStatus.SC_INTERNAL_SERVER_ERROR;
633         }
634         String message = ex.getMessage();
635         if (message == null) {
636             message = ex.toString();
637         }
638         final HttpResponse response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
639                 code, context);
640         return new ErrorResponseProducer(response,
641                 new NStringEntity(message, ContentType.DEFAULT_TEXT), false);
642     }
643 
644     /**
645      * This method can be used to handle callback set up happened after
646      * response submission.
647      *
648      * @param cancellable Request cancellation callback.
649      * @param context Request context.
650      *
651      * @since 4.4
652      */
653     protected void handleAlreadySubmittedResponse(
654             final Cancellable cancellable, final HttpContext context) {
655         throw new IllegalStateException("Response already submitted");
656     }
657 
658     /**
659      * This method can be used to handle double response submission.
660      *
661      * @param responseProducer Response producer for second response.
662      * @param context Request context.
663      *
664      * @since 4.4
665      */
666     protected void handleAlreadySubmittedResponse(
667             final HttpAsyncResponseProducer responseProducer,
668             final HttpContext context) {
669         throw new IllegalStateException("Response already submitted");
670     }
671 
672     private boolean canResponseHaveBody(final HttpRequest request, final HttpResponse response) {
673         if (request != null && "HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
674             return false;
675         }
676         final int status = response.getStatusLine().getStatusCode();
677         return status >= HttpStatus.SC_OK
678             && status != HttpStatus.SC_NO_CONTENT
679             && status != HttpStatus.SC_NOT_MODIFIED
680             && status != HttpStatus.SC_RESET_CONTENT;
681     }
682 
683     private void completeRequest(
684             final Incoming incoming,
685             final NHttpServerConnection conn,
686             final State state) throws IOException, HttpException {
687         state.setRequestState(MessageState.READY);
688         state.setIncoming(null);
689 
690         final PipelineEntry pipelineEntry;
691         final HttpAsyncRequestConsumer<?> consumer = incoming.getConsumer();
692         try {
693             final HttpContext context = incoming.getContext();
694             consumer.requestCompleted(context);
695             pipelineEntry = new PipelineEntry(
696                     incoming.getRequest(),
697                     consumer.getResult(),
698                     consumer.getException(),
699                     incoming.getHandler(),
700                     context);
701         } finally {
702             consumer.close();
703         }
704         final Queue<PipelineEntry> pipeline = state.getPipeline();
705         pipeline.add(pipelineEntry);
706         if (state.getResponseState() == MessageState.READY) {
707             conn.requestOutput();
708         }
709     }
710 
711     private void commitFinalResponse(
712             final NHttpServerConnection conn,
713             final State state) throws IOException, HttpException {
714         final Outgoing outgoing = state.getOutgoing();
715         Asserts.notNull(outgoing, "Outgoing response");
716         final HttpRequest request = outgoing.getRequest();
717         final HttpResponse response = outgoing.getResponse();
718         final HttpContext context = outgoing.getContext();
719 
720         context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
721         this.httpProcessor.process(response, context);
722 
723         HttpEntity entity = response.getEntity();
724         if (entity != null && !canResponseHaveBody(request, response)) {
725             response.setEntity(null);
726             entity = null;
727         }
728 
729         conn.submitResponse(response);
730 
731         if (entity == null) {
732             completeResponse(outgoing, conn, state);
733         } else {
734             state.setResponseState(MessageState.BODY_STREAM);
735         }
736     }
737 
738     private void completeResponse(
739             final Outgoing outgoing,
740             final NHttpServerConnection conn,
741             final State state) throws IOException, HttpException {
742         final HttpContext context = outgoing.getContext();
743         final HttpResponse response = outgoing.getResponse();
744         final HttpAsyncResponseProducer responseProducer = outgoing.getProducer();
745         try {
746             responseProducer.responseCompleted(context);
747             state.setOutgoing(null);
748             state.setCancellable(null);
749             state.setResponseState(MessageState.READY);
750         } finally {
751             responseProducer.close();
752         }
753         if (!this.connStrategy.keepAlive(response, context)) {
754             conn.close();
755         } else {
756             // Ready to process new request
757             final Queue<PipelineEntry> pipeline = state.getPipeline();
758             if (pipeline.isEmpty()) {
759                 conn.suspendOutput();
760             }
761             conn.requestInput();
762         }
763     }
764 
765     @SuppressWarnings("unchecked")
766     private HttpAsyncRequestHandler<Object> getRequestHandler(final HttpRequest request) {
767         HttpAsyncRequestHandler<Object> handler = null;
768         if (this.handlerMapper != null) {
769             handler = (HttpAsyncRequestHandler<Object>) this.handlerMapper.lookup(request);
770         }
771         if (handler == null) {
772             handler = new NullRequestHandler();
773         }
774         return handler;
775     }
776 
777     static class Incoming {
778 
779         private final HttpRequest request;
780         private final HttpAsyncRequestHandler<Object> handler;
781         private final HttpAsyncRequestConsumer<Object> consumer;
782         private final HttpContext context;
783 
784         Incoming(
785                 final HttpRequest request,
786                 final HttpAsyncRequestHandler<Object> handler,
787                 final HttpAsyncRequestConsumer<Object> consumer,
788                 final HttpContext context) {
789             this.request = request;
790             this.handler = handler;
791             this.consumer = consumer;
792             this.context = context;
793         }
794 
795         public HttpRequest getRequest() {
796             return this.request;
797         }
798 
799         public HttpAsyncRequestHandler<Object> getHandler() {
800             return this.handler;
801         }
802 
803         public HttpAsyncRequestConsumer<Object> getConsumer() {
804             return this.consumer;
805         }
806 
807         public HttpContext getContext() {
808             return this.context;
809         }
810     }
811 
812     static class Outgoing {
813 
814         private final HttpRequest request;
815         private final HttpResponse response;
816         private final HttpAsyncResponseProducer producer;
817         private final HttpContext context;
818 
819         Outgoing(
820                 final HttpRequest request,
821                 final HttpResponse response,
822                 final HttpAsyncResponseProducer producer,
823                 final HttpContext context) {
824             this.request = request;
825             this.response = response;
826             this.producer = producer;
827             this.context = context;
828         }
829 
830         public HttpRequest getRequest() {
831             return this.request;
832         }
833 
834         public HttpResponse getResponse() {
835             return this.response;
836         }
837 
838         public HttpAsyncResponseProducer getProducer() {
839             return this.producer;
840         }
841 
842         public HttpContext getContext() {
843             return this.context;
844         }
845     }
846 
847     static class PipelineEntry {
848 
849         private final HttpRequest request;
850         private final Object result;
851         private final Exception exception;
852         private final HttpAsyncRequestHandler<Object> handler;
853         private final HttpContext context;
854 
855         PipelineEntry(
856                 final HttpRequest request,
857                 final Object result,
858                 final Exception exception,
859                 final HttpAsyncRequestHandler<Object> handler,
860                 final HttpContext context) {
861             this.request = request;
862             this.result = result;
863             this.exception = exception;
864             this.handler = handler;
865             this.context = context;
866         }
867 
868         public HttpRequest getRequest() {
869             return this.request;
870         }
871 
872         public Object getResult() {
873             return this.result;
874         }
875 
876         public Exception getException() {
877             return this.exception;
878         }
879 
880         public HttpAsyncRequestHandler<Object> getHandler() {
881             return this.handler;
882         }
883 
884         public HttpContext getContext() {
885             return this.context;
886         }
887 
888     }
889 
890     static class State {
891 
892         private final Queue<PipelineEntry> pipeline;
893         private volatile boolean terminated;
894         private volatile MessageState requestState;
895         private volatile MessageState responseState;
896         private volatile Incoming incoming;
897         private volatile Outgoing outgoing;
898         private volatile Cancellable cancellable;
899 
900         State() {
901             super();
902             this.pipeline = new ConcurrentLinkedQueue<PipelineEntry>();
903             this.requestState = MessageState.READY;
904             this.responseState = MessageState.READY;
905         }
906 
907         public boolean isTerminated() {
908             return this.terminated;
909         }
910 
911         public void setTerminated() {
912             this.terminated = true;
913         }
914 
915         public MessageState getRequestState() {
916             return this.requestState;
917         }
918 
919         public void setRequestState(final MessageState state) {
920             this.requestState = state;
921         }
922 
923         public MessageState getResponseState() {
924             return this.responseState;
925         }
926 
927         public void setResponseState(final MessageState state) {
928             this.responseState = state;
929         }
930 
931         public Incoming getIncoming() {
932             return this.incoming;
933         }
934 
935         public void setIncoming(final Incoming incoming) {
936             this.incoming = incoming;
937         }
938 
939         public Outgoing getOutgoing() {
940             return this.outgoing;
941         }
942 
943         public void setOutgoing(final Outgoing outgoing) {
944             this.outgoing = outgoing;
945         }
946 
947         public Cancellable getCancellable() {
948             return this.cancellable;
949         }
950 
951         public void setCancellable(final Cancellable cancellable) {
952             this.cancellable = cancellable;
953         }
954 
955         public Queue<PipelineEntry> getPipeline() {
956             return this.pipeline;
957         }
958 
959         @Override
960         public String toString() {
961             final StringBuilder buf = new StringBuilder();
962             buf.append("[incoming ");
963             buf.append(this.requestState);
964             if (this.incoming != null) {
965                 buf.append(" ");
966                 buf.append(this.incoming.getRequest().getRequestLine());
967             }
968             buf.append("; outgoing ");
969             buf.append(this.responseState);
970             if (this.outgoing != null) {
971                 buf.append(" ");
972                 buf.append(this.outgoing.getResponse().getStatusLine());
973             }
974             buf.append("]");
975             return buf.toString();
976         }
977 
978     }
979 
980     class HttpAsyncExchangeImpl implements HttpAsyncExchange {
981 
982         private final AtomicBoolean completed = new AtomicBoolean();
983         private final HttpRequest request;
984         private final HttpResponse response;
985         private final State state;
986         private final NHttpServerConnection conn;
987         private final HttpContext context;
988 
989         public HttpAsyncExchangeImpl(
990                 final HttpRequest request,
991                 final HttpResponse response,
992                 final State state,
993                 final NHttpServerConnection conn,
994                 final HttpContext context) {
995             super();
996             this.request = request;
997             this.response = response;
998             this.state = state;
999             this.conn = conn;
1000             this.context = context;
1001         }
1002 
1003         @Override
1004         public HttpRequest getRequest() {
1005             return this.request;
1006         }
1007 
1008         @Override
1009         public HttpResponse getResponse() {
1010             return this.response;
1011         }
1012 
1013         @Override
1014         public void setCallback(final Cancellable cancellable) {
1015             if (this.completed.get()) {
1016                 handleAlreadySubmittedResponse(cancellable, context);
1017             } else if (this.state.isTerminated() && cancellable != null) {
1018                 cancellable.cancel();
1019             } else {
1020                 this.state.setCancellable(cancellable);
1021             }
1022         }
1023 
1024         @Override
1025         public void submitResponse(final HttpAsyncResponseProducer responseProducer) {
1026             Args.notNull(responseProducer, "Response producer");
1027             if (this.completed.getAndSet(true)) {
1028                 handleAlreadySubmittedResponse(responseProducer, context);
1029             } else if (!this.state.isTerminated()) {
1030                 final HttpResponse response = responseProducer.generateResponse();
1031                 final Outgoing outgoing = new Outgoing(
1032                         this.request, response, responseProducer, this.context);
1033 
1034                 synchronized (this.state) {
1035                     this.state.setOutgoing(outgoing);
1036                     this.state.setCancellable(null);
1037                     this.conn.requestOutput();
1038                 }
1039 
1040             } else {
1041                 try {
1042                     responseProducer.close();
1043                 } catch (final IOException ex) {
1044                     log(ex);
1045                 }
1046             }
1047         }
1048 
1049         @Override
1050         public void submitResponse() {
1051             submitResponse(new BasicAsyncResponseProducer(this.response));
1052         }
1053 
1054         @Override
1055         public boolean isCompleted() {
1056             return this.completed.get();
1057         }
1058 
1059         @Override
1060         public void setTimeout(final int timeout) {
1061             this.conn.setSocketTimeout(timeout);
1062         }
1063 
1064         @Override
1065         public int getTimeout() {
1066             return this.conn.getSocketTimeout();
1067         }
1068 
1069     }
1070 
1071     /**
1072      * Adaptor class to transition from HttpAsyncRequestHandlerResolver to HttpAsyncRequestHandlerMapper.
1073      */
1074     @Deprecated
1075     private static class HttpAsyncRequestHandlerResolverAdapter implements HttpAsyncRequestHandlerMapper {
1076 
1077         private final HttpAsyncRequestHandlerResolver resolver;
1078 
1079         public HttpAsyncRequestHandlerResolverAdapter(final HttpAsyncRequestHandlerResolver resolver) {
1080             this.resolver = resolver;
1081         }
1082 
1083         @Override
1084         public HttpAsyncRequestHandler<?> lookup(final HttpRequest request) {
1085             return resolver.lookup(request.getRequestLine().getUri());
1086         }
1087 
1088     }
1089 
1090 }