Package io.netty.handler.codec.http.multipart

Examples of io.netty.handler.codec.http.multipart.HttpPostRequestDecoder


            }
            responseContent.append("\r\n\r\n");

            // if GET Method: should not try to create a HttpPostRequestDecoder
            try {
                decoder = new HttpPostRequestDecoder(factory, request);
            } catch (ErrorDataDecoderException e1) {
                e1.printStackTrace();
                responseContent.append(e1.getMessage());
                writeResponse(ctx.channel());
                ctx.channel().close();
View Full Code Here


                writeResponse(ctx.channel());
                return;
            }

            try {
                decoder = new HttpPostRequestDecoder(factory, request);
            } catch (ErrorDataDecoderException e1) {
                e1.printStackTrace();
                responseContent.append(e1.getMessage());
                writeResponse(ctx.channel());
                ctx.channel().close();
View Full Code Here

  public static Form parseForm(Context context, TypedData requestBody) throws RuntimeException {
    Request request = context.getRequest();
    HttpMethod method = io.netty.handler.codec.http.HttpMethod.valueOf(request.getMethod().getName());
    HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(HttpHeaders.Names.CONTENT_TYPE, request.getBody().getContentType().toString());
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest);

    HttpContent content = new DefaultHttpContent(requestBody.getBuffer());

    decoder.offer(content);
    decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);

    Map<String, List<String>> attributes = new LinkedHashMap<>();
    Map<String, List<UploadedFile>> files = new LinkedHashMap<>();

    try {
      InterfaceHttpData data = decoder.next();
      while (data != null) {
        if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
          List<String> values = attributes.get(data.getName());
          if (values == null) {
            values = new ArrayList<>(1);
            attributes.put(data.getName(), values);
          }
          try {
            values.add(((Attribute) data).getValue());
          } catch (IOException e) {
            throw uncheck(e);
          } finally {
            data.release();
          }
        } else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) {
          List<UploadedFile> values = files.get(data.getName());
          if (values == null) {
            values = new ArrayList<>(1);
            files.put(data.getName(), values);
          }
          try {
            FileUpload nettyFileUpload = (FileUpload) data;
            final ByteBuf byteBuf = nettyFileUpload.getByteBuf();
            byteBuf.retain();
            context.onClose(new Action<RequestOutcome>() {
              @Override
              public void execute(RequestOutcome thing) throws Exception {
                byteBuf.release();
              }
            });

            MediaType contentType;
            String rawContentType = nettyFileUpload.getContentType();
            if (rawContentType == null) {
              contentType = null;
            } else {
              Charset charset = nettyFileUpload.getCharset();
              if (charset == null) {
                contentType = DefaultMediaType.get(rawContentType);
              } else {
                contentType = DefaultMediaType.get(rawContentType + ";charset=" + charset);
              }
            }

            UploadedFile fileUpload = new DefaultUploadedFile(new ByteBufBackedTypedData(byteBuf, contentType), nettyFileUpload.getFilename());

            values.add(fileUpload);
          } catch (IOException e) {
            throw uncheck(e);
          } finally {
            data.release();
          }
        }
        data = decoder.next();
      }
    } catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) {
      // ignore
    } finally {
      decoder.destroy();
    }

    return new DefaultForm(new ImmutableDelegatingMultiValueMap<>(attributes), new ImmutableDelegatingMultiValueMap<>(files));
  }
View Full Code Here

                responseContent.append("\r\n\r\nEND OF GET CONTENT\r\n");
                // Not now: LastHttpContent will be sent writeResponse(ctx.channel());
                return;
            }
            try {
                decoder = new HttpPostRequestDecoder(factory, request);
            } catch (ErrorDataDecoderException e1) {
                e1.printStackTrace();
                responseContent.append(e1.getMessage());
                writeResponse(ctx.channel());
                ctx.channel().close();
View Full Code Here

import static com.google.common.collect.Maps.newHashMap;

public class FormsRequestExtractor extends HttpRequestExtractor<ImmutableMap<String, String>> {
    @Override
    protected Optional<ImmutableMap<String, String>> doExtract(final HttpRequest request) {
        HttpPostRequestDecoder decoder = null;
        try {
            decoder = new HttpPostRequestDecoder(((DefaultHttpRequest)request).toFullHttpRequest());
            return of(doExtractForms(decoder));
        } catch (HttpPostRequestDecoder.IncompatibleDataDecoderException idde) {
            return absent();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (decoder != null) {
                decoder.destroy();
            }
        }
    }
View Full Code Here

        HttpMethod method = request.getMethod();
        String lowerCaseContentType = contentType.toLowerCase();
        isURLEncoded = lowerCaseContentType.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
        if ((lowerCaseContentType.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA) || isURLEncoded) &&
            (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH))) {
          decoder = new HttpPostRequestDecoder(new DataFactory(), request);
        }
      }
    } else {
      decoder = null;
    }
View Full Code Here

                responseContent.append("\r\n\r\nEND OF GET CONTENT\r\n");
                writeResponse(ctx.channel());
                return;
            }
            try {
                decoder = new HttpPostRequestDecoder(factory, request);
            } catch (ErrorDataDecoderException e1) {
                e1.printStackTrace();
                responseContent.append(e1.getMessage());
                writeResponse(ctx.channel());
                ctx.channel().close();
View Full Code Here

            throw new IllegalArgumentException("Cannot parse HttpData for requests other than POST");
        }

        try {
            if (httpDataAttributes == null) {
                final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request);
                final Map<String, List<String>> map = new HashMap<String, List<String>>();

                try {
                    for (InterfaceHttpData data : decoder.getBodyHttpDatas()) {
                        if (data.getHttpDataType() == HttpDataType.Attribute) {
                            Attribute attribute = (Attribute) data;

                            List<String> list = map.get(attribute.getName());
                            if (list == null) {
                                list = new LinkedList<String>();
                                map.put(attribute.getName(), list);
                            }

                            list.add(attribute.getValue());
                        }
                    }
                } finally {
                    decoder.destroy();
                }

                httpDataAttributes = map;
            }
View Full Code Here

                responseContent.append("\r\n\r\nEND OF GET CONTENT\r\n");
                // Not now: LastHttpContent will be sent writeResponse(ctx.channel());
                return;
            }
            try {
                decoder = new HttpPostRequestDecoder(factory, request);
            } catch (ErrorDataDecoderException e1) {
                e1.printStackTrace();
                responseContent.append(e1.getMessage());
                writeResponse(ctx.channel());
                ctx.channel().close();
View Full Code Here

TOP

Related Classes of io.netty.handler.codec.http.multipart.HttpPostRequestDecoder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.