Search in sources :

Example 1 with DiskFileUpload

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

the class DefaultAttachmentBuilder method call.

@Override
public HttpData call(final Attachment attachment) {
    final File file = new File(attachment.filename);
    final DiskFileUpload filePayload = new DiskFileUpload(null != attachment.name ? attachment.name : FilenameUtils.getBaseName(attachment.filename), attachment.filename, attachment.contentType, null, null, file.length()) {

        @Override
        public Charset getCharset() {
            return null;
        }
    };
    try {
        filePayload.setContent(file);
    } catch (IOException e) {
        LOG.warn("exception when filePayload.setContent, detail: {}", ExceptionUtils.exception2detail(e));
    }
    return filePayload;
}
Also used : DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) IOException(java.io.IOException) File(java.io.File)

Example 2 with DiskFileUpload

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

the class HttpPostRequestEncoderTestCase method testDiskFileUploadEquals.

@Test
public final void testDiskFileUploadEquals() {
    final DiskFileUpload f2 = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
    assertEquals(f2, f2);
}
Also used : DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) Test(org.junit.Test)

Example 3 with DiskFileUpload

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload 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 4 with DiskFileUpload

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

the class HttpPostRequestEncoderTestCase method testGetBodyListAttributes.

@Test
public final void testGetBodyListAttributes() throws Exception {
    final HttpDataFactory factory = new DefaultHttpDataFactory(false);
    final HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    // Use the PostBody encoder
    final HttpPostRequestEncoder postRequestEncoder = // true => multipart
    new HttpPostRequestEncoder(factory, request, true);
    final MemoryFileUpload f1 = new MemoryFileUpload("m1", "m1", "application/json", null, null, 100);
    final DiskFileUpload f2 = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
    final DiskFileUpload f3 = new DiskFileUpload("d2", "d2", "application/json", null, null, 100);
    postRequestEncoder.addBodyHttpData(f1);
    postRequestEncoder.addBodyHttpData(f2);
    postRequestEncoder.addBodyHttpData(f3);
    final List<InterfaceHttpData> attrs = postRequestEncoder.getBodyListAttributes();
    final InterfaceHttpData[] datas = new InterfaceHttpData[] { f1, f2, f3 };
    for (int idx = 0; idx < datas.length; idx++) {
        assertEquals(datas[idx], attrs.toArray(new InterfaceHttpData[0])[idx]);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) MemoryFileUpload(io.netty.handler.codec.http.multipart.MemoryFileUpload) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) Test(org.junit.Test)

Example 5 with DiskFileUpload

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

the class FileUploadHandler method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final HttpObject msg) throws Exception {
    try {
        if (msg instanceof HttpRequest) {
            final HttpRequest httpRequest = (HttpRequest) msg;
            LOG.trace("Received request. URL:{} Method:{}", httpRequest.getUri(), httpRequest.getMethod());
            if (httpRequest.getMethod().equals(HttpMethod.POST)) {
                if (HttpPostRequestDecoder.isMultipart(httpRequest)) {
                    LOG.trace("Initializing multipart file upload.");
                    checkState(currentHttpPostRequestDecoder == null);
                    checkState(currentHttpRequest == null);
                    checkState(currentUploadDir == null);
                    currentHttpPostRequestDecoder = new HttpPostRequestDecoder(DATA_FACTORY, httpRequest);
                    currentHttpRequest = ReferenceCountUtil.retain(httpRequest);
                    // make sure that we still have a upload dir in case that it got deleted in
                    // the meanwhile
                    RestServerEndpoint.createUploadDir(uploadDir, LOG, false);
                    currentUploadDir = Files.createDirectory(uploadDir.resolve(UUID.randomUUID().toString()));
                } else {
                    ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
                }
            } else {
                ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
            }
        } else if (msg instanceof HttpContent && currentHttpPostRequestDecoder != null) {
            LOG.trace("Received http content.");
            // make sure that we still have a upload dir in case that it got deleted in the
            // meanwhile
            RestServerEndpoint.createUploadDir(uploadDir, LOG, false);
            final HttpContent httpContent = (HttpContent) msg;
            currentHttpPostRequestDecoder.offer(httpContent);
            while (httpContent != LastHttpContent.EMPTY_LAST_CONTENT && currentHttpPostRequestDecoder.hasNext()) {
                final InterfaceHttpData data = currentHttpPostRequestDecoder.next();
                if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload) {
                    final DiskFileUpload fileUpload = (DiskFileUpload) data;
                    checkState(fileUpload.isCompleted());
                    // wrapping around another File instantiation is a simple way to remove any
                    // path information - we're
                    // solely interested in the filename
                    final Path dest = currentUploadDir.resolve(new File(fileUpload.getFilename()).getName());
                    fileUpload.renameTo(dest.toFile());
                    LOG.trace("Upload of file {} into destination {} complete.", fileUpload.getFilename(), dest.toString());
                } else if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                    final Attribute request = (Attribute) data;
                    // this could also be implemented by using the first found Attribute as the
                    // payload
                    LOG.trace("Upload of attribute {} complete.", request.getName());
                    if (data.getName().equals(HTTP_ATTRIBUTE_REQUEST)) {
                        currentJsonPayload = request.get();
                    } else {
                        handleError(ctx, "Received unknown attribute " + data.getName() + '.', HttpResponseStatus.BAD_REQUEST, null);
                        return;
                    }
                }
            }
            if (httpContent instanceof LastHttpContent) {
                LOG.trace("Finalizing multipart file upload.");
                ctx.channel().attr(UPLOADED_FILES).set(new FileUploads(currentUploadDir));
                if (currentJsonPayload != null) {
                    currentHttpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, currentJsonPayload.length);
                    currentHttpRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);
                    ctx.fireChannelRead(currentHttpRequest);
                    ctx.fireChannelRead(httpContent.replace(Unpooled.wrappedBuffer(currentJsonPayload)));
                } else {
                    currentHttpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);
                    currentHttpRequest.headers().remove(HttpHeaders.Names.CONTENT_TYPE);
                    ctx.fireChannelRead(currentHttpRequest);
                    ctx.fireChannelRead(LastHttpContent.EMPTY_LAST_CONTENT);
                }
                reset();
            }
        } else {
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    } catch (Exception e) {
        handleError(ctx, "File upload failed.", HttpResponseStatus.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) Path(java.nio.file.Path) FileUploads(org.apache.flink.runtime.rest.handler.FileUploads) DiskFileUpload(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload) Attribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute) DiskAttribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskAttribute) InterfaceHttpData(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData) 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) 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) IOException(java.io.IOException)

Aggregations

DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)3 File (java.io.File)3 IOException (java.io.IOException)3 HttpContent (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent)2 HttpRequest (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest)2 LastHttpContent (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent)2 DiskFileUpload (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload)2 HttpPostRequestDecoder (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)2 InterfaceHttpData (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData)2 Test (org.junit.Test)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)1 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)1 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1 MemoryFileUpload (io.netty.handler.codec.http.multipart.MemoryFileUpload)1 Path (java.nio.file.Path)1 FileUploads (org.apache.flink.runtime.rest.handler.FileUploads)1 DefaultFullHttpResponse (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse)1