Example usage for io.netty.channel ChannelHandlerContext read

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

Introduction

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

Prototype

@Override
    ChannelHandlerContext read();

Source Link

Usage

From source file:io.urmia.api.handler.RestApiHandler.java

License:Open Source License

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

    if (msg instanceof HttpRequest) {

        if (requestState != RequestState.EXPECT_REQUEST) {
            sendError(ctx, ERROR_BAD_REQUEST);
            return;
        }//w  w  w.j  a  v  a 2 s  .c o m

        request = (HttpRequest) msg;

        log.info("received HttpRequest: {} {}", request.getMethod(), request.getUri());

        final ObjectRequest r;

        try {
            r = new ObjectRequest(request);
        } catch (ExceptionInInitializerError e) {
            log.warn("unable to parse the request into a command: {}", request.getUri());
            return;
        }

        objectRequest = r;
        httpRequest = request;

        if (HttpMethod.PUT.equals(request.getMethod())) {
            if (objectRequest.isNotDirectory()) // make dir request
                setupProxyToPUT(ctx, objectRequest);

            requestState = RequestState.EXPECT_CONTENT_OR_LAST;
        } else {
            requestState = RequestState.EXPECT_LAST;
            ctx.read();
        }

        return;
    }

    if (objectRequest == null) {
        String uri = request == null ? "" : request.getUri();
        log.warn("not expecting an empty objectRequest. parse error maybe: {}", uri);
        ByteBuf body = request == null || request.getMethod().equals(HttpMethod.HEAD) ? null
                : errorBody("ResourceNotFoundError", uri + " does not exist");
        sendError(ctx, HttpResponseStatus.NOT_FOUND, body);
        return;
    }

    if (msg instanceof HttpContent) {

        if (requestState != RequestState.EXPECT_LAST && requestState != RequestState.EXPECT_CONTENT_OR_LAST) {
            log.warn("not expecting LAST or CONTENT, requestState: {}", requestState);
            sendError(ctx, HttpResponseStatus.NOT_EXTENDED);
            return;
        }

        final boolean last = msg instanceof LastHttpContent;
        final boolean emptyLast = last && msg == LastHttpContent.EMPTY_LAST_CONTENT;

        if (proxyMode && !emptyLast) // todo: the emptyLast was added for mln
            ctx.fireChannelRead(msg);

        // example of reading only if at the end
        if (last) {

            log.debug("received LastHttpContent: {}", msg);
            requestState = RequestState.EXPECT_REQUEST;

            final HttpRequest request = httpRequest;

            if (HttpMethod.HEAD.equals(request.getMethod())) {
                handleHEAD(ctx, objectRequest);
                return;
            }

            if (HttpMethod.GET.equals(request.getMethod())) {
                handleGET(ctx, objectRequest);
                return;
            }

            if (HttpMethod.DELETE.equals(request.getMethod())) {
                handleDELETE(ctx, objectRequest);
                return;
            }

            if (HttpMethod.PUT.equals(request.getMethod())) {
                if (proxyMode)
                    log.info("finished file upload: {}", objectRequest);
                else
                    handlePUTmkdir(ctx, objectRequest);
                return;
            }

            log.warn("unknown request: {}", request);
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        }

        return;
    }

    log.warn("unexpected msg type: {}", msg);
    sendError(ctx, HttpResponseStatus.BAD_REQUEST);
}

From source file:io.urmia.api.handler.RestApiHandler.java

License:Open Source License

private void setupProxyToGET(final ChannelHandlerContext ctx, final FullObjectName fon) throws Exception {

    log.info("proxy GET storage node: download mode: {}", true);

    final Future<List<String>> futureStorageNodes = mds.storedNodes(ctx.executor(), fon.attributes.etag);

    futureStorageNodes.addListener(new GenericFutureListener<Future<List<String>>>() {
        @Override//  ww w . ja  v a  2 s  .co m
        public void operationComplete(Future<List<String>> future) throws Exception {
            if (future.isSuccess()) {
                final List<String> st = future.getNow();

                Optional<ServiceInstance<NodeType>> si = findStorageInstanceUp(st);

                if (!si.isPresent()) {
                    log.error("failed to find running node, req: {}", objectRequest);
                    sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                    return;
                }

                List<ServiceInstance<NodeType>> l = Lists.newArrayList(si.get());
                log.info("proxy storage node: {}, download mode: {}, durability: {}", st, true,
                        objectRequest.getDurability());
                HttpProxyFrontendHandler proxy = new HttpProxyFrontendHandler(l, mds, httpRequest, ctx, true,
                        Optional.<FullObjectName>absent());

                ctx.pipeline().remove("encoder");

                ctx.pipeline().addLast("proxy", proxy);

                proxyMode = true;

                ctx.read(); // todo: can ve removed?

            } else {
                log.error("failed to fetch futureStorageNodes, req: {}", objectRequest, future.cause());
                sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
            }
        }
    });

}

From source file:io.urmia.proxy.DirectWriteBackHttpProxyBackendHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    log.info("backend active: {}", ctx);
    ctx.read();
    ctx.write(Unpooled.EMPTY_BUFFER);//from w  ww.java 2  s.c o  m
}

From source file:io.urmia.proxy.HttpProxyBackendHandler.java

License:Open Source License

@Override
public void channelRead0(final ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    //log.info("backend read. direct write back: {}. writing to inbound: {}", directWriteBack, msg);

    if (msg instanceof HttpResponse) {
        httpResponse = (HttpResponse) msg;
        log.info("backend http response: {}, status code: {}", httpResponse, httpResponse.getStatus().code());
        ctx.channel().read();/*from w w w .j a v  a 2 s . c o  m*/
        return;
    }

    if (httpResponse == null) {
        log.error("httpResponse is null when received: {}", msg);
        inboundCtx.pipeline().fireUserEventTriggered(new ProxyUserEvent(OUTBOUND_ERROR, index));
        return;
    }

    if (!(msg instanceof LastHttpContent)) {
        log.error("input message is not supported: {}", msg);
        inboundCtx.pipeline().fireUserEventTriggered(new ProxyUserEvent(OUTBOUND_ERROR, index));
        return;
    }

    if (httpResponse.getStatus().equals(HttpResponseStatus.CONTINUE)) {
        log.info("continue from client.");
        ProxyUserEvent pevt = new ProxyUserEvent(OUTBOUND_CONTINUE, index);
        log.info("fire user event: {}", pevt);
        inboundCtx.pipeline().fireUserEventTriggered(pevt);
        ctx.read();

        return;
    }

    ProxyUserEvent pevt = new ProxyUserEvent(OUTBOUND_COMPLETED, index);
    log.info("fire user event: {}", pevt);
    inboundCtx.pipeline().fireUserEventTriggered(pevt);
    ctx.read();
}

From source file:io.urmia.st.StorageServerHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);
    ctx.read();
}

From source file:io.urmia.st.StorageServerHandler.java

License:Open Source License

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

    if (msg instanceof HttpRequest) {

        requestStartMS = System.currentTimeMillis();

        request = (HttpRequest) msg;/*from w ww .j a va 2  s  .  c  om*/

        log.info("received HttpRequest: {} {}", request.getMethod(), request.getUri());

        // todo: what's this?
        //if (is100ContinueExpected(request)) {
        //    send100Continue(ctx);
        //}

        if (HttpMethod.PUT.equals(request.getMethod())) {
            // start doing the fs put. next msg is not a LastHttpContent
            handlePUT(ctx, request);
            //return;
        }

        ctx.read();
        return;
    }

    if (msg instanceof HttpContent) {

        // New chunk is received
        HttpContent chunk = (HttpContent) msg;

        //log.info("chunk {} of size: {}", chunk, chunk.content().readableBytes());

        if (fileChannel != null) {
            writeToFile(chunk.content());
        }

        // example of reading only if at the end
        if (chunk instanceof LastHttpContent) {

            log.trace("received LastHttpContent: {}", chunk);

            if (HttpMethod.HEAD.equals(request.getMethod())) {
                handleHEAD(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.GET.equals(request.getMethod())) {
                handleGET(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.DELETE.equals(request.getMethod())) {
                handleDELETE(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.PUT.equals(request.getMethod())) {
                // TODO: reset() if exception catch or timeout (i.e. no LastHttpContent)
                writeResponse(ctx, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK),
                        true); // close the connection after upload (mput) done
                reset();
                access.success(ctx, "PUT", uri, requestStartMS);
                ctx.read();
                return;
            }

            log.warn("unknown request: {}", request);
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        }

        ctx.read();
        return;
    }

    log.warn("unknown msg type: {}", msg);
}

From source file:malcolm.HttpProxyBackendHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    // TODO what does this to?
    ctx.read();
    ctx.write(Unpooled.EMPTY_BUFFER);//  ww  w. j a  v a 2s .  c o  m
}

From source file:me.bigteddy98.mcproxy.protocol.handlers.ServerSideHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    networkManager.serversidePipeline = ctx.pipeline();
    ctx.read();
    ctx.write(Unpooled.EMPTY_BUFFER);//  ww  w.jav  a  2  s  .c o  m
}

From source file:me.bigteddy98.movingmotd.ServerSideConnection.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.read();
    ctx.write(Unpooled.EMPTY_BUFFER);//w w w . j a  va 2s . c  om
    this.networkManager.serversidePipeline = ctx.pipeline();
}

From source file:org.apache.hyracks.http.server.HttpRequestCapacityController.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    if (overloaded()) {
        reject(ctx);/* www.  ja v a 2 s .com*/
        return;
    }
    // We disable auto read to avoid reading at all if we can't handle any more requests
    ctx.read();
    super.channelActive(ctx);
}