Search in sources :

Example 11 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testCompletedFileUploadWithNoPart.

@Test
public void testCompletedFileUploadWithNoPart() {
    MultipartBody body = MultipartBody.builder().addPart("filex", "", MediaType.APPLICATION_JSON_TYPE, new byte[0]).build();
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/upload/completed", body).contentType(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpClientResponseException ex = Assertions.assertThrows(HttpClientResponseException.class, () -> flowable.blockFirst());
    Map<String, Object> embedded = (Map<String, Object>) ex.getResponse().getBody(Map.class).get().get("_embedded");
    Object message = ((Map<String, Object>) ((List) embedded.get("errors")).get(0)).get("message");
    assertEquals("Required argument [CompletedFileUpload file] not specified", message);
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Map(java.util.Map) Test(org.junit.Test)

Example 12 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class DefaultHttpClient method buildMultipartRequest.

private HttpPostRequestEncoder buildMultipartRequest(MutableHttpRequest clientHttpRequest, Object bodyValue) throws HttpPostRequestEncoder.ErrorDataEncoderException {
    HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
    io.netty.handler.codec.http.HttpRequest request = NettyHttpRequestBuilder.toHttpRequest(clientHttpRequest);
    HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(factory, request, true, CharsetUtil.UTF_8, HttpPostRequestEncoder.EncoderMode.HTML5);
    if (bodyValue instanceof MultipartBody.Builder) {
        bodyValue = ((MultipartBody.Builder) bodyValue).build();
    }
    if (bodyValue instanceof MultipartBody) {
        final MultipartBody multipartBody = (MultipartBody) bodyValue;
        postRequestEncoder.setBodyHttpDatas(multipartBody.getData(new MultipartDataFactory<InterfaceHttpData>() {

            @NonNull
            @Override
            public InterfaceHttpData createFileUpload(@NonNull String name, @NonNull String filename, @NonNull MediaType contentType, @Nullable String encoding, @Nullable Charset charset, long length) {
                return factory.createFileUpload(request, name, filename, contentType.toString(), encoding, charset, length);
            }

            @NonNull
            @Override
            public InterfaceHttpData createAttribute(@NonNull String name, @NonNull String value) {
                return factory.createAttribute(request, name, value);
            }

            @Override
            public void setContent(InterfaceHttpData fileUploadObject, Object content) throws IOException {
                if (fileUploadObject instanceof FileUpload) {
                    FileUpload fu = (FileUpload) fileUploadObject;
                    if (content instanceof InputStream) {
                        fu.setContent((InputStream) content);
                    } else if (content instanceof File) {
                        fu.setContent((File) content);
                    } else if (content instanceof byte[]) {
                        final ByteBuf buffer = Unpooled.wrappedBuffer((byte[]) content);
                        fu.setContent(buffer);
                    }
                }
            }
        }));
    } else {
        throw new MultipartException(String.format("The type %s is not a supported type for a multipart request body", bodyValue.getClass().getName()));
    }
    return postRequestEncoder;
}
Also used : InputStream(java.io.InputStream) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) NettyClientSslBuilder(io.micronaut.http.client.netty.ssl.NettyClientSslBuilder) NettyHttpRequestBuilder(io.micronaut.http.netty.NettyHttpRequestBuilder) NettyHttpResponseBuilder(io.micronaut.http.netty.NettyHttpResponseBuilder) UriBuilder(io.micronaut.http.uri.UriBuilder) Charset(java.nio.charset.Charset) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) NonNull(io.micronaut.core.annotation.NonNull) MultipartException(io.micronaut.http.multipart.MultipartException) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) io.netty.handler.codec.http(io.netty.handler.codec.http) MediaType(io.micronaut.http.MediaType) MultipartDataFactory(io.micronaut.http.client.multipart.MultipartDataFactory) File(java.io.File) Nullable(io.micronaut.core.annotation.Nullable) FileUpload(io.netty.handler.codec.http.multipart.FileUpload)

Example 13 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class MultipartFileUploadSpec method testMultipartFileRequestByteArray.

@Test
public void testMultipartFileRequestByteArray() throws IOException {
    // tag::file[]
    String toWrite = "test file";
    File file = File.createTempFile("data", ".txt");
    FileWriter writer = new FileWriter(file);
    writer.write(toWrite);
    writer.close();
    // end::file[]
    // tag::multipartBody[]
    MultipartBody requestBody = // <1>
    MultipartBody.builder().addPart("data", file.getName(), MediaType.TEXT_PLAIN_TYPE, file).build();
    // end::multipartBody[]
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(// tag::request[]
    // <1>
    HttpRequest.POST("/multipart/upload", requestBody).contentType(// <2>
    MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpResponse<String> response = flowable.blockFirst();
    String body = response.getBody().get();
    assertEquals("Uploaded 9 bytes", body);
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) FileWriter(java.io.FileWriter) HttpResponse(io.micronaut.http.HttpResponse) File(java.io.File) Test(org.junit.Test)

Example 14 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class MultipartFileUploadSpec method testMultipartFileRequestByteArrayWithContentType.

@Test
public void testMultipartFileRequestByteArrayWithContentType() {
    // tag::multipartBodyBytes[]
    MultipartBody requestBody = MultipartBody.builder().addPart("data", "sample.txt", MediaType.TEXT_PLAIN_TYPE, "test content".getBytes()).build();
    // end::multipartBodyBytes[]
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/multipart/upload", requestBody).contentType(MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpResponse<String> response = flowable.blockFirst();
    String body = response.getBody().get();
    assertEquals("Uploaded 12 bytes", body);
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.Test)

Example 15 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project check-ins by objectcomputing.

the class EmployeeHoursControllerTest method testCreateEmployeeHoursWithNoAdminRole.

@Test
void testCreateEmployeeHoursWithNoAdminRole() {
    File file = new File("src/test/java/com/objectcomputing/checkins/services/employee_hours/test.csv");
    MultipartBody multipartBody = MultipartBody.builder().addPart("file", "test.csv", new MediaType("text/csv"), file).build();
    final HttpRequest<MultipartBody> request = HttpRequest.POST("/upload", multipartBody).basicAuth(MEMBER_ROLE, MEMBER_ROLE).contentType(MediaType.MULTIPART_FORM_DATA);
    HttpClientResponseException responseException = assertThrows(HttpClientResponseException.class, () -> client.toBlocking().exchange(request, Map.class));
    assertEquals(HttpStatus.BAD_REQUEST, responseException.getStatus());
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) MediaType(io.micronaut.http.MediaType) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

MultipartBody (io.micronaut.http.client.multipart.MultipartBody)17 HttpResponse (io.micronaut.http.HttpResponse)12 Test (org.junit.Test)10 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)6 MediaType (io.micronaut.http.MediaType)5 File (java.io.File)5 Map (java.util.Map)3 Test (org.junit.jupiter.api.Test)3 MemberProfile (com.objectcomputing.checkins.services.memberprofile.MemberProfile)2 NettyCookie (io.micronaut.http.netty.cookies.NettyCookie)2 EmployeeHours (com.objectcomputing.checkins.services.employee_hours.EmployeeHours)1 EmployeeHoursServices (com.objectcomputing.checkins.services.employee_hours.EmployeeHoursServices)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 MultipartDataFactory (io.micronaut.http.client.multipart.MultipartDataFactory)1 NettyClientSslBuilder (io.micronaut.http.client.netty.ssl.NettyClientSslBuilder)1 MultipartException (io.micronaut.http.multipart.MultipartException)1 NettyHttpRequestBuilder (io.micronaut.http.netty.NettyHttpRequestBuilder)1 NettyHttpResponseBuilder (io.micronaut.http.netty.NettyHttpResponseBuilder)1 UriBuilder (io.micronaut.http.uri.UriBuilder)1