Example usage for io.netty.handler.codec.http.multipart HttpData release

List of usage examples for io.netty.handler.codec.http.multipart HttpData release

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.multipart HttpData release.

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

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 . java2s  .  c  o  m
        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;
}