Skip to content

Commit 520f332

Browse files
committed
fix: Improvements to handling content types
1 parent 1f644fd commit 520f332

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

driver/src/main/java/uniresolver/driver/util/HttpBindingServerUtil.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import foundation.identity.did.representations.Representations;
6-
import foundation.identity.did.representations.consumption.RepresentationConsumerDID;
76
import foundation.identity.did.representations.production.RepresentationProducerDID;
87
import foundation.identity.did.representations.production.RepresentationProducerDIDCBOR;
98
import foundation.identity.did.representations.production.RepresentationProducerDIDJSON;
@@ -85,9 +84,9 @@ public static String httpBodyForResult(Result result) throws IOException {
8584
byte[] content = result.getFunctionContent();
8685
if (content == null || content.length == 0) {
8786
json.put(functionContentProperty, null);
88-
} else if (isContentTypeJson(result)) {
87+
} else if (isContentJson(result)) {
8988
json.put(functionContentProperty, objectMapper.readValue(new ByteArrayInputStream(content), LinkedHashMap.class));
90-
} else if (isContentTypeText(result)) {
89+
} else if (isContentText(result)) {
9190
json.put(functionContentProperty, new String(content, StandardCharsets.UTF_8));
9291
} else {
9392
json.put(functionContentProperty, Base64.getEncoder().encodeToString(content));
@@ -142,11 +141,13 @@ public static String dereferenceAcceptForHttpAccepts(List<MediaType> httpAcceptM
142141
* Helper methods
143142
*/
144143

145-
private static boolean isContentTypeJson(Result result) {
146-
return RepresentationProducerDID.MEDIA_TYPE.equals(result.getContentType()) || result.getContentType().contains("+json") || result.getContentType().contains("/json");
144+
private static boolean isContentJson(Result result) throws IOException {
145+
boolean isContentTypeJson = RepresentationProducerDID.MEDIA_TYPE.equals(result.getContentType()) || result.getContentType().contains("+json") || result.getContentType().contains("/json");
146+
boolean isContentJson = result.getFunctionContent()[0] == '{' || result.getFunctionContent()[0] == '[';
147+
return isContentTypeJson && isContentJson;
147148
}
148149

149-
private static boolean isContentTypeText(Result result) {
150+
private static boolean isContentText(Result result) {
150151
return result.getContentType().startsWith("text/");
151152
}
152153
}

0 commit comments

Comments
 (0)