Java Code Examples for org.jboss.netty.handler.codec.http.HttpChunk#isLast()

The following examples show how to use org.jboss.netty.handler.codec.http.HttpChunk#isLast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpServerHandler.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (!readingChunks) {
        request = (HttpRequest) e.getMessage();
        String uri = request.getUri();
        if (uri.equals("/exception")) {
            throw new Exception("Test");
        }
        if (request.isChunked()) {
            readingChunks = true;
        } else {
            writeResponse(e);
        }
    } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        if (chunk.isLast()) {
            readingChunks = false;
            writeResponse(e);
        }
    }
}
 
Example 2
Source File: FileClientHandler.java    From netty-file-parent with Apache License 2.0 5 votes vote down vote up
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
		throws Exception {
	if (!this.readingChunks) {
		HttpResponse response = (HttpResponse) e.getMessage();
		LOGGER.info("STATUS: " + response.getStatus());
		if ((response.getStatus().getCode() == 200)
				&& (response.isChunked())) {
			this.readingChunks = true;
		} else {
			ChannelBuffer content = response.getContent();
			if (content.readable())
				this.responseContent.append(content
						.toString(CharsetUtil.UTF_8));
		}
	} else {
		HttpChunk chunk = (HttpChunk) e.getMessage();
		if (chunk.isLast()) {
			this.readingChunks = false;
			this.responseContent.append(chunk.getContent().toString(
					CharsetUtil.UTF_8));

			String json = this.responseContent.toString();
			this.result = ((Result) JSONUtil.parseObject(json,Result.class));
		} else {
			this.responseContent.append(chunk.getContent().toString(
					CharsetUtil.UTF_8));
		}
	}
}
 
Example 3
Source File: StreamChunkAggregator.java    From restcommander with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    if (!(msg instanceof HttpMessage) && !(msg instanceof HttpChunk)) {
        ctx.sendUpstream(e);
        return;
    }

    HttpMessage currentMessage = this.currentMessage;
    File localFile = this.file;
    if (currentMessage == null) {
        HttpMessage m = (HttpMessage) msg;
        if (m.isChunked()) {
            final String localName = UUID.randomUUID().toString();
            // A chunked message - remove 'Transfer-Encoding' header,
            // initialize the cumulative buffer, and wait for incoming chunks.
            List<String> encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
            encodings.remove(HttpHeaders.Values.CHUNKED);
            if (encodings.isEmpty()) {
                m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
            }
            this.currentMessage = m;
            this.file = new File(Play.tmpDir, localName);
            this.out = new FileOutputStream(file, true);
        } else {
            // Not a chunked message - pass through.
            ctx.sendUpstream(e);
        }
    } else {
        // TODO: If less that threshold then in memory
        // Merge the received chunk into the content of the current message.
        final HttpChunk chunk = (HttpChunk) msg;
        if (maxContentLength != -1 && (localFile.length() > (maxContentLength - chunk.getContent().readableBytes()))) {
            currentMessage.setHeader(HttpHeaders.Names.WARNING, "play.netty.content.length.exceeded");
        } else {
            IOUtils.copyLarge(new ChannelBufferInputStream(chunk.getContent()), this.out);

            if (chunk.isLast()) {
                this.out.flush();
                this.out.close();

                currentMessage.setHeader(
                        HttpHeaders.Names.CONTENT_LENGTH,
                        String.valueOf(localFile.length()));

                currentMessage.setContent(new FileChannelBuffer(localFile));
                this.out = null;
                this.currentMessage = null;
                this.file = null;
                Channels.fireMessageReceived(ctx, currentMessage, e.getRemoteAddress());
            }
        }
    }

}