Search in sources :

Example 1 with EndOfDataDecoderException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException in project flink by apache.

the class HttpRequestHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    try {
        if (msg instanceof HttpRequest) {
            currentRequest = (HttpRequest) msg;
            currentRequestPath = null;
            if (currentDecoder != null) {
                currentDecoder.destroy();
                currentDecoder = null;
            }
            if (currentRequest.getMethod() == HttpMethod.GET || currentRequest.getMethod() == HttpMethod.DELETE) {
                // directly delegate to the router
                ctx.fireChannelRead(currentRequest);
            } else if (currentRequest.getMethod() == HttpMethod.POST) {
                // POST comes in multiple objects. First the request, then the contents
                // keep the request and path for the remaining objects of the POST request
                currentRequestPath = new QueryStringDecoder(currentRequest.getUri(), ENCODING).path();
                currentDecoder = new HttpPostRequestDecoder(DATA_FACTORY, currentRequest, ENCODING);
            } else {
                throw new IOException("Unsupported HTTP method: " + currentRequest.getMethod().name());
            }
        } else if (currentDecoder != null && msg instanceof HttpContent) {
            // received new chunk, give it to the current decoder
            HttpContent chunk = (HttpContent) msg;
            currentDecoder.offer(chunk);
            try {
                while (currentDecoder.hasNext()) {
                    InterfaceHttpData data = currentDecoder.next();
                    if (data.getHttpDataType() == HttpDataType.FileUpload && tmpDir != null) {
                        DiskFileUpload file = (DiskFileUpload) data;
                        if (file.isCompleted()) {
                            String name = file.getFilename();
                            File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
                            if (!tmpDir.exists()) {
                                logExternalUploadDirDeletion(tmpDir);
                                checkAndCreateUploadDir(tmpDir);
                            }
                            file.renameTo(target);
                            QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
                            encoder.addParam("filepath", target.getAbsolutePath());
                            encoder.addParam("filename", name);
                            currentRequest.setUri(encoder.toString());
                        }
                    }
                }
            } catch (EndOfDataDecoderException ignored) {
            }
            if (chunk instanceof LastHttpContent) {
                HttpRequest request = currentRequest;
                currentRequest = null;
                currentRequestPath = null;
                currentDecoder.destroy();
                currentDecoder = null;
                // fire next channel handler
                ctx.fireChannelRead(request);
            }
        } else {
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    } catch (Throwable t) {
        currentRequest = null;
        currentRequestPath = null;
        if (currentDecoder != null) {
            currentDecoder.destroy();
            currentDecoder = null;
        }
        if (ctx.channel().isActive()) {
            byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);
            DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
            response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
            ctx.writeAndFlush(response);
        }
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) EndOfDataDecoderException(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException) IOException(java.io.IOException) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpPostRequestDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder) DiskFileUpload(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload) InterfaceHttpData(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData) File(java.io.File) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent) QueryStringEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder)

Example 2 with EndOfDataDecoderException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException in project jocean-http by isdom.

the class AsBlob method onNext4Multipart.

private Observable<? extends Blob> onNext4Multipart(final HttpContent content) {
    try {
        _postDecoder.offer(content);
    } catch (ErrorDataDecoderException e) {
        LOG.warn("exception when postDecoder.offer, detail: {}", ExceptionUtils.exception2detail(e));
    }
    final List<Blob> blobs = new ArrayList<>();
    try {
        while (_postDecoder.hasNext()) {
            final InterfaceHttpData data = _postDecoder.next();
            if (data != null) {
                try {
                    final Blob blob = this._holder.hold(processHttpData(data));
                    if (null != blob) {
                        blobs.add(blob);
                        LOG.info("onNext4Multipart: add Blob {}", blob);
                    }
                } finally {
                    data.release();
                }
            }
        }
    } catch (EndOfDataDecoderException e) {
        LOG.warn("exception when postDecoder.hasNext, detail: {}", ExceptionUtils.exception2detail(e));
    }
    return blobs.isEmpty() ? Observable.<Blob>empty() : Observable.from(blobs);
}
Also used : Blob(org.jocean.netty.BlobRepo.Blob) EndOfDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) ArrayList(java.util.ArrayList) ErrorDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException)

Example 3 with EndOfDataDecoderException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException in project netty by netty.

the class HttpUploadServerHandler method readHttpDataChunkByChunk.

/**
 * Example of reading request by chunk and getting values from chunk to chunk
 */
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;
                }
                // new value
                writeHttpData(data);
            }
        }
        // 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");
    }
}
Also used : EndOfDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) FileUpload(io.netty.handler.codec.http.multipart.FileUpload)

Aggregations

EndOfDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException)2 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)2 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)1 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)1 ErrorDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DefaultFullHttpResponse (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse)1 HttpContent (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent)1 HttpRequest (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest)1 LastHttpContent (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent)1 QueryStringDecoder (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder)1 QueryStringEncoder (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder)1 DiskFileUpload (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload)1 HttpPostRequestDecoder (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)1 EndOfDataDecoderException (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException)1 InterfaceHttpData (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData)1 Blob (org.jocean.netty.BlobRepo.Blob)1