@Override public final int available() { final AsyncResponseProducer responseProducer = responseProducerRef.get(); if (responseProducer == null) { return exchangeHandler.available(); } else { return responseProducer.available(); } }
@Override public final void consume(final ByteBuffer src) throws IOException { final AsyncResponseProducer responseProducer = responseProducerRef.get(); if (responseProducer == null) { exchangeHandler.consume(src); } }
@Override public final void failed(final Exception cause) { try { exchangeHandler.failed(cause); final AsyncResponseProducer dataProducer = responseProducerRef.getAndSet(null); if (dataProducer != null) { dataProducer.failed(cause); } } finally { releaseResources(); } }
@Override public final void produce(final DataStreamChannel channel) throws IOException { final AsyncResponseProducer responseProducer = responseProducerRef.get(); if (responseProducer == null) { exchangeHandler.produce(channel); } else { responseProducer.produce(channel); } }
@Override public void consumeData(final ByteBuffer src, final boolean endStream) throws HttpException, IOException { if (done.get() || requestState != MessageState.BODY) { throw new ProtocolException("Unexpected message data"); } Asserts.notNull(exchangeHandler, "Exchange handler"); if (src != null) { exchangeHandler.consume(src); } if (endStream) { requestState = MessageState.COMPLETE; exchangeHandler.streamEnd(null); } }
@Override public void handleRequest( final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException { final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION); final String challengeResponse = h != null ? authTokenExtractor.extract(h.getValue()) : null; final URIAuthority authority = request.getAuthority(); final String requestUri = request.getRequestUri(); final boolean authenticated = authenticator.authenticate(authority, requestUri, challengeResponse); final Header expect = request.getFirstHeader(HttpHeaders.EXPECT); final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue()); if (authenticated) { if (expectContinue) { responseChannel.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE), context); } exchangeHandler.handleRequest(request, entityDetails, responseChannel, context); } else { final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED); final String realm = authenticator.getRealm(authority, requestUri); unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"" + realm + "\""); customizeUnauthorizedResponse(unauthorized); final AsyncResponseProducer responseProducer = new BasicResponseProducer( unauthorized, new BasicAsyncEntityProducer("Unauthorized", ContentType.TEXT_PLAIN)); responseProducerRef.set(responseProducer); responseProducer.sendResponse(responseChannel, context); } }
@Override public final void releaseResources() { exchangeHandler.releaseResources(); final AsyncResponseProducer dataProducer = responseProducerRef.getAndSet(null); if (dataProducer != null) { dataProducer.releaseResources(); } }
@Override public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException { final AsyncResponseProducer responseProducer = responseProducerRef.get(); if (responseProducer == null) { exchangeHandler.updateCapacity(capacityChannel); } else { capacityChannel.update(Integer.MAX_VALUE); } }
@Override public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException { final AsyncResponseProducer responseProducer = responseProducerRef.get(); if (responseProducer == null) { exchangeHandler.streamEnd(trailers); } }
receivedRequest = request; exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context); } catch (final HttpException ex) { if (!responseCommitted.get()) { final AsyncResponseProducer responseProducer = ServerSupport.handleException(ex); exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer); exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context); } else { throw ex; case BODY: responseState = MessageState.COMPLETE; exchangeHandler.streamEnd(headers); break; default:
void produceOutput() throws HttpException, IOException { switch (responseState) { case BODY: exchangeHandler.produce(internalDataChannel); break; } }
@Override public final void handleRequest( final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException { if (entityDetails != null) { final Header h = request.getFirstHeader(HttpHeaders.EXPECT); if (h != null && "100-continue".equalsIgnoreCase(h.getValue())) { final AsyncResponseProducer producer = verify(request, context); if (producer != null) { responseProducerRef.set(producer); producer.sendResponse(responseChannel, context); return; } responseChannel.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE), context); } } handler.handleRequest(request, entityDetails, responseChannel, context); }
@Override public void releaseResources() { if (done.compareAndSet(false, true)) { requestState = MessageState.COMPLETE; responseState = MessageState.COMPLETE; exchangeHandler.releaseResources(); } }
void updateCapacity(final CapacityChannel capacityChannel) throws IOException { exchangeHandler.updateCapacity(capacityChannel); }
@Override public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException { final AsyncResponseProducer responseProducer = responseProducerRef.get(); if (responseProducer == null) { handler.streamEnd(trailers); } }
void produceOutput() throws HttpException, IOException { switch (responseState) { case BODY: exchangeHandler.produce(internalDataChannel); break; } }
@Override public final void handleRequest( final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException { if (entityDetails != null) { final Header h = request.getFirstHeader(HttpHeaders.EXPECT); if (h != null && "100-continue".equalsIgnoreCase(h.getValue())) { final AsyncResponseProducer producer = verify(request, context); if (producer != null) { responseProducerRef.set(producer); producer.sendResponse(responseChannel, context); return; } responseChannel.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE), context); } } handler.handleRequest(request, entityDetails, responseChannel, context); }
boolean isOutputReady() { switch (responseState) { case BODY: return exchangeHandler.available() > 0; default: return false; } }
@Override public void releaseResources() { if (done.compareAndSet(false, true)) { requestState = MessageState.COMPLETE; responseState = MessageState.COMPLETE; exchangeHandler.releaseResources(); } }
void updateCapacity(final CapacityChannel capacityChannel) throws IOException { exchangeHandler.updateCapacity(capacityChannel); }