Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 150: Fix problems with encoding/decoding and add encoding unit tests. #151

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/edu/tamu/iiif/exception/NotFoundException.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public NotFoundException(String message) {
super(message);
}

public NotFoundException(String message, Exception e) {
super(message, e);
}

}
87 changes: 37 additions & 50 deletions src/main/java/edu/tamu/iiif/service/AbstractManifestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,12 @@
import static edu.tamu.iiif.utility.RdfModelUtility.createRdfModel;
import static edu.tamu.iiif.utility.RdfModelUtility.getObjects;
import static edu.tamu.iiif.utility.StringUtility.encode;
import static edu.tamu.iiif.utility.StringUtility.encodeSpaces;
import static edu.tamu.iiif.utility.StringUtility.joinPath;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import de.digitalcollections.iiif.presentation.model.api.v2.Canvas;
import de.digitalcollections.iiif.presentation.model.api.v2.Image;
import de.digitalcollections.iiif.presentation.model.api.v2.ImageResource;
Expand All @@ -66,6 +35,30 @@
import edu.tamu.iiif.model.RedisManifest;
import edu.tamu.iiif.model.rdf.RdfResource;
import edu.tamu.iiif.model.repo.RedisManifestRepo;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

public abstract class AbstractManifestService implements ManifestService {

Expand Down Expand Up @@ -134,7 +127,6 @@ public String getManifest(ManifestRequest request) throws IOException, URISyntax
redisManifestRepo.save(new RedisManifest(encode(path), getManifestType(), getRepository(), request.getAllowed(), request.getDisallowed(), manifest));
update = false;
}

if (update) {
RedisManifest redisManifest = optionalRedisManifest.get();
manifest = generateManifest(request);
Expand All @@ -144,7 +136,6 @@ public String getManifest(ManifestRequest request) throws IOException, URISyntax
} else {
logger.info("Manifest requested: " + path);
}

return manifest;
}

Expand All @@ -170,25 +161,20 @@ protected Model getRdfModel(String url) throws NotFoundException {
}

private String getRdf(String url) throws NotFoundException {
logger.debug("Requesting RDF for {}", url);

try {
url = URLDecoder.decode(url, StandardCharsets.UTF_8);
if (logger.isDebugEnabled()) {
logger.info("Requesting RDF for {}", url);
}
Optional<String> rdf = Optional.ofNullable(restTemplate.getForObject(url, String.class));
if (rdf.isPresent()) {
logger.debug("RDF for {}: \n{}\n", url, rdf.get());
return rdf.get();
}
String rdf = restTemplate.getForObject(url, String.class);
logger.debug("RDF for {}: \n{}\n", url, rdf);

return rdf;
} catch (RestClientException e) {
logger.error("Failed to get RDF for {}: {}", url, e.getMessage());
logger.debug("Error while requesting RDF for {}: {}", url, e.getMessage(), e);
throw new NotFoundException("RDF not found for " + url, e);
}
throw new NotFoundException("RDF not found! " + url);
}

protected URI buildId(String path) throws URISyntaxException {
return new URI(encodeSpaces(getIiifServiceUrl() + FORWARD_SLASH + getManifestType().getName() + FORWARD_SLASH + path));
return new URI(getIiifServiceUrl() + FORWARD_SLASH + getManifestType().getName() + FORWARD_SLASH + path);
}

protected String getLogo(RdfResource rdfResource) {
Expand Down Expand Up @@ -285,11 +271,12 @@ protected boolean includeResource(ManifestRequest request, String mimeType) {

protected String fetchImageInfo(String url) throws NotFoundException {
logger.debug("Fetching image info {}", url);
Optional<String> imageInfo = Optional.ofNullable(restTemplate.getForObject(url, String.class));
if (imageInfo.isPresent()) {
return imageInfo.get();

try {
return restTemplate.getForObject(url, String.class);
} catch (RestClientException e) {
throw new NotFoundException("Image not found for " + url, e);
}
throw new NotFoundException("Image information not found!");
}

protected URI getImageUri(String url) throws URISyntaxException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/tamu/iiif/service/ManifestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
public interface ManifestService {

public String getManifest(ManifestRequest request) throws IOException, URISyntaxException;

public String getRepository();

public ManifestType getManifestType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,8 @@
import static edu.tamu.iiif.constants.Constants.PRESENTATION_IDENTIFIER;
import static edu.tamu.iiif.constants.Constants.SEQUENCE_IDENTIFIER;
import static edu.tamu.iiif.utility.RdfModelUtility.hasObject;
import static edu.tamu.iiif.utility.StringUtility.encodeSpaces;
import static edu.tamu.iiif.utility.StringUtility.joinPath;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.NodeIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;

import de.digitalcollections.iiif.presentation.model.api.v2.Canvas;
import de.digitalcollections.iiif.presentation.model.api.v2.ImageResource;
import de.digitalcollections.iiif.presentation.model.api.v2.Sequence;
Expand All @@ -43,6 +28,18 @@
import edu.tamu.iiif.model.rdf.RdfResource;
import edu.tamu.iiif.service.AbstractManifestService;
import edu.tamu.iiif.utility.RdfModelUtility;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.NodeIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;

@ConditionalOnExpression(DSPACE_RDF_CONDITION)
public abstract class AbstractDSpaceRdfManifestService extends AbstractManifestService {
Expand Down Expand Up @@ -90,19 +87,19 @@ protected boolean isItem(Model model) {
}

protected URI getDSpaceIiifCollectionUri(String handle) throws URISyntaxException {
return getDSpaceIiifUri(encodeSpaces(handle), COLLECTION_IDENTIFIER);
return getDSpaceIiifUri(handle, COLLECTION_IDENTIFIER);
}

protected URI getDSpaceIiifPresentationUri(String handle) throws URISyntaxException {
return getDSpaceIiifUri(encodeSpaces(handle), PRESENTATION_IDENTIFIER);
return getDSpaceIiifUri(handle, PRESENTATION_IDENTIFIER);
}

protected URI getDSpaceIiifSequenceUri(String handle) throws URISyntaxException {
return getDSpaceIiifUri(encodeSpaces(handle), SEQUENCE_IDENTIFIER);
return getDSpaceIiifUri(handle, SEQUENCE_IDENTIFIER);
}

protected URI getDSpaceIiifCanvasUri(String handle) throws URISyntaxException {
return getDSpaceIiifUri(encodeSpaces(handle), CANVAS_IDENTIFIER);
return getDSpaceIiifUri(handle, CANVAS_IDENTIFIER);
}

protected String getHandle(String uri) {
Expand Down Expand Up @@ -181,7 +178,7 @@ private URI getDSpaceIiifUri(String handle, String type) throws URISyntaxExcepti
private List<Canvas> getCanvases(ManifestRequest request, RdfResource rdfResource) throws IOException, URISyntaxException {
List<Canvas> canvases = new ArrayList<Canvas>();
// NOTE: canvas per bitstream and bitstreams uri must contain the context handle path of the desired resource
String contextHandlePath = encodeSpaces(getHandlePath(rdfResource.getId()));
String contextHandlePath = getHandlePath(rdfResource.getId());
NodeIterator bitstreamIterator = rdfResource.getAllNodesOfPropertyWithId(DSPACE_HAS_BITSTREAM_PREDICATE);
while (bitstreamIterator.hasNext()) {
String uri = bitstreamIterator.next().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,7 @@
import static edu.tamu.iiif.utility.RdfModelUtility.findObject;
import static edu.tamu.iiif.utility.StringUtility.joinPath;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.NodeIterator;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;

import com.fasterxml.jackson.core.JsonProcessingException;

import de.digitalcollections.iiif.presentation.model.api.v2.Canvas;
import de.digitalcollections.iiif.presentation.model.api.v2.ImageResource;
import de.digitalcollections.iiif.presentation.model.api.v2.Metadata;
Expand All @@ -56,6 +39,20 @@
import edu.tamu.iiif.model.rdf.RdfResource;
import edu.tamu.iiif.service.AbstractManifestService;
import edu.tamu.iiif.utility.RdfModelUtility;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.NodeIterator;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;

@ConditionalOnExpression(FEDORA_PCDM_CONDITION)
public abstract class AbstractFedoraPcdmManifestService extends AbstractManifestService {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/edu/tamu/iiif/utility/StringUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ public static String decode(String value) {
return new String(Base64.getDecoder().decode(value.getBytes()));
}

public static String encodeSpaces(String value) {
return value.replace(" ", "%20");
}

}
Loading
Loading