Java Code Examples for io.netty.handler.codec.http.multipart.HttpPostRequestDecoder#destroy()

The following examples show how to use io.netty.handler.codec.http.multipart.HttpPostRequestDecoder#destroy() . 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: HttpRequestParameters.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private Map<String, List<String>> parsePostFormParameters(FullHttpRequest request) {
   HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request);
   Map<String, List<String>> attributes = new HashMap<String, List<String>>();
   List<InterfaceHttpData> datas = decoder.getBodyHttpDatas();
   for (InterfaceHttpData data : datas) {
      if (data.getHttpDataType() == HttpDataType.Attribute) {
         try {
            String name = data.getName();
            String value = ((Attribute) data).getString();
            attributes.putIfAbsent(name, new ArrayList<String>());
            attributes.get(name).add(value);
         } catch (IOException e) {
            LOGGER.error("Error getting HTTP attribute from POST request");
         }
      }
   }
   decoder.destroy();
   return attributes;
}
 
Example 2
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));
}
 
Example 3
Source File: FormData.java    From styx with Apache License 2.0 4 votes vote down vote up
FormData(HttpPostRequestDecoder content) {
    requireNonNull(content);
    parameters = extractParameters(content);
    content.destroy();
}