Skip to content

Commit

Permalink
Fix for jersey2
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoMolinari93 committed Aug 29, 2024
1 parent a5be4d3 commit 0d8fc6b
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -988,24 +988,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
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();
// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
if (param.getValue() instanceof Iterable) {
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
}
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
Expand Down Expand Up @@ -1034,6 +1020,36 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
return entity;
}

/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
.fileName(file.getName()).size(file.length()).build();
// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
}
}

/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON, HTTP form is supported for now).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
MultiPart multiPart = new MultiPart();
for (Entry<String, Object> param: formParams.entrySet()) {
if (param.getValue() instanceof Iterable) {
Stream.of(param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
Expand Down Expand Up @@ -1020,6 +1020,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
return entity;
}

/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,24 +767,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
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();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
if (param.getValue() instanceof Iterable) {
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
}
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
Expand Down Expand Up @@ -813,6 +799,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
return entity;
}

/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
.fileName(file.getName()).size(file.length()).build();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
}
}

/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON, HTTP form is supported for now).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,24 +767,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
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();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
if (param.getValue() instanceof Iterable) {
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
}
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
Expand Down Expand Up @@ -813,6 +799,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
return entity;
}

/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
.fileName(file.getName()).size(file.length()).build();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
}
}

/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON, HTTP form is supported for now).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,24 +908,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
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();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
if (param.getValue() instanceof Iterable) {
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
}
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
Expand Down Expand Up @@ -954,6 +940,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
return entity;
}

/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
.fileName(file.getName()).size(file.length()).build();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
}
}

/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON, HTTP form is supported for now).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,24 +908,10 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
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();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
if (param.getValue() instanceof Iterable) {
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
}
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
Expand Down Expand Up @@ -954,6 +940,36 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
return entity;
}

/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
.fileName(file.getName()).size(file.length()).build();

// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
}
}

/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON, HTTP form is supported for now).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
MultiPart multiPart = new MultiPart();
for (Entry<String, Object> param: formParams.entrySet()) {
if (param.getValue() instanceof Iterable) {
Stream.of(param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
Expand Down Expand Up @@ -1022,6 +1022,14 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
return entity;
}

/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
Expand Down
Loading

0 comments on commit 0d8fc6b

Please sign in to comment.