List of usage examples for io.netty.channel ChannelFutureListener CLOSE
ChannelFutureListener CLOSE
To view the source code for io.netty.channel ChannelFutureListener CLOSE.
Click Source Link
From source file:nettyhttpfileserverdemo.HttpFileServerHandler.java
private void sendRedirect(ChannelHandlerContext ctx, String newUri) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND); response.headers().set(LOCATION, newUri); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:nikoladasm.aspark.ResponseImpl.java
License:Open Source License
private ChannelFuture writeObjectToChannel(Object object) { ChannelFuture lastContentFuture = ctx.channel().writeAndFlush(object); if (!keepAlive || HTTP_1_0.equals(version)) lastContentFuture.addListener(ChannelFutureListener.CLOSE); return lastContentFuture; }
From source file:nikoladasm.aspark.server.ServerHandler.java
License:Open Source License
private void sendResponse(ChannelHandlerContext ctx, HttpVersion version, HttpResponseStatus status, boolean keepAlive, String body) { FullHttpResponse response = new DefaultFullHttpResponse((version == null) ? HTTP_1_1 : version, status, Unpooled.copiedBuffer((body == null) ? "" : body, CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (keepAlive) response.headers().set(CONNECTION, KEEP_ALIVE); ChannelFuture lastContentFuture = ctx.channel().writeAndFlush(response); if (!keepAlive || HTTP_1_0.equals(version)) lastContentFuture.addListener(ChannelFutureListener.CLOSE); }
From source file:nikoladasm.aspark.server.ServerHandler.java
License:Open Source License
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { WebSocketContextImpl wctx = ctx.channel().attr(WEBSOCKET_CONTEXT_ATTR_KEY).get(); WebSocketHandler wsHandler = ctx.channel().attr(WEBSOCKET_HANDLER_ATTR_KEY).get(); if (frame instanceof CloseWebSocketFrame) { WebSocketServerHandshaker handshaker = ctx.channel().attr(HANDSHAKER_ATTR_KEY).get(); if (handshaker != null) { frame.retain();/*from www .j a v a 2s .co m*/ handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame); if (wsHandler != null) { ctx.channel().attr(WEBSOCKET_HANDLER_ATTR_KEY).remove(); ctx.channel().attr(WEBSOCKET_CONTEXT_ATTR_KEY).remove(); String reason = ((CloseWebSocketFrame) frame).reasonText(); int statusCode = ((CloseWebSocketFrame) frame).statusCode(); wsHandler.onClose(wctx, statusCode, reason); } } else { ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } return; } if (wsHandler == null) return; if (frame instanceof PingWebSocketFrame) { frame.content().retain(); ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); return; } if (frame instanceof PongWebSocketFrame) { return; } if (frame instanceof TextWebSocketFrame) { wctx.textFrameBegin(true); String request = ((TextWebSocketFrame) frame).text(); if (frame.isFinalFragment()) { wsHandler.onMessage(wctx, request); } else { wctx.stringBuilder().append(request); } return; } if (frame instanceof BinaryWebSocketFrame) { wctx.textFrameBegin(false); byte[] request = new byte[((BinaryWebSocketFrame) frame).content().readableBytes()]; ((BinaryWebSocketFrame) frame).content().readBytes(request); if (frame.isFinalFragment()) { wsHandler.onMessage(wctx, request); } else { wctx.frameBuffer().writeBytes(request); } return; } if (frame instanceof ContinuationWebSocketFrame) { if (wctx.textFrameBegin()) { String request = ((ContinuationWebSocketFrame) frame).text(); wctx.stringBuilder().append(request); if (frame.isFinalFragment()) { wsHandler.onMessage(wctx, wctx.stringBuilder().toString()); wctx.stringBuilder(new StringBuilder()); } } else { byte[] request = new byte[((BinaryWebSocketFrame) frame).content().readableBytes()]; ((BinaryWebSocketFrame) frame).content().readBytes(request); wctx.frameBuffer().writeBytes(request); if (frame.isFinalFragment()) { request = new byte[wctx.frameBuffer().readableBytes()]; wctx.frameBuffer().readBytes(request); wsHandler.onMessage(wctx, request); wctx.frameBuffer().clear(); } } return; } }
From source file:org.acmsl.katas.antlr4netty.InterpreterServerChannelHandler.java
License:Open Source License
/** * {@inheritDoc}/*from ww w . jav a2 s . co m*/ */ @Override public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }
From source file:org.aesh.terminal.http.netty.HttpRequestHandler.java
License:Open Source License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (wsUri.equalsIgnoreCase(request.getUri())) { ctx.fireChannelRead(request.retain()); } else {/*from w w w .ja va 2 s . c o m*/ if (HttpHeaders.is100ContinueExpected(request)) { send100Continue(ctx); } HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR); String path = request.getUri(); if ("/".equals(path)) { path = "/index.html"; } URL res = HttpTtyConnection.class.getResource("/org/aesh/terminal/http" + path); try { if (res != null) { DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK); InputStream in = res.openStream(); byte[] tmp = new byte[256]; for (int l = 0; l != -1; l = in.read(tmp)) { fullResp.content().writeBytes(tmp, 0, l); } int li = path.lastIndexOf('.'); if (li != -1 && li != path.length() - 1) { String ext = path.substring(li + 1, path.length()); String contentType; switch (ext) { case "html": contentType = "text/html"; break; case "js": contentType = "application/javascript"; break; default: contentType = null; break; } if (contentType != null) { fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType); } } response = fullResp; } else { response.setStatus(HttpResponseStatus.NOT_FOUND); } } catch (Exception e) { e.printStackTrace(); } finally { ctx.write(response); ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); future.addListener(ChannelFutureListener.CLOSE); } } }
From source file:org.apache.activemq.artemis.core.protocol.stomp.WebSocketServerHandler.java
License:Apache License
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().code() != 200) { res.content().writeBytes(res.getStatus().toString().getBytes(StandardCharsets.UTF_8)); setContentLength(res, res.content().readableBytes()); }/*from w w w . ja v a 2 s . c o m*/ // Send the response and close the connection if necessary. ChannelFuture f = ctx.writeAndFlush(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.activemq.artemis.core.server.protocol.websocket.WebSocketServerHandler.java
License:Apache License
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.status().code() != 200) { res.content().writeBytes(res.status().toString().getBytes(StandardCharsets.UTF_8)); setContentLength(res, res.content().readableBytes()); }/* ww w. j a v a 2s . c om*/ // Send the response and close the connection if necessary. ChannelFuture f = ctx.writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.activemq.jms.example.HttpStaticFileServerHandler.java
License:Apache License
@Override public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e) throws Exception { HttpRequest request = (HttpRequest) e.getMessage(); if (request.getMethod() != HttpMethod.GET) { sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED); return;//from w w w . j a va 2 s . com } if (request.isChunked()) { sendError(ctx, HttpResponseStatus.BAD_REQUEST); return; } String path = sanitizeUri(request.getUri()); if (path == null) { sendError(ctx, HttpResponseStatus.FORBIDDEN); return; } File file = new File(path); if (file.isHidden() || !file.exists()) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } if (!file.isFile()) { sendError(ctx, HttpResponseStatus.FORBIDDEN); return; } RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException fnfe) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } long fileLength = raf.length(); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(fileLength)); Channel ch = e.getChannel(); // Write the initial line and the header. ch.write(response); // Write the content. ChannelFuture writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192)); // Decide whether to close the connection or not. boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE .equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION)); if (close) { // Close the connection when the whole content is written out. writeFuture.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.activemq.jms.example.HttpStaticFileServerHandler.java
License:Apache License
private void sendError(final ChannelHandlerContext ctx, final HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer("Failure: " + status.toString() + "\r\n", "UTF-8")); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }