Search in sources :

Example 41 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project sidewinder by srotya.

the class HTTPDataPointDecoder method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        if (ResourceMonitor.getInstance().isReject()) {
            logger.warning("Write rejected, insufficient memory");
            if (writeResponse(request, ctx)) {
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
            return;
        }
        if (msg instanceof HttpRequest) {
            HttpRequest request = this.request = (HttpRequest) msg;
            if (HttpUtil.is100ContinueExpected(request)) {
                send100Continue(ctx);
            }
            QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
            path = queryStringDecoder.path();
            Map<String, List<String>> params = queryStringDecoder.parameters();
            if (!params.isEmpty()) {
                for (Entry<String, List<String>> p : params.entrySet()) {
                    String key = p.getKey();
                    if (key.equalsIgnoreCase("db")) {
                        dbName = p.getValue().get(0);
                    }
                }
            }
            if (path != null && path.contains("query")) {
                Gson gson = new Gson();
                JsonObject obj = new JsonObject();
                JsonArray ary = new JsonArray();
                ary.add(new JsonObject());
                obj.add("results", ary);
                responseString.append(gson.toJson(obj));
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf byteBuf = httpContent.content();
            if (byteBuf.isReadable()) {
                requestBuffer.append(byteBuf.toString(CharsetUtil.UTF_8));
            }
            if (msg instanceof LastHttpContent) {
                if (dbName == null) {
                    responseString.append("Invalid database null");
                    logger.severe("Invalid database null");
                } else {
                    String payload = requestBuffer.toString();
                    logger.fine("Request:" + payload);
                    List<Point> dps = InfluxDecoder.pointsFromString(dbName, payload);
                    meter.inc(dps.size());
                    for (Point dp : dps) {
                        try {
                            engine.writeDataPointWithLock(dp, false);
                            logger.fine("Accepted:" + dp + "\t" + new Date(dp.getTimestamp()));
                        } catch (IOException e) {
                            logger.fine("Dropped:" + dp + "\t" + e.getMessage());
                            responseString.append("Dropped:" + dp);
                        }
                    }
                }
                if (writeResponse(request, ctx)) {
                    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Point(com.srotya.sidewinder.core.rpc.Point) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) Date(java.util.Date) IOException(java.io.IOException) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) JsonArray(com.google.gson.JsonArray) List(java.util.List) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 42 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 43 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)

Example 44 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project BRFS by zhangnianli.

the class DiskNettyHttpRequestHandler method requestReceived.

@Override
public void requestReceived(ChannelHandlerContext ctx, FullHttpRequest request) {
    LOG.debug("handle request[{}:{}]", request.method(), request.uri());
    MessageHandler<DiskMessage> handler = methodToOps.get(request.method());
    if (handler == null) {
        ResponseSender.sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED);
        return;
    }
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri(), CharsetUtil.UTF_8, true);
    DiskMessage message = new DiskMessage();
    message.setFilePath(decoder.path());
    Map<String, String> params = new HashMap<String, String>();
    for (String paramName : decoder.parameters().keySet()) {
        params.put(paramName, decoder.parameters().get(paramName).get(0));
    }
    message.setParams(params);
    byte[] data = new byte[request.content().readableBytes()];
    request.content().readBytes(data);
    message.setData(data);
    handler.handle(message, new DefaultHandleResultCallback(ctx));
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DiskMessage(com.bonree.brfs.disknode.server.handler.DiskMessage) HashMap(java.util.HashMap)

Example 45 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project BRFS by zhangnianli.

the class StorageNameRequestHandler method requestReceived.

@Override
public void requestReceived(ChannelHandlerContext ctx, FullHttpRequest request) {
    LOG.debug("handle request[{}:{}]", request.method(), request.uri());
    MessageHandler<StorageNameMessage> handler = handlers.get(request.method().name());
    if (handler == null) {
        ResponseSender.sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED);
        return;
    }
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri(), CharsetUtil.UTF_8, true);
    StorageNameMessage message = new StorageNameMessage();
    message.setName(parseName(decoder.path()));
    System.out.println("name----" + message.getName());
    byte[] data = new byte[request.content().readableBytes()];
    request.content().readBytes(data);
    // TODO set other properties of StorageNameMessage
    handler.handle(message, new DefaultHandleResultCallback(ctx));
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder)

Aggregations

QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)74 List (java.util.List)30 Test (org.junit.Test)15 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 Map (java.util.Map)11 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 URI (java.net.URI)7 ByteBuf (io.netty.buffer.ByteBuf)6 HttpContent (io.netty.handler.codec.http.HttpContent)6 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)6 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)6 ArrayList (java.util.ArrayList)6 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 DeviceSession (org.traccar.DeviceSession)5 Position (org.traccar.model.Position)5 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 Channel (io.netty.channel.Channel)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3