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