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;
}
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);
}
}
}
}
Aggregations