Search in sources :

Example 1 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData 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 2 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project dubbo by alibaba.

the class HttpCommandDecoder method decode.

public static CommandContext decode(HttpRequest request) {
    CommandContext commandContext = null;
    if (request != null) {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        String path = queryStringDecoder.path();
        String[] array = path.split("/");
        if (array.length == 2) {
            String name = array[1];
            // process GET request and POST request separately. Check url for GET, and check body for POST
            if (request.getMethod() == HttpMethod.GET) {
                if (queryStringDecoder.parameters().isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    List<String> valueList = new ArrayList<String>();
                    for (List<String> values : queryStringDecoder.parameters().values()) {
                        valueList.addAll(values);
                    }
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            } else if (request.getMethod() == HttpMethod.POST) {
                HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
                List<String> valueList = new ArrayList<String>();
                for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) {
                    if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                        Attribute attribute = (Attribute) interfaceHttpData;
                        try {
                            valueList.add(attribute.getValue());
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                if (valueList.isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            }
        }
    }
    return commandContext;
}
Also used : CommandContext(com.alibaba.dubbo.qos.command.CommandContext) Attribute(io.netty.handler.codec.http.multipart.Attribute) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project cosmic by MissionCriticalCloud.

the class HttpUploadServerHandler method readFileUploadData.

private HttpResponseStatus readFileUploadData() throws IOException {
    while (decoder.hasNext()) {
        final InterfaceHttpData data = decoder.next();
        if (data != null) {
            try {
                logger.info("BODY FileUpload: " + data.getHttpDataType().name() + ": " + data);
                if (data.getHttpDataType() == HttpDataType.FileUpload) {
                    final FileUpload fileUpload = (FileUpload) data;
                    if (fileUpload.isCompleted()) {
                        requestProcessed = true;
                        final String format = ImageStoreUtil.checkTemplateFormat(fileUpload.getFile().getAbsolutePath(), fileUpload.getFilename());
                        if (StringUtils.isNotBlank(format)) {
                            final String errorString = "File type mismatch between the sent file and the actual content. Received: " + format;
                            logger.error(errorString);
                            responseContent.append(errorString);
                            storageResource.updateStateMapWithError(uuid, errorString);
                            return HttpResponseStatus.BAD_REQUEST;
                        }
                        final String status = storageResource.postUpload(uuid, fileUpload.getFile().getName());
                        if (status != null) {
                            responseContent.append(status);
                            storageResource.updateStateMapWithError(uuid, status);
                            return HttpResponseStatus.INTERNAL_SERVER_ERROR;
                        } else {
                            responseContent.append("upload successful.");
                            return HttpResponseStatus.OK;
                        }
                    }
                }
            } finally {
                data.release();
            }
        }
    }
    responseContent.append("received entity is not a file");
    return HttpResponseStatus.UNPROCESSABLE_ENTITY;
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) FileUpload(io.netty.handler.codec.http.multipart.FileUpload)

Example 4 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project duangframework by tcrct.

the class MultiPartPostDecoder method decoder.

@Override
public Map<String, String[]> decoder() throws Exception {
    HttpPostMultipartRequestDecoder requestDecoder = new HttpPostMultipartRequestDecoder(HTTP_DATA_FACTORY, request);
    List<InterfaceHttpData> paramsList = requestDecoder.getBodyHttpDatas();
    if (null != paramsList && !paramsList.isEmpty()) {
        Map<String, List<String>> params = new HashMap<>();
        for (InterfaceHttpData httpData : paramsList) {
            MemoryAttribute attribute = (MemoryAttribute) httpData;
            String key = attribute.getName();
            String value = attribute.getValue();
            parseValue2List(params, key, value);
            paramsMap.put(key, params.get(key).toArray(EMPTY_ARRAYS));
        }
    }
    return paramsMap;
}
Also used : HashMap(java.util.HashMap) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostMultipartRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder) List(java.util.List) ArrayList(java.util.ArrayList) MemoryAttribute(io.netty.handler.codec.http.multipart.MemoryAttribute)

Example 5 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project duangframework by tcrct.

the class PostDecoder method decoder.

@Override
public Map<String, String[]> decoder() throws Exception {
    HttpPostRequestDecoder requestDecoder = new HttpPostRequestDecoder(HTTP_DATA_FACTORY, request);
    List<InterfaceHttpData> paramsList = requestDecoder.getBodyHttpDatas();
    if (null != paramsList && !paramsList.isEmpty()) {
        Map<String, List<String>> params = new HashMap<>();
        for (InterfaceHttpData httpData : paramsList) {
            Attribute attribute = (Attribute) httpData;
            String key = attribute.getName();
            String value = attribute.getValue();
            parseValue2List(params, key, value);
            paramsMap.put(key, params.get(key).toArray(EMPTY_ARRAYS));
        }
    }
    return paramsMap;
}
Also used : HashMap(java.util.HashMap) Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) List(java.util.List) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)

Aggregations

InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)23 Attribute (io.netty.handler.codec.http.multipart.Attribute)9 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)7 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)6 List (java.util.List)5 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)4 Test (org.junit.Test)4 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)3 File (java.io.File)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)2 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)2 HttpPostMultipartRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder)2 EndOfDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException)2 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)2 StreamResetException (io.vertx.core.http.StreamResetException)2