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