Search in sources :

Example 1 with Attribute

use of io.netty.handler.codec.http.multipart.Attribute in project netty by netty.

the class HttpUploadServerHandler method writeHttpData.

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;
        try {
            value = attribute.getValue();
        } catch (IOException e1) {
            // Error while reading data from File, only print name and error
            e1.printStackTrace();
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": " + attribute.getName() + " Error while reading value: " + e1.getMessage() + "\r\n");
            return;
        }
        if (value.length() > 100) {
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": " + attribute.getName() + " data too long\r\n");
        } else {
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": " + attribute + "\r\n");
        }
    } else {
        responseContent.append("\r\nBODY FileUpload: " + data.getHttpDataType().name() + ": " + data + "\r\n");
        if (data.getHttpDataType() == HttpDataType.FileUpload) {
            FileUpload fileUpload = (FileUpload) data;
            if (fileUpload.isCompleted()) {
                if (fileUpload.length() < 10000) {
                    responseContent.append("\tContent of file\r\n");
                    try {
                        responseContent.append(fileUpload.getString(fileUpload.getCharset()));
                    } catch (IOException e1) {
                        // do nothing for the example
                        e1.printStackTrace();
                    }
                    responseContent.append("\r\n");
                } else {
                    responseContent.append("\tFile too long to be printed out:" + fileUpload.length() + "\r\n");
                }
            // fileUpload.isInMemory();// tells if the file is in Memory
            // or on File
            // fileUpload.renameTo(dest); // enable to move into another
            // File dest
            // decoder.removeFileUploadFromClean(fileUpload); //remove
            // the File of to delete file
            } else {
                responseContent.append("\tFile to be continued but should not!\r\n");
            }
        }
    }
}
Also used : Attribute(io.netty.handler.codec.http.multipart.Attribute) DiskAttribute(io.netty.handler.codec.http.multipart.DiskAttribute) IOException(java.io.IOException) DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) FileUpload(io.netty.handler.codec.http.multipart.FileUpload)

Example 2 with Attribute

use of io.netty.handler.codec.http.multipart.Attribute in project asterixdb by apache.

the class PostRequest method create.

public static IServletRequest create(FullHttpRequest request) throws IOException {
    List<String> names = new ArrayList<>();
    List<String> values = new ArrayList<>();
    HttpPostRequestDecoder decoder = null;
    try {
        decoder = new HttpPostRequestDecoder(request);
    } catch (Exception e) {
        //ignore. this means that the body of the POST request does not have key value pairs
        LOGGER.log(Level.WARNING, "Failed to decode a post message. Fix the API not to have queries as POST body", e);
    }
    if (decoder != null) {
        try {
            List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
            for (InterfaceHttpData data : bodyHttpDatas) {
                if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
                    Attribute attr = (MixedAttribute) data;
                    names.add(data.getName());
                    values.add(attr.getValue());
                }
            }
        } finally {
            decoder.destroy();
        }
    }
    return new PostRequest(request, new QueryStringDecoder(request.uri()).parameters(), names, values);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Attribute(io.netty.handler.codec.http.multipart.Attribute) MixedAttribute(io.netty.handler.codec.http.multipart.MixedAttribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) ArrayList(java.util.ArrayList) MixedAttribute(io.netty.handler.codec.http.multipart.MixedAttribute) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) IOException(java.io.IOException)

Example 3 with Attribute

use of io.netty.handler.codec.http.multipart.Attribute in project moco by dreamhead.

the class FormsRequestExtractor method doExtractForms.

private ImmutableMap<String, String> doExtractForms(final HttpPostRequestDecoder decoder) throws IOException {
    List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
    Map<String, String> forms = newHashMap();
    for (InterfaceHttpData data : bodyHttpDatas) {
        if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
            Attribute attribute = (Attribute) data;
            forms.put(attribute.getName(), attribute.getValue());
        }
    }
    return copyOf(forms);
}
Also used : Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData)

Example 4 with Attribute

use of io.netty.handler.codec.http.multipart.Attribute in project vert.x by eclipse.

the class Http2ServerRequestImpl method handleEnd.

void handleEnd(MultiMap trailers) {
    ended = true;
    conn.reportBytesRead(bytesRead);
    if (postRequestDecoder != null) {
        try {
            postRequestDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
            while (postRequestDecoder.hasNext()) {
                InterfaceHttpData data = postRequestDecoder.next();
                if (data instanceof Attribute) {
                    Attribute attr = (Attribute) data;
                    try {
                        formAttributes().add(attr.getName(), attr.getValue());
                    } catch (Exception e) {
                        // Will never happen, anyway handle it somehow just in case
                        handleException(e);
                    }
                }
            }
        } catch (HttpPostRequestDecoder.EndOfDataDecoderException e) {
        // ignore this as it is expected
        } catch (Exception e) {
            handleException(e);
        } finally {
            postRequestDecoder.destroy();
        }
    }
    if (endHandler != null) {
        endHandler.handle(null);
    }
}
Also used : Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) URISyntaxException(java.net.URISyntaxException) StreamResetException(io.vertx.core.http.StreamResetException) ClosedChannelException(java.nio.channels.ClosedChannelException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 5 with Attribute

use of io.netty.handler.codec.http.multipart.Attribute in project ratpack by ratpack.

the class FormDecoder method parseForm.

@SuppressWarnings("deprecation")
public static Form parseForm(Context context, TypedData body, MultiValueMap<String, String> base) 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(HttpHeaderNames.CONTENT_TYPE, body.getContentType().toString());
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest);
    HttpContent content = new DefaultHttpContent(body.getBuffer());
    decoder.offer(content);
    decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
    Map<String, List<String>> attributes = new LinkedHashMap<>(base.getAll());
    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(ro -> 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));
}
Also used : Context(ratpack.handling.Context) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) MultiValueMap(ratpack.util.MultiValueMap) Form(ratpack.form.Form) ImmutableDelegatingMultiValueMap(ratpack.util.internal.ImmutableDelegatingMultiValueMap) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) DefaultMediaType(ratpack.http.internal.DefaultMediaType) IOException(java.io.IOException) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) UploadedFile(ratpack.form.UploadedFile) ArrayList(java.util.ArrayList) TypedData(ratpack.http.TypedData) LinkedHashMap(java.util.LinkedHashMap) Attribute(io.netty.handler.codec.http.multipart.Attribute) io.netty.handler.codec.http(io.netty.handler.codec.http) Request(ratpack.http.Request) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Charset(java.nio.charset.Charset) MediaType(ratpack.http.MediaType) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) Map(java.util.Map) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) LinkedHashMap(java.util.LinkedHashMap) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DefaultMediaType(ratpack.http.internal.DefaultMediaType) MediaType(ratpack.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) Request(ratpack.http.Request) Charset(java.nio.charset.Charset) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) UploadedFile(ratpack.form.UploadedFile)

Aggregations

Attribute (io.netty.handler.codec.http.multipart.Attribute)6 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)5 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)4 IOException (java.io.IOException)4 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)2 ArrayList (java.util.ArrayList)2 ByteBuf (io.netty.buffer.ByteBuf)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)1 DiskAttribute (io.netty.handler.codec.http.multipart.DiskAttribute)1 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)1 MixedAttribute (io.netty.handler.codec.http.multipart.MixedAttribute)1 StreamResetException (io.vertx.core.http.StreamResetException)1 URISyntaxException (java.net.URISyntaxException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1