Search in sources :

Example 21 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project ballerina by ballerina-lang.

the class Util method encodeBodyPart.

/**
 * Encode a given body part and add it to multipart request encoder.
 *
 * @param nettyEncoder Helps encode multipart/form-data
 * @param httpRequest  Represent top level http request that should hold multiparts
 * @param bodyPart     Represent a ballerina body part
 * @throws HttpPostRequestEncoder.ErrorDataEncoderException when an error occurs while encoding
 */
private static void encodeBodyPart(HttpPostRequestEncoder nettyEncoder, HttpRequest httpRequest, BStruct bodyPart) throws HttpPostRequestEncoder.ErrorDataEncoderException {
    try {
        InterfaceHttpData encodedData;
        Channel byteChannel = EntityBodyHandler.getByteChannel(bodyPart);
        FileUploadContentHolder contentHolder = new FileUploadContentHolder();
        contentHolder.setRequest(httpRequest);
        contentHolder.setBodyPartName(getBodyPartName(bodyPart));
        contentHolder.setFileName(TEMP_FILE_NAME + TEMP_FILE_EXTENSION);
        contentHolder.setContentType(MimeUtil.getBaseType(bodyPart));
        contentHolder.setBodyPartFormat(org.ballerinalang.mime.util.Constants.BodyPartForm.INPUTSTREAM);
        String contentTransferHeaderValue = HeaderUtil.getHeaderValue(bodyPart, HttpHeaderNames.CONTENT_TRANSFER_ENCODING.toString());
        if (contentTransferHeaderValue != null) {
            contentHolder.setContentTransferEncoding(contentTransferHeaderValue);
        }
        if (byteChannel != null) {
            contentHolder.setContentStream(byteChannel.getInputStream());
            encodedData = getFileUpload(contentHolder);
            if (encodedData != null) {
                nettyEncoder.addBodyHttpData(encodedData);
            }
        }
    } catch (IOException e) {
        log.error("Error occurred while encoding body part in ", e.getMessage());
    }
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) EntityBodyChannel(org.ballerinalang.mime.util.EntityBodyChannel) BString(org.ballerinalang.model.values.BString) IOException(java.io.IOException)

Example 22 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project netty by netty.

the class HttpUploadClient method formpost.

/**
 * Standard post without multipart but already support on Factory (memory management)
 *
 * @return the list of HttpData object (attribute and file) to be reused on next post
 */
private static List<InterfaceHttpData> formpost(Bootstrap bootstrap, String host, int port, URI uriSimple, File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception {
    // XXX /formpost
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(SocketUtils.socketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();
    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriSimple.toASCIIString());
    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder = // false => not multipart
    new HttpPostRequestEncoder(factory, request, false);
    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }
    // add Form attribute
    bodyRequestEncoder.addBodyAttribute("getform", "POST");
    bodyRequestEncoder.addBodyAttribute("info", "first value");
    bodyRequestEncoder.addBodyAttribute("secondinfo", "secondvalue ���&");
    bodyRequestEncoder.addBodyAttribute("thirdinfo", textArea);
    bodyRequestEncoder.addBodyAttribute("fourthinfo", textAreaLong);
    bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false);
    // finalize request
    request = bodyRequestEncoder.finalizeRequest();
    // Create the bodylist to be reused on the last version with Multipart support
    List<InterfaceHttpData> bodylist = bodyRequestEncoder.getBodyListAttributes();
    // send request
    channel.write(request);
    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) {
        // could do either request.isChunked()
        // either do it through ChunkedWriteHandler
        channel.write(bodyRequestEncoder);
    }
    channel.flush();
    // Do not clear here since we will reuse the InterfaceHttpData on the next request
    // for the example (limit action on client side). Take this as a broadcast of the same
    // request on both Post actions.
    // 
    // On standard program, it is clearly recommended to clean all files after each request
    // bodyRequestEncoder.cleanFiles();
    // Wait for the server to close the connection.
    channel.closeFuture().sync();
    return bodylist;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)

Example 23 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData 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)

Example 24 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData 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)

Example 25 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project vert.x by eclipse.

the class Http1xServerRequest method handleException.

void handleException(Throwable t) {
    HttpEventHandler handler = null;
    Http1xServerResponse resp = null;
    InterfaceHttpData upload = null;
    synchronized (conn) {
        if (!isEnded()) {
            handler = eventHandler;
            if (decoder != null) {
                upload = decoder.currentPartialHttpData();
            }
        }
        if (!response.ended()) {
            if (METRICS_ENABLED) {
                reportRequestReset(t);
            }
            resp = response;
        }
    }
    if (resp != null) {
        resp.handleException(t);
    }
    if (upload instanceof NettyFileUpload) {
        ((NettyFileUpload) upload).handleException(t);
    }
    if (handler != null) {
        handler.handleException(t);
    }
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData)

Aggregations

InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)23 Attribute (io.netty.handler.codec.http.multipart.Attribute)9 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)7 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)6 List (java.util.List)5 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)4 Test (org.junit.Test)4 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)3 File (java.io.File)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)2 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)2 HttpPostMultipartRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder)2 EndOfDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException)2 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)2 StreamResetException (io.vertx.core.http.StreamResetException)2