Example usage for io.netty.channel ChannelHandlerContext flush

List of usage examples for io.netty.channel ChannelHandlerContext flush

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext flush.

Prototype

@Override
    ChannelHandlerContext flush();

Source Link

Usage

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPFramingEncoder.java

License:Apache License

@Override
public void flush(final ChannelHandlerContext ctx) throws Exception {
    if (messages == null) {
        return;//  w w w .java2  s. com
    }
    estimator.reset();
    for (final Object message : messages) {
        encoder.estimate(message, estimator);
    }
    final ByteBuf output = ctx.alloc().buffer(estimator.size());
    writer.reset(output);
    for (final Object message : messages) {
        encoder.encode(message, writer);
        ReferenceCountUtil.release(message);
    }
    final ChannelPromise aggregate = new AggregatePromise(ctx.channel(), promises);
    messages.clear();
    promises.clear();
    ctx.write(output, aggregate);
    ctx.flush();
}

From source file:com.topsec.bdc.platform.api.server.http.HttpSnoopServerHandler.java

License:Apache License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {

    ctx.flush();
}

From source file:com.twocater.diamond.core.test.DiscardServerHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf byteBuf = (ByteBuf) msg;/* w w  w. ja  v a2  s  .  c o m*/

    ctx.write(msg);
    ctx.flush();
    //        ReferenceCountUtil.release(msg);
}

From source file:com.weibo.api.motan.protocol.grpc.http.NettyHttpRequestHandler.java

License:Apache License

private void sendResponse(ChannelHandlerContext ctx, FullHttpResponse httpResponse) {
    boolean close = false;
    try {//  w w w  .j  a  v  a 2s  .co  m
        ctx.write(httpResponse);
        ctx.flush();
    } catch (Exception e) {
        LoggerUtil.error("NettyHttpHandler write response fail.", e);
        close = true;
    } finally {
        // close connection
        if (close || httpResponse == null || !HttpHeaderValues.KEEP_ALIVE
                .equals(httpResponse.headers().get(HttpHeaderNames.CONNECTION))) {
            ctx.close();
        }
    }
}

From source file:com.weibo.api.motan.transport.netty4.http.NettyHttpRequestHandler.java

License:Apache License

private void sendResponse(ChannelHandlerContext ctx, FullHttpResponse httpResponse) {
    boolean close = false;
    try {/*from w w w. j  a v a2s  .c o  m*/
        ctx.write(httpResponse);
        ctx.flush();
    } catch (Exception e) {
        LoggerUtil.error("NettyHttpHandler write response fail.", e);
        close = true;
    } finally {
        // close connection
        if (close || httpResponse == null
                || !Values.KEEP_ALIVE.equals(httpResponse.headers().get(HttpHeaders.Names.CONNECTION))) {
            ctx.close();
        }
    }
}

From source file:com.weibo.yar.yarserver.HttpServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {

    ByteBuf buf = msg.content();/*from  ww  w.ja va 2s .c  om*/
    byte[] bytes = new byte[buf.readableBytes()];
    buf.getBytes(0, bytes);

    YarRequest yarRequest = YarProtocol.buildRequest(bytes);
    YarResponse yarResponse = process(yarRequest);

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            Unpooled.wrappedBuffer(YarProtocol.toProtocolBytes(yarResponse)));
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
    if (HttpHeaders.isKeepAlive(msg)) {
        response.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE);
    }
    ctx.write(response);
    ctx.flush();
    ctx.close();
}

From source file:com.xxx.demo.netty.echo.EchoClientHandler.java

License:Apache License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    ctx.flush();
    ctx.close();
}

From source file:com.xxx.util.io.client.netty.EchoClientHandler.java

License:Apache License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    ctx.flush();
    if (!isLongConnect) {
        ctx.close();
    }
}

From source file:com.xx_dev.apn.proxy.ApnProxyPreHandler.java

License:Apache License

private boolean preCheck(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;

        String originalHost = HostNamePortUtil.getHostName(httpRequest);

        LoggerUtil.info(httpRestLogger, ctx.channel().remoteAddress().toString(),
                httpRequest.getMethod().name(), httpRequest.getUri(), httpRequest.getProtocolVersion().text(),
                httpRequest.headers().get(HttpHeaders.Names.USER_AGENT));

        isPacRequest = false;/*from w ww.ja v a2 s.  co  m*/

        // pac request
        if (StringUtils.equals(originalHost, ApnProxyConfig.getConfig().getPacHost())) {
            isPacRequest = true;

            String pacContent = null;
            if (ApnProxyConfig.getConfig().getListenType() == ApnProxyListenType.SSL) {
                pacContent = buildPacForSsl();
            } else {
                pacContent = buildPacForPlain();
            }

            ByteBuf pacResponseContent = Unpooled.copiedBuffer(pacContent, CharsetUtil.UTF_8);
            FullHttpMessage pacResponseMsg = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK, pacResponseContent);
            HttpHeaders.setContentLength(pacResponseMsg, pacResponseContent.readableBytes());
            HttpHeaders.setHeader(pacResponseMsg, "X-APN-PROXY-PAC", "OK");
            HttpHeaders.setHeader(pacResponseMsg, "X-APN-PROXY-URL", "https://github.com/apn-proxy/apn-proxy");
            HttpHeaders.setHeader(pacResponseMsg, "X-APN-PROXY-MSG", "We need more commiters!");

            ctx.write(pacResponseMsg);
            ctx.flush();
            return false;
        }

        // forbid request to proxy server internal network
        for (String forbiddenIp : forbiddenIps) {
            if (StringUtils.startsWith(originalHost, forbiddenIp)) {
                String errorMsg = "Forbidden";
                ctx.write(HttpErrorUtil.buildHttpErrorMessage(HttpResponseStatus.FORBIDDEN, errorMsg));
                ctx.flush();
                return false;
            }
        }

        // forbid request to proxy server local
        if (StringUtils.equals(originalHost, "127.0.0.1") || StringUtils.equals(originalHost, "localhost")) {
            String errorMsg = "Forbidden Host";
            ctx.write(HttpErrorUtil.buildHttpErrorMessage(HttpResponseStatus.FORBIDDEN, errorMsg));
            ctx.flush();
            return false;
        }

        // forbid reqeust to some port
        int originalPort = HostNamePortUtil.getPort(httpRequest);
        for (int fobiddenPort : forbiddenPorts) {
            if (originalPort == fobiddenPort) {
                String errorMsg = "Forbidden Port";
                ctx.write(HttpErrorUtil.buildHttpErrorMessage(HttpResponseStatus.FORBIDDEN, errorMsg));
                ctx.flush();
                return false;
            }
        }

    } else {
        if (isPacRequest) {
            return false;
        }
    }

    return true;
}

From source file:com.xx_dev.apn.socks.test.SocksClientHandler.java

License:Apache License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    super.channelReadComplete(ctx);
    ctx.flush();
}