Skip to content

Commit

Permalink
Merge pull request #90 from TAMULib/issue-89
Browse files Browse the repository at this point in the history
condition on resource content type for collection manifest
  • Loading branch information
wwelling authored Sep 7, 2020
2 parents 501f466 + e8ae3e5 commit 1eb31e4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>edu.tamu</groupId>
<artifactId>ir-iiif-service</artifactId>
<version>0.5.1</version>
<version>0.5.2</version>
<packaging>war</packaging>

<name>IR IIIF Service</name>
Expand Down
85 changes: 51 additions & 34 deletions src/main/java/edu/tamu/iiif/service/AbstractManifestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import de.digitalcollections.iiif.presentation.model.api.v2.Canvas;
Expand Down Expand Up @@ -189,41 +190,13 @@ protected Optional<Image> generateImage(ManifestRequest request, RdfResource rdf
}

protected Optional<ImageResource> generateImageResource(ManifestRequest request, RdfResource rdfResource) throws URISyntaxException {
String url = rdfResource.getResource().getURI();

Optional<ImageResource> optionalImageResource = Optional.empty();

Optional<String> optionalMimeType = getMimeType(url);

boolean include = false;

if (optionalMimeType.isPresent()) {

String mimeType = optionalMimeType.get();
String url = rdfResource.getResource().getURI();

logger.debug("Mime type: " + mimeType);
Optional<String> optionalMimeType = getMimeType(url);

if (mimeType.contains(SEMI_COLON)) {
mimeType = mimeType.split(SEMI_COLON)[0];
}

include = mimeType.startsWith(IMAGE) || mimeType.equals(APPLICATION_PDF);
if (include) {
String allowed = request.getAllowed();
if (allowed.length() > 0) {
logger.debug("Allowed: " + allowed);
include = allowed.contains(mimeType);
} else {
String disallowed = request.getDisallowed();
if (disallowed.length() > 0) {
logger.debug("Disallowed: " + disallowed);
include = !disallowed.contains(mimeType);
}
}
}
} else {
logger.warn("Unable to get mime type: " + url);
}
boolean include = optionalMimeType.isPresent() ? includeResource(request, optionalMimeType.get()) : false;

if (include) {
logger.info("Including: " + url);
Expand All @@ -232,7 +205,6 @@ protected Optional<ImageResource> generateImageResource(ManifestRequest request,
Optional<JsonNode> imageInfoNode = getImageInfo(infoUri.toString());

if (imageInfoNode.isPresent()) {

ImageResource imageResource = new ImageResourceImpl(getImageFullUrl(url));

imageResource.setFormat(optionalMimeType.get());
Expand All @@ -254,6 +226,47 @@ protected Optional<ImageResource> generateImageResource(ManifestRequest request,
return optionalImageResource;
}

protected boolean includeResourceWithUrl(ManifestRequest request, String url) {
boolean include = false;

Optional<String> optionalMimeType = getMimeType(url);

if (optionalMimeType.isPresent()) {
include = includeResource(request, optionalMimeType.get());
} else {
logger.warn("Unable to get mime type: " + url);
}

return include;
}

private boolean includeResource(ManifestRequest request, String mimeType) {
boolean include = false;

logger.debug("Mime type: " + mimeType);

if (mimeType.contains(SEMI_COLON)) {
mimeType = mimeType.split(SEMI_COLON)[0];
}

include = mimeType.startsWith(IMAGE) || mimeType.equals(APPLICATION_PDF);
if (include) {
String allowed = request.getAllowed();
if (allowed.length() > 0) {
logger.debug("Allowed: " + allowed);
include = allowed.contains(mimeType);
} else {
String disallowed = request.getDisallowed();
if (disallowed.length() > 0) {
logger.debug("Disallowed: " + disallowed);
include = !disallowed.contains(mimeType);
}
}
}

return include;
}

protected String fetchImageInfo(String url) throws NotFoundException {
Optional<String> imageInfo = Optional.ofNullable(restTemplate.getForObject(url, String.class));
if (imageInfo.isPresent()) {
Expand Down Expand Up @@ -445,8 +458,12 @@ private Optional<JsonNode> getImageInfo(String url) {
}

private Optional<String> getMimeType(String url) {
HttpHeaders headers = restTemplate.headForHeaders(url);
return Optional.ofNullable(headers.getFirst(HttpHeaders.CONTENT_TYPE));
try {
HttpHeaders headers = restTemplate.headForHeaders(url);
return Optional.ofNullable(headers.getFirst(HttpHeaders.CONTENT_TYPE));
} catch (RestClientException e) {
return Optional.empty();
}
}

private Metadata buildMetadata(String label, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ private List<ManifestReference> getResourceManifests(ManifestRequest request, Rd
if (isItem(rdfResource.getModel())) {
NodeIterator hasBitstreamIterator = rdfResource.getAllNodesOfPropertyWithId(DSPACE_HAS_BITSTREAM_PREDICATE);
while (hasBitstreamIterator.hasNext()) {
String hasBitstreamHandlePath = getHandlePath(hasBitstreamIterator.next().toString());
String parameterizedHasBitstreamHandlePath = RdfModelUtility.getParameterizedId(hasBitstreamHandlePath, request);
RdfResource hasBitstreamRdfResource = getRdfResourceByContextPath(hasBitstreamHandlePath);
manifests.add(new ManifestReferenceImpl(getDSpaceIiifPresentationUri(parameterizedHasBitstreamHandlePath), getLabel(hasBitstreamRdfResource)));
String resourceUrl = hasBitstreamIterator.next().toString();
if (includeResourceWithUrl(request, resourceUrl)) {
String hasBitstreamHandlePath = getHandlePath(resourceUrl);
RdfResource hasBitstreamRdfResource = getRdfResourceByContextPath(hasBitstreamHandlePath);
String parameterizedHasBitstreamHandlePath = RdfModelUtility.getParameterizedId(hasBitstreamHandlePath, request);
manifests.add(new ManifestReferenceImpl(getDSpaceIiifPresentationUri(parameterizedHasBitstreamHandlePath), getLabel(hasBitstreamRdfResource)));
}
}
} else {
NodeIterator hasItemIterator = rdfResource.getAllNodesOfPropertyWithId(DSPACE_HAS_ITEM_PREDICATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ private List<ManifestReference> getResourceManifests(ManifestRequest request, Rd
while (nodes.hasNext()) {
RDFNode node = nodes.next();
String parameterizedId = RdfModelUtility.getParameterizedId(node.toString(), request);
manifests.add(new ManifestReferenceImpl(getFedoraIiifPresentationUri(parameterizedId), getLabel(getRdfResourceByUrl(node.toString()))));
RdfResource hasMemberRdfResource = getRdfResourceByUrl(node.toString());
manifests.add(new ManifestReferenceImpl(getFedoraIiifPresentationUri(parameterizedId), getLabel(hasMemberRdfResource)));
}
}
if (manifests.isEmpty()) {
ResIterator resources = rdfResource.listResourcesWithPropertyWithId(PCDM_HAS_FILE_PREDICATE);
while (resources.hasNext()) {
Resource resource = resources.next();
String parameterizedId = RdfModelUtility.getParameterizedId(resource.getURI(), request);
manifests.add(new ManifestReferenceImpl(getFedoraIiifPresentationUri(parameterizedId), getLabel(getRdfResourceByUrl(resource.getURI()))));
String resourceUrl = resource.getURI();
if (includeResourceWithUrl(request, resourceUrl)) {
RdfResource hasFileRdfResource = getRdfResourceByUrl(resourceUrl);
String parameterizedId = RdfModelUtility.getParameterizedId(resourceUrl, request);
manifests.add(new ManifestReferenceImpl(getFedoraIiifPresentationUri(parameterizedId), getLabel(hasFileRdfResource)));
}
}
}
return manifests;
Expand Down

0 comments on commit 1eb31e4

Please sign in to comment.