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:org.tiger.netty.netty.demo1.TimeServerHandler.java

License:Apache License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    System.out.println("complemete....");
    ctx.flush();
}

From source file:org.waarp.openr66.proxy.protocol.http.adminssl.HttpSslHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
    FullHttpRequest request = this.request = msg;
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
    uriRequest = queryStringDecoder.path();
    logger.debug("Msg: " + uriRequest);
    if (uriRequest.contains("gre/") || uriRequest.contains("img/") || uriRequest.contains("res/")
            || uriRequest.contains("favicon.ico")) {
        HttpWriteCacheEnable.writeFile(request, ctx, Configuration.configuration.getHttpBasePath() + uriRequest,
                R66SESSION + Configuration.configuration.getHOST_ID());
        ctx.flush();
        return;//  ww w .  j a va2  s.co m
    }
    checkSession(ctx.channel());
    if (!authentHttp.isAuthenticated()) {
        logger.debug("Not Authent: " + uriRequest + ":{}", authentHttp);
        checkAuthent(ctx);
        return;
    }
    String find = uriRequest;
    if (uriRequest.charAt(0) == '/') {
        find = uriRequest.substring(1);
    }
    find = find.substring(0, find.indexOf("."));
    REQUEST req = REQUEST.index;
    try {
        req = REQUEST.valueOf(find);
    } catch (IllegalArgumentException e1) {
        req = REQUEST.index;
        logger.debug("NotFound: " + find + ":" + uriRequest);
    }
    switch (req) {
    case index:
        responseContent.append(index());
        break;
    case Logon:
        responseContent.append(index());
        break;
    case System:
        responseContent.append(System());
        break;
    default:
        responseContent.append(index());
        break;
    }
    writeResponse(ctx);
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.http2.Http2ResponseWriter.java

License:Open Source License

/**
 * writing a response on wire/*from   www  .  j a v  a  2 s.com*/
 *
 * @param synCtx
 * @throws AxisFault
 */
public void writeNormalResponse(MessageContext synCtx) throws AxisFault {
    org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) synCtx)
            .getAxis2MessageContext();
    Http2Headers transportHeaders = new DefaultHttp2Headers();
    InboundResponseSender responseSender = synCtx
            .getProperty(InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER) == null ? null
                    : (InboundHttp2ResponseSender) synCtx
                            .getProperty(InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER);
    try {
        sourceConfiguration = PassThroughInboundEndpointHandler.getPassThroughSourceConfiguration();
    } catch (Exception e) {
        throw new AxisFault("Error while building sourceConfiguration " + e);
    }

    //status
    try {
        int statusCode = PassThroughTransportUtils.determineHttpStatusCode(msgContext);
        if (statusCode > 0) {
            HttpResponseStatus status1 = HttpResponseStatus.valueOf(statusCode);
            transportHeaders.status(status1.codeAsText());
        }
    } catch (Exception e) {
        throw new AxisFault("Error occured while parsing response status", e);
    }
    //content_type
    Boolean noEntityBody = msgContext.getProperty(NhttpConstants.NO_ENTITY_BODY) != null
            ? (boolean) msgContext.getProperty(NhttpConstants.NO_ENTITY_BODY)
            : null;
    if (noEntityBody == null || Boolean.FALSE == noEntityBody) {
        Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
        if (pipe == null) {
            pipe = new Pipe(sourceConfiguration.getBufferFactory().getBuffer(), "Test", sourceConfiguration);
            msgContext.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe);
            msgContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
        }

        OMOutputFormat format = NhttpUtil.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = MessageFormatterDecoratorFactory
                .createMessageFormatterDecorator(msgContext);
        if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) == null) {
            transportHeaders.add(HttpHeaderNames.CONTENT_TYPE,
                    messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));
        }
    }

    if (transportHeaders != null
            && msgContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE) != null) {
        if (msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE) != null
                && msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE).toString()
                        .contains(PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED)) {
            transportHeaders.add(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE,
                    PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED);
        } else {
            Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
            if (pipe != null
                    && !Boolean.TRUE
                            .equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))
                    && msgContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE) != null) {
                transportHeaders.add(HttpHeaderNames.CONTENT_TYPE, msgContext
                        .getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE).toString());
            }
        }
    }

    //Excess headers
    String excessProp = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
    Map excessHeaders = msgContext.getProperty(excessProp) == null ? null
            : (Map) msgContext.getProperty(excessProp);
    if (excessHeaders != null) {
        for (Iterator iterator = excessHeaders.keySet().iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            for (String excessVal : (Collection<String>) excessHeaders.get(key)) {
                transportHeaders.add(key.toLowerCase(), (String) excessVal);
            }
        }
    }

    boolean hasBody = false;
    if (!transportHeaders.contains(HttpHeaderNames.CONTENT_TYPE)) {
        String contentType = null;
        try {
            contentType = new InboundMessageHandler(responseSender, config).getContentType(msgContext);
        } catch (Exception e) {
            throw new AxisFault("Error while parsing content type", e);
        }
        if (contentType != null) {
            transportHeaders.add(HttpHeaderNames.CONTENT_TYPE, contentType);
            hasBody = true;
        }
    } else
        hasBody = true;

    int streamId = (int) synCtx.getProperty("stream-id");
    ChannelHandlerContext c = (ChannelHandlerContext) synCtx.getProperty("stream-channel");
    if (c == null) {
        c = chContext;
    }
    ChannelPromise promise = c.newPromise();
    if (hasBody) {
        Pipe pipe = msgContext.getProperty("pass-through.pipe") == null ? null
                : (Pipe) msgContext.getProperty("pass-through.pipe");
        encoder.writeHeaders(c, streamId, transportHeaders, 0, false, promise);
        http2Encoder pipeEncoder = new http2Encoder(c, streamId, encoder, c.newPromise());
        if (pipe != null) {
            pipe.attachConsumer(new Http2CosumerIoControl());
            try {
                if (Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(msgContext);
                    OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
                    formatter.writeTo(msgContext, format, out, false);
                    OutputStream _out = pipe.getOutputStream();
                    IOUtils.write(out.toByteArray(), _out);
                }
                int t = pipe.consume(pipeEncoder);
                if (t < 1)
                    throw new AxisFault("Pipe consuming failed");
            } catch (Exception e) {
                throw new AxisFault("Error while writing built message back to pipe", e);
            }
        }
        c.flush();
    } else {
        encoder.writeHeaders(c, streamId, transportHeaders, 0, true, promise);
        c.flush();
    }
}

From source file:org.wso2.gw.emulator.http.consumer.HttpResponseProcessHandler.java

License:Open Source License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    if (httpResponseProcessor != null) {
        waitingDelay(consumerContext.getWritingDelay());
        this.httpResponseProcessor.process(httpRequestContext, ctx);
    }/*from   w ww .j  av a 2s.  c o  m*/
    ctx.flush();
}

From source file:org.wso2.gw.emulator.http.server.handler.HttpServerHandler.java

License:Open Source License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    if (httpResponseProcessor != null) {
        // waitingDelay(consumerContext.getWritingDelay());
        this.httpResponseProcessor.process(httpProcessorContext);
        FullHttpResponse response = httpProcessorContext.getFinalResponse();
        if (httpProcessorContext.getHttpRequestContext().isKeepAlive()) {
            ctx.write(response);/* w w  w.  j  ava  2s.  co m*/
        } else {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        }
    }
    ctx.flush();
}

From source file:org.wyb.smtp.mosmtp.SmtpServerHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    // Send greeting for a new connection.
    ctx.write("220 " + InetAddress.getLocalHost().getHostName() + " at your service.\r\n");
    ctx.flush();
}

From source file:p2p_server.AppHandler.java

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

    ctx.flush();
}

From source file:reactor.io.net.impl.netty.http.NettyHttpWSClientHandler.java

License:Open Source License

@Override
protected void doOnTerminate(ChannelHandlerContext ctx, ChannelFuture last, final ChannelPromise promise) {
    if (ctx.channel().isOpen()) {
        ChannelFutureListener listener = new ChannelFutureListener() {
            @Override/* w  w w  .  java  2  s.co m*/
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    promise.trySuccess();
                } else {
                    promise.tryFailure(future.cause());
                }
            }
        };

        if (last != null) {
            ctx.flush();
            last.addListener(listener);
        } else {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(listener);
        }
    } else {
        promise.trySuccess();
    }
}

From source file:reactor.io.net.impl.netty.NettyChannelHandlerBridge.java

License:Apache License

protected void doOnTerminate(ChannelHandlerContext ctx, ChannelFuture last, final ChannelPromise promise) {
    if (ctx.channel().isOpen()) {
        ChannelFutureListener listener = new ChannelFutureListener() {
            @Override/*from   w w  w . j  ava2 s.co m*/
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    promise.trySuccess();
                } else {
                    promise.tryFailure(future.cause());
                }
            }
        };

        if (last != null) {
            ctx.flush();
            last.addListener(listener);
        } else {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(listener);
        }
    } else {
        promise.trySuccess();
    }
}

From source file:reactor.io.net.netty.NettyNetChannelOutboundHandler.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from   ww w.j  av  a2s  .  c  om*/
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (Tuple2.class.isInstance(msg)) {
        Tuple2<Object, Boolean> tup = (Tuple2<Object, Boolean>) msg;
        if (null != tup.getT1()) {
            super.write(ctx, tup.getT1(), promise);
        }
        if (tup.getT2()) {
            ctx.flush();
        }
    } else {
        super.write(ctx, msg, promise);
        ctx.flush();
    }
}