Skip to content

Commit

Permalink
include provenance anchor on unsuccessful redirects; related to #199
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorrit Poelen committed Jan 22, 2024
1 parent a858b89 commit d05cc78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void handleRequest(HttpServletResponse response,
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
log("found [" + requestedIdIRI.getIRIString() + "] but no associated content");
} else {
populateReponseHeaderWithProvenanceGraphVersion(response, provInfo);
populateReponseHeaderWithProvenanceAnchor(response, getProvenanceId().getIRIString());
renderTemplate(
response,
URI.create(getResolverEndpoint() + requestedIdIRI.getIRIString()),
Expand All @@ -76,6 +76,7 @@ protected void handleRequest(HttpServletResponse response,
log("did not find origin of [" + requestedIdIRI.getIRIString() + "]");
}
} catch (Throwable th) {
populateReponseHeaderWithProvenanceAnchor(response, getProvenanceId().getIRIString());
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
log("not found [" + requestedIdIRI.getIRIString() + "]");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class RedirectingServlet extends HttpServlet {
public static final String DOI = "doi";
public static final String CONTENT_ID = "contentId";
public static final String ACTIVITY = "activity";
public static final String X_PROV_HAD_PRIMARY_SOURCE = "X-PROV-hadPrimarySource";

@Override
public void destroy() {
Expand Down Expand Up @@ -126,9 +127,11 @@ protected void handleRequest(HttpServletResponse response,
populateResponseHeader(response, provInfo);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
populateReponseHeaderWithProvenanceAnchor(response, getProvenanceId().getIRIString());
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
} catch (Throwable th) {
populateReponseHeaderWithProvenanceAnchor(response, getProvenanceId().getIRIString());
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
log("not found [" + requestedIdIRI.getIRIString() + "]");
try {
Expand Down Expand Up @@ -169,11 +172,11 @@ protected void populateResponseHeader(HttpServletResponse response, Map<String,
response.setHeader("X-PROV-wasGeneratedBy", provInfo.get(ACTIVITY));
response.setHeader("X-PROV-generatedAtTime", provInfo.get(SEEN_AT));
response.setHeader("X-DC-format", provInfo.get(CONTENT_TYPE));
populateReponseHeaderWithProvenanceGraphVersion(response, provInfo);
populateReponseHeaderWithProvenanceAnchor(response, provInfo.get(PROVENANCE_ID));
}

protected void populateReponseHeaderWithProvenanceGraphVersion(HttpServletResponse response, Map<String, String> provInfo) {
response.setHeader("X-PROV-hadPrimarySource", provInfo.get(PROVENANCE_ID));
protected void populateReponseHeaderWithProvenanceAnchor(HttpServletResponse response, String provenanceAnchor) {
response.setHeader(X_PROV_HAD_PRIMARY_SOURCE, provenanceAnchor);
}

protected URI getResolverURI(String resolverEndpoint, String contentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import bio.guoda.preston.MimeTypes;
import bio.guoda.preston.RefNodeFactory;
import bio.guoda.preston.ResourcesHTTP;
import org.apache.commons.rdf.api.IRI;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.hamcrest.core.Is;
import org.junit.Test;

Expand All @@ -18,6 +25,7 @@
import static bio.guoda.preston.server.RedirectingServlet.PROVENANCE_ID;
import static bio.guoda.preston.server.RedirectingServlet.SEEN_AT;
import static bio.guoda.preston.server.RedirectingServlet.UUID;
import static bio.guoda.preston.server.RedirectingServlet.X_PROV_HAD_PRIMARY_SOURCE;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -77,6 +85,32 @@ public void dealiasUUID_iDigBio_EML() throws IOException, URISyntaxException {
assertThat(contentId.get(CONTENT_TYPE), Is.is("application/eml"));
}

@Test
public void dealiasNonExistentUUID_iDigBio_EML() throws IOException, URISyntaxException {
String anchor = "hash://sha256/5b7fa37bf8b64e7c935c4ff3389e36f8dd162f0705410dd719fd089e1ea253cd";

CloseableHttpClient build = HttpClientBuilder.create().build();
String nonExistentUUID = "urn:uuid:c9316f11-d955-4472-a276-6a26a6514599";
CloseableHttpResponse response = build.execute(new HttpGet("http://localhost:8080/" + nonExistentUUID));

Header firstHeader = response.getFirstHeader(X_PROV_HAD_PRIMARY_SOURCE);
assertThat(firstHeader.getName(), Is.is(X_PROV_HAD_PRIMARY_SOURCE));
assertThat(firstHeader.getValue(), Is.is("urn:uuid:0659a54f-b713-4f86-a917-5be166a14110"));
}

@Test
public void dealiasBadgeNonExistentUUID_iDigBio_EML() throws IOException, URISyntaxException {
String anchor = "hash://sha256/5b7fa37bf8b64e7c935c4ff3389e36f8dd162f0705410dd719fd089e1ea253cd";

CloseableHttpClient build = HttpClientBuilder.create().build();
String nonExistentUUID = "urn:uuid:c9316f11-d955-4472-a276-6a26a6514599";
CloseableHttpResponse response = build.execute(new HttpGet("http://localhost:8080/badge/" + nonExistentUUID));

Header firstHeader = response.getFirstHeader(X_PROV_HAD_PRIMARY_SOURCE);
assertThat(firstHeader.getName(), Is.is(X_PROV_HAD_PRIMARY_SOURCE));
assertThat(firstHeader.getValue(), Is.is("urn:uuid:0659a54f-b713-4f86-a917-5be166a14110"));
}

@Test
public void dealiasContentIdGBIF() throws IOException, URISyntaxException {
Map<String, String> contentId = ProvUtil.findMostRecentContentId(
Expand Down

0 comments on commit d05cc78

Please sign in to comment.