Example usage for io.netty.channel ChannelFutureListener CLOSE

List of usage examples for io.netty.channel ChannelFutureListener CLOSE

Introduction

In this page you can find the example usage for io.netty.channel ChannelFutureListener CLOSE.

Prototype

ChannelFutureListener CLOSE

To view the source code for io.netty.channel ChannelFutureListener CLOSE.

Click Source Link

Document

A ChannelFutureListener that closes the Channel which is associated with the specified ChannelFuture .

Usage

From source file:it.jnrpe.net.JNRPEServerHandler.java

License:Apache License

/**
 * Method channelRead.//  www.ja  v a 2  s  .co  m
 * @param ctx ChannelHandlerContext
 * @param msg Object
 * @see io.netty.channel.ChannelInboundHandler#channelRead(ChannelHandlerContext, Object)
 */
@Override
public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    try {
        JNRPERequest req = (JNRPERequest) msg;

        ReturnValue ret = commandInvoker.invoke(req.getCommand(), req.getArguments());

        JNRPEResponse res = new JNRPEResponse();
        res.setPacketVersion(PacketVersion.VERSION_2);

        res.setResultCode(ret.getStatus().intValue());
        res.setMessage(ret.getMessage());

        ChannelFuture channelFuture = ctx.writeAndFlush(res);
        channelFuture.addListener(ChannelFutureListener.CLOSE);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:jj.http.server.EngineHttpHandlerTest.java

License:Apache License

@Test
public void testExceptionCaught() throws Exception {

    given(ctx.writeAndFlush(any())).willReturn(channelFuture);

    Throwable t = new Throwable();
    handler.exceptionCaught(ctx, t);//from  ww  w . j a va  2s  .  co m

    // validate that the exception is logged because we
    // care about that
    verify(publisher).publish(isA(Emergency.class));
    verifyNoMoreInteractions(publisher);

    verify(ctx).writeAndFlush(responseCaptor.capture());

    FullHttpResponse response = responseCaptor.getValue();

    assertThat(response.getStatus(), is(HttpResponseStatus.INTERNAL_SERVER_ERROR));

    verify(channelFuture).addListener(futureListenerCaptor.capture());

    assertThat(futureListenerCaptor.getValue(), is(ChannelFutureListener.CLOSE));
}

From source file:jj.http.server.HttpRequestListeningHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    this.request = request;

    if (request.getDecoderResult().isFailure()) {
        // respond with BAD_REQUEST and close the connection
        // (unless we are being proxied and the connection is keep-alive, that is)
        BAD_REQUEST.writeAndFlush(ctx).addListener(ChannelFutureListener.CLOSE);

    } else if (methodHandlers.containsKey(request.getMethod())) {

        HttpMethodHandler methodHandler = methodHandlers.get(request.getMethod()).get();
        methodHandler.request(request);/*from   w  w w.  jav a  2  s.c o  m*/

        ChannelPipeline p = ctx.pipeline();
        p.addAfter(name, "method handler", methodHandler);

        ctx.fireChannelRead(request);

    } else {
        // respond with NOT_IMPLEMENTED
        // unless we are being proxied and the connection is keep-alive
        NOT_IMPLEMENTED.writeAndFlush(ctx).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:jj.http.server.HttpRequestListeningHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    publisher.publish(new RequestErrored(request, cause));
    INTERNAL_SERVER_ERROR.writeAndFlush(ctx).addListener(ChannelFutureListener.CLOSE);
}

From source file:jj.http.server.HttpServerResponseImpl.java

License:Apache License

private ChannelFuture maybeClose(final ChannelFuture f) {
    if (!HttpHeaders.isKeepAlive(request.request())) {
        f.addListener(ChannelFutureListener.CLOSE);
    }//from   ww w  . j a v  a  2  s .co m

    publisher.publish(new RequestResponded(request, this));

    return f;
}

From source file:jj.http.server.methods.GetMethodHandler.java

License:Apache License

@Override
protected void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest request) {
    // shoopy doopy doo

    ctx.writeAndFlush(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK))
            .addListener(ChannelFutureListener.CLOSE);
}

From source file:jj.http.server.methods.OptionsMethodHandler.java

License:Apache License

@Override
protected void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest request) {
    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    StringBuilder sb = new StringBuilder();
    if ("*".equals(request.getUri())) {
        for (HttpMethod method : methodHandlers.keySet()) {
            sb.append(method).append(",");
        }/*from   w  ww.ja  va  2s  . co m*/
        sb.deleteCharAt(sb.length() - 1);
    } else {
        for (HttpMethod method : router.matchURI(OPTIONS, new URIMatch(request.getUri())).routes.keySet()) {
            sb.append(method).append(",");
        }
        sb.append(HEAD).append(",").append(TRACE).append(",").append(OPTIONS);
    }
    response.headers().set(HttpHeaders.Names.ALLOW, sb.toString());
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

From source file:jj.http.server.websocket.WebSocketConnectionMakerTest.java

License:Apache License

@Test
public void testObseleteWebSocketConnectionHost() throws Exception {

    // given//from   ww  w  . j a  v  a2s  .com
    given(ctx.channel()).willReturn(channel);
    given(handshakerFactory.newHandshaker(request)).willReturn(handshaker);
    given(handshaker.handshake(channel, request)).willReturn(channelFuture);

    // when
    wscm.handshakeWebsocket();

    // then
    verify(channelFuture).addListener(futureListenerCaptor.capture());

    // given
    String uri = "/1234567890123456789012345678901234567890/uri.socket";
    given(request.getUri()).willReturn(uri);
    given(scriptEnvironment.sha1()).willReturn("ABCDEF");
    given(resourceFinder.findResource(eq(DocumentScriptEnvironment.class), eq(AppLocation.Virtual),
            anyString())).willReturn(scriptEnvironment);
    given(channelFuture.isSuccess()).willReturn(true);

    // when
    futureListenerCaptor.getValue().operationComplete(channelFuture);

    // then
    verify(ctx).writeAndFlush(textFrameCaptor.capture());

    assertThat(textFrameCaptor.getValue().text(), is("jj-reload"));
    verify(ctx.writeAndFlush(textFrameCaptor.getValue())).addListener(futureListenerCaptor.capture());

    // resetting ctx here to eliminate the earlier verifications we've already done
    // so our close frame capture works
    reset(ctx);

    // when
    futureListenerCaptor.getValue().operationComplete(channelFuture);

    // then
    verify(ctx).writeAndFlush(closeFrameCaptor.capture());
    assertThat(closeFrameCaptor.getValue().statusCode(), is(1001));
    verify(ctx.writeAndFlush(closeFrameCaptor.getValue())).addListener(ChannelFutureListener.CLOSE);
}

From source file:jj.http.server.websocket.WebSocketConnectionTest.java

License:Apache License

@Test
public void testClose() {

    connection.close();//from   ww w . j  a  v a 2  s.c  om

    verify(ctx).writeAndFlush(closeFrameCaptor.capture());

    assertThat(closeFrameCaptor.getValue().statusCode(), is(1000));

    verify(ctx.writeAndFlush(any())).addListener(ChannelFutureListener.CLOSE);
}

From source file:jlibs.wamp4j.netty.NettyWebSocket.java

License:Apache License

@Override
public void close() {
    ctx.writeAndFlush(new CloseWebSocketFrame()).addListener(ChannelFutureListener.CLOSE);
}