From eede715ba69786b1c97ab8a0f9c8dbaa9804ab3e Mon Sep 17 00:00:00 2001 From: William Welling Date: Tue, 5 Jun 2018 16:36:39 -0500 Subject: [PATCH 1/9] head request for content type --- .../iiif/service/AbstractManifestService.java | 79 ++++++++++-------- .../edu/tamu/iiif/service/HttpService.java | 83 ++++++++++++------- .../dspace/AbstractDSpaceManifestService.java | 25 +++--- .../fedora/AbstractFedoraManifestService.java | 26 +++--- src/main/resources/application.properties | 3 - src/test/resources/application.properties | 3 - 6 files changed, 126 insertions(+), 93 deletions(-) diff --git a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java index ea30eaa..557e7e3 100644 --- a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java @@ -119,66 +119,60 @@ protected Optional getLicense(RdfResource rdfResource) { return Optional.empty(); } - protected String fetchImageInfo(String url) throws NotFoundException { - Optional imageInfo = Optional.ofNullable(httpService.get(url)); - if (imageInfo.isPresent()) { - return imageInfo.get(); - } - throw new NotFoundException("Image information not found!"); - } - - protected Image generateImage(RdfResource rdfResource, String canvasId) throws URISyntaxException { + protected Optional generateImage(RdfResource rdfResource, String canvasId) throws URISyntaxException { String url = rdfResource.getResource().getURI(); - Image image = new ImageImpl(getImageInfoUri(url)); - + Optional optionalImage = Optional.empty(); Optional imageResource = generateImageResource(rdfResource); if (imageResource.isPresent()) { + Image image = new ImageImpl(getImageInfoUri(url)); image.setResource(imageResource.get()); + image.setOn(getCanvasUri(canvasId)); + optionalImage = Optional.of(image); } - - image.setOn(getCanvasUri(canvasId)); - return image; + return optionalImage; } protected Optional generateImageResource(RdfResource rdfResource) throws URISyntaxException { String url = rdfResource.getResource().getURI(); - URI infoUri = getImageInfoUri(url); + Optional mimeType = getMimeType(url); Optional optionalImageResource = Optional.empty(); - Optional imageInfoNode = getImageInfo(infoUri.toString()); + if (mimeType.isPresent()) { + URI infoUri = getImageInfoUri(url); + + Optional imageInfoNode = getImageInfo(infoUri.toString()); - if (imageInfoNode.isPresent()) { - ImageResource imageResource = new ImageResourceImpl(getImageFullUrl(url)); + if (imageInfoNode.isPresent()) { + + ImageResource imageResource = new ImageResourceImpl(getImageFullUrl(url)); - Optional mimeType = Optional.empty(); - if (mimeType.isPresent()) { imageResource.setFormat(mimeType.get()); - } - imageResource.setHeight(imageInfoNode.get().get("height").asInt()); + imageResource.setHeight(imageInfoNode.get().get("height").asInt()); - imageResource.setWidth(imageInfoNode.get().get("width").asInt()); + imageResource.setWidth(imageInfoNode.get().get("width").asInt()); - imageResource.setServices(getServices(rdfResource, getIiifImageServiceName())); + imageResource.setServices(getServices(rdfResource, getIiifImageServiceName())); - optionalImageResource = Optional.of(imageResource); + optionalImageResource = Optional.of(imageResource); + } else { + LOG.info("Unable to get image info: " + infoUri.toString()); + } + } else { + LOG.info("Unable to get mime type: " + url); } return optionalImageResource; } - protected Optional getImageInfo(String url) { - Optional imageInfoNode = Optional.empty(); - - try { - imageInfoNode = Optional.of(objectMapper.readTree(fetchImageInfo(url))); - } catch (IOException e) { - LOG.warn(e.getMessage()); + protected String fetchImageInfo(String url) throws NotFoundException { + Optional imageInfo = Optional.ofNullable(httpService.get(url)); + if (imageInfo.isPresent()) { + return imageInfo.get(); } - - return imageInfoNode; + throw new NotFoundException("Image information not found!"); } protected URI getImageUri(String url) throws URISyntaxException { @@ -299,4 +293,21 @@ private Metadata generateMetadatum(Statement statement) throws IOException { return new MetadataImpl(label, value); } + private Optional getImageInfo(String url) { + Optional imageInfoNode = Optional.empty(); + + try { + imageInfoNode = Optional.of(objectMapper.readTree(fetchImageInfo(url))); + } catch (IOException e) { + LOG.info("Unable to get image info: " + url); + LOG.warn(e.getMessage()); + } + + return imageInfoNode; + } + + private Optional getMimeType(String url) { + return Optional.ofNullable(httpService.contentType(url)); + } + } diff --git a/src/main/java/edu/tamu/iiif/service/HttpService.java b/src/main/java/edu/tamu/iiif/service/HttpService.java index c682d8d..2dec121 100644 --- a/src/main/java/edu/tamu/iiif/service/HttpService.java +++ b/src/main/java/edu/tamu/iiif/service/HttpService.java @@ -1,19 +1,24 @@ package edu.tamu.iiif.service; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import org.apache.http.Header; import org.apache.http.NameValuePair; import org.apache.http.StatusLine; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; @@ -40,9 +45,6 @@ public class HttpService { @Value("${iiif.service.socket.timeout}") private int socketTimeout; - @Value("${iiif.service.request.retries}") - private int retries; - private CloseableHttpClient httpClient; @PostConstruct @@ -57,53 +59,72 @@ private void cleanUp() throws IOException { } public String get(String url) { - LOG.debug("Request: " + url); - return attemptGet(url, EMPTY_PARAMETERS, 0); + LOG.debug("GET: " + url); + return get(url, EMPTY_PARAMETERS); } public String get(String url, String context) { - LOG.debug("Request: " + url.concat("?context=").concat(context)); - return attemptGet(url, Arrays.asList(new BasicNameValuePair("context", context)), 0); + LOG.debug("GET: " + url.concat("?context=").concat(context)); + return get(url, Arrays.asList(new BasicNameValuePair("context", context))); } - private String attemptGet(String url, List parameters, int retry) { - String response = null; + public String contentType(String url) { + LOG.debug("HEAD: " + url); + String mimeType = null; try { - response = get(url, parameters); - } catch (IOException e) { - if (retry < retries) { - LOG.debug("Request failed. Retry attempt " + retry + "."); - response = attemptGet(url, parameters, ++retry); + CloseableHttpResponse response = request(craftHead(url, EMPTY_PARAMETERS)); + Optional
contentType = Optional.ofNullable(response.getFirstHeader("Content-Type")); + if (contentType.isPresent()) { + mimeType = contentType.get().getValue(); + LOG.debug("Mime Type: " + mimeType); } else { - LOG.warn(e.getMessage()); + LOG.warn("No Content-Type!"); } + response.close(); + } catch (IOException e) { + LOG.warn("Error performing GET request: " + url); } catch (URISyntaxException e) { - LOG.warn(e.getMessage()); + LOG.warn("Invalid URI: " + url); } - return response; + return mimeType; } - private String get(String url, List parameters) throws IOException, URISyntaxException { - CloseableHttpResponse response = httpClient.execute(craftRequest(url, parameters)); + private String get(String url, List parameters) { + String body = null; try { - StatusLine sl = response.getStatusLine(); - int sc = sl.getStatusCode(); - switch (sc) { - case 200: - break; - default: - throw new IOException("Incorrect response status: " + sc); - } - return EntityUtils.toString(response.getEntity()); - } finally { + CloseableHttpResponse response = request(craftGet(url, parameters)); + body = EntityUtils.toString(response.getEntity()); response.close(); + } catch (IOException e) { + LOG.warn("Error performing GET request: " + url); + } catch (URISyntaxException e) { + LOG.warn("Invalid URI: " + url); } + return body; + } + + private CloseableHttpResponse request(HttpRequestBase request) throws IOException, URISyntaxException { + CloseableHttpResponse response = httpClient.execute(request); + StatusLine sl = response.getStatusLine(); + int sc = sl.getStatusCode(); + if (sc < 200 || sc >= 300) { + throw new IOException("Incorrect response status: " + sc); + } + return response; + } + + private HttpGet craftGet(String url, List parameters) throws URISyntaxException { + return new HttpGet(buildUrl(url, parameters)); + } + + private HttpHead craftHead(String url, List parameters) throws URISyntaxException { + return new HttpHead(buildUrl(url, parameters)); } - private HttpGet craftRequest(String url, List parameters) throws URISyntaxException { + private URI buildUrl(String url, List parameters) throws URISyntaxException { URIBuilder builder = new URIBuilder(url); builder.setParameters(parameters); - return new HttpGet(builder.build()); + return builder.build(); } } diff --git a/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java b/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java index dfd8c08..c520556 100644 --- a/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java @@ -178,6 +178,7 @@ private String getRdf(String dspaceRdfUrl) throws NotFoundException { private List getCanvases(RdfResource rdfResource) throws IOException, URISyntaxException { List canvases = new ArrayList(); + // NOTE: canvas per bitstream NodeIterator collectionIterator = rdfResource.getAllNodesOfPropertyWithId(DSPACE_HAS_BITSTREAM_PREDICATE); while (collectionIterator.hasNext()) { String uri = collectionIterator.next().toString(); @@ -193,21 +194,23 @@ private RdfCanvas getDSpaceRdfCanvas(RdfResource rdfResource) throws URISyntaxEx RdfResource fileFedoraRdfResource = new RdfResource(rdfResource, uri); - Image image = generateImage(fileFedoraRdfResource, canvasId); + Optional image = generateImage(fileFedoraRdfResource, canvasId); + if (image.isPresent()) { - rdfCanvas.addImage(image); + rdfCanvas.addImage(image.get()); - Optional imageResource = Optional.ofNullable(image.getResource()); + Optional imageResource = Optional.ofNullable(image.get().getResource()); - if (imageResource.isPresent()) { - int height = imageResource.get().getHeight(); - if (height > rdfCanvas.getHeight()) { - rdfCanvas.setHeight(height); - } + if (imageResource.isPresent()) { + int height = imageResource.get().getHeight(); + if (height > rdfCanvas.getHeight()) { + rdfCanvas.setHeight(height); + } - int width = imageResource.get().getWidth(); - if (width > rdfCanvas.getWidth()) { - rdfCanvas.setWidth(width); + int width = imageResource.get().getWidth(); + if (width > rdfCanvas.getWidth()) { + rdfCanvas.setWidth(width); + } } } diff --git a/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java b/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java index d237e5e..473d23c 100644 --- a/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java @@ -226,27 +226,31 @@ private RdfCanvas getFedoraRdfCanvas(RdfResource rdfResource) throws URISyntaxEx String parentId = canvasStatement.getObject().toString(); + // NOTE: all resources within container + for (Resource resource : rdfResource.listResourcesWithPropertyWithId(FEDORA_HAS_PARENT_PREDICATE).toList()) { if (resource.getProperty(rdfResource.getProperty(FEDORA_HAS_PARENT_PREDICATE)).getObject().toString().equals(parentId)) { RdfResource fileFedoraRdfResource = new RdfResource(rdfResource, resource.getURI()); - Image image = generateImage(fileFedoraRdfResource, canvasId); + Optional image = generateImage(fileFedoraRdfResource, canvasId); - rdfCanvas.addImage(image); + if (image.isPresent()) { + rdfCanvas.addImage(image.get()); - Optional imageResource = Optional.ofNullable(image.getResource()); + Optional imageResource = Optional.ofNullable(image.get().getResource()); - if (imageResource.isPresent()) { - int height = imageResource.get().getHeight(); - if (height > rdfCanvas.getHeight()) { - rdfCanvas.setHeight(height); - } + if (imageResource.isPresent()) { + int height = imageResource.get().getHeight(); + if (height > rdfCanvas.getHeight()) { + rdfCanvas.setHeight(height); + } - int width = imageResource.get().getWidth(); - if (width > rdfCanvas.getWidth()) { - rdfCanvas.setWidth(width); + int width = imageResource.get().getWidth(); + if (width > rdfCanvas.getWidth()) { + rdfCanvas.setWidth(width); + } } } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f7e39db..dde7697 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -37,6 +37,3 @@ iiif.service.connection.request.timeout: 15000 # cache request socket timeout in milliseconds iiif.service.socket.timeout: 60000 - -# number of retries per request -iiif.service.request.retries: 3 diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index c06e0cf..7b7e1ee 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -37,6 +37,3 @@ iiif.service.connection.request.timeout: 120000 # cache request socket timeout, 2 minutes in milliseconds iiif.service.socket.timeout: 120000 - -# number of retries per request -iiif.service.request.retries: 3 From e59630e1b7bd8a30a2c695c2aad1b717c0b90afc Mon Sep 17 00:00:00 2001 From: William Welling Date: Tue, 5 Jun 2018 17:00:08 -0500 Subject: [PATCH 2/9] fixed tests --- .../edu/tamu/iiif/service/HttpServiceTest.java | 15 ++++++++++++++- .../dspace/DSpaceCanvasManifestServiceTest.java | 1 + .../DSpacePresentationManifestServiceTest.java | 1 + .../dspace/DSpaceSequenceManifestServiceTest.java | 1 + .../fedora/FedoraCanvasManifestServiceTest.java | 1 + .../FedoraPresentationManifestServiceTest.java | 4 ++++ .../fedora/FedoraSequenceManifestServiceTest.java | 2 ++ src/test/resources/mock/dspace/json/canvas.json | 1 + .../resources/mock/dspace/json/presentation.json | 1 + src/test/resources/mock/dspace/json/sequence.json | 1 + src/test/resources/mock/fedora/json/canvas.json | 1 + .../resources/mock/fedora/json/presentation.json | 2 ++ src/test/resources/mock/fedora/json/sequence.json | 2 ++ 13 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java b/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java index 91292ad..ce1de2e 100644 --- a/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java @@ -7,6 +7,7 @@ import java.io.IOException; import org.apache.commons.io.FileUtils; +import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; @@ -15,6 +16,7 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicStatusLine; import org.junit.Assert; import org.junit.Before; @@ -41,6 +43,8 @@ public class HttpServiceTest { private HttpEntity entity; + private Header header; + @InjectMocks private HttpService httpService; @@ -62,16 +66,18 @@ public void setup() throws ClientProtocolException, IOException { entity = mock(HttpEntity.class); + header = new BasicHeader("Content-Type", "image/jpeg"); + ReflectionTestUtils.setField(httpService, "connectionTimeout", 1000); ReflectionTestUtils.setField(httpService, "connectionRequestTimeout", 1000); ReflectionTestUtils.setField(httpService, "socketTimeout", 1000); - ReflectionTestUtils.setField(httpService, "retries", 3); ReflectionTestUtils.setField(httpService, "httpClient", httpClient); Assert.assertNotNull(httpService); when(response.getEntity()).thenReturn(entity); + when(response.getFirstHeader("Content-Type")).thenReturn(header); when(httpClient.execute(any(HttpGet.class))).thenReturn(response); @@ -85,6 +91,13 @@ public void testGet() throws UnsupportedOperationException, IOException { Assert.assertEquals(objectMapper.readValue(image0.getFile(), JsonNode.class), objectMapper.readValue(response, JsonNode.class)); } + @Test + public void testContentType() throws UnsupportedOperationException, IOException { + when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK")); + String response = httpService.contentType("http://localhost:8182/iiif/2/test/info.json"); + Assert.assertEquals(header.getValue(), response); + } + @Test public void testGetIncorrectStatusCode() throws UnsupportedOperationException, IOException { when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "BAD REQUEST")); diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java index 050e550..682337e 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java @@ -44,6 +44,7 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); + when(httpService.contentType(eq(DSPACE_URL + "/xmlui/bitstream/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn("image/png"); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/info.json"))).thenReturn(FileUtils.readFileToString(image.getFile(), "UTF-8")); String manifest = dSpaceCanvasManifestService.getManifest("123456789/158308/1/sports-car-146873_960_720.png", false); Assert.assertEquals(objectMapper.readValue(canvas.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java index 7080b78..6027c53 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java @@ -44,6 +44,7 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); + when(httpService.contentType(eq(DSPACE_URL + "/xmlui/bitstream/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn("image/png"); when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/info.json"))).thenReturn(FileUtils.readFileToString(image.getFile(), "UTF-8")); String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", false); diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java index 1a3d506..290f0b5 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java @@ -44,6 +44,7 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); + when(httpService.contentType(eq(DSPACE_URL + "/xmlui/bitstream/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn("image/png"); when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/info.json"))).thenReturn(FileUtils.readFileToString(image.getFile(), "UTF-8")); String manifest = dSpaceSequenceManifestService.getManifest("123456789/158308", false); diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java index f4a0b5e..0ebc14c 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java @@ -45,6 +45,7 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); + when(httpService.contentType(any(String.class))).thenReturn("image/png"); when(httpService.get(any(String.class))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); String manifest = fedoraCanvasManifestService.getManifest("cars_pcdm_objects/chevy/pages/page_0", false); Assert.assertEquals(objectMapper.readValue(canvas.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java index ecbcae8..63ba4cf 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java @@ -57,6 +57,8 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); + + when(httpService.contentType(any(String.class))).thenReturn("image/png"); when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_0_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy0Rdf.getFile(), "UTF-8")); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/info.json"))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); @@ -72,6 +74,8 @@ public void testGetManifest() throws IOException, URISyntaxException { @Test public void testGetManifestWithoutOrder() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdfWithoutOrder.getFile(), "UTF-8")); + + when(httpService.contentType(any(String.class))).thenReturn("image/png"); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/info.json"))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java index c76f09e..73cb7eb 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java @@ -54,6 +54,8 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); + + when(httpService.contentType(any(String.class))).thenReturn("image/png"); when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_0_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy0Rdf.getFile(), "UTF-8")); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/info.json"))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); diff --git a/src/test/resources/mock/dspace/json/canvas.json b/src/test/resources/mock/dspace/json/canvas.json index 2a29673..a59bfc6 100644 --- a/src/test/resources/mock/dspace/json/canvas.json +++ b/src/test/resources/mock/dspace/json/canvas.json @@ -10,6 +10,7 @@ "resource" : { "@id" : "http://localhost:8182/iiif/2/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/full/full/0/default.jpg", "@type" : "dctypes:Image", + "format": "image/png", "height" : 480, "service" : { "label" : "DSpace IIIF Image Resource Service", diff --git a/src/test/resources/mock/dspace/json/presentation.json b/src/test/resources/mock/dspace/json/presentation.json index eadb6c2..be1d46b 100644 --- a/src/test/resources/mock/dspace/json/presentation.json +++ b/src/test/resources/mock/dspace/json/presentation.json @@ -30,6 +30,7 @@ "resource": { "@id": "http://localhost:8182/iiif/2/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/full/full/0/default.jpg", "@type": "dctypes:Image", + "format": "image/png", "height": 480, "service": { "label": "DSpace IIIF Image Resource Service", diff --git a/src/test/resources/mock/dspace/json/sequence.json b/src/test/resources/mock/dspace/json/sequence.json index f1c3a7d..6a9c18e 100644 --- a/src/test/resources/mock/dspace/json/sequence.json +++ b/src/test/resources/mock/dspace/json/sequence.json @@ -13,6 +13,7 @@ "resource": { "@id": "http://localhost:8182/iiif/2/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/full/full/0/default.jpg", "@type": "dctypes:Image", + "format": "image/png", "height": 480, "service": { "label": "DSpace IIIF Image Resource Service", diff --git a/src/test/resources/mock/fedora/json/canvas.json b/src/test/resources/mock/fedora/json/canvas.json index 919cacf..ddd506c 100644 --- a/src/test/resources/mock/fedora/json/canvas.json +++ b/src/test/resources/mock/fedora/json/canvas.json @@ -10,6 +10,7 @@ "resource" : { "@id" : "http://localhost:8182/iiif/2/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/full/full/0/default.jpg", "@type" : "dctypes:Image", + "format": "image/png", "height" : 400, "service" : { "label" : "Fedora IIIF Image Resource Service", diff --git a/src/test/resources/mock/fedora/json/presentation.json b/src/test/resources/mock/fedora/json/presentation.json index 7881316..ee9a99a 100644 --- a/src/test/resources/mock/fedora/json/presentation.json +++ b/src/test/resources/mock/fedora/json/presentation.json @@ -27,6 +27,7 @@ "resource" : { "@id" : "http://localhost:8182/iiif/2/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/full/full/0/default.jpg", "@type" : "dctypes:Image", + "format": "image/png", "height" : 400, "service" : { "label" : "Fedora IIIF Image Resource Service", @@ -52,6 +53,7 @@ "resource" : { "@id" : "http://localhost:8182/iiif/2/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMS9maWxlcy9jYXIyLmpwZw==/full/full/0/default.jpg", "@type" : "dctypes:Image", + "format": "image/png", "height" : 1080, "service" : { "label" : "Fedora IIIF Image Resource Service", diff --git a/src/test/resources/mock/fedora/json/sequence.json b/src/test/resources/mock/fedora/json/sequence.json index b224b69..12a2289 100644 --- a/src/test/resources/mock/fedora/json/sequence.json +++ b/src/test/resources/mock/fedora/json/sequence.json @@ -13,6 +13,7 @@ "resource" : { "@id" : "http://localhost:8182/iiif/2/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/full/full/0/default.jpg", "@type" : "dctypes:Image", + "format": "image/png", "height" : 400, "service" : { "label" : "Fedora IIIF Image Resource Service", @@ -38,6 +39,7 @@ "resource" : { "@id" : "http://localhost:8182/iiif/2/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMS9maWxlcy9jYXIyLmpwZw==/full/full/0/default.jpg", "@type" : "dctypes:Image", + "format": "image/png", "height" : 1080, "service" : { "label" : "Fedora IIIF Image Resource Service", From b6cf88b1928711a8411cb9f32b43a16bdc1c96ec Mon Sep 17 00:00:00 2001 From: William Welling Date: Wed, 6 Jun 2018 10:27:21 -0500 Subject: [PATCH 3/9] increased coverage for http service --- .../tamu/iiif/service/HttpServiceTest.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java b/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java index ce1de2e..68870fa 100644 --- a/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/HttpServiceTest.java @@ -92,10 +92,11 @@ public void testGet() throws UnsupportedOperationException, IOException { } @Test - public void testContentType() throws UnsupportedOperationException, IOException { + public void testGetWithContext() throws UnsupportedOperationException, IOException { when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK")); - String response = httpService.contentType("http://localhost:8182/iiif/2/test/info.json"); - Assert.assertEquals(header.getValue(), response); + when(entity.getContent()).thenReturn(rdf.getInputStream()); + String response = httpService.get("http://localhost:9107", "pcdm_cars"); + Assert.assertEquals(FileUtils.readFileToString(rdf.getFile(), "UTF-8"), response); } @Test @@ -115,11 +116,32 @@ public void testGetMalformedUrl() throws UnsupportedOperationException, IOExcept } @Test - public void testGetWithContext() throws UnsupportedOperationException, IOException { + public void testContentType() throws UnsupportedOperationException, IOException { when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK")); - when(entity.getContent()).thenReturn(rdf.getInputStream()); - String response = httpService.get("http://localhost:9107", "pcdm_cars"); - Assert.assertEquals(FileUtils.readFileToString(rdf.getFile(), "UTF-8"), response); + String response = httpService.contentType("https://brandguide.tamu.edu/assets/downloads/logos/TAM-Logo.png"); + Assert.assertEquals(header.getValue(), response); + } + + @Test + public void testContentTypeNoContentTypeHeader() throws UnsupportedOperationException, IOException { + when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK")); + when(response.getFirstHeader("Content-Type")).thenReturn(null); + String response = httpService.contentType("https://brandguide.tamu.edu/assets/downloads/logos/TAM-Logo.png"); + Assert.assertNull(response); + } + + @Test + public void testContentTypeIncorrectStatusCode() throws UnsupportedOperationException, IOException { + when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "BAD REQUEST")); + String response = httpService.contentType("https://brandguide.tamu.edu/assets/downloads/logos/TAM-Logo.png"); + Assert.assertNull(response); + } + + @Test + public void testContentTypeMalformedUrl() throws UnsupportedOperationException, IOException { + when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK")); + String response = httpService.contentType("@foo://invalid.bar"); + Assert.assertNull(response); } } From eada10738db41b677a3c4000e1d1a938c7603cd2 Mon Sep 17 00:00:00 2001 From: William Welling Date: Wed, 6 Jun 2018 12:11:20 -0500 Subject: [PATCH 4/9] increased coverage for abstract manifest service --- .../service/AbstractManifestServiceTest.java | 2 +- ...DSpacePresentationManifestServiceTest.java | 32 ++++++++++++-- ...FedoraPresentationManifestServiceTest.java | 44 ++++++++++++++----- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java index c0c338a..311805e 100644 --- a/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java @@ -34,7 +34,7 @@ public abstract class AbstractManifestServiceTest implements ManifestServiceTest protected HttpService httpService; @Mock - private RedisManifestRepo redisManifestRepo; + protected RedisManifestRepo redisManifestRepo; @Before public void init() { diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java index 6027c53..f6aba33 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java @@ -1,10 +1,12 @@ package edu.tamu.iiif.service.dspace; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; import java.io.IOException; import java.net.URISyntaxException; +import java.util.Optional; import org.apache.commons.io.FileUtils; import org.junit.Assert; @@ -17,6 +19,10 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.model.ManifestType; +import edu.tamu.iiif.model.RedisManifest; +import edu.tamu.iiif.model.RepositoryType; + public class DSpacePresentationManifestServiceTest extends AbstractDSpaceManifestServiceTest { @InjectMocks @@ -43,13 +49,33 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { + setupMocks(); + String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", false); + Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); + } + + @Test + public void testGetManifestCached() throws IOException, URISyntaxException { + RedisManifest redisManifest = new RedisManifest("123456789/158308", ManifestType.PRESENTATION, RepositoryType.DSPACE, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); + when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); + String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", false); + Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); + } + + @Test + public void testGetManifestUpdateCached() throws IOException, URISyntaxException { + setupMocks(); + RedisManifest redisManifest = new RedisManifest("123456789/158308", ManifestType.PRESENTATION, RepositoryType.DSPACE, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); + when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); + String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", true); + Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); + } + + private void setupMocks() throws IOException { when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); when(httpService.contentType(eq(DSPACE_URL + "/xmlui/bitstream/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn("image/png"); when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/info.json"))).thenReturn(FileUtils.readFileToString(image.getFile(), "UTF-8")); - String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", false); - - Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } } diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java index 63ba4cf..65c5e1f 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.Optional; import org.apache.commons.io.FileUtils; import org.junit.Assert; @@ -18,6 +19,10 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.model.ManifestType; +import edu.tamu.iiif.model.RedisManifest; +import edu.tamu.iiif.model.RepositoryType; + public class FedoraPresentationManifestServiceTest extends AbstractFedoraManifestServiceTest { @InjectMocks @@ -56,25 +61,32 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { - when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); - - when(httpService.contentType(any(String.class))).thenReturn("image/png"); - - when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_0_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy0Rdf.getFile(), "UTF-8")); - when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/info.json"))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); - - when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_1_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy1Rdf.getFile(), "UTF-8")); - when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMS9maWxlcy9jYXIyLmpwZw==/info.json"))).thenReturn(FileUtils.readFileToString(image1.getFile(), "UTF-8")); + setupMocks(); + String manifest = fedoraPresentationManifestService.getManifest("cars_pcdm_objects/chevy", false); + Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); + } + @Test + public void testGetManifestCached() throws IOException, URISyntaxException { + RedisManifest redisManifest = new RedisManifest("cars_pcdm_objects/chevy", ManifestType.PRESENTATION, RepositoryType.FEDORA, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); + when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); String manifest = fedoraPresentationManifestService.getManifest("cars_pcdm_objects/chevy", false); + Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); + } + @Test + public void testGetManifestUpdateCached() throws IOException, URISyntaxException { + setupMocks(); + RedisManifest redisManifest = new RedisManifest("cars_pcdm_objects/chevy", ManifestType.PRESENTATION, RepositoryType.FEDORA, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); + when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); + String manifest = fedoraPresentationManifestService.getManifest("cars_pcdm_objects/chevy", true); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } @Test public void testGetManifestWithoutOrder() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdfWithoutOrder.getFile(), "UTF-8")); - + when(httpService.contentType(any(String.class))).thenReturn("image/png"); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/info.json"))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); @@ -86,4 +98,16 @@ public void testGetManifestWithoutOrder() throws IOException, URISyntaxException Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } + private void setupMocks() throws IOException { + when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); + + when(httpService.contentType(any(String.class))).thenReturn("image/png"); + + when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_0_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy0Rdf.getFile(), "UTF-8")); + when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMC9maWxlcy9QVEFSXzgwMHg0MDAucG5n/info.json"))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); + + when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_1_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy1Rdf.getFile(), "UTF-8")); + when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMS9maWxlcy9jYXIyLmpwZw==/info.json"))).thenReturn(FileUtils.readFileToString(image1.getFile(), "UTF-8")); + } + } From 8055420e14ac6a26bcbdc72aa49f5e0510c9c847 Mon Sep 17 00:00:00 2001 From: William Welling Date: Wed, 6 Jun 2018 16:27:29 -0500 Subject: [PATCH 5/9] applying allowed and disallowed content types --- .../AbstractManifestController.java | 6 +-- .../tamu/iiif/controller/ManifestBuilder.java | 30 ++++++++++++ .../tamu/iiif/controller/ManifestRequest.java | 47 ++++++++++++++++++ .../DSpaceCanvasManifestController.java | 14 +++++- .../DSpaceCollectionManifestController.java | 14 +++++- .../dspace/DSpaceImageManifestController.java | 14 +++++- .../DSpacePresentationManifestController.java | 14 +++++- .../DSpaceSequenceManifestController.java | 14 +++++- .../FedoraCanvasManifestController.java | 14 +++++- .../FedoraCollectionManifestController.java | 14 +++++- .../fedora/FedoraImageManifestController.java | 14 +++++- .../FedoraPresentationManifestController.java | 14 +++++- .../FedoraSequenceManifestController.java | 14 +++++- .../edu/tamu/iiif/model/RedisManifest.java | 42 ++++++++++------ .../iiif/model/repo/RedisManifestRepo.java | 2 +- .../iiif/service/AbstractManifestService.java | 49 +++++++++++++------ .../tamu/iiif/service/ManifestService.java | 4 +- .../dspace/AbstractDSpaceManifestService.java | 17 ++++--- .../dspace/DSpaceCanvasManifestService.java | 12 +++-- .../DSpaceCollectionManifestService.java | 6 ++- .../dspace/DSpaceImageManifestService.java | 6 ++- .../DSpacePresentationManifestService.java | 22 +++++---- .../dspace/DSpaceSequenceManifestService.java | 8 +-- .../fedora/AbstractFedoraManifestService.java | 25 +++++----- .../fedora/FedoraCanvasManifestService.java | 8 +-- .../FedoraCollectionManifestService.java | 12 +++-- .../fedora/FedoraImageManifestService.java | 6 ++- .../FedoraPresentationManifestService.java | 14 +++--- .../fedora/FedoraSequenceManifestService.java | 8 +-- .../iiif/controller/ManifestBuilderTest.java | 29 +++++++++++ .../iiif/controller/ManifestRequestTest.java | 29 +++++++++++ .../DSpaceCanvasManifestControllerTest.java | 3 +- ...SpaceCollectionManifestControllerTest.java | 3 +- .../DSpaceImageManifestControllerTest.java | 3 +- ...acePresentationManifestControllerTest.java | 3 +- .../DSpaceSequenceManifestControllerTest.java | 3 +- .../FedoraCanvasManifestControllerTest.java | 3 +- ...edoraCollectionManifestControllerTest.java | 3 +- .../FedoraImageManifestControllerTest.java | 3 +- ...oraPresentationManifestControllerTest.java | 3 +- .../FedoraSequenceManifestControllerTest.java | 3 +- .../tamu/iiif/model/RedisManifestTest.java | 24 +++++---- .../service/AbstractManifestServiceTest.java | 2 +- .../DSpaceCanvasManifestServiceTest.java | 4 +- .../DSpaceCollectionManifestServiceTest.java | 6 ++- .../DSpaceImageManifestServiceTest.java | 4 +- ...DSpacePresentationManifestServiceTest.java | 11 +++-- .../DSpaceSequenceManifestServiceTest.java | 4 +- .../FedoraCanvasManifestServiceTest.java | 4 +- .../FedoraCollectionManifestServiceTest.java | 4 +- .../FedoraImageManifestServiceTest.java | 4 +- ...FedoraPresentationManifestServiceTest.java | 13 ++--- .../FedoraSequenceManifestServiceTest.java | 6 ++- 53 files changed, 480 insertions(+), 158 deletions(-) create mode 100644 src/main/java/edu/tamu/iiif/controller/ManifestBuilder.java create mode 100644 src/main/java/edu/tamu/iiif/controller/ManifestRequest.java create mode 100644 src/test/java/edu/tamu/iiif/controller/ManifestBuilderTest.java create mode 100644 src/test/java/edu/tamu/iiif/controller/ManifestRequestTest.java diff --git a/src/main/java/edu/tamu/iiif/controller/AbstractManifestController.java b/src/main/java/edu/tamu/iiif/controller/AbstractManifestController.java index b8327b2..f806a37 100644 --- a/src/main/java/edu/tamu/iiif/controller/AbstractManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/AbstractManifestController.java @@ -19,9 +19,9 @@ public abstract class AbstractManifestController { @Autowired private S manifestService; - protected void sendManifest(HttpServletResponse response, String path, Boolean update) throws IOException, URISyntaxException { - setResponseFile(response, getFileName(path)); - sendJsonFile(response, manifestService.getManifest(path, update)); + protected void sendManifest(ManifestBuilder builder) throws IOException, URISyntaxException { + setResponseFile(builder.getResponse(), getFileName(builder.getRequest().getContext())); + sendJsonFile(builder.getResponse(), manifestService.getManifest(builder.getRequest())); } private void setResponseFile(HttpServletResponse response, String filename) { diff --git a/src/main/java/edu/tamu/iiif/controller/ManifestBuilder.java b/src/main/java/edu/tamu/iiif/controller/ManifestBuilder.java new file mode 100644 index 0000000..fcf0a63 --- /dev/null +++ b/src/main/java/edu/tamu/iiif/controller/ManifestBuilder.java @@ -0,0 +1,30 @@ +package edu.tamu.iiif.controller; + +import java.util.List; + +import javax.servlet.http.HttpServletResponse; + +public class ManifestBuilder { + + private final HttpServletResponse response; + + private final ManifestRequest request; + + public ManifestBuilder(HttpServletResponse response, ManifestRequest request) { + this.response = response; + this.request = request; + } + + public HttpServletResponse getResponse() { + return response; + } + + public ManifestRequest getRequest() { + return request; + } + + public static ManifestBuilder of(HttpServletResponse response, String path, boolean update, List allowed, List disallowed) { + return new ManifestBuilder(response, ManifestRequest.of(path, update, allowed, disallowed)); + } + +} diff --git a/src/main/java/edu/tamu/iiif/controller/ManifestRequest.java b/src/main/java/edu/tamu/iiif/controller/ManifestRequest.java new file mode 100644 index 0000000..09c8dba --- /dev/null +++ b/src/main/java/edu/tamu/iiif/controller/ManifestRequest.java @@ -0,0 +1,47 @@ +package edu.tamu.iiif.controller; + +import java.util.ArrayList; +import java.util.List; + +public class ManifestRequest { + + private final String context; + + private final boolean update; + + private final List allowed; + + private final List disallowed; + + public ManifestRequest(String context, boolean update, List allowed, List disallowed) { + this.context = context; + this.update = update; + this.allowed = allowed; + this.disallowed = disallowed; + } + + public String getContext() { + return context; + } + + public boolean isUpdate() { + return update; + } + + public String getAllowed() { + return String.join(",", allowed); + } + + public String getDisallowed() { + return String.join(",", disallowed); + } + + public static ManifestRequest of(String path, boolean update, List allowed, List disallowed) { + return new ManifestRequest(path, update, allowed, disallowed); + } + + public static ManifestRequest of(String path, boolean update) { + return new ManifestRequest(path, update, new ArrayList(), new ArrayList()); + } + +} diff --git a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestController.java b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestController.java index 7b04119..24d08a6 100644 --- a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.dspace.DSpaceCanvasManifestService; @RestController @@ -23,8 +25,16 @@ public class DSpaceCanvasManifestController extends AbstractManifestController { @RequestMapping("/" + CANVAS_IDENTIFIER) - public void image(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void image( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestController.java b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestController.java index 0581a7c..29cb999 100644 --- a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.dspace.DSpaceCollectionManifestService; @RestController @@ -23,8 +25,16 @@ public class DSpaceCollectionManifestController extends AbstractManifestController { @RequestMapping("/" + COLLECECTION_IDENTIFIER) - public void collection(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void collection( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestController.java b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestController.java index 8145df4..c622c85 100644 --- a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.dspace.DSpaceImageManifestService; @RestController @@ -23,8 +25,16 @@ public class DSpaceImageManifestController extends AbstractManifestController { @RequestMapping("/" + IMAGE_IDENTIFIER) - public void image(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void image( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestController.java b/src/main/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestController.java index 0fbee02..a6c388e 100644 --- a/src/main/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.dspace.DSpacePresentationManifestService; @RestController @@ -23,8 +25,16 @@ public class DSpacePresentationManifestController extends AbstractManifestController { @RequestMapping("/" + PRESENTATION_IDENTIFIER) - public void presentation(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void presentation( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestController.java b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestController.java index e32cf51..fa7881a 100644 --- a/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.dspace.DSpaceSequenceManifestService; @RestController @@ -23,8 +25,16 @@ public class DSpaceSequenceManifestController extends AbstractManifestController { @RequestMapping("/" + SEQUENCE_IDENTIFIER) - public void image(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void image( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestController.java b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestController.java index 452a7e4..2c65d6c 100644 --- a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.fedora.FedoraCanvasManifestService; @RestController @@ -23,8 +25,16 @@ public class FedoraCanvasManifestController extends AbstractManifestController { @RequestMapping("/" + CANVAS_IDENTIFIER) - public void image(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void image( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestController.java b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestController.java index 4f901b2..73c5245 100644 --- a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.fedora.FedoraCollectionManifestService; @RestController @@ -23,8 +25,16 @@ public class FedoraCollectionManifestController extends AbstractManifestController { @RequestMapping("/" + COLLECECTION_IDENTIFIER) - public void collection(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void collection( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestController.java b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestController.java index a6bbf83..01e5b91 100644 --- a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.fedora.FedoraImageManifestService; @RestController @@ -23,8 +25,16 @@ public class FedoraImageManifestController extends AbstractManifestController { @RequestMapping("/" + IMAGE_IDENTIFIER) - public void image(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void image( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestController.java b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestController.java index b195bbc..e45b11c 100644 --- a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.fedora.FedoraPresentationManifestService; @RestController @@ -23,8 +25,16 @@ public class FedoraPresentationManifestController extends AbstractManifestController { @RequestMapping("/" + PRESENTATION_IDENTIFIER) - public void presentation(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void presentation( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestController.java b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestController.java index e86bcfa..743604b 100644 --- a/src/main/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestController.java +++ b/src/main/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestController.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import edu.tamu.iiif.controller.AbstractManifestController; +import edu.tamu.iiif.controller.ManifestBuilder; import edu.tamu.iiif.service.fedora.FedoraSequenceManifestService; @RestController @@ -23,8 +25,16 @@ public class FedoraSequenceManifestController extends AbstractManifestController { @RequestMapping("/" + SEQUENCE_IDENTIFIER) - public void image(HttpServletResponse response, @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, @RequestParam(value = "update", required = false, defaultValue = "false") boolean update) throws IOException, URISyntaxException { - sendManifest(response, path, update); + public void image( + // @formatter:off + HttpServletResponse response, + @RequestParam(value = CONTEXT_IDENTIFIER, required = true) String path, + @RequestParam(value = "update", required = false, defaultValue = "false") boolean update, + @RequestParam(value = "allow", required = false, defaultValue = "") List allowed, + @RequestParam(value = "disallow", required = false, defaultValue = "") List disallowed + // @formatter:on + ) throws IOException, URISyntaxException { + sendManifest(ManifestBuilder.of(response, path, update, allowed, disallowed)); } } diff --git a/src/main/java/edu/tamu/iiif/model/RedisManifest.java b/src/main/java/edu/tamu/iiif/model/RedisManifest.java index 2fd5865..8decfa3 100644 --- a/src/main/java/edu/tamu/iiif/model/RedisManifest.java +++ b/src/main/java/edu/tamu/iiif/model/RedisManifest.java @@ -13,28 +13,42 @@ public class RedisManifest { private String id; @Indexed - private String path; + private final String path; @Indexed - private ManifestType type; + private final ManifestType type; @Indexed - private RepositoryType repository; + private final RepositoryType repository; + + @Indexed + private final String allowed; + + @Indexed + private final String disallowed; private String json; private final Long creation; - public RedisManifest() { + public RedisManifest(String path, ManifestType type, RepositoryType repository, String json) { this.creation = new Date().getTime(); + this.path = path; + this.type = type; + this.repository = repository; + this.json = json; + this.allowed = ""; + this.disallowed = ""; } - public RedisManifest(String path, ManifestType type, RepositoryType repository, String json) { - this(); + public RedisManifest(String path, ManifestType type, RepositoryType repository, String allowed, String disallowed, String json) { + this.creation = new Date().getTime(); this.path = path; this.type = type; this.repository = repository; this.json = json; + this.allowed = allowed; + this.disallowed = disallowed; } public String getId() { @@ -49,24 +63,20 @@ public String getPath() { return path; } - public void setPath(String path) { - this.path = path; - } - public ManifestType getType() { return type; } - public void setType(ManifestType type) { - this.type = type; - } - public RepositoryType getRepository() { return repository; } - public void setRepository(RepositoryType repository) { - this.repository = repository; + public String getAllowed() { + return allowed; + } + + public String getDisallowed() { + return disallowed; } public String getJson() { diff --git a/src/main/java/edu/tamu/iiif/model/repo/RedisManifestRepo.java b/src/main/java/edu/tamu/iiif/model/repo/RedisManifestRepo.java index 4c7e22d..8e09fbb 100644 --- a/src/main/java/edu/tamu/iiif/model/repo/RedisManifestRepo.java +++ b/src/main/java/edu/tamu/iiif/model/repo/RedisManifestRepo.java @@ -10,6 +10,6 @@ public interface RedisManifestRepo extends CrudRepository { - Optional findByPathAndTypeAndRepository(String path, ManifestType type, RepositoryType repository); + Optional findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(String path, ManifestType type, RepositoryType repository, String allowed, String disallowed); } diff --git a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java index 557e7e3..86c9735 100644 --- a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java @@ -41,6 +41,7 @@ import de.digitalcollections.iiif.presentation.model.impl.v2.MetadataImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.PropertyValueSimpleImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.ServiceImpl; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.exception.NotFoundException; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.RedisManifest; @@ -79,24 +80,25 @@ private void init() { mapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); } - public String getManifest(String path, boolean update) throws IOException, URISyntaxException { - + public String getManifest(ManifestRequest request) throws IOException, URISyntaxException { + String path = request.getContext(); + boolean update = request.isUpdate(); String manifest; - Optional optionalRedisManifest = getRedisManifest(path); + Optional optionalRedisManifest = getRedisManifest(request); if (optionalRedisManifest.isPresent()) { LOG.info("Manifest already in redis: " + optionalRedisManifest.get().getId() + " (" + optionalRedisManifest.get().getCreation() + ")"); manifest = optionalRedisManifest.get().getJson(); } else { LOG.info("Generating new manifest."); - manifest = generateManifest(path); - redisManifestRepo.save(new RedisManifest(StringUtility.encode(path), getManifestType(), getRepositoryType(), manifest)); + manifest = generateManifest(request); + redisManifestRepo.save(new RedisManifest(StringUtility.encode(path), getManifestType(), getRepositoryType(), request.getAllowed(), request.getDisallowed(), manifest)); update = false; } if (update) { RedisManifest redisManifest = optionalRedisManifest.get(); - manifest = generateManifest(path); + manifest = generateManifest(request); redisManifest.setJson(manifest); redisManifestRepo.save(redisManifest); LOG.info("Manifest update requested: " + path); @@ -119,10 +121,10 @@ protected Optional getLicense(RdfResource rdfResource) { return Optional.empty(); } - protected Optional generateImage(RdfResource rdfResource, String canvasId) throws URISyntaxException { + protected Optional generateImage(ManifestRequest request, RdfResource rdfResource, String canvasId) throws URISyntaxException { String url = rdfResource.getResource().getURI(); Optional optionalImage = Optional.empty(); - Optional imageResource = generateImageResource(rdfResource); + Optional imageResource = generateImageResource(request, rdfResource); if (imageResource.isPresent()) { Image image = new ImageImpl(getImageInfoUri(url)); image.setResource(imageResource.get()); @@ -132,14 +134,33 @@ protected Optional generateImage(RdfResource rdfResource, String canvasId return optionalImage; } - protected Optional generateImageResource(RdfResource rdfResource) throws URISyntaxException { + protected Optional generateImageResource(ManifestRequest request, RdfResource rdfResource) throws URISyntaxException { String url = rdfResource.getResource().getURI(); + Optional optionalImageResource = Optional.empty(); + Optional mimeType = getMimeType(url); - Optional optionalImageResource = Optional.empty(); + boolean include = false; if (mimeType.isPresent()) { + include = true; + String allowed = request.getAllowed(); + if (allowed.length() > 0) { + include = allowed.contains(mimeType.get()); + } else { + String disallowed = request.getDisallowed(); + if (disallowed.length() > 0) { + if (disallowed.contains(mimeType.get())) { + include = false; + } + } + } + } else { + LOG.info("Unable to get mime type: " + url); + } + + if (include) { URI infoUri = getImageInfoUri(url); Optional imageInfoNode = getImageInfo(infoUri.toString()); @@ -161,7 +182,7 @@ protected Optional generateImageResource(RdfResource rdfResource) LOG.info("Unable to get image info: " + infoUri.toString()); } } else { - LOG.info("Unable to get mime type: " + url); + LOG.info("Excluding: " + url); } return optionalImageResource; @@ -227,7 +248,7 @@ protected String getHandle(String uri) { return handle; } - protected abstract String generateManifest(String handle) throws URISyntaxException, IOException; + protected abstract String generateManifest(ManifestRequest request) throws URISyntaxException, IOException; protected abstract String getIiifServiceUrl(); @@ -249,8 +270,8 @@ private Service getService(RdfResource rdfResource, String name) throws URISynta return service; } - private Optional getRedisManifest(String path) { - return redisManifestRepo.findByPathAndTypeAndRepository(StringUtility.encode(path), getManifestType(), getRepositoryType()); + private Optional getRedisManifest(ManifestRequest request) { + return redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(StringUtility.encode(request.getContext()), getManifestType(), getRepositoryType(), request.getAllowed(), request.getDisallowed()); } private List getMetadata(RdfResource rdfResource, String prefix) { diff --git a/src/main/java/edu/tamu/iiif/service/ManifestService.java b/src/main/java/edu/tamu/iiif/service/ManifestService.java index daed0e7..11c5acf 100644 --- a/src/main/java/edu/tamu/iiif/service/ManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/ManifestService.java @@ -3,8 +3,10 @@ import java.io.IOException; import java.net.URISyntaxException; +import edu.tamu.iiif.controller.ManifestRequest; + public interface ManifestService { - public String getManifest(String path, boolean update) throws IOException, URISyntaxException; + public String getManifest(ManifestRequest request) throws IOException, URISyntaxException; } diff --git a/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java b/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java index c520556..9cb14ed 100644 --- a/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/dspace/AbstractDSpaceManifestService.java @@ -40,6 +40,7 @@ import de.digitalcollections.iiif.presentation.model.impl.v2.CanvasImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.PropertyValueSimpleImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.SequenceImpl; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.exception.NotFoundException; import edu.tamu.iiif.model.RepositoryType; import edu.tamu.iiif.model.rdf.RdfCanvas; @@ -64,21 +65,21 @@ protected RdfResource getDSpaceRdfModel(String handle) throws NotFoundException return new RdfResource(model, model.getResource(dspaceRdfUrl)); } - protected Sequence generateSequence(RdfResource rdfResource) throws IOException, URISyntaxException { + protected Sequence generateSequence(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { String uri = rdfResource.getResource().getURI(); String handle = getHandle(uri); PropertyValueSimpleImpl label = new PropertyValueSimpleImpl(handle); Sequence sequence = new SequenceImpl(getDSpaceIiifSequenceUri(handle), label); - sequence.setCanvases(getCanvases(rdfResource)); + sequence.setCanvases(getCanvases(request, rdfResource)); return sequence; } - protected Canvas generateCanvas(RdfResource rdfResource) throws IOException, URISyntaxException { + protected Canvas generateCanvas(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { String uri = rdfResource.getResource().getURI(); String handle = getHandle(uri); PropertyValueSimpleImpl label = new PropertyValueSimpleImpl(handle); - RdfCanvas rdfCanvas = getDSpaceRdfCanvas(rdfResource); + RdfCanvas rdfCanvas = getDSpaceRdfCanvas(request, rdfResource); Canvas canvas = new CanvasImpl(getDSpaceIiifCanvasUri(handle), label, rdfCanvas.getHeight(), rdfCanvas.getWidth()); @@ -176,25 +177,25 @@ private String getRdf(String dspaceRdfUrl) throws NotFoundException { throw new NotFoundException("DSpace RDF not found!"); } - private List getCanvases(RdfResource rdfResource) throws IOException, URISyntaxException { + private List getCanvases(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { List canvases = new ArrayList(); // NOTE: canvas per bitstream NodeIterator collectionIterator = rdfResource.getAllNodesOfPropertyWithId(DSPACE_HAS_BITSTREAM_PREDICATE); while (collectionIterator.hasNext()) { String uri = collectionIterator.next().toString(); - canvases.add(generateCanvas(new RdfResource(rdfResource, uri))); + canvases.add(generateCanvas(request, new RdfResource(rdfResource, uri))); } return canvases; } - private RdfCanvas getDSpaceRdfCanvas(RdfResource rdfResource) throws URISyntaxException { + private RdfCanvas getDSpaceRdfCanvas(ManifestRequest request, RdfResource rdfResource) throws URISyntaxException { String uri = rdfResource.getResource().getURI(); RdfCanvas rdfCanvas = new RdfCanvas(); String canvasId = getHandle(uri); RdfResource fileFedoraRdfResource = new RdfResource(rdfResource, uri); - Optional image = generateImage(fileFedoraRdfResource, canvasId); + Optional image = generateImage(request, fileFedoraRdfResource, canvasId); if (image.isPresent()) { rdfCanvas.addImage(image.get()); diff --git a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestService.java b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestService.java index 36dcd4d..ac6a8f3 100644 --- a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestService.java @@ -8,22 +8,24 @@ import org.springframework.stereotype.Service; import de.digitalcollections.iiif.presentation.model.api.v2.Canvas; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @Service public class DSpaceCanvasManifestService extends AbstractDSpaceManifestService { - public String generateManifest(String path) throws IOException, URISyntaxException { - String handle = extractHandle(path); + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); + String handle = extractHandle(context); RdfResource rdfResource = getDSpaceRdfModel(handle); String url = rdfResource.getResource().getURI(); - Canvas canvas = generateCanvas(new RdfResource(rdfResource, url.replace("rdf/handle", dspaceWebapp != null && dspaceWebapp.length() > 0 ? dspaceWebapp + "/bitstream" : "bitstream").replaceAll(handle, path))); + Canvas canvas = generateCanvas(request, new RdfResource(rdfResource, url.replace("rdf/handle", dspaceWebapp != null && dspaceWebapp.length() > 0 ? dspaceWebapp + "/bitstream" : "bitstream").replaceAll(handle, context))); return mapper.writeValueAsString(canvas); } - private String extractHandle(String path) { - String[] parts = path.split("/"); + private String extractHandle(String context) { + String[] parts = context.split("/"); return parts[0] + "/" + parts[1]; } diff --git a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestService.java b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestService.java index fbfd011..1897ec6 100644 --- a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestService.java @@ -22,6 +22,7 @@ import de.digitalcollections.iiif.presentation.model.impl.v2.PropertyValueSimpleImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.references.CollectionReferenceImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.references.ManifestReferenceImpl; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.exception.NotFoundException; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @@ -30,8 +31,9 @@ public class DSpaceCollectionManifestService extends AbstractDSpaceManifestService { @Override - protected String generateManifest(String handle) throws URISyntaxException, IOException { - return mapper.writeValueAsString(generateCollection(handle)); + protected String generateManifest(ManifestRequest request) throws URISyntaxException, IOException { + String context = request.getContext(); + return mapper.writeValueAsString(generateCollection(context)); } private Collection generateCollection(String handle) throws URISyntaxException, NotFoundException { diff --git a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestService.java b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestService.java index 8f13f39..ac53539 100644 --- a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestService.java @@ -9,13 +9,15 @@ import org.springframework.stereotype.Service; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; @Service public class DSpaceImageManifestService extends AbstractDSpaceManifestService { - public String generateManifest(String path) throws IOException, URISyntaxException { - String dspacePath = dspaceWebapp != null && dspaceWebapp.length() > 0 ? joinPath(dspaceUrl, dspaceWebapp, "bitstream", path) : joinPath(dspaceUrl, "bitstream", path); + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); + String dspacePath = dspaceWebapp != null && dspaceWebapp.length() > 0 ? joinPath(dspaceUrl, dspaceWebapp, "bitstream", context) : joinPath(dspaceUrl, "bitstream", context); URI uri = getImageUri(dspacePath); return fetchImageInfo(uri.toString()); } diff --git a/src/main/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestService.java b/src/main/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestService.java index 43f93bc..2c136ac 100644 --- a/src/main/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestService.java @@ -23,17 +23,19 @@ import de.digitalcollections.iiif.presentation.model.impl.v2.ManifestImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.PropertyValueSimpleImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.ThumbnailImpl; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @Service public class DSpacePresentationManifestService extends AbstractDSpaceManifestService { - public String generateManifest(String handle) throws IOException, URISyntaxException { + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); - RdfResource rdfResource = getDSpaceRdfModel(handle); + RdfResource rdfResource = getDSpaceRdfModel(context); - URI id = buildId(handle); + URI id = buildId(context); PropertyValueSimpleImpl label = getTitle(rdfResource); @@ -41,7 +43,7 @@ public String generateManifest(String handle) throws IOException, URISyntaxExcep manifest.setDescription(getDescription(rdfResource)); - List sequences = getSequences(rdfResource); + List sequences = getSequences(request, rdfResource); manifest.setSequences(sequences); @@ -66,11 +68,11 @@ public String generateManifest(String handle) throws IOException, URISyntaxExcep return mapper.writeValueAsString(manifest); } - private List getSequences(RdfResource rdfResource) throws IOException, URISyntaxException { + private List getSequences(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { List sequences = new ArrayList(); // NOTE: flattening all sequence canvases into a single canvas, feature parity with Fedora presentation - List broadSequences = aggregateSequences(rdfResource); + List broadSequences = aggregateSequences(request, rdfResource); if (broadSequences.size() > 0) { Sequence sequence = broadSequences.get(0); @@ -92,7 +94,7 @@ private List getSequences(RdfResource rdfResource) throws IOException, return sequences; } - private List aggregateSequences(RdfResource rdfResource) throws IOException, URISyntaxException { + private List aggregateSequences(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { List sequences = new ArrayList(); if (isTopLevelCommunity(rdfResource.getModel()) || isSubcommunity(rdfResource.getModel())) { @@ -100,7 +102,7 @@ private List aggregateSequences(RdfResource rdfResource) throws IOExce while (collectionIterator.hasNext()) { String uri = collectionIterator.next().toString(); String handle = getHandle(uri); - sequences.addAll(aggregateSequences(getDSpaceRdfModel(handle))); + sequences.addAll(aggregateSequences(request, getDSpaceRdfModel(handle))); } } else if (isCollection(rdfResource.getModel())) { @@ -109,11 +111,11 @@ private List aggregateSequences(RdfResource rdfResource) throws IOExce while (collectionIterator.hasNext()) { String uri = collectionIterator.next().toString(); String handle = getHandle(uri); - sequences.addAll(aggregateSequences(getDSpaceRdfModel(handle))); + sequences.addAll(aggregateSequences(request, getDSpaceRdfModel(handle))); } } else if (isItem(rdfResource.getModel())) { - sequences.add(generateSequence(rdfResource)); + sequences.add(generateSequence(request, rdfResource)); } else { } diff --git a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestService.java b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestService.java index 68165f5..f2d807f 100644 --- a/src/main/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestService.java @@ -8,17 +8,19 @@ import org.springframework.stereotype.Service; import de.digitalcollections.iiif.presentation.model.api.v2.Sequence; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @Service public class DSpaceSequenceManifestService extends AbstractDSpaceManifestService { - public String generateManifest(String handle) throws IOException, URISyntaxException { + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); - RdfResource rdfResource = getDSpaceRdfModel(handle); + RdfResource rdfResource = getDSpaceRdfModel(context); - Sequence sequence = generateSequence(rdfResource); + Sequence sequence = generateSequence(request, rdfResource); sequence.setDescription(getDescription(rdfResource)); diff --git a/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java b/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java index 473d23c..6edd02f 100644 --- a/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/fedora/AbstractFedoraManifestService.java @@ -45,6 +45,7 @@ import de.digitalcollections.iiif.presentation.model.impl.v2.CanvasImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.PropertyValueSimpleImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.SequenceImpl; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.exception.NotFoundException; import edu.tamu.iiif.model.RepositoryType; import edu.tamu.iiif.model.rdf.RdfCanvas; @@ -79,19 +80,19 @@ protected Model getRdfModel(String url) throws NotFoundException { throw new NotFoundException("Fedora RDF not found!"); } - protected Sequence generateSequence(RdfResource rdfResource) throws IOException, URISyntaxException { + protected Sequence generateSequence(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { String id = rdfResource.getResource().getURI(); PropertyValueSimpleImpl label = new PropertyValueSimpleImpl(getRepositoryPath(id)); Sequence sequence = new SequenceImpl(getFedoraIiifSequenceUri(id), label); - sequence.setCanvases(getCanvases(rdfResource)); + sequence.setCanvases(getCanvases(request, rdfResource)); return sequence; } - protected Canvas generateCanvas(RdfResource rdfResource) throws IOException, URISyntaxException { + protected Canvas generateCanvas(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { String id = rdfResource.getResource().getURI(); PropertyValueSimpleImpl label = new PropertyValueSimpleImpl(getRepositoryPath(id)); - RdfCanvas rdfCanvas = getFedoraRdfCanvas(rdfResource); + RdfCanvas rdfCanvas = getFedoraRdfCanvas(request, rdfResource); Canvas canvas = new CanvasImpl(getFedoraIiifCanvasUri(id), label, rdfCanvas.getHeight(), rdfCanvas.getWidth()); @@ -162,7 +163,7 @@ private String getPCDMRdf(String fedoraPath) throws NotFoundException { throw new NotFoundException("Fedora PCDM RDF not found!"); } - private List getCanvases(RdfResource rdfResource) throws IOException, URISyntaxException { + private List getCanvases(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { List canvases = new ArrayList(); Optional firstId = getIdByPredicate(rdfResource.getModel(), IANA_FIRST_PREDICATE); @@ -172,7 +173,7 @@ private List getCanvases(RdfResource rdfResource) throws IOException, UR if (lastId.isPresent()) { Resource firstResource = rdfResource.getModel().getResource(firstId.get()); - generateOrderedCanvases(new RdfOrderedSequence(rdfResource.getModel(), firstResource, firstId.get(), lastId.get()), canvases); + generateOrderedCanvases(request, new RdfOrderedSequence(rdfResource.getModel(), firstResource, firstId.get(), lastId.get()), canvases); } } @@ -182,7 +183,7 @@ private List getCanvases(RdfResource rdfResource) throws IOException, UR while (resItr.hasNext()) { Resource resource = resItr.next(); if (resource.getProperty(rdfResource.getProperty(PCDM_HAS_FILE_PREDICATE)) != null) { - canvases.add(generateCanvas(new RdfResource(rdfResource, resource))); + canvases.add(generateCanvas(request, new RdfResource(rdfResource, resource))); } } @@ -191,7 +192,7 @@ private List getCanvases(RdfResource rdfResource) throws IOException, UR return canvases; } - private void generateOrderedCanvases(RdfOrderedSequence rdfOrderedSequence, List canvases) throws IOException, URISyntaxException { + private void generateOrderedCanvases(ManifestRequest request, RdfOrderedSequence rdfOrderedSequence, List canvases) throws IOException, URISyntaxException { Model model = getRdfModel(rdfOrderedSequence.getResource().getURI()); @@ -203,7 +204,7 @@ private void generateOrderedCanvases(RdfOrderedSequence rdfOrderedSequence, List if (id.isPresent()) { - canvases.add(generateCanvas(new RdfResource(rdfOrderedSequence, rdfOrderedSequence.getModel().getResource(id.get())))); + canvases.add(generateCanvas(request, new RdfResource(rdfOrderedSequence, rdfOrderedSequence.getModel().getResource(id.get())))); Optional nextId = getIdByPredicate(model, IANA_NEXT_PREDICATE); @@ -211,13 +212,13 @@ private void generateOrderedCanvases(RdfOrderedSequence rdfOrderedSequence, List Resource resource = rdfOrderedSequence.getModel().getResource(nextId.get()); rdfOrderedSequence.setResource(resource); rdfOrderedSequence.setCurrentId(nextId.get()); - generateOrderedCanvases(rdfOrderedSequence, canvases); + generateOrderedCanvases(request, rdfOrderedSequence, canvases); } } } - private RdfCanvas getFedoraRdfCanvas(RdfResource rdfResource) throws URISyntaxException, JsonProcessingException, MalformedURLException, IOException { + private RdfCanvas getFedoraRdfCanvas(ManifestRequest request, RdfResource rdfResource) throws URISyntaxException, JsonProcessingException, MalformedURLException, IOException { RdfCanvas rdfCanvas = new RdfCanvas(); String canvasId = rdfResource.getResource().getURI(); @@ -234,7 +235,7 @@ private RdfCanvas getFedoraRdfCanvas(RdfResource rdfResource) throws URISyntaxEx RdfResource fileFedoraRdfResource = new RdfResource(rdfResource, resource.getURI()); - Optional image = generateImage(fileFedoraRdfResource, canvasId); + Optional image = generateImage(request, fileFedoraRdfResource, canvasId); if (image.isPresent()) { rdfCanvas.addImage(image.get()); diff --git a/src/main/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestService.java b/src/main/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestService.java index c19d534..25c2328 100644 --- a/src/main/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestService.java @@ -8,15 +8,17 @@ import org.springframework.stereotype.Service; import de.digitalcollections.iiif.presentation.model.api.v2.Canvas; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @Service public class FedoraCanvasManifestService extends AbstractFedoraManifestService { - public String generateManifest(String path) throws IOException, URISyntaxException { - RdfResource rdfResource = getRdfResource(path); - Canvas canvas = generateCanvas(rdfResource); + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); + RdfResource rdfResource = getRdfResource(context); + Canvas canvas = generateCanvas(request, rdfResource); return mapper.writeValueAsString(canvas); } diff --git a/src/main/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestService.java b/src/main/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestService.java index a0917d7..f149ec3 100644 --- a/src/main/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestService.java @@ -21,6 +21,7 @@ import de.digitalcollections.iiif.presentation.model.impl.v2.CollectionImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.PropertyValueSimpleImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.references.ManifestReferenceImpl; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.exception.NotFoundException; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @@ -29,14 +30,15 @@ public class FedoraCollectionManifestService extends AbstractFedoraManifestService { @Override - protected String generateManifest(String handle) throws URISyntaxException, IOException { - return mapper.writeValueAsString(generateCollection(handle)); + protected String generateManifest(ManifestRequest request) throws URISyntaxException, IOException { + String context = request.getContext(); + return mapper.writeValueAsString(generateCollection(context)); } - private Collection generateCollection(String path) throws URISyntaxException, IOException { - RdfResource rdfResource = getRdfResource(path); + private Collection generateCollection(String context) throws URISyntaxException, IOException { + RdfResource rdfResource = getRdfResource(context); - URI id = buildId(path); + URI id = buildId(context); PropertyValueSimpleImpl label = getTitle(rdfResource); diff --git a/src/main/java/edu/tamu/iiif/service/fedora/FedoraImageManifestService.java b/src/main/java/edu/tamu/iiif/service/fedora/FedoraImageManifestService.java index 603f092..01ad9e1 100644 --- a/src/main/java/edu/tamu/iiif/service/fedora/FedoraImageManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/fedora/FedoraImageManifestService.java @@ -9,13 +9,15 @@ import org.springframework.stereotype.Service; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; @Service public class FedoraImageManifestService extends AbstractFedoraManifestService { - public String generateManifest(String path) throws IOException, URISyntaxException { - String fedoraPath = joinPath(fedoraUrl, path); + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); + String fedoraPath = joinPath(fedoraUrl, context); URI uri = getImageUri(fedoraPath); return fetchImageInfo(uri.toString()); } diff --git a/src/main/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestService.java b/src/main/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestService.java index e8d6518..4d92a68 100644 --- a/src/main/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestService.java @@ -19,16 +19,18 @@ import de.digitalcollections.iiif.presentation.model.impl.v2.ManifestImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.PropertyValueSimpleImpl; import de.digitalcollections.iiif.presentation.model.impl.v2.ThumbnailImpl; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @Service public class FedoraPresentationManifestService extends AbstractFedoraManifestService { - public String generateManifest(String path) throws IOException, URISyntaxException { - RdfResource rdfResource = getRdfResource(path); + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); + RdfResource rdfResource = getRdfResource(context); - URI id = buildId(path); + URI id = buildId(context); PropertyValueSimpleImpl label = getTitle(rdfResource); @@ -38,7 +40,7 @@ public String generateManifest(String path) throws IOException, URISyntaxExcepti manifest.setMetadata(getDublinCoreMetadata(rdfResource)); - List sequences = getSequences(rdfResource); + List sequences = getSequences(request, rdfResource); manifest.setSequences(sequences); @@ -57,10 +59,10 @@ public String generateManifest(String path) throws IOException, URISyntaxExcepti return mapper.writeValueAsString(manifest); } - private List getSequences(RdfResource rdfResource) throws IOException, URISyntaxException { + private List getSequences(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException { List sequences = new ArrayList(); - sequences.add(generateSequence(rdfResource)); + sequences.add(generateSequence(request, rdfResource)); return sequences; } diff --git a/src/main/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestService.java b/src/main/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestService.java index ab7097a..2099703 100644 --- a/src/main/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestService.java @@ -8,15 +8,17 @@ import org.springframework.stereotype.Service; import de.digitalcollections.iiif.presentation.model.api.v2.Sequence; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.rdf.RdfResource; @Service public class FedoraSequenceManifestService extends AbstractFedoraManifestService { - public String generateManifest(String path) throws IOException, URISyntaxException { - RdfResource rdfResource = getRdfResource(path); - Sequence senquence = generateSequence(rdfResource); + public String generateManifest(ManifestRequest request) throws IOException, URISyntaxException { + String context = request.getContext(); + RdfResource rdfResource = getRdfResource(context); + Sequence senquence = generateSequence(request, rdfResource); return mapper.writeValueAsString(senquence); } diff --git a/src/test/java/edu/tamu/iiif/controller/ManifestBuilderTest.java b/src/test/java/edu/tamu/iiif/controller/ManifestBuilderTest.java new file mode 100644 index 0000000..d08125d --- /dev/null +++ b/src/test/java/edu/tamu/iiif/controller/ManifestBuilderTest.java @@ -0,0 +1,29 @@ +package edu.tamu.iiif.controller; + +import static org.mockito.Mockito.mock; + +import java.util.Arrays; + +import javax.servlet.http.HttpServletResponse; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +public class ManifestBuilderTest { + + @Test + public void testManifestBuilder() { + HttpServletResponse response = mock(HttpServletResponse.class); + ManifestBuilder builder = ManifestBuilder.of(response, "test", false, Arrays.asList(new String[] { "allow" }), Arrays.asList(new String[] { "disallow" })); + ManifestRequest request = builder.getRequest(); + Assert.assertEquals(response, builder.getResponse()); + Assert.assertEquals("test", request.getContext()); + Assert.assertEquals(false, request.isUpdate()); + Assert.assertEquals("allow", request.getAllowed()); + Assert.assertEquals("disallow", request.getDisallowed()); + } + +} diff --git a/src/test/java/edu/tamu/iiif/controller/ManifestRequestTest.java b/src/test/java/edu/tamu/iiif/controller/ManifestRequestTest.java new file mode 100644 index 0000000..cba93dd --- /dev/null +++ b/src/test/java/edu/tamu/iiif/controller/ManifestRequestTest.java @@ -0,0 +1,29 @@ +package edu.tamu.iiif.controller; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +public class ManifestRequestTest { + + @Test + public void testManifestRequest() { + ManifestRequest request = ManifestRequest.of("test", false); + Assert.assertEquals("test", request.getContext()); + Assert.assertEquals(false, request.isUpdate()); + } + + @Test + public void testManifestRequestAdvanced() { + ManifestRequest request = ManifestRequest.of("test", false, Arrays.asList(new String[] { "allow" }), Arrays.asList(new String[] { "disallow" })); + Assert.assertEquals("test", request.getContext()); + Assert.assertEquals(false, request.isUpdate()); + Assert.assertEquals("allow", request.getAllowed()); + Assert.assertEquals("disallow", request.getDisallowed()); + } + +} diff --git a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestControllerTest.java index a8d9027..4440cf1 100644 --- a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCanvasManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.dspace.DSpaceCanvasManifestService; @WebMvcTest(value = DSpaceCanvasManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class DSpaceCanvasManifestControllerTest extends AbstractManifestControll @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(dSpaceCanvasManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(dSpaceCanvasManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/dspace/canvas?context=123456789/158308/1/sports-car-146873_960_720.png").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestControllerTest.java index ba57bc7..ab2404b 100644 --- a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceCollectionManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.dspace.DSpaceCollectionManifestService; @WebMvcTest(value = DSpaceCollectionManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class DSpaceCollectionManifestControllerTest extends AbstractManifestCont @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(dSpaceCollectionManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(dSpaceCollectionManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/dspace/collection?context=123456789/158298").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestControllerTest.java index ce3836d..7506401 100644 --- a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceImageManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.dspace.DSpaceImageManifestService; @WebMvcTest(value = DSpaceImageManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class DSpaceImageManifestControllerTest extends AbstractManifestControlle @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(dSpaceImageManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(dSpaceImageManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/dspace/image?context=123456789/158308/1/sports-car-146873_960_20.png").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestControllerTest.java index 4b38f32..9d4c6a3 100644 --- a/src/test/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/dspace/DSpacePresentationManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.dspace.DSpacePresentationManifestService; @WebMvcTest(value = DSpacePresentationManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class DSpacePresentationManifestControllerTest extends AbstractManifestCo @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(dSpacePresentationManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(dSpacePresentationManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/dspace/presentation?context=123456789/158308").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestControllerTest.java index 53c66db..65e6a29 100644 --- a/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/dspace/DSpaceSequenceManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.dspace.DSpaceSequenceManifestService; @WebMvcTest(value = DSpaceSequenceManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class DSpaceSequenceManifestControllerTest extends AbstractManifestContro @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(dSpaceSequenceManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(dSpaceSequenceManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/dspace/sequence?context=123456789/158308").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestControllerTest.java index 3251be3..9b9e66b 100644 --- a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCanvasManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.fedora.FedoraCanvasManifestService; @WebMvcTest(value = FedoraCanvasManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class FedoraCanvasManifestControllerTest extends AbstractManifestControll @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(fedaorCanvasManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(fedaorCanvasManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fedora/canvas?context=cars_pcdm_objects/chevy/pages/page_0").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestControllerTest.java index 3e90fe3..44f2653 100644 --- a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraCollectionManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.fedora.FedoraCollectionManifestService; @WebMvcTest(value = FedoraCollectionManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class FedoraCollectionManifestControllerTest extends AbstractManifestCont @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(fedaorCollectionManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(fedaorCollectionManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fedora/collection?context=cars_pcdm").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestControllerTest.java index e29ac23..1c96996 100644 --- a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraImageManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.fedora.FedoraImageManifestService; @WebMvcTest(value = FedoraImageManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class FedoraImageManifestControllerTest extends AbstractManifestControlle @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(image0.getFile(), "UTF-8"); - when(fedaorImageManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(fedaorImageManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fedora/image?context=cars_pcdm_objects/chevy/pages/page_0/files/PTAR_800x400.png").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestControllerTest.java index e14da13..99c72eb 100644 --- a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraPresentationManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.fedora.FedoraPresentationManifestService; @WebMvcTest(value = FedoraPresentationManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class FedoraPresentationManifestControllerTest extends AbstractManifestCo @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(fedaorPresentationManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(fedaorPresentationManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fedora/presentation?context=cars_pcdm_objects/chevy").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestControllerTest.java b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestControllerTest.java index b2b38c1..18bc52b 100644 --- a/src/test/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestControllerTest.java +++ b/src/test/java/edu/tamu/iiif/controller/fedora/FedoraSequenceManifestControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import edu.tamu.iiif.controller.AbstractManifestControllerTest; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.service.fedora.FedoraSequenceManifestService; @WebMvcTest(value = FedoraSequenceManifestController.class, secure = false) @@ -30,7 +31,7 @@ public class FedoraSequenceManifestControllerTest extends AbstractManifestContro @Test public void testGetManifest() throws Exception { String expected = FileUtils.readFileToString(json.getFile(), "UTF-8"); - when(fedaorSequenceManifestService.getManifest(any(String.class), any(Boolean.class))).thenReturn(expected); + when(fedaorSequenceManifestService.getManifest(any(ManifestRequest.class))).thenReturn(expected); RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fedora/sequence?context=cars_pcdm_objects/chevy").accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false); diff --git a/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java b/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java index 9e753a9..3a30e91 100644 --- a/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java +++ b/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java @@ -9,36 +9,40 @@ public class RedisManifestTest { @Test - public void testCreateDefault() { - RedisManifest redisManifest = new RedisManifest(); + public void testCreate() { + RedisManifest redisManifest = new RedisManifest("path", ManifestType.COLLECTION, RepositoryType.FEDORA, "{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}"); Assert.assertNotNull(redisManifest); Assert.assertNotNull(redisManifest.getCreation()); + Assert.assertEquals("path", redisManifest.getPath()); + Assert.assertEquals(ManifestType.COLLECTION, redisManifest.getType()); + Assert.assertEquals(RepositoryType.FEDORA, redisManifest.getRepository()); + Assert.assertEquals("{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}", redisManifest.getJson()); + Assert.assertEquals("", redisManifest.getAllowed()); + Assert.assertEquals("", redisManifest.getDisallowed()); } @Test public void testCreateComplete() { - RedisManifest redisManifest = new RedisManifest("path", ManifestType.COLLECTION, RepositoryType.FEDORA, "{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}"); + RedisManifest redisManifest = new RedisManifest("path", ManifestType.COLLECTION, RepositoryType.FEDORA, "apple,banana", "cat,dog", "{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}"); Assert.assertNotNull(redisManifest); Assert.assertNotNull(redisManifest.getCreation()); Assert.assertEquals("path", redisManifest.getPath()); Assert.assertEquals(ManifestType.COLLECTION, redisManifest.getType()); Assert.assertEquals(RepositoryType.FEDORA, redisManifest.getRepository()); Assert.assertEquals("{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}", redisManifest.getJson()); + Assert.assertEquals("apple,banana", redisManifest.getAllowed()); + Assert.assertEquals("cat,dog", redisManifest.getDisallowed()); } @Test public void testUpdate() { - RedisManifest redisManifest = new RedisManifest("path", ManifestType.COLLECTION, RepositoryType.FEDORA, "{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}"); + RedisManifest redisManifest = new RedisManifest("path", ManifestType.COLLECTION, RepositoryType.FEDORA, "apple,banana", "cat,dog", "{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}"); redisManifest.setId("1"); - redisManifest.setPath("new/path"); - redisManifest.setType(ManifestType.PRESENTATION); - redisManifest.setRepository(RepositoryType.DSPACE); redisManifest.setJson("{\"@id\":\"http:localhost/dspace/presentation?context=123456789/123456\"}"); Assert.assertEquals("1", redisManifest.getId()); - Assert.assertEquals("new/path", redisManifest.getPath()); - Assert.assertEquals(ManifestType.PRESENTATION, redisManifest.getType()); - Assert.assertEquals(RepositoryType.DSPACE, redisManifest.getRepository()); Assert.assertEquals("{\"@id\":\"http:localhost/dspace/presentation?context=123456789/123456\"}", redisManifest.getJson()); + Assert.assertEquals("apple,banana", redisManifest.getAllowed()); + Assert.assertEquals("cat,dog", redisManifest.getDisallowed()); } } diff --git a/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java index 311805e..53169b9 100644 --- a/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/AbstractManifestServiceTest.java @@ -39,7 +39,7 @@ public abstract class AbstractManifestServiceTest implements ManifestServiceTest @Before public void init() { MockitoAnnotations.initMocks(this); - when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.empty()); + when(redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(any(String.class), any(ManifestType.class), any(RepositoryType.class), any(String.class), any(String.class))).thenReturn(Optional.empty()); } } diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java index 682337e..8c6c129 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCanvasManifestServiceTest.java @@ -17,6 +17,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class DSpaceCanvasManifestServiceTest extends AbstractDSpaceManifestServiceTest { @InjectMocks @@ -46,7 +48,7 @@ public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); when(httpService.contentType(eq(DSPACE_URL + "/xmlui/bitstream/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn("image/png"); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/info.json"))).thenReturn(FileUtils.readFileToString(image.getFile(), "UTF-8")); - String manifest = dSpaceCanvasManifestService.getManifest("123456789/158308/1/sports-car-146873_960_720.png", false); + String manifest = dSpaceCanvasManifestService.getManifest(ManifestRequest.of("123456789/158308/1/sports-car-146873_960_720.png", false)); Assert.assertEquals(objectMapper.readValue(canvas.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestServiceTest.java index 92492d0..8cf4a96 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceCollectionManifestServiceTest.java @@ -17,6 +17,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class DSpaceCollectionManifestServiceTest extends AbstractDSpaceManifestServiceTest { @InjectMocks @@ -52,11 +54,11 @@ public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158299"))).thenReturn(FileUtils.readFileToString(collectionRdf.getFile(), "UTF-8")); when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158301"))).thenReturn(FileUtils.readFileToString(subcommunityRdf.getFile(), "UTF-8")); when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158298"))).thenReturn(FileUtils.readFileToString(communityRdf.getFile(), "UTF-8")); - String collectionManifest = dSpaceCollectionManifestService.getManifest("123456789/158299", false); + String collectionManifest = dSpaceCollectionManifestService.getManifest(ManifestRequest.of("123456789/158299", false)); Assert.assertEquals(objectMapper.readValue(collection.getFile(), JsonNode.class), objectMapper.readValue(collectionManifest, JsonNode.class)); - String collectionsManifest = dSpaceCollectionManifestService.getManifest("123456789/158298", false); + String collectionsManifest = dSpaceCollectionManifestService.getManifest(ManifestRequest.of("123456789/158298", false)); Assert.assertEquals(objectMapper.readValue(collections.getFile(), JsonNode.class), objectMapper.readValue(collectionsManifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestServiceTest.java index 49b9118..e606c1e 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceImageManifestServiceTest.java @@ -17,6 +17,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class DSpaceImageManifestServiceTest extends AbstractDSpaceManifestServiceTest { @InjectMocks @@ -38,7 +40,7 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(any(String.class))).thenReturn(FileUtils.readFileToString(image.getFile(), "UTF-8")); - String manifest = dSpaceImageManifestService.getManifest("123456789/158308/1/sports-car-146873_960_720.png", false); + String manifest = dSpaceImageManifestService.getManifest(ManifestRequest.of("123456789/158308/1/sports-car-146873_960_720.png", false)); Assert.assertEquals(objectMapper.readValue(image.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java index f6aba33..880be3a 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpacePresentationManifestServiceTest.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.RedisManifest; import edu.tamu.iiif.model.RepositoryType; @@ -50,15 +51,15 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { setupMocks(); - String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", false); + String manifest = dSpacePresentationManifestService.getManifest(ManifestRequest.of("123456789/158308", false)); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } @Test public void testGetManifestCached() throws IOException, URISyntaxException { RedisManifest redisManifest = new RedisManifest("123456789/158308", ManifestType.PRESENTATION, RepositoryType.DSPACE, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); - when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); - String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", false); + when(redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(any(String.class), any(ManifestType.class), any(RepositoryType.class), any(String.class), any(String.class))).thenReturn(Optional.of(redisManifest)); + String manifest = dSpacePresentationManifestService.getManifest(ManifestRequest.of("123456789/158308", false)); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } @@ -66,8 +67,8 @@ public void testGetManifestCached() throws IOException, URISyntaxException { public void testGetManifestUpdateCached() throws IOException, URISyntaxException { setupMocks(); RedisManifest redisManifest = new RedisManifest("123456789/158308", ManifestType.PRESENTATION, RepositoryType.DSPACE, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); - when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); - String manifest = dSpacePresentationManifestService.getManifest("123456789/158308", true); + when(redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(any(String.class), any(ManifestType.class), any(RepositoryType.class), any(String.class), any(String.class))).thenReturn(Optional.of(redisManifest)); + String manifest = dSpacePresentationManifestService.getManifest(ManifestRequest.of("123456789/158308", true)); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java index 290f0b5..6736890 100644 --- a/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/dspace/DSpaceSequenceManifestServiceTest.java @@ -17,6 +17,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class DSpaceSequenceManifestServiceTest extends AbstractDSpaceManifestServiceTest { @InjectMocks @@ -47,7 +49,7 @@ public void testGetManifest() throws IOException, URISyntaxException { when(httpService.contentType(eq(DSPACE_URL + "/xmlui/bitstream/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn("image/png"); when(httpService.get(eq(DSPACE_URL + "/rdf/handle/123456789/158308/1/sports-car-146873_960_720.png"))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZHNwYWNlOnhtbHVpL2JpdHN0cmVhbS8xMjM0NTY3ODkvMTU4MzA4LzEvc3BvcnRzLWNhci0xNDY4NzNfOTYwXzcyMC5wbmc=/info.json"))).thenReturn(FileUtils.readFileToString(image.getFile(), "UTF-8")); - String manifest = dSpaceSequenceManifestService.getManifest("123456789/158308", false); + String manifest = dSpaceSequenceManifestService.getManifest(ManifestRequest.of("123456789/158308", false)); Assert.assertEquals(objectMapper.readValue(sequence.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java index 0ebc14c..150d353 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraCanvasManifestServiceTest.java @@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class FedoraCanvasManifestServiceTest extends AbstractFedoraManifestServiceTest { @InjectMocks @@ -47,7 +49,7 @@ public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); when(httpService.contentType(any(String.class))).thenReturn("image/png"); when(httpService.get(any(String.class))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); - String manifest = fedoraCanvasManifestService.getManifest("cars_pcdm_objects/chevy/pages/page_0", false); + String manifest = fedoraCanvasManifestService.getManifest(ManifestRequest.of("cars_pcdm_objects/chevy/pages/page_0", false)); Assert.assertEquals(objectMapper.readValue(canvas.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestServiceTest.java index bacddc7..b7f5f1e 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraCollectionManifestServiceTest.java @@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class FedoraCollectionManifestServiceTest extends AbstractFedoraManifestServiceTest { @InjectMocks @@ -43,7 +45,7 @@ public void setup() { public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); - String manifest = fedoraCollectionManifestService.getManifest("cars_pcdm", false); + String manifest = fedoraCollectionManifestService.getManifest(ManifestRequest.of("cars_pcdm", false)); Assert.assertEquals(objectMapper.readValue(collection.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraImageManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraImageManifestServiceTest.java index 0eaef68..8cc64bf 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraImageManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraImageManifestServiceTest.java @@ -17,6 +17,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class FedoraImageManifestServiceTest extends AbstractFedoraManifestServiceTest { @InjectMocks @@ -38,7 +40,7 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(any(String.class))).thenReturn(FileUtils.readFileToString(image0.getFile(), "UTF-8")); - String manifest = fedoraImageManifestService.getManifest("cars_pcdm_objects/chevy/pages/page_0/files/PTAR_800x400.png", false); + String manifest = fedoraImageManifestService.getManifest(ManifestRequest.of("cars_pcdm_objects/chevy/pages/page_0/files/PTAR_800x400.png", false)); Assert.assertEquals(objectMapper.readValue(image0.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java index 65c5e1f..554abe6 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraPresentationManifestServiceTest.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; import edu.tamu.iiif.model.ManifestType; import edu.tamu.iiif.model.RedisManifest; import edu.tamu.iiif.model.RepositoryType; @@ -62,15 +63,15 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { setupMocks(); - String manifest = fedoraPresentationManifestService.getManifest("cars_pcdm_objects/chevy", false); + String manifest = fedoraPresentationManifestService.getManifest(ManifestRequest.of("cars_pcdm_objects/chevy", false)); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } @Test public void testGetManifestCached() throws IOException, URISyntaxException { RedisManifest redisManifest = new RedisManifest("cars_pcdm_objects/chevy", ManifestType.PRESENTATION, RepositoryType.FEDORA, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); - when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); - String manifest = fedoraPresentationManifestService.getManifest("cars_pcdm_objects/chevy", false); + when(redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(any(String.class), any(ManifestType.class), any(RepositoryType.class), any(String.class), any(String.class))).thenReturn(Optional.of(redisManifest)); + String manifest = fedoraPresentationManifestService.getManifest(ManifestRequest.of("cars_pcdm_objects/chevy", false)); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } @@ -78,8 +79,8 @@ public void testGetManifestCached() throws IOException, URISyntaxException { public void testGetManifestUpdateCached() throws IOException, URISyntaxException { setupMocks(); RedisManifest redisManifest = new RedisManifest("cars_pcdm_objects/chevy", ManifestType.PRESENTATION, RepositoryType.FEDORA, FileUtils.readFileToString(presentation.getFile(), "UTF-8")); - when(redisManifestRepo.findByPathAndTypeAndRepository(any(String.class), any(ManifestType.class), any(RepositoryType.class))).thenReturn(Optional.of(redisManifest)); - String manifest = fedoraPresentationManifestService.getManifest("cars_pcdm_objects/chevy", true); + when(redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(any(String.class), any(ManifestType.class), any(RepositoryType.class), any(String.class), any(String.class))).thenReturn(Optional.of(redisManifest)); + String manifest = fedoraPresentationManifestService.getManifest(ManifestRequest.of("cars_pcdm_objects/chevy", true)); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } @@ -93,7 +94,7 @@ public void testGetManifestWithoutOrder() throws IOException, URISyntaxException when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMS9maWxlcy9jYXIyLmpwZw==/info.json"))).thenReturn(FileUtils.readFileToString(image1.getFile(), "UTF-8")); - String manifest = fedoraPresentationManifestService.getManifest("cars_pcdm_objects/chevy", false); + String manifest = fedoraPresentationManifestService.getManifest(ManifestRequest.of("cars_pcdm_objects/chevy", false)); Assert.assertEquals(objectMapper.readValue(presentation.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } diff --git a/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java b/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java index 73cb7eb..8262426 100644 --- a/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java +++ b/src/test/java/edu/tamu/iiif/service/fedora/FedoraSequenceManifestServiceTest.java @@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.JsonNode; +import edu.tamu.iiif.controller.ManifestRequest; + public class FedoraSequenceManifestServiceTest extends AbstractFedoraManifestServiceTest { @InjectMocks @@ -54,7 +56,7 @@ public void setup() { @Test public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(PCDM_RDF_URL), any(String.class))).thenReturn(FileUtils.readFileToString(rdf.getFile(), "UTF-8")); - + when(httpService.contentType(any(String.class))).thenReturn("image/png"); when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_0_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy0Rdf.getFile(), "UTF-8")); @@ -63,7 +65,7 @@ public void testGetManifest() throws IOException, URISyntaxException { when(httpService.get(eq(FEDORA_URL + "/cars_pcdm_objects/chevy/orderProxies/page_1_proxy/fcr:metadata"))).thenReturn(FileUtils.readFileToString(proxy1Rdf.getFile(), "UTF-8")); when(httpService.get(eq(IMAGE_SERVICE_URL + "/ZmVkb3JhOmNhcnNfcGNkbV9vYmplY3RzL2NoZXZ5L3BhZ2VzL3BhZ2VfMS9maWxlcy9jYXIyLmpwZw==/info.json"))).thenReturn(FileUtils.readFileToString(image1.getFile(), "UTF-8")); - String manifest = fedoraSequenceManifestService.getManifest("cars_pcdm_objects/chevy", false); + String manifest = fedoraSequenceManifestService.getManifest(ManifestRequest.of("cars_pcdm_objects/chevy", false)); Assert.assertEquals(objectMapper.readValue(sequence.getFile(), JsonNode.class), objectMapper.readValue(manifest, JsonNode.class)); } From ffbaeebc53af2989a7ec0447b3df956d2bd2357e Mon Sep 17 00:00:00 2001 From: William Welling Date: Wed, 6 Jun 2018 16:51:23 -0500 Subject: [PATCH 6/9] handle mime type with character set --- .../iiif/service/AbstractManifestService.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java index 86c9735..01fc9d8 100644 --- a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java @@ -139,21 +139,28 @@ protected Optional generateImageResource(ManifestRequest request, Optional optionalImageResource = Optional.empty(); - Optional mimeType = getMimeType(url); + Optional optionalMimeType = getMimeType(url); boolean include = false; - if (mimeType.isPresent()) { + if (optionalMimeType.isPresent()) { + + String mimeType = optionalMimeType.get(); + + if (mimeType.contains(";")) { + mimeType = mimeType.split(";")[0]; + } + include = true; String allowed = request.getAllowed(); if (allowed.length() > 0) { - include = allowed.contains(mimeType.get()); + LOG.info("Allowed: " + allowed); + include = allowed.contains(mimeType); } else { String disallowed = request.getDisallowed(); if (disallowed.length() > 0) { - if (disallowed.contains(mimeType.get())) { - include = false; - } + LOG.info("Disallowed: " + allowed); + include = !disallowed.contains(mimeType); } } } else { @@ -169,7 +176,7 @@ protected Optional generateImageResource(ManifestRequest request, ImageResource imageResource = new ImageResourceImpl(getImageFullUrl(url)); - imageResource.setFormat(mimeType.get()); + imageResource.setFormat(optionalMimeType.get()); imageResource.setHeight(imageInfoNode.get().get("height").asInt()); From db4c718ab62dd385c79283710266bd95fe30e3f9 Mon Sep 17 00:00:00 2001 From: William Welling Date: Wed, 6 Jun 2018 16:56:32 -0500 Subject: [PATCH 7/9] updated logging --- .../edu/tamu/iiif/service/AbstractManifestService.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java index 01fc9d8..108c59a 100644 --- a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java @@ -147,6 +147,8 @@ protected Optional generateImageResource(ManifestRequest request, String mimeType = optionalMimeType.get(); + LOG.debug("Mime type: " + mimeType); + if (mimeType.contains(";")) { mimeType = mimeType.split(";")[0]; } @@ -154,12 +156,12 @@ protected Optional generateImageResource(ManifestRequest request, include = true; String allowed = request.getAllowed(); if (allowed.length() > 0) { - LOG.info("Allowed: " + allowed); + LOG.debug("Allowed: " + allowed); include = allowed.contains(mimeType); } else { String disallowed = request.getDisallowed(); if (disallowed.length() > 0) { - LOG.info("Disallowed: " + allowed); + LOG.debug("Disallowed: " + disallowed); include = !disallowed.contains(mimeType); } } @@ -168,6 +170,7 @@ protected Optional generateImageResource(ManifestRequest request, } if (include) { + LOG.debug("Including: " + url); URI infoUri = getImageInfoUri(url); Optional imageInfoNode = getImageInfo(infoUri.toString()); @@ -189,7 +192,7 @@ protected Optional generateImageResource(ManifestRequest request, LOG.info("Unable to get image info: " + infoUri.toString()); } } else { - LOG.info("Excluding: " + url); + LOG.debug("Excluding: " + url); } return optionalImageResource; From 529ff11c1c10c6028c6ee8f11dfed6967e79c914 Mon Sep 17 00:00:00 2001 From: William Welling Date: Thu, 7 Jun 2018 08:54:30 -0500 Subject: [PATCH 8/9] added redis manifest default constructor back --- .../edu/tamu/iiif/model/RedisManifest.java | 22 +++++++++++-------- .../iiif/service/AbstractManifestService.java | 10 ++++----- .../tamu/iiif/model/RedisManifestTest.java | 9 ++++++++ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main/java/edu/tamu/iiif/model/RedisManifest.java b/src/main/java/edu/tamu/iiif/model/RedisManifest.java index 8decfa3..36cdd4a 100644 --- a/src/main/java/edu/tamu/iiif/model/RedisManifest.java +++ b/src/main/java/edu/tamu/iiif/model/RedisManifest.java @@ -13,36 +13,40 @@ public class RedisManifest { private String id; @Indexed - private final String path; + private String path; @Indexed - private final ManifestType type; + private ManifestType type; @Indexed - private final RepositoryType repository; + private RepositoryType repository; @Indexed - private final String allowed; + private String allowed; @Indexed - private final String disallowed; + private String disallowed; private String json; private final Long creation; - public RedisManifest(String path, ManifestType type, RepositoryType repository, String json) { + public RedisManifest() { this.creation = new Date().getTime(); + this.allowed = ""; + this.disallowed = ""; + } + + public RedisManifest(String path, ManifestType type, RepositoryType repository, String json) { + this(); this.path = path; this.type = type; this.repository = repository; this.json = json; - this.allowed = ""; - this.disallowed = ""; } public RedisManifest(String path, ManifestType type, RepositoryType repository, String allowed, String disallowed, String json) { - this.creation = new Date().getTime(); + this(); this.path = path; this.type = type; this.repository = repository; diff --git a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java index 108c59a..3621694 100644 --- a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java @@ -147,7 +147,7 @@ protected Optional generateImageResource(ManifestRequest request, String mimeType = optionalMimeType.get(); - LOG.debug("Mime type: " + mimeType); + LOG.info("Mime type: " + mimeType); if (mimeType.contains(";")) { mimeType = mimeType.split(";")[0]; @@ -156,12 +156,12 @@ protected Optional generateImageResource(ManifestRequest request, include = true; String allowed = request.getAllowed(); if (allowed.length() > 0) { - LOG.debug("Allowed: " + allowed); + LOG.info("Allowed: " + allowed); include = allowed.contains(mimeType); } else { String disallowed = request.getDisallowed(); if (disallowed.length() > 0) { - LOG.debug("Disallowed: " + disallowed); + LOG.info("Disallowed: " + disallowed); include = !disallowed.contains(mimeType); } } @@ -170,7 +170,7 @@ protected Optional generateImageResource(ManifestRequest request, } if (include) { - LOG.debug("Including: " + url); + LOG.info("Including: " + url); URI infoUri = getImageInfoUri(url); Optional imageInfoNode = getImageInfo(infoUri.toString()); @@ -192,7 +192,7 @@ protected Optional generateImageResource(ManifestRequest request, LOG.info("Unable to get image info: " + infoUri.toString()); } } else { - LOG.debug("Excluding: " + url); + LOG.info("Excluding: " + url); } return optionalImageResource; diff --git a/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java b/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java index 3a30e91..e28e955 100644 --- a/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java +++ b/src/test/java/edu/tamu/iiif/model/RedisManifestTest.java @@ -8,6 +8,15 @@ @RunWith(SpringRunner.class) public class RedisManifestTest { + @Test + public void testCreateDefault() { + RedisManifest redisManifest = new RedisManifest(); + Assert.assertNotNull(redisManifest); + Assert.assertNotNull(redisManifest.getCreation()); + Assert.assertEquals("", redisManifest.getAllowed()); + Assert.assertEquals("", redisManifest.getDisallowed()); + } + @Test public void testCreate() { RedisManifest redisManifest = new RedisManifest("path", ManifestType.COLLECTION, RepositoryType.FEDORA, "{\"@id\":\"http:localhost/fedora/collection?context=pcdm\"}"); From 8c95deaf678b06bf577b77e6d4757d141d144fae Mon Sep 17 00:00:00 2001 From: William Welling Date: Thu, 7 Jun 2018 08:55:46 -0500 Subject: [PATCH 9/9] updated logging for generate image resource --- .../edu/tamu/iiif/service/AbstractManifestService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java index 3621694..9a061f4 100644 --- a/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java +++ b/src/main/java/edu/tamu/iiif/service/AbstractManifestService.java @@ -147,7 +147,7 @@ protected Optional generateImageResource(ManifestRequest request, String mimeType = optionalMimeType.get(); - LOG.info("Mime type: " + mimeType); + LOG.debug("Mime type: " + mimeType); if (mimeType.contains(";")) { mimeType = mimeType.split(";")[0]; @@ -156,17 +156,17 @@ protected Optional generateImageResource(ManifestRequest request, include = true; String allowed = request.getAllowed(); if (allowed.length() > 0) { - LOG.info("Allowed: " + allowed); + LOG.debug("Allowed: " + allowed); include = allowed.contains(mimeType); } else { String disallowed = request.getDisallowed(); if (disallowed.length() > 0) { - LOG.info("Disallowed: " + disallowed); + LOG.debug("Disallowed: " + disallowed); include = !disallowed.contains(mimeType); } } } else { - LOG.info("Unable to get mime type: " + url); + LOG.warn("Unable to get mime type: " + url); } if (include) {