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

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

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.multipart InterfaceHttpData 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:cc.io.lessons.server.HttpUploadServerHandler.java

License:Apache License

/**
 * Example of reading request by chunk and getting values from chunk to chunk
 *//*from   w w  w .j  a  va 2 s  . co m*/
private void readHttpDataChunkByChunk() {
    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                try {
                    // new value
                    writeHttpData(data);
                } finally {
                    data.release();
                }
            }
        }
    } catch (EndOfDataDecoderException e1) {
        // end
        responseContent.append("\r\n\r\nEND OF CONTENT CHUNK BY CHUNK\r\n\r\n");
    }
}

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/*ww w  .  ja  va  2  s.  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.bay1ts.bay.core.Request.java

License:Apache License

public Set<String> postBodyNames() {
    Set<String> set = new HashSet<>();
    HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(
            new DefaultHttpDataFactory(false), this.fullHttpRequest);
    for (InterfaceHttpData data : httpPostRequestDecoder.getBodyHttpDatas()) {
        data = httpPostRequestDecoder.next();
        set.add(data.getName());// w  ww .jav  a2s  . c  o  m
        data.release();
        //            if (data!=null){
        //                try {
        //                    Attribute attribute=(Attribute) data;
        //                    set.add(attribute.getName());
        //                }finally {
        //                    data.release();
        //                }
        //            }
    }
    return set;
}

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

License:Apache License

public Set<String> postBodyValues() {
    HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(
            new DefaultHttpDataFactory(false), this.fullHttpRequest);
    Set<String> set = new HashSet<>();
    for (; httpPostRequestDecoder.hasNext();) {
        InterfaceHttpData data = httpPostRequestDecoder.next();
        if (data != null) {
            try {
                Attribute attribute = (Attribute) data;
                set.add(attribute.getValue());
            } catch (IOException e) {
                e.printStackTrace();/*from  w w  w  .  j ava  2  s.c om*/
            } finally {
                data.release();
            }
        }
    }
    return set;
}

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

License:Apache License

/**
 * Example of reading request by chunk and getting values from chunk to chunk
 *//*from   ww  w  .  j  a  va  2  s.  c  o  m*/
private void readHttpDataChunkByChunk() {
    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                // check if current HttpData is a FileUpload and previously set as partial
                if (partialContent == data) {
                    logger.info(" 100% (FinalSize: " + partialContent.length() + ")");
                    partialContent = null;
                }
                try {
                    // new value
                    writeHttpData(data);
                } finally {
                    data.release();
                }
            }
        }
        // Check partial decoding for a FileUpload
        InterfaceHttpData data = decoder.currentPartialHttpData();
        if (data != null) {
            StringBuilder builder = new StringBuilder();
            if (partialContent == null) {
                partialContent = (HttpData) data;
                if (partialContent instanceof FileUpload) {
                    builder.append("Start FileUpload: ").append(((FileUpload) partialContent).getFilename())
                            .append(" ");
                } else {
                    builder.append("Start Attribute: ").append(partialContent.getName()).append(" ");
                }
                builder.append("(DefinedSize: ").append(partialContent.definedLength()).append(")");
            }
            if (partialContent.definedLength() > 0) {
                builder.append(" ").append(partialContent.length() * 100 / partialContent.definedLength())
                        .append("% ");
                logger.info(builder.toString());
            } else {
                builder.append(" ").append(partialContent.length()).append(" ");
                logger.info(builder.toString());
            }
        }
    } catch (EndOfDataDecoderException e1) {
        // end
        responseContent.append("\r\n\r\nEND OF CONTENT CHUNK BY CHUNK\r\n\r\n");
    }
}

From source file:com.nextcont.ecm.fileengine.http.nettyServer.HttpUploadServerHandler.java

License:Apache License

/**
 * Example of reading request by chunk and getting values from chunk to chunk
 *///w  ww  .  j av a 2 s  . c  o m
private String readHttpDataChunkByChunk(String globalId, boolean isChunked) throws EndOfDataDecoderException {
    String resultInfo = "";
    while (decoder.hasNext()) {
        InterfaceHttpData data = decoder.next();
        try {
            // new value
            resultInfo = writeHttpData(data, globalId, isChunked);
        } catch (IOException e) {
            e.printStackTrace();
            resultInfo = e.getMessage();
        } finally {
            data.release();
        }
    }
    return resultInfo;
}

From source file:com.shiyq.netty.http.HttpUploadServerHandler1.java

/**
 * Example of reading request by chunk and getting values from chunk to chunk
 *///w  w  w.j  av a2s . c  om
private void readHttpDataChunkByChunk() {
    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                // check if current HttpData is a FileUpload and previously set as partial
                if (partialContent == data) {
                    logger.info(" 100% (FinalSize: " + partialContent.length() + ")");
                    partialContent = null;
                }
                try {
                    // new value
                    writeHttpData(data);
                } finally {
                    data.release();
                }
            }
        }
        // Check partial decoding for a FileUpload
        InterfaceHttpData data = decoder.currentPartialHttpData();
        if (data != null) {
            StringBuilder builder = new StringBuilder();
            /*
            if (partialContent == null) {
            partialContent = (HttpData) data;
            if (partialContent instanceof FileUpload) {
                builder.append("Start FileUpload: ")
                    .append(((FileUpload) partialContent).getFilename()).append(" ");
            } else {
                builder.append("Start Attribute: ")
                    .append(partialContent.getName()).append(" ");
            }
            builder.append("(DefinedSize: ").append(partialContent.definedLength()).append(")");
            }
            */
            if (null != partialContent && partialContent.definedLength() > 0) {
                builder.append(" ").append(partialContent.length() * 100 / partialContent.definedLength())
                        .append("% ");
                logger.info(builder.toString());
            } else {
                builder.append(" ").append(partialContent.length()).append(" ");
                logger.info(builder.toString());
            }
        }
    } catch (EndOfDataDecoderException e1) {
        // end
        //responseContent.append("\r\n\r\nEND OF CONTENT CHUNK BY CHUNK\r\n\r\n");
    }
}

From source file:com.topsec.bdc.platform.api.test.http.upload.HttpUploadServerHandler.java

License:Apache License

/**
 * Example of reading request by chunk and getting values from chunk to chunk
 *//*from w  w  w.j av a2 s. c  o m*/
private void readHttpDataChunkByChunk() {

    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                try {
                    // new value
                    writeHttpData(data);
                } finally {
                    data.release();
                }
            }
        }
    } catch (EndOfDataDecoderException e1) {
        // end
        responseContent.append("\r\n\r\nEND OF CONTENT CHUNK BY CHUNK\r\n\r\n");
    }
}

From source file:dpfmanager.shell.modules.server.post.HttpPostHandler.java

License:Open Source License

/**
 * Example of reading request by chunk and getting values from chunk to chunk
 *//*from   w w  w.ja  va2  s  .  co m*/
private void readHttpDataChunkByChunk() {
    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                if (partialContent == data) {
                    partialContent = null;
                }
                try {
                    if (data.getHttpDataType() == HttpDataType.Attribute) {
                        parseAttributeData((Attribute) data);
                    } else if (data.getHttpDataType() == HttpDataType.FileUpload) {
                        parseFileUploadData((FileUpload) data);
                    } else {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                } finally {
                    data.release();
                }
            }
        }
    } catch (HttpPostRequestDecoder.EndOfDataDecoderException e1) {
        // End
    }
}

From source file:firebats.http.server.exts.form.Form.java

License:Apache License

private static Form decodeWithContent(Context context, ByteBuf content) {
    //new DefaultHttpDataFactory(/*useDisk*/true)decoder?
    //Mix?16K???//  w  w w.  j av  a 2 s  . co  m
    //       final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(/*useDisk*/true),toNettyHttpRequest(context.request));
    HttpServerRequest<ByteBuf> rxRequest = context.getRequest();
    HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, rxRequest.getHttpMethod(),
            rxRequest.getUri());
    for (Map.Entry<String, String> header : rxRequest.getHeaders().entries()) {
        nettyRequest.headers().add(header.getKey(), header.getValue());
    }
    final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest);
    HttpContent httpContent = new DefaultHttpContent(content);
    decoder.offer(httpContent);
    decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);

    Map<String, String> formParams = new LinkedHashMap<>();
    Map<String, UploadedFile> files = new LinkedHashMap<>();
    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
                try {
                    Attribute attr = (Attribute) data;
                    if (!formParams.containsKey(data.getName())) {
                        formParams.put(attr.getName(), attr.getValue());
                    }
                } catch (IOException e) {
                    Throwables.propagate(e);
                } finally {
                    //?
                    data.release();
                }
            } else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) {
                try {
                    if (!files.containsKey(data.getName())) {
                        final FileUpload nettyFileUpload = (FileUpload) data;
                        final ByteBuf byteBuf = nettyFileUpload.content();
                        byteBuf.retain();
                        context.onComplete(new Action0() {
                            @Override
                            public void call() {
                                if (log.isDebugEnabled()) {
                                    log.debug("form upload file release[" + data.getName() + ":"
                                            + nettyFileUpload.getFilename() + "]");
                                }
                                byteBuf.release();
                            }
                        });
                        UploadedFile fileUpload = new UploadedFile(nettyFileUpload.getFilename(),
                                nettyFileUpload.getContentType(), byteBuf);
                        files.put(data.getName(), fileUpload);
                    }
                } finally {
                    data.release();
                }
            }
        }
    } catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) {
        // ignore
    } finally {
        decoder.destroy();
    }
    Map<String, String> query = Form.toFlatQueryParams(context.getRequest().getQueryParameters());
    return fromAll(query, formParams, files);
}