List of usage examples for io.netty.handler.stream ChunkedFile close
@Override
public void close() throws Exception
From source file:org.glowroot.agent.plugin.netty.Http1ServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest request = (HttpRequest) msg; @SuppressWarnings("deprecation") String uri = request.getUri(); if (uri.equals("/exception")) { throw new Exception("Test"); }//from w w w . ja v a 2s .c om if (uri.equals("/chunked")) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.headers().set("transfer-encoding", "chunked"); response.headers().set("content-type", "text/plain"); ctx.write(response); final File file = File.createTempFile("glowroot-netty-plugin-it-", ".txt"); final ChunkedFile chunkedFile = new ChunkedFile(file); Files.write(CONTENT, file); ctx.write(chunkedFile).addListener(ChannelFutureListener.CLOSE) .addListener(new GenericFutureListener<Future<Void>>() { @Override public void operationComplete(Future<Void> arg0) throws Exception { chunkedFile.close(); if (!file.delete()) { throw new IllegalStateException("Could not delete file: " + file.getPath()); } } }); return; } FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT)); response.headers().set("Content-Type", "text/plain"); response.headers().set("Content-Length", response.content().readableBytes()); ctx.write(response).addListener(ChannelFutureListener.CLOSE); } }
From source file:org.ratpackframework.file.internal.FileHttpTransmitter.java
License:Apache License
public boolean transmit(final File targetFile, HttpResponse response, Channel channel) { final RandomAccessFile raf; try {//ww w . jav a2 s . com raf = new RandomAccessFile(targetFile, "r"); } catch (FileNotFoundException fnfe) { throw new RuntimeException(fnfe); } long fileLength; try { fileLength = raf.length(); } catch (IOException e) { closeQuietly(raf); throw new RuntimeException(e); } response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, fileLength); HttpHeaders.setDateHeader(response, HttpHeaders.Names.LAST_MODIFIED, new Date(targetFile.lastModified())); // Write the initial line and the header. if (!channel.isOpen()) { closeQuietly(raf); return false; } try { channel.write(response); } catch (Exception e) { closeQuietly(raf); } // Write the content. ChannelFuture writeFuture; ChunkedFile message = null; try { message = new ChunkedFile(raf, 0, fileLength, 8192); writeFuture = channel.write(message); } catch (Exception ignore) { if (channel.isOpen()) { channel.close(); } if (message != null) { try { message.close(); } catch (Exception e) { throw new RuntimeException(e); } } return false; } final ChunkedFile finalMessage = message; writeFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { try { finalMessage.close(); } catch (Exception e) { throw new RuntimeException(e); } future.addListener(ChannelFutureListener.CLOSE); } }); return true; }