Search in sources :

Example 36 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 37 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 38 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 39 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)

Example 40 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project pulsar by yahoo.

the class FunctionsImpl method createFunctionWithUrlAsync.

@Override
public CompletableFuture<Void> createFunctionWithUrlAsync(FunctionConfig functionConfig, String pkgUrl) {
    WebTarget path = functions.path(functionConfig.getTenant()).path(functionConfig.getNamespace()).path(functionConfig.getName());
    final FormDataMultiPart mp = new FormDataMultiPart();
    mp.bodyPart(new FormDataBodyPart("url", pkgUrl, MediaType.TEXT_PLAIN_TYPE));
    mp.bodyPart(new FormDataBodyPart("functionConfig", new Gson().toJson(functionConfig), MediaType.APPLICATION_JSON_TYPE));
    return asyncPostRequest(path, Entity.entity(mp, MediaType.MULTIPART_FORM_DATA));
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Gson(com.google.gson.Gson) WebTarget(javax.ws.rs.client.WebTarget)

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