Search in sources :

Example 1 with HttpData

use of io.netty.handler.codec.http.multipart.HttpData in project jocean-http by isdom.

the class DefaultSignalClient method addAttachmentsToMultipart.

private long addAttachmentsToMultipart(final HttpPostRequestEncoder postRequestEncoder, final Attachment[] attachments) {
    long size = 0;
    for (Attachment attachment : attachments) {
        try {
            final HttpData httpData = this._attachmentBuilder.call(attachment);
            size += httpData.length();
            postRequestEncoder.addBodyHttpData(httpData);
        } catch (Exception e) {
            LOG.warn("exception when invoke AttachmentBuilder({}).call, detail: {}", this._attachmentBuilder, ExceptionUtils.exception2detail(e));
        }
    }
    return size;
}
Also used : HttpData(io.netty.handler.codec.http.multipart.HttpData) TransportException(org.jocean.http.TransportException) IOException(java.io.IOException) ErrorDataEncoderException(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException)

Example 2 with HttpData

use of io.netty.handler.codec.http.multipart.HttpData in project reactor-netty by reactor.

the class HttpServerPostFormTests method doTestPostForm.

private void doTestPostForm(HttpServer server, HttpClient client, Consumer<HttpServerFormDecoderProvider.Builder> provider, boolean configOnServer, boolean multipart, boolean streaming, @Nullable String expectedResponse) throws Exception {
    AtomicReference<List<HttpData>> originalHttpData1 = new AtomicReference<>(new ArrayList<>());
    AtomicReference<List<HttpData>> originalHttpData2 = new AtomicReference<>(new ArrayList<>());
    AtomicReference<Map<String, CompositeByteBuf>> copiedHttpData = new AtomicReference<>(new HashMap<>());
    server = (configOnServer ? server.httpFormDecoder(provider) : server).handle((req, res) -> res.sendString((configOnServer ? req.receiveForm() : req.receiveForm(provider)).flatMap(data -> {
        if ("0".equals(req.path())) {
            originalHttpData1.get().add(data);
            if (streaming) {
                CompositeByteBuf copy = copiedHttpData.get().computeIfAbsent(data.getName(), k -> Unpooled.compositeBuffer());
                try {
                    // In case of streaming this is not a blocking call
                    copy.writeBytes(data.get());
                } catch (IOException e) {
                    return Mono.error(e);
                }
            }
        } else {
            originalHttpData2.get().add(data);
        }
        return Mono.just('[' + data.getName() + ' ' + data.getClass().getSimpleName() + ' ' + data.isCompleted() + "] ");
    }).onErrorResume(t -> Mono.just(t.getCause().getMessage())).log()));
    disposableServer = server.bindNow();
    List<Tuple2<Integer, String>> responses;
    Path file = Paths.get(getClass().getResource("/largeFile1.txt").toURI());
    responses = Flux.range(0, 2).flatMap(i -> client.port(disposableServer.port()).post().uri("/" + i).sendForm((req, form) -> form.multipart(multipart).file("test1", "largeFile1.txt", file.toFile(), null).attr("attr1", "attr2").file("test2", "largeFile1.txt", file.toFile(), null)).responseSingle((r, buf) -> buf.asString().map(s -> Tuples.of(r.status().code(), s)))).collectList().block(Duration.ofSeconds(30));
    assertThat(responses).as("response").isNotNull();
    for (Tuple2<Integer, String> response : responses) {
        assertThat(response.getT1()).as("status code").isEqualTo(200);
        if (expectedResponse != null) {
            assertThat(response.getT2()).as("response body reflecting request").contains(expectedResponse);
        }
    }
    assertThat(originalHttpData1.get()).allMatch(data -> data.refCnt() == 0);
    assertThat(originalHttpData2.get()).allMatch(data -> data.refCnt() == 0);
    if (streaming) {
        if (expectedResponse == null) {
            assertThat(copiedHttpData.get()).hasSize(3);
            byte[] fileBytes = Files.readAllBytes(file);
            testContent(copiedHttpData.get().get("test1"), fileBytes);
            testContent(copiedHttpData.get().get("test2"), fileBytes);
            copiedHttpData.get().forEach((s, buffer) -> buffer.release());
        } else {
            List<HttpProtocol> serverProtocols = Arrays.asList(server.configuration().protocols());
            if (serverProtocols.size() == 1 && serverProtocols.get(0).equals(HttpProtocol.HTTP11)) {
                assertThat(copiedHttpData.get()).hasSize(1);
                copiedHttpData.get().forEach((s, buffer) -> buffer.release());
            } else {
                assertThat(copiedHttpData.get()).hasSize(0);
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) Tuples(reactor.util.function.Tuples) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) HashMap(java.util.HashMap) Nullable(reactor.util.annotation.Nullable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpData(io.netty.handler.codec.http.multipart.HttpData) Duration(java.time.Duration) Map(java.util.Map) Path(java.nio.file.Path) MethodSource(org.junit.jupiter.params.provider.MethodSource) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) Consumer(java.util.function.Consumer) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Paths(java.nio.file.Paths) HttpClient(reactor.netty.http.client.HttpClient) RetentionPolicy(java.lang.annotation.RetentionPolicy) Path(java.nio.file.Path) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpProtocol(reactor.netty.http.HttpProtocol) Tuple2(reactor.util.function.Tuple2) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HttpData (io.netty.handler.codec.http.multipart.HttpData)2 IOException (java.io.IOException)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 ErrorDataEncoderException (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException)1 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 ElementType (java.lang.annotation.ElementType)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1