List of usage examples for io.netty.handler.codec.http.multipart DefaultHttpDataFactory DefaultHttpDataFactory
public DefaultHttpDataFactory()
From source file:org.jooby.internal.netty.NettyRequest.java
License:Apache License
private Multimap<String, String> decodeParams() throws IOException { if (params == null) { params = ArrayListMultimap.create(); files = ArrayListMultimap.create(); query.parameters().forEach((name, values) -> values.forEach(value -> params.put(name, value))); HttpMethod method = req.method(); boolean hasBody = method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH); boolean formLike = false; if (req.headers().contains("Content-Type")) { String contentType = req.headers().get("Content-Type").toLowerCase(); formLike = (contentType.startsWith(MediaType.multipart.name()) || contentType.startsWith(MediaType.form.name())); }//from w w w . j a v a 2 s. c om if (hasBody && formLike) { HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(), req); try { Function<HttpPostRequestDecoder, Boolean> hasNext = it -> { try { return it.hasNext(); } catch (HttpPostRequestDecoder.EndOfDataDecoderException ex) { return false; } }; while (hasNext.apply(decoder)) { HttpData field = (HttpData) decoder.next(); try { String name = field.getName(); if (field.getHttpDataType() == HttpDataType.FileUpload) { files.put(name, new NettyUpload((FileUpload) field, tmpdir)); } else { params.put(name, field.getString()); } } finally { field.release(); } } } finally { decoder.destroy(); } } } return params; }
From source file:org.restnext.core.http.RequestImpl.java
License:Apache License
/** * Create a new instance.// w w w . j a va 2 s. c o m * * @param context netty channel handler context * @param request netty full http request */ public RequestImpl(final ChannelHandlerContext context, final FullHttpRequest request) { Objects.requireNonNull(request, "request"); Objects.requireNonNull(context, "context"); this.charset = HttpUtil.getCharset(request, StandardCharsets.UTF_8); this.version = HttpVersion.HTTP_1_0.equals(request.protocolVersion()) ? Version.HTTP_1_0 : Version.HTTP_1_1; this.method = Method.valueOf(request.method().name()); this.uri = URI.create(normalize(request.uri())); this.baseUri = createBaseUri(context, request); this.keepAlive = HttpUtil.isKeepAlive(request); // copy the inbound netty request headers. this.headers = new MultivaluedHashMap<>(); for (Map.Entry<String, String> entry : request.headers()) { this.headers.add(entry.getKey().toLowerCase(), entry.getValue()); } this.parameters = new MultivaluedHashMap<>(); // decode the inbound netty request uri parameters. QueryStringDecoder queryDecoder = new QueryStringDecoder(request.uri(), charset); for (Map.Entry<String, List<String>> entry : queryDecoder.parameters().entrySet()) { this.parameters.addAll(entry.getKey(), entry.getValue()); } // decode the inbound netty request body parameters. if (Method.POST.equals(method)) { CharSequence charSequence = HttpUtil.getMimeType(request); AsciiString mimeType = charSequence != null ? AsciiString.of(charSequence) : AsciiString.EMPTY_STRING; boolean isFormData = mimeType.contains(HttpHeaderValues.FORM_DATA); if (isFormData) { // decode the inbound netty request body multipart/form-data parameters. HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(), request, charset); try { for (InterfaceHttpData data : decoder.getBodyHttpDatas()) { InterfaceHttpData.HttpDataType type = data.getHttpDataType(); switch (type) { case Attribute: { try { Attribute attribute = (Attribute) data; this.parameters.add(attribute.getName(), attribute.getValue()); } catch (IOException ignore) { LOGGER.warn("Could not get attribute value"); } break; } case FileUpload: break; case InternalAttribute: break; default: //nop } } } finally { decoder.destroy(); } } else { // decode the inbound netty request body raw | form-url-encoded | octet-stream parameters. this.content = request.content().hasArray() ? request.content().array() : request.content().toString(charset).getBytes(); } } }