Search in sources :

Example 26 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project opencga by opencb.

the class OpenCGAWSServerTest method testConnectivity.

/**
 * First echo message to test Server connectivity *
 */
@Test
public void testConnectivity() throws InterruptedException, IOException {
    String message = "Test";
    WebTarget testPath = webTarget.path("test").path("echo").path(message);
    System.out.println("testPath = " + testPath);
    String s = testPath.request().get(String.class);
    assertEquals("Expected [" + message + "], actual [" + s + "]", message, s);
    testPath = webTarget.path("test").path("echo");
    System.out.println("testPath = " + testPath);
    MultiPart multiPart = new MultiPart();
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
    FormDataBodyPart bodyPart = new FormDataBodyPart("message", message);
    multiPart.bodyPart(bodyPart);
    s = testPath.request().post(Entity.entity(multiPart, multiPart.getMediaType()), String.class);
    assertEquals("Expected [" + message + "], actual [" + s + "]", message, s);
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) WebTarget(javax.ws.rs.client.WebTarget)

Example 27 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project datadog-api-client-java by DataDog.

the class ApiClient method serialize.

/**
 * Serialize the given Java object into string entity according the given Content-Type (only JSON
 * is supported for now).
 *
 * @param obj Object
 * @param formParams Form parameters
 * @param contentType Context type
 * @return Entity
 */
public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType, String contentEncoding, boolean isBodyNullable) {
    Entity<?> entity;
    Variant variant = new Variant(MediaType.valueOf(contentType), "", contentEncoding);
    if (contentType.startsWith("multipart/form-data")) {
        MultiPart multiPart = new MultiPart();
        for (Entry<String, Object> param : formParams.entrySet()) {
            if (param.getValue() instanceof File) {
                File file = (File) param.getValue();
                FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).fileName(file.getName()).size(file.length()).build();
                multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
            } else {
                FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
                multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
            }
        }
        entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
    } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
        Form form = new Form();
        for (Entry<String, Object> param : formParams.entrySet()) {
            form.param(param.getKey(), parameterToString(param.getValue()));
        }
        entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    } else {
        // We let jersey handle the serialization
        if (isBodyNullable) {
            // payload is nullable
            if (obj instanceof String) {
                entity = Entity.entity(obj == null ? "null" : "\"" + ((String) obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", variant);
            } else {
                entity = Entity.entity(obj == null ? "null" : obj, variant);
            }
        } else {
            if (obj instanceof String) {
                entity = Entity.entity(obj == null ? "" : "\"" + ((String) obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", variant);
            } else {
                entity = Entity.entity(obj == null ? "" : obj, variant);
            }
        }
    }
    return entity;
}
Also used : Variant(javax.ws.rs.core.Variant) Entry(java.util.Map.Entry) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) Form(javax.ws.rs.core.Form) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) File(java.io.File)

Example 28 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project datadog-api-client-java by DataDog.

the class ApiClient method serialize.

/**
 * Serialize the given Java object into string entity according the given Content-Type (only JSON
 * is supported for now).
 *
 * @param obj Object
 * @param formParams Form parameters
 * @param contentType Context type
 * @return Entity
 */
public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType, String contentEncoding, boolean isBodyNullable) {
    Entity<?> entity;
    Variant variant = new Variant(MediaType.valueOf(contentType), "", contentEncoding);
    if (contentType.startsWith("multipart/form-data")) {
        MultiPart multiPart = new MultiPart();
        for (Entry<String, Object> param : formParams.entrySet()) {
            if (param.getValue() instanceof File) {
                File file = (File) param.getValue();
                FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).fileName(file.getName()).size(file.length()).build();
                multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
            } else {
                FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
                multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
            }
        }
        entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
    } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
        Form form = new Form();
        for (Entry<String, Object> param : formParams.entrySet()) {
            form.param(param.getKey(), parameterToString(param.getValue()));
        }
        entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    } else {
        // We let jersey handle the serialization
        if (isBodyNullable) {
            // payload is nullable
            if (obj instanceof String) {
                entity = Entity.entity(obj == null ? "null" : "\"" + ((String) obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", variant);
            } else {
                entity = Entity.entity(obj == null ? "null" : obj, variant);
            }
        } else {
            if (obj instanceof String) {
                entity = Entity.entity(obj == null ? "" : "\"" + ((String) obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", variant);
            } else {
                entity = Entity.entity(obj == null ? "" : obj, variant);
            }
        }
    }
    return entity;
}
Also used : Variant(javax.ws.rs.core.Variant) Entry(java.util.Map.Entry) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) Form(javax.ws.rs.core.Form) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) File(java.io.File)

Example 29 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project vnfsdk-refrepo by onap.

the class VTPExecutionResource method storeTestCaseInputFiles.

private Map<String, String> storeTestCaseInputFiles(List<FormDataBodyPart> bodyParts) throws IOException {
    Map<String, String> map = new HashMap<>();
    if (bodyParts != null) {
        for (FormDataBodyPart part : bodyParts) {
            String name = part.getContentDisposition().getFileName();
            // NOSONAR
            String path = VTP_EXECUTION_TEMP_STORE + "/" + name;
            File f = new File(path);
            if (f.exists()) {
                FileUtils.forceDelete(f);
            }
            FileUtils.forceMkdir(f.getParentFile());
            BodyPartEntity fileEntity = (BodyPartEntity) part.getEntity();
            java.nio.file.Files.copy(fileEntity.getInputStream(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);
            IOUtils.closeQuietly(fileEntity.getInputStream());
            map.put(name, path);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) File(java.io.File) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity)

Example 30 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project flexify-api-java by flexifyio.

the class ApiClient method serialize.

/**
 * Serialize the given Java object into string entity according the given
 * Content-Type (only JSON is supported for now).
 * @param obj Object
 * @param formParams Form parameters
 * @param contentType Context type
 * @return Entity
 * @throws ApiException API exception
 */
public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType) throws ApiException {
    Entity<?> entity;
    if (contentType.startsWith("multipart/form-data")) {
        MultiPart multiPart = new MultiPart();
        for (Entry<String, Object> param : formParams.entrySet()) {
            if (param.getValue() instanceof File) {
                File file = (File) param.getValue();
                FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).fileName(file.getName()).size(file.length()).build();
                multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
            } else {
                FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
                multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
            }
        }
        entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
    } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
        Form form = new Form();
        for (Entry<String, Object> param : formParams.entrySet()) {
            form.param(param.getKey(), parameterToString(param.getValue()));
        }
        entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    } else {
        // We let jersey handle the serialization
        entity = Entity.entity(obj, contentType);
    }
    return entity;
}
Also used : Entry(java.util.Map.Entry) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) Form(javax.ws.rs.core.Form) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) File(java.io.File)

Aggregations

FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)109 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)66 File (java.io.File)35 WebTarget (javax.ws.rs.client.WebTarget)33 Test (org.junit.Test)29 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)28 Response (javax.ws.rs.core.Response)27 InputStream (java.io.InputStream)17 JerseyTest (org.glassfish.jersey.test.JerseyTest)17 Gson (com.google.gson.Gson)16 CompletableFuture (java.util.concurrent.CompletableFuture)16 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)15 IOException (java.io.IOException)14 Map (java.util.Map)14 FormDataContentDisposition (org.glassfish.jersey.media.multipart.FormDataContentDisposition)14 HashMap (java.util.HashMap)12 List (java.util.List)12 Consumes (javax.ws.rs.Consumes)12 POST (javax.ws.rs.POST)11 Form (javax.ws.rs.core.Form)11