Example usage for io.netty.handler.codec.http.multipart InterfaceHttpData getHttpDataType

List of usage examples for io.netty.handler.codec.http.multipart InterfaceHttpData getHttpDataType

Introduction

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

Prototype

HttpDataType getHttpDataType();

Source Link

Usage

From source file:HttpUploadServerHandler.java

License:Apache License

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;/*w  w  w .j  a  v  a2 s .  c om*/
        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.toString() + "\r\n");
        }
    } else {
        responseContent.append(
                "\r\nBODY FileUpload: " + data.getHttpDataType().name() + ": " + data.toString() + "\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");
            }
        }
    }
    //BufUtil.release(data);
}

From source file:cc.io.lessons.server.HttpUploadServerHandler.java

License:Apache License

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;/*from  ww  w  .j a  v  a 2  s.c o m*/
        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.toString() + "\r\n");
        }
    } else {
        responseContent.append(
                "\r\nBODY FileUpload: " + data.getHttpDataType().name() + ": " + data.toString() + "\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");
            }
        }
    }
}

From source file:com.alibaba.dubbo.qos.command.decoder.HttpCommandDecoder.java

License:Apache License

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);
                    }/*from w  ww.ja v a2  s  . com*/
                    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;
}

From source file:com.bay1ts.bay.core.Request.java

License:Apache License

/**
 * ?Content-Type:application/x-www-form-urlencoded post body ?(htmlform)
 *
 * @param name ??form post inputname/* w w w.  j  av  a 2s  .c  o  m*/
 * @return value
 */
public String postBody(String name) {
    HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(
            new DefaultHttpDataFactory(false), this.fullHttpRequest);
    InterfaceHttpData data = httpPostRequestDecoder.getBodyHttpData(name);
    if (data != null && data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value = null;
        try {
            value = attribute.getValue();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            data.release();
        }
        return value;
    }
    return null;
}

From source file:com.cats.version.httpserver.VersionProtocolMessageHandler.java

License:Apache License

private String decodeMessage(FullHttpRequest request) throws IOException {
    HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
    InterfaceHttpData data = httpPostRequestDecoder.getBodyHttpData("msg");
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String strMsg = attribute.getValue();
        return strMsg;
    }// w  ww  .  ja  v  a2s  . com
    return null;
}

From source file:com.chiorichan.http.HttpHandler.java

License:Mozilla Public License

private void writeHttpData(InterfaceHttpData data) throws IOException {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;/* w w  w .  ja  v  a2  s .c o m*/
        try {
            value = attribute.getValue();
        } catch (IOException e) {
            e.printStackTrace();
            response.sendException(e);
            return;
        }

        request.putPostMap(attribute.getName(), value);

        /*
         * Should resolve the problem described in Issue #9 on our GitHub
         */
        attribute.delete();
    } else if (data.getHttpDataType() == HttpDataType.FileUpload) {
        FileUpload fileUpload = (FileUpload) data;
        if (fileUpload.isCompleted())
            try {
                request.putUpload(fileUpload.getName(), new UploadedFile(fileUpload));
            } catch (IOException e) {
                e.printStackTrace();
                response.sendException(e);
            }
        else
            NetworkManager.getLogger().warning("File to be continued but should not!");
    }
}

From source file:com.cmz.http.upload.HttpUploadServerHandler.java

License:Apache License

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;//from   w  w w  . j  a v  a  2  s  . c om
        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");
                }
                try {

                    fileUpload.setCharset(Charset.forName("UTF-8"));
                    fileUpload.renameTo(new File("11.txt"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // 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");
            }
        }
    }
}

From source file:com.fire.login.http.HttpInboundHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        URI uri = URI.create(req.getUri());
        ctx.channel().attr(KEY_PATH).set(uri.getPath());

        if (req.getMethod().equals(HttpMethod.GET)) {
            QueryStringDecoder decoder = new QueryStringDecoder(URI.create(req.getUri()));
            Map<String, List<String>> parameter = decoder.parameters();
            dispatch(ctx.channel(), parameter);
            return;
        }//from www.  ja  v  a  2  s.  c  o m

        decoder = new HttpPostRequestDecoder(factory, req);
    }

    if (decoder != null) {
        if (msg instanceof HttpContent) {
            HttpContent chunk = (HttpContent) msg;
            decoder.offer(chunk);

            List<InterfaceHttpData> list = decoder.getBodyHttpDatas();
            Map<String, List<String>> parameter = new HashMap<>();
            for (InterfaceHttpData data : list) {
                if (data.getHttpDataType() == HttpDataType.Attribute) {
                    Attribute attribute = (Attribute) data;
                    addParameter(attribute.getName(), attribute.getValue(), parameter);
                }
            }

            if (chunk instanceof LastHttpContent) {
                reset();
                dispatch(ctx.channel(), parameter);
            }
        }
    }
}

From source file:com.flysoloing.learning.network.netty.http.upload.HttpUploadServerHandler.java

License:Apache License

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;//from ww  w. j av  a2 s . co  m
        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");
            }
        }
    }
}

From source file:com.github.ambry.rest.NettyMultipartRequest.java

License:Open Source License

/**
 * Processes a single decoded part in a multipart request. Exposes the data in the part either through the channel
 * itself (if it is the blob part) or via {@link #getArgs()}.
 * @param part the {@link InterfaceHttpData} that needs to be processed.
 * @throws RestServiceException if the request channel is closed, if there is more than one part of the same name, if
 *                              the size obtained from the headers does not match the actual size of the blob part or
 *                              if {@code part} is not of the expected type ({@link FileUpload}).
 *///from   w ww . ja  va  2s  .c o m
private void processPart(InterfaceHttpData part) throws RestServiceException {
    if (part.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload) {
        FileUpload fileUpload = (FileUpload) part;
        if (fileUpload.getName().equals(RestUtils.MultipartPost.BLOB_PART)) {
            // this is actual data.
            if (hasBlob) {
                nettyMetrics.repeatedPartsError.inc();
                throw new RestServiceException("Request has more than one " + RestUtils.MultipartPost.BLOB_PART,
                        RestServiceErrorCode.BadRequest);
            } else {
                hasBlob = true;
                if (fileUpload.length() != getSize()) {
                    nettyMetrics.multipartRequestSizeMismatchError.inc();
                    throw new RestServiceException("Request size [" + fileUpload.length()
                            + "] does not match Content-Length [" + getSize() + "]",
                            RestServiceErrorCode.BadRequest);
                } else {
                    contentLock.lock();
                    try {
                        if (isOpen()) {
                            requestContents.add(
                                    new DefaultHttpContent(ReferenceCountUtil.retain(fileUpload.content())));
                        } else {
                            nettyMetrics.multipartRequestAlreadyClosedError.inc();
                            throw new RestServiceException("Request is closed",
                                    RestServiceErrorCode.RequestChannelClosed);
                        }
                    } finally {
                        contentLock.unlock();
                    }
                }
            }
        } else {
            // this is any kind of data. (For ambry, this will be user metadata).
            // TODO: find a configurable way of rejecting unexpected file parts.
            String name = fileUpload.getName();
            if (allArgs.containsKey(name)) {
                nettyMetrics.repeatedPartsError.inc();
                throw new RestServiceException("Request already has a component named " + name,
                        RestServiceErrorCode.BadRequest);
            } else {
                ByteBuffer buffer = ByteBuffer.allocate(fileUpload.content().readableBytes());
                // TODO: Possible optimization - Upgrade ByteBufferReadableStreamChannel to take a list of ByteBuffer. This
                // TODO: will avoid the copy.
                fileUpload.content().readBytes(buffer);
                buffer.flip();
                allArgs.put(name, buffer);
            }
        }
    } else {
        nettyMetrics.unsupportedPartError.inc();
        throw new RestServiceException("Unexpected HTTP data", RestServiceErrorCode.BadRequest);
    }
}