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:org.apache.cxf.transport.http.netty.server.NettyHttpServletHandler.java
License:Apache License
protected void handleHttpServletRequest(ChannelHandlerContext ctx, HttpRequest request, NettyHttpContextHandler nettyHttpContextHandler) throws Exception { interceptOnRequestReceived(ctx, request); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); NettyServletResponse nettyServletResponse = buildHttpServletResponse(response); NettyHttpServletRequest nettyServletRequest = buildHttpServletRequest(request, nettyHttpContextHandler.getContextPath(), ctx); nettyHttpContextHandler.handle(nettyServletRequest.getRequestURI(), nettyServletRequest, nettyServletResponse);/*www. j a va 2s . c o m*/ interceptOnRequestSuccessed(ctx, response); nettyServletResponse.getWriter().flush(); boolean keepAlive = HttpHeaders.isKeepAlive(request); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes()); // Add keep alive header as per: // - // http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection response.headers().set(Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // write response... ChannelFuture future = ctx.write(response); if (!keepAlive) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.cxf.transport.http.netty.server.NettyHttpServletHandler.java
License:Apache License
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { ByteBuf content = Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content); response.headers().set(Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); ctx.write(response).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.dubbo.qos.server.handler.HttpProcessHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception { CommandContext commandContext = HttpCommandDecoder.decode(msg); // return 404 when fail to construct command context if (commandContext == null) { log.warn("can not found commandContext url: " + msg.getUri()); FullHttpResponse response = http404(); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else {/*from w w w.j a va2s .c o m*/ commandContext.setRemote(ctx.channel()); try { String result = commandExecutor.execute(commandContext); FullHttpResponse response = http200(result); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } catch (NoSuchCommandException ex) { log.error("can not find commandContext: " + commandContext, ex); FullHttpResponse response = http404(); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } catch (Exception qosEx) { log.error("execute commandContext: " + commandContext + " got exception", qosEx); FullHttpResponse response = http500(qosEx.getMessage()); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } } }
From source file:org.apache.dubbo.qos.server.handler.HttpProcessHandlerTest.java
License:Apache License
@Test public void test1() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); ChannelFuture future = mock(ChannelFuture.class); when(context.writeAndFlush(any(FullHttpResponse.class))).thenReturn(future); HttpRequest message = Mockito.mock(HttpRequest.class); when(message.getUri()).thenReturn("test"); HttpProcessHandler handler = new HttpProcessHandler(); handler.channelRead0(context, message); verify(future).addListener(ChannelFutureListener.CLOSE); ArgumentCaptor<FullHttpResponse> captor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(context).writeAndFlush(captor.capture()); FullHttpResponse response = captor.getValue(); assertThat(response.getStatus().code(), equalTo(404)); }
From source file:org.apache.dubbo.qos.server.handler.HttpProcessHandlerTest.java
License:Apache License
@Test public void test2() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); ChannelFuture future = mock(ChannelFuture.class); when(context.writeAndFlush(any(FullHttpResponse.class))).thenReturn(future); HttpRequest message = Mockito.mock(HttpRequest.class); when(message.getUri()).thenReturn("localhost:80/greeting"); when(message.getMethod()).thenReturn(HttpMethod.GET); HttpProcessHandler handler = new HttpProcessHandler(); handler.channelRead0(context, message); verify(future).addListener(ChannelFutureListener.CLOSE); ArgumentCaptor<FullHttpResponse> captor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(context).writeAndFlush(captor.capture()); FullHttpResponse response = captor.getValue(); assertThat(response.getStatus().code(), equalTo(200)); }
From source file:org.apache.dubbo.qos.server.handler.HttpProcessHandlerTest.java
License:Apache License
@Test public void test3() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); ChannelFuture future = mock(ChannelFuture.class); when(context.writeAndFlush(any(FullHttpResponse.class))).thenReturn(future); HttpRequest message = Mockito.mock(HttpRequest.class); when(message.getUri()).thenReturn("localhost:80/test"); when(message.getMethod()).thenReturn(HttpMethod.GET); HttpProcessHandler handler = new HttpProcessHandler(); handler.channelRead0(context, message); verify(future).addListener(ChannelFutureListener.CLOSE); ArgumentCaptor<FullHttpResponse> captor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(context).writeAndFlush(captor.capture()); FullHttpResponse response = captor.getValue(); assertThat(response.getStatus().code(), equalTo(404)); }
From source file:org.apache.dubbo.qos.server.handler.LocalHostPermitHandlerTest.java
License:Apache License
@Test public void testHandlerAdded() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); Channel channel = mock(Channel.class); when(context.channel()).thenReturn(channel); InetAddress addr = mock(InetAddress.class); when(addr.isLoopbackAddress()).thenReturn(false); InetSocketAddress address = new InetSocketAddress(addr, 12345); when(channel.remoteAddress()).thenReturn(address); ChannelFuture future = mock(ChannelFuture.class); when(context.writeAndFlush(any(ByteBuf.class))).thenReturn(future); LocalHostPermitHandler handler = new LocalHostPermitHandler(false); handler.handlerAdded(context);//from w w w. j ava 2 s . c o m ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class); verify(context).writeAndFlush(captor.capture()); assertThat(new String(captor.getValue().array()), containsString("Foreign Ip Not Permitted")); verify(future).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.dubbo.qos.server.handler.TelnetProcessHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { if (StringUtils.isBlank(msg)) { ctx.writeAndFlush(QosProcessHandler.prompt); } else {//from w w w. ja v a2s . c om CommandContext commandContext = TelnetCommandDecoder.decode(msg); commandContext.setRemote(ctx.channel()); try { String result = commandExecutor.execute(commandContext); if (StringUtils.isEquals(QosConstants.CLOSE, result)) { ctx.writeAndFlush(getByeLabel()).addListener(ChannelFutureListener.CLOSE); } else { ctx.writeAndFlush(result + QosConstants.BR_STR + QosProcessHandler.prompt); } } catch (NoSuchCommandException ex) { ctx.writeAndFlush(msg + " :no such command"); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("can not found command " + commandContext, ex); } catch (Exception ex) { ctx.writeAndFlush(msg + " :fail to execute commandContext by " + ex.getMessage()); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("execute commandContext got exception " + commandContext, ex); } } }
From source file:org.apache.dubbo.qos.server.handler.TelnetProcessHandlerTest.java
License:Apache License
@Test public void testBye() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); TelnetProcessHandler handler = new TelnetProcessHandler(); ChannelFuture future = mock(ChannelFuture.class); when(context.writeAndFlush("BYE!\n")).thenReturn(future); handler.channelRead0(context, "quit"); verify(future).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.flink.runtime.io.network.netty.PartitionRequestQueue.java
License:Apache License
private void handleException(Channel channel, Throwable cause) throws IOException { fatalError = true;//from w w w . ja va 2s . c o m releaseAllResources(); if (channel.isActive()) { channel.writeAndFlush(new NettyMessage.ErrorResponse(cause)).addListener(ChannelFutureListener.CLOSE); } }