Search in sources :

Example 21 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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)

Example 22 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project riposte by Nike-Inc.

the class RequestInfoImplTest method getMultipartParts_returns_data_from_multipartData.

@Test
public void getMultipartParts_returns_data_from_multipartData() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    Whitebox.setInternalState(requestInfo, "isMultipart", true);
    requestInfo.isCompleteRequestWithAllChunks = true;
    HttpPostMultipartRequestDecoder multipartDataMock = mock(HttpPostMultipartRequestDecoder.class);
    List<InterfaceHttpData> dataListMock = mock(List.class);
    doReturn(dataListMock).when(multipartDataMock).getBodyHttpDatas();
    requestInfo.multipartData = multipartDataMock;
    // when
    List<InterfaceHttpData> result = requestInfo.getMultipartParts();
    // then
    assertThat(result, is(dataListMock));
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostMultipartRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder) Test(org.junit.Test)

Example 23 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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 24 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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 25 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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)

Aggregations

InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)24 Attribute (io.netty.handler.codec.http.multipart.Attribute)9 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)9 IOException (java.io.IOException)9 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)7 ArrayList (java.util.ArrayList)7 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