use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class HttpRequestImpl method sendForm.
@Override
public void sendForm(MultiMap body, String charset, Handler<AsyncResult<HttpResponse<T>>> handler) {
MultipartForm parts = MultipartForm.create();
for (Map.Entry<String, String> attribute : body) {
parts.attribute(attribute.getKey(), attribute.getValue());
}
parts.setCharset(charset);
send("application/x-www-form-urlencoded", parts, handler);
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class HttpContext method handleCreateRequest.
private void handleCreateRequest() {
requestPromise = Promise.promise();
if (body != null || "application/json".equals(contentType)) {
if (body instanceof MultipartForm) {
MultipartFormUpload multipartForm;
try {
boolean multipart = "multipart/form-data".equals(contentType);
HttpPostRequestEncoder.EncoderMode encoderMode = this.request.multipartMixed ? HttpPostRequestEncoder.EncoderMode.RFC1738 : HttpPostRequestEncoder.EncoderMode.HTML5;
multipartForm = new MultipartFormUpload(context, (MultipartForm) this.body, multipart, encoderMode);
this.body = multipartForm;
} catch (Exception e) {
fail(e);
return;
}
for (String headerName : this.request.headers().names()) {
requestOptions.putHeader(headerName, this.request.headers().get(headerName));
}
multipartForm.headers().forEach(header -> {
requestOptions.putHeader(header.getKey(), header.getValue());
});
}
if (body instanceof ReadStream<?>) {
ReadStream<Buffer> stream = (ReadStream<Buffer>) body;
// Shouldn't this be called in an earlier phase ?
Pipe<Buffer> pipe = stream.pipe();
requestPromise.future().onComplete(ar -> {
if (ar.succeeded()) {
HttpClientRequest req = ar.result();
if (this.request.headers == null || !this.request.headers.contains(HttpHeaders.CONTENT_LENGTH)) {
req.setChunked(true);
}
pipe.endOnFailure(false);
pipe.to(req, ar2 -> {
clientRequest = null;
if (ar2.failed()) {
req.reset(0L, ar2.cause());
}
});
if (body instanceof MultipartFormUpload) {
((MultipartFormUpload) body).run();
}
} else {
// Test this
clientRequest = null;
pipe.close();
}
});
} else {
Buffer buffer;
if (body instanceof Buffer) {
buffer = (Buffer) body;
} else if (body instanceof JsonObject) {
buffer = Buffer.buffer(((JsonObject) body).encode());
} else {
buffer = Buffer.buffer(Json.encode(body));
}
requestOptions.putHeader(HttpHeaders.CONTENT_LENGTH, "" + buffer.length());
requestPromise.future().onSuccess(request -> {
clientRequest = null;
request.end(buffer);
});
}
} else {
requestPromise.future().onSuccess(request -> {
clientRequest = null;
request.end();
});
}
client.request(requestOptions).onComplete(ar1 -> {
if (ar1.succeeded()) {
sendRequest(ar1.result());
} else {
fail(ar1.cause());
requestPromise.fail(ar1.cause());
}
});
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class WebClientTest method testFileUploads.
@Test
public void testFileUploads() throws Exception {
server.requestHandler(req -> {
fail("Should not be called");
});
startServer();
HttpRequest<Buffer> builder = webClient.post("somepath");
MultipartForm form = MultipartForm.create().textFileUpload("file", "nonexistentFilename", "nonexistentPathname", "text/plain");
builder.sendMultipartForm(form, onFailure(err -> {
assertEquals(err.getClass(), HttpPostRequestEncoder.ErrorDataEncoderException.class);
complete();
}));
await();
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class WebClientTest method testFileUploadWhenFileDoesNotExist.
@Test
public void testFileUploadWhenFileDoesNotExist() {
HttpRequest<Buffer> builder = webClient.post("somepath");
MultipartForm form = MultipartForm.create().textFileUpload("file", "nonexistentFilename", "nonexistentPathname", "text/plain");
builder.sendMultipartForm(form, onFailure(err -> {
assertEquals(err.getClass(), HttpPostRequestEncoder.ErrorDataEncoderException.class);
assertEquals(err.getCause().getClass(), FileNotFoundException.class);
complete();
}));
await();
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class RouterBuilderIntegrationTest method testComplexMultipart.
@Timeout(2000)
@Test
public void testComplexMultipart(Vertx vertx, VertxTestContext testContext) {
Checkpoint checkpoint = testContext.checkpoint(2);
loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
routerBuilder.operation("complexMultipartRequest").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
if (params.body() == null) {
routingContext.response().setStatusCode(200).end();
} else {
routingContext.response().putHeader("content-type", "application/json").setStatusCode(200).end(params.body().getJsonObject().toBuffer());
}
});
}).onComplete(h -> {
JsonObject pet = new JsonObject();
pet.put("id", 14612);
pet.put("name", "Willy");
MultipartForm form = MultipartForm.create().textFileUpload("param1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("param2", pet.encode()).textFileUpload("param3", "random.csv", "src/test/resources/random.txt", "text/csv").attribute("param4", "1.2").attribute("param4", "5.2").attribute("param4", "6.2").attribute("param5", "2").binaryFileUpload("param1NotRealBinary", "random-file", "src/test/resources/random-file", "text/plain").binaryFileUpload("param1Binary", "random-file", "src/test/resources/random-file", "application/octet-stream");
JsonObject expected = new JsonObject().put("param2", pet).put("param4", new JsonArray().add(1.2).add(5.2).add(6.2)).put("param5", 2);
testRequest(client, HttpMethod.POST, "/multipart/complex").expect(statusCode(200)).expect(jsonBodyResponse(expected)).sendMultipartForm(form, testContext, checkpoint);
testRequest(client, HttpMethod.POST, "/multipart/complex").expect(statusCode(200)).send(testContext, checkpoint);
});
}
Aggregations