Skip to content

Commit

Permalink
fix: Some updates to MIME type handling.
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Sabadello <markus@danubetech.com>
  • Loading branch information
peacekeeper committed Sep 25, 2020
1 parent b8c2c5c commit cbbc63a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class HttpDriver implements Driver {

public static final String MIME_TYPES = ResolveResult.MIME_TYPE + "," + DIDDocument.MIME_TYPE + "," + "application/ld+json";
public static final String MIME_TYPES = ResolveResult.MIME_TYPE + "," + DIDDocument.MIME_TYPE_JSON_LD + "," + "application/ld+json";

private static Logger log = LoggerFactory.getLogger(HttpDriver.class);

Expand Down
2 changes: 1 addition & 1 deletion resolver/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<!-- Dependency Versions -->

<key-formats-java.version>0.1.1</key-formats-java.version>
<did-common-java.version>0.1.0</did-common-java.version>
<did-common-java.version>0.2-SNAPSHOT</did-common-java.version>

<eclipse.jetty.version>9.4.18.v20190429</eclipse.jetty.version>
<slf4j.version>1.7.25</slf4j.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ public static ResolveResult build(@JsonProperty(value="didDocument", required=tr

public static ResolveResult build(DIDDocument didDocument, Map<String, Object> resolverMetadata, Map<String, Object> methodMetadata) {

return new ResolveResult(didDocument, null, DIDDocument.MIME_TYPE, resolverMetadata, methodMetadata);
return new ResolveResult(didDocument, null, null, resolverMetadata, methodMetadata);
}

public static ResolveResult build(DIDDocument didDocument) {

return new ResolveResult(didDocument, null, DIDDocument.MIME_TYPE, new HashMap<String, Object> (), new HashMap<String, Object> ());
return new ResolveResult(didDocument, null, null, new HashMap<String, Object> (), new HashMap<String, Object> ());
}

public static ResolveResult build(Map<String, Object> didDocument) {

return new ResolveResult(DIDDocument.build(didDocument), null, DIDDocument.MIME_TYPE, new HashMap<String, Object> (), new HashMap<String, Object> ());
return new ResolveResult(DIDDocument.build(didDocument), null, null, new HashMap<String, Object> (), new HashMap<String, Object> ());
}

public static ResolveResult build() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -11,6 +12,7 @@
import org.slf4j.LoggerFactory;

import did.DIDDocument;
import org.springframework.http.MediaType;
import uniresolver.result.ResolveResult;
import uniresolver.web.WebUniResolver;

Expand Down Expand Up @@ -58,44 +60,65 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
// execute the request

ResolveResult resolveResult;
String resolveResultString;

try {

resolveResult = this.resolve(identifier);
resolveResultString = resolveResult == null ? null : resolveResult.toJson();
} catch (Exception ex) {

if (log.isWarnEnabled()) log.warn("Resolve problem for " + identifier + ": " + ex.getMessage(), ex);
WebUniResolver.sendResponse(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null, "Resolve problem for " + identifier + ": " + ex.getMessage());
return;
}

if (log.isInfoEnabled()) log.info("Resolve result for " + identifier + ": " + resolveResultString);
if (log.isInfoEnabled()) log.info("Resolve result for " + identifier + ": " + resolveResult);

// no resolve result?

if (resolveResultString == null || (resolveResult.getDidDocument() == null && resolveResult.getContent() == null)) {
if (resolveResult == null || (resolveResult.getDidDocument() == null && resolveResult.getContent() == null)) {

WebUniResolver.sendResponse(response, HttpServletResponse.SC_NOT_FOUND, null, "No resolve result for " + identifier + ": " + resolveResultString);
WebUniResolver.sendResponse(response, HttpServletResponse.SC_NOT_FOUND, null, "No resolve result for " + identifier + ": " + resolveResult);
return;
}

// write resolve result

String acceptHeader = request.getHeader("Accept");
if ((acceptHeader == null || acceptHeader.contains(DIDDocument.MIME_TYPE)) && resolveResult.getDidDocument() != null) {
List<MediaType> acceptMediaTypes = MediaType.parseMediaTypes(acceptHeader != null ? acceptHeader : "*/*");
MediaType.sortBySpecificityAndQuality(acceptMediaTypes);

WebUniResolver.sendResponse(response, HttpServletResponse.SC_OK, DIDDocument.MIME_TYPE, resolveResultString);
return;
} else if (resolveResult.getContent() != null) {
for (MediaType acceptMediaType : acceptMediaTypes) {

WebUniResolver.sendResponse(response, HttpServletResponse.SC_OK, resolveResult.getContentType(), resolveResult.getContent());
return;
} else {
if (log.isDebugEnabled()) log.warn("Trying to find content for media type " + acceptMediaType);

WebUniResolver.sendResponse(response, HttpServletResponse.SC_OK, ResolveResult.MIME_TYPE, resolveResult.toJson());
return;
if (acceptMediaType.includes(MediaType.valueOf(ResolveResult.MIME_TYPE))) {

WebUniResolver.sendResponse(response, HttpServletResponse.SC_OK, ResolveResult.MIME_TYPE, resolveResult.toJson());
return;
} else if (acceptMediaType.includes(MediaType.valueOf(DIDDocument.MIME_TYPE_JSON_LD))) {

if (resolveResult.getDidDocument() == null) {

WebUniResolver.sendResponse(response, HttpServletResponse.SC_NOT_FOUND, null, "No DID document for " + identifier);
return;
}

WebUniResolver.sendResponse(response, HttpServletResponse.SC_OK, DIDDocument.MIME_TYPE_JSON_LD, resolveResult.getDidDocument().toJson());
return;
} else if (resolveResult.getContentType() != null && acceptMediaType.includes(MediaType.valueOf(resolveResult.getContentType()))) {

if (resolveResult.getContent() == null) {

WebUniResolver.sendResponse(response, HttpServletResponse.SC_NOT_FOUND, null, "No content for " + identifier);
return;
}

WebUniResolver.sendResponse(response, HttpServletResponse.SC_OK, resolveResult.getContentType(), resolveResult.getContent());
return;
}
}

WebUniResolver.sendResponse(response, HttpServletResponse.SC_NOT_ACCEPTABLE, null, "Not acceptable media type " + acceptHeader);
return;
}
}

0 comments on commit cbbc63a

Please sign in to comment.