Search in sources :

Example 1 with FileUploads

use of org.apache.flink.runtime.rest.handler.FileUploads 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

File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 FileUploads (org.apache.flink.runtime.rest.handler.FileUploads)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 Attribute (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute)1 DiskAttribute (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskAttribute)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 InterfaceHttpData (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData)1