void chunk(HttpChunk chunk) throws Exception { Preconditions.checkState( bodyConsumer != null, "Received chunked content without BodyConsumer."); if (chunk.isLast()) { bodyConsumer.finished(responder); bodyConsumer = null; } else { try { bodyConsumer.chunk(chunk.getContent(), responder); } catch (Throwable t) { bodyConsumer.handleError(t); bodyConsumer = null; throw new HandlerException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "", t); } } }
/** * This is determined finished when the body has been consumed. If only the header has been * consumed then the body will be created using the header information, the body is then read from * the cursor, which may read nothing for an empty body. * * @return this returns true if the entity has been built */ public boolean isFinished() { if (header.isFinished()) { if (body == null) { body = factory.getInstance(); } return body.isFinished(); } return false; }
/** Calls the httpHandler method. */ void invoke() throws Exception { if (isStreaming) { // Casting guarantee to be succeeded. bodyConsumer = (BodyConsumer) method.invoke(handler, args); if (requestContent.readable()) { try { bodyConsumer.chunk(requestContent, responder); } catch (Throwable t) { bodyConsumer.handleError(t); bodyConsumer = null; throw new HandlerException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "", t); } } if (!isChunkedRequest) { bodyConsumer.finished(responder); bodyConsumer = null; } } else { // Actually <T> would be void method.invoke(handler, args); bodyConsumer = null; } }
/** * This consumes the header and body from the cursor. The header is consumed first followed by the * body if there is any. There is a body of there is a Content-Length or a Transfer-Encoding * header present. If there is no body then a substitute body is given which has an empty input * stream. * * @param cursor used to consumed the bytes for the entity */ public void consume(Cursor cursor) throws IOException { while (cursor.isReady()) { if (header.isFinished()) { if (body == null) { body = factory.getInstance(); } body.consume(cursor); if (body.isFinished()) { break; } } else { header.consume(cursor); } } if (header.isFinished()) { if (body == null) { expect.execute(header); body = factory.getInstance(); } builder.setBody(body); builder.setHeader(header); } }