diff --git a/resolver/java/driver-http/src/main/java/uniresolver/driver/http/HttpDriver.java b/resolver/java/driver-http/src/main/java/uniresolver/driver/http/HttpDriver.java index cc0388b45..1d4d9caac 100644 --- a/resolver/java/driver-http/src/main/java/uniresolver/driver/http/HttpDriver.java +++ b/resolver/java/driver-http/src/main/java/uniresolver/driver/http/HttpDriver.java @@ -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); diff --git a/resolver/java/pom.xml b/resolver/java/pom.xml index 5578c3025..2e50a6619 100644 --- a/resolver/java/pom.xml +++ b/resolver/java/pom.xml @@ -76,7 +76,7 @@ 0.1.1 - 0.1.0 + 0.2-SNAPSHOT 9.4.18.v20190429 1.7.25 diff --git a/resolver/java/uni-resolver-core/src/main/java/uniresolver/result/ResolveResult.java b/resolver/java/uni-resolver-core/src/main/java/uniresolver/result/ResolveResult.java index 4390e388f..6ceccf911 100644 --- a/resolver/java/uni-resolver-core/src/main/java/uniresolver/result/ResolveResult.java +++ b/resolver/java/uni-resolver-core/src/main/java/uniresolver/result/ResolveResult.java @@ -63,17 +63,17 @@ public static ResolveResult build(@JsonProperty(value="didDocument", required=tr public static ResolveResult build(DIDDocument didDocument, Map resolverMetadata, Map 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 (), new HashMap ()); + return new ResolveResult(didDocument, null, null, new HashMap (), new HashMap ()); } public static ResolveResult build(Map didDocument) { - return new ResolveResult(DIDDocument.build(didDocument), null, DIDDocument.MIME_TYPE, new HashMap (), new HashMap ()); + return new ResolveResult(DIDDocument.build(didDocument), null, null, new HashMap (), new HashMap ()); } public static ResolveResult build() { diff --git a/resolver/java/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java b/resolver/java/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java index a1c07e58d..ff08dce93 100644 --- a/resolver/java/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java +++ b/resolver/java/uni-resolver-web/src/main/java/uniresolver/web/servlet/ResolveServlet.java @@ -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; @@ -11,6 +12,7 @@ import org.slf4j.LoggerFactory; import did.DIDDocument; +import org.springframework.http.MediaType; import uniresolver.result.ResolveResult; import uniresolver.web.WebUniResolver; @@ -58,12 +60,10 @@ 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); @@ -71,31 +71,54 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t 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 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; } } \ No newline at end of file