Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

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

Introduction

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

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:io.crate.protocols.http.HttpAuthUpstreamHandler.java

private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest request) {
    SSLSession session = getSession(ctx.channel());
    Tuple<String, SecureString> credentials = credentialsFromRequest(request, session, settings);
    String username = credentials.v1();
    SecureString password = credentials.v2();
    InetAddress address = addressFromRequestOrChannel(request, ctx.channel());
    ConnectionProperties connectionProperties = new ConnectionProperties(address, Protocol.HTTP, session);
    AuthenticationMethod authMethod = authService.resolveAuthenticationType(username, connectionProperties);
    if (authMethod == null) {
        String errorMessage = String.format(Locale.ENGLISH,
                "No valid auth.host_based.config entry found for host \"%s\", user \"%s\", protocol \"%s\"",
                address.getHostAddress(), username, Protocol.HTTP.toString());
        sendUnauthorized(ctx.channel(), errorMessage);
    } else {//from  www .  j av  a 2 s . co m
        try {
            User user = authMethod.authenticate(username, password, connectionProperties);
            if (user != null && LOGGER.isTraceEnabled()) {
                LOGGER.trace("Authentication succeeded user \"{}\" and method \"{}\".", username,
                        authMethod.name());
            }
            authorized = true;
            ctx.fireChannelRead(request);
        } catch (Exception e) {
            sendUnauthorized(ctx.channel(), e.getMessage());
        }
    }
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandler.java

private void handleHttpChunk(ChannelHandlerContext ctx, HttpContent msg) {
    if (authorized) {
        ctx.fireChannelRead(msg);
    } else {/*w  w w. j a  v a2s  . c o m*/
        // We won't forward the message downstream, thus we have to release
        msg.release();
        sendUnauthorized(ctx.channel(), null);
    }
}

From source file:io.crate.protocols.http.HttpBlobHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest request = currentMessage = (HttpRequest) msg;
        String uri = request.uri();

        if (!uri.startsWith(BLOBS_ENDPOINT)) {
            reset();//  w w w.  j  a  v a  2 s.  c  o m
            ctx.fireChannelRead(msg);
            return;
        }

        Matcher matcher = blobsMatcher.reset(uri);
        if (!matcher.matches()) {
            simpleResponse(HttpResponseStatus.NOT_FOUND);
            reset();
            return;
        }
        digestBlob = null;
        index = BlobIndex.fullIndexName(matcher.group(1));
        digest = matcher.group(2);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("matches index:{} digest:{}", index, digest);
            LOGGER.trace("HTTPMessage:%n{}", request);
        }
        handleBlobRequest(request, null);
    } else if (msg instanceof HttpContent) {
        if (currentMessage == null) {
            // the chunk is probably from a regular non-blob request.
            ctx.fireChannelRead(msg);
            return;
        }

        handleBlobRequest(currentMessage, (HttpContent) msg);
    } else {
        // Neither HttpMessage or HttpChunk
        ctx.fireChannelRead(msg);
    }
}

From source file:io.gatling.http.client.impl.ChunkedInboundHttp2ToHttpAdapter.java

License:Apache License

@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream)
        throws Http2Exception {
    Http2Stream stream = connection.stream(streamId);

    if (stream == null) {
        throw connectionError(PROTOCOL_ERROR, "Data Frame received for unknown stream id " + streamId);
    }/*from w  w w  .  j  a  va  2  s. c om*/

    final int processedBytes = data.readableBytes();

    HttpContent content = endOfStream ? new DefaultLastHttpContent(data) : new DefaultHttpContent(data);
    ctx.fireChannelRead(new Http2Content(content, streamId));
    return processedBytes + padding;
}

From source file:io.gatling.http.client.impl.ChunkedInboundHttp2ToHttpAdapter.java

License:Apache License

private void convertAndFire(ChannelHandlerContext ctx, int streamId, Http2Headers headers, boolean endOfStream)
        throws Http2Exception {
    HttpResponse response = HttpConversionUtil.toHttpResponse(streamId, headers, validateHttpHeaders);
    ctx.fireChannelRead(response);
    if (endOfStream) {
        ctx.fireChannelRead(new Http2Content(LastHttpContent.EMPTY_LAST_CONTENT, streamId));
    }/*from w ww  . j  av  a  2 s  .  c  o  m*/
}

From source file:io.gatling.http.client.impl.ChunkedInboundHttp2ToHttpAdapter.java

License:Apache License

@Override
public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) {

    if (!whenHttp2Handshake.isDone()) {
        whenHttp2Handshake.setSuccess(ctx.channel());
    }/*from   w  ww.  j av a  2 s.c o m*/

    if (propagateSettings) {
        ctx.fireChannelRead(settings);
    }
}

From source file:io.gatling.http.client.impl.DigestAuthHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    if (digestHeader == null) {
        // initial state
        if (msg instanceof HttpResponse) {
            HttpResponse response = (HttpResponse) msg;
            if (response.status() == HttpResponseStatus.UNAUTHORIZED) {
                String authenticateHeader = getHeaderWithPrefix(response.headers().getAll(WWW_AUTHENTICATE),
                        "Digest");
                if (authenticateHeader != null) {
                    digestHeader = realm.computeAuthorizationHeader(tx.request.getMethod(), tx.request.getUri(),
                            authenticateHeader);
                    return;
                }/* w w  w  . j  a  v a  2s . c  o  m*/
            }
        }

        // nothing this handler can do about it
        // either we don't need authentication, or auth scheme is not Digest
        ctx.fireChannelRead(msg);

    } else if (msg instanceof LastHttpContent) {
        // send new request
        // FIXME make sure connection can be reused, otherwise use a new one
        // FIXME check what happens if buildRequest throws
        WritableRequest request = WritableRequestBuilder.buildRequest(tx.request, ctx.alloc(), config, false);
        request.getRequest().headers().add(AUTHORIZATION, digestHeader);

        // FIXME write can throw Exception!!!
        request.write(ctx);
        ctx.pipeline().remove(this);
    }
    // initial response chunks are just ignored
}

From source file:io.hekate.network.netty.NettyClientTimeoutHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    // Ignore heartbeats.
    if (msg instanceof Heartbeat) {
        if (trace) {
            log.trace("Received heartbeat from server [from={}, message={}]", id, msg);
        }//w w  w  .  jav a 2s.  c o  m

        return;
    }

    ctx.fireChannelRead(msg);
}

From source file:io.hydramq.core.net.netty.CompletableFutureHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Command command) throws Exception {
    if (command instanceof Response) {
        CompletableFuture<Command> future = ctx.channel().attr(ChannelAttributes.COMMAND_FUTURES).get()
                .remove(command.correlationId());
        if (future != null) {
            future.complete(command);//from  w  w  w.  j  a  v  a2  s  .  c o  m
        } else {
            throw new HydraRuntimeException(
                    "Unexpected Response " + command + ". correlationId: " + command.correlationId());
        }
    } else {
        ctx.fireChannelRead(command);
    }
}

From source file:io.hydramq.network.server.ProtocolSelector.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    ByteBuf buffer = (ByteBuf) msg;// w w  w. j  av a2 s.c  o  m
    for (ProtocolHandler protocolHandler : protocols) {
        if (protocolHandler.accept(buffer)) {
            logger.info("Loading {} protocol handler", protocolHandler);
            ctx.pipeline().addLast("commandEncoder",
                    new CommandEncoder(protocolHandler.getConversionContext()));
            ctx.pipeline().addLast("commandDecoder",
                    new CommandDecoder(protocolHandler.getConversionContext()));
            ctx.pipeline().addLast("protocol", protocolHandler);
            ctx.pipeline().addLast("responseHandler", completableFutureHandler);
            ctx.pipeline().remove(this);
            ctx.pipeline().fireChannelActive();
            ctx.fireChannelRead(msg);
            return;
        }
    }
    logger.error("No acceptable protocols found to handle client with protocol id of " + buffer.getInt(0));
    ctx.writeAndFlush(new Error(buffer.getInt(1), 5)).addListener(ChannelFutureListener.CLOSE);
}