Java Code Examples for io.netty.handler.codec.http.FullHttpRequest#decoderResult()

The following examples show how to use io.netty.handler.codec.http.FullHttpRequest#decoderResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestDecoderAux.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    LOG.debug("    httpRequest  ---->   UnitRequest Pojo");
    /*if (!HttpMethod.POST.equals(msg.method())) {
        throw new BadRequestException(new IllegalArgumentException("拒绝非POST请求!"));
    }*/
    DecoderResult result = msg.decoderResult();
    if (!result.isSuccess()) {
        throw new BadRequestException(result.cause());
    }
    updateLongConnectionStatus(msg, ctx);
    Request request = new Request(msg, MsgIdHolder.get());
    offerReqQueue(ctx, request);
    out.add(request);
}
 
Example 2
Source File: HttpServerHandler.java    From cantor with Apache License 2.0 5 votes vote down vote up
@Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (FullHttpRequest.class.isAssignableFrom(msg.getClass())) {
            FullHttpRequest req = FullHttpRequest.class.cast(msg);
            DecoderResult result = req.decoderResult();

            if (result.isFailure()) {
                if (log.isWarnEnabled())
                    log.warn("http decoder failure", result.cause());
                ReferenceCountUtil.release(msg);
                ctx.writeAndFlush(HttpResponses.badRequest());
                ctx.channel().close();
                return;
            }

            if (HttpUtil.is100ContinueExpected(req))
                ctx.writeAndFlush(new DefaultFullHttpResponse(req.protocolVersion(), CONTINUE));

            FullHttpRequest safeReq = new DefaultFullHttpRequest(req.protocolVersion(),
                                                                 req.method(),
                                                                 req.uri(),
//                                                                 Buffers.safeByteBuf(req.content(), ctx.alloc()),
                                                                 req.content(),
                                                                 req.headers(),
                                                                 req.trailingHeaders());
            channelRead(ctx, safeReq);
        } else
            ctx.fireChannelRead(msg);
    }
 
Example 3
Source File: SearchQueryDecoder.java    From elastic-rabbitmq with MIT License 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest) throws Exception {
    DecoderResult result = httpRequest.decoderResult();
    if (!result.isSuccess()) {
        throw new BadRequestException(result.cause());
    }
    logger.info("Get search request: " + httpRequest.uri());

    // only decode get path is enough
    Map<String, List<String>> requestParameters;
    QueryStringDecoder stringDecoder = new QueryStringDecoder(httpRequest.uri());
    requestParameters = stringDecoder.parameters();

    QueryMeta meta = new QueryMeta();

    for(Map.Entry<String, List<String>> entry : requestParameters.entrySet()) {
        if (entry.getKey().equals("options[]")) {
            // add filters
            List<String> filters = entry.getValue();
            filters.forEach(filter -> {
                String[] typeVal = filter.split(":");
                meta.addMeta(typeVal[0], typeVal[1]);
            });
        } else if (entry.getKey().equals("orderby")) {
            meta.setOrderBy(entry.getValue().get(0));
        } else {
            logger.warn("Unknown query parameter, ignore it:" + entry.toString());
        }
    }

    DecodedSearchRequest searchRequest = new DecodedSearchRequest(httpRequest, meta, orderNumber++);
    ctx.fireChannelRead(searchRequest);
}
 
Example 4
Source File: HttpRequestParamDecoder.java    From fastjgame with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
    DecoderResult decoderResult = msg.decoderResult();
    if (!decoderResult.isSuccess()) {
        ctx.writeAndFlush(HttpResponseHelper.newBadRequestResponse())
                .addListener(ChannelFutureListener.CLOSE);
        logger.warn("decode failed.", decoderResult.cause());
        return;
    }
    HttpMethod method = msg.method();
    // 仅限get和post请求
    if (method != HttpMethod.GET && method != HttpMethod.POST) {
        ctx.writeAndFlush(HttpResponseHelper.newBadRequestResponse())
                .addListener(ChannelFutureListener.CLOSE);
        logger.info("unsupported method {}", method.name());
        return;
    }

    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(msg.uri());
    String path = queryStringDecoder.path();
    Map<String, String> paramsMap = new LinkedHashMap<>();

    if (method == HttpMethod.GET) {
        for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) {
            paramsMap.put(entry.getKey(), entry.getValue().get(0));
        }
    } else {
        // You <strong>MUST</strong> call {@link #destroy()} after completion to release all resources.
        HttpPostRequestDecoder postRequestDecoder = new HttpPostRequestDecoder(msg);
        try {
            for (InterfaceHttpData httpData : postRequestDecoder.getBodyHttpDatas()) {
                if (httpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                    Attribute attribute = (Attribute) httpData;
                    paramsMap.put(attribute.getName(), attribute.getValue());
                }
            }
        } finally {
            postRequestDecoder.destroy();
        }
    }
    final HttpRequestParam httpRequestParam = new HttpRequestParam(method, paramsMap);
    publish(new HttpRequestEvent(ctx.channel(), path, httpRequestParam, portExtraInfo));
}