Search in sources :

Example 26 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project netty-socketio by mrniko.

the class PollingTransport method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;
        QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
        List<String> transport = queryDecoder.parameters().get("transport");
        if (transport != null && NAME.equals(transport.get(0))) {
            List<String> sid = queryDecoder.parameters().get("sid");
            List<String> j = queryDecoder.parameters().get("j");
            List<String> b64 = queryDecoder.parameters().get("b64");
            String origin = req.headers().get(HttpHeaderNames.ORIGIN);
            ctx.channel().attr(EncoderHandler.ORIGIN).set(origin);
            String userAgent = req.headers().get(HttpHeaderNames.USER_AGENT);
            ctx.channel().attr(EncoderHandler.USER_AGENT).set(userAgent);
            if (j != null && j.get(0) != null) {
                Integer index = Integer.valueOf(j.get(0));
                ctx.channel().attr(EncoderHandler.JSONP_INDEX).set(index);
            }
            if (b64 != null && b64.get(0) != null) {
                Integer enable = Integer.valueOf(b64.get(0));
                ctx.channel().attr(EncoderHandler.B64).set(enable == 1);
            }
            try {
                if (sid != null && sid.get(0) != null) {
                    final UUID sessionId = UUID.fromString(sid.get(0));
                    handleMessage(req, sessionId, queryDecoder, ctx);
                } else {
                    // first connection
                    ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
                    handleMessage(req, client.getSessionId(), queryDecoder, ctx);
                }
            } finally {
                req.release();
            }
            return;
        }
    }
    ctx.fireChannelRead(msg);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ClientHead(com.corundumstudio.socketio.handler.ClientHead) UUID(java.util.UUID)

Example 27 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project netty-socketio by mrniko.

the class WebSocketTransport method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof CloseWebSocketFrame) {
        ctx.channel().close();
        ReferenceCountUtil.release(msg);
    } else if (msg instanceof BinaryWebSocketFrame || msg instanceof TextWebSocketFrame) {
        ByteBufHolder frame = (ByteBufHolder) msg;
        ClientHead client = clientsBox.get(ctx.channel());
        if (client == null) {
            log.debug("Client with was already disconnected. Channel closed!");
            ctx.channel().close();
            frame.release();
            return;
        }
        ctx.pipeline().fireChannelRead(new PacketsMessage(client, frame.content(), Transport.WEBSOCKET));
        frame.release();
    } else if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;
        QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
        String path = queryDecoder.path();
        List<String> transport = queryDecoder.parameters().get("transport");
        List<String> sid = queryDecoder.parameters().get("sid");
        if (transport != null && NAME.equals(transport.get(0))) {
            try {
                if (!configuration.getTransports().contains(Transport.WEBSOCKET)) {
                    log.debug("{} transport not supported by configuration.", Transport.WEBSOCKET);
                    ctx.channel().close();
                    return;
                }
                if (sid != null && sid.get(0) != null) {
                    final UUID sessionId = UUID.fromString(sid.get(0));
                    handshake(ctx, sessionId, path, req);
                } else {
                    ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
                    // first connection
                    handshake(ctx, client.getSessionId(), path, req);
                }
            } finally {
                req.release();
            }
        } else {
            ctx.fireChannelRead(msg);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) PacketsMessage(com.corundumstudio.socketio.messages.PacketsMessage) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ByteBufHolder(io.netty.buffer.ByteBufHolder) ClientHead(com.corundumstudio.socketio.handler.ClientHead) UUID(java.util.UUID)

Example 28 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project hadoop by apache.

the class FSImageHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    if (request.getMethod() != HttpMethod.GET) {
        DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
        resp.headers().set(CONNECTION, CLOSE);
        ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
        return;
    }
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
    final String op = getOp(decoder);
    final String content;
    String path = getPath(decoder);
    switch(op) {
        case "GETFILESTATUS":
            content = image.getFileStatus(path);
            break;
        case "LISTSTATUS":
            content = image.listStatus(path);
            break;
        case "GETACLSTATUS":
            content = image.getAclStatus(path);
            break;
        case "GETXATTRS":
            List<String> names = getXattrNames(decoder);
            String encoder = getEncoder(decoder);
            content = image.getXAttrs(path, names, encoder);
            break;
        case "LISTXATTRS":
            content = image.listXAttrs(path);
            break;
        case "GETCONTENTSUMMARY":
            content = image.getContentSummary(path);
            break;
        default:
            throw new IllegalArgumentException("Invalid value for webhdfs parameter" + " \"op\"");
    }
    LOG.info("op=" + op + " target=" + path);
    DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(content.getBytes(Charsets.UTF_8)));
    resp.headers().set(CONTENT_TYPE, APPLICATION_JSON_UTF8);
    resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
    resp.headers().set(CONNECTION, CLOSE);
    ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse)

Example 29 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project asterixdb by apache.

the class BaseRequest method create.

public static IServletRequest create(FullHttpRequest request) throws IOException {
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    Map<String, List<String>> param = decoder.parameters();
    return new BaseRequest(request, param);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) List(java.util.List)

Aggregations

QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)29 List (java.util.List)11 Test (org.junit.Test)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)5 HttpContent (io.netty.handler.codec.http.HttpContent)5 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)3 Map (java.util.Map)3 UUID (java.util.UUID)3 ClientHead (com.corundumstudio.socketio.handler.ClientHead)2 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2