Skip to content

Commit

Permalink
BXC-3284 update controller and service classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Luong committed Nov 7, 2023
1 parent a7b8223 commit 4f4215c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 177 deletions.
6 changes: 6 additions & 0 deletions web-access-app/src/main/webapp/WEB-INF/service-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<property name="basePath" value="${access.base.url}"/>
<property name="httpClientConnectionManager" ref="httpClientConnectionManager" />
</bean>

<bean id="imageServerProxyService" class="edu.unc.lib.boxc.web.common.services.ImageServerProxyService">
<property name="imageServerProxyPath" value="${imageServerProxy.base.url}"/>
<property name="basePath" value="${access.base.url}"/>
<property name="httpClientConnectionManager" ref="httpClientConnectionManager" />
</bean>

<bean id="findingAidUrlService" class="edu.unc.lib.boxc.web.common.services.FindingAidUrlService"
init-method="init">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void getRegion(@PathVariable("id") String id,
response.addHeader("Access-Control-Allow-Origin", "*");
imageServerProxyService.streamJP2(
id, region, size, rotation, quality, format,
response.getOutputStream(), response);
response.getOutputStream(), response, 1);
} catch (IOException e) {
LOG.error("Error retrieving streaming JP2 content for {}", id, e);
}
Expand All @@ -100,7 +100,7 @@ public void getMetadata(@PathVariable("id") String id, HttpServletResponse respo
if (this.hasAccess(pid)) {
try {
response.addHeader("Access-Control-Allow-Origin", "*");
imageServerProxyService.getMetadata(id, response.getOutputStream(), response);
imageServerProxyService.getMetadata(id, response.getOutputStream(), response, 1);
} catch (IOException e) {
LOG.error("Error retrieving JP2 metadata content for {}", id, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

import java.io.OutputStream;
import java.net.URI;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
Expand All @@ -22,23 +19,12 @@
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import de.digitalcollections.iiif.model.ImageContent;
import de.digitalcollections.iiif.model.image.ImageApiProfile;
import de.digitalcollections.iiif.model.image.ImageService;
import de.digitalcollections.iiif.model.jackson.IiifObjectMapper;
import de.digitalcollections.iiif.model.sharedcanvas.Canvas;
import de.digitalcollections.iiif.model.sharedcanvas.Manifest;
import de.digitalcollections.iiif.model.sharedcanvas.Sequence;
import edu.unc.lib.boxc.common.util.URIUtil;
import edu.unc.lib.boxc.model.api.DatastreamType;
import edu.unc.lib.boxc.model.api.ResourceType;
import edu.unc.lib.boxc.search.api.models.ContentObjectRecord;
import edu.unc.lib.boxc.search.api.models.Datastream;
import edu.unc.lib.boxc.web.common.exceptions.ClientAbortException;
import edu.unc.lib.boxc.web.common.utils.FileIOUtil;

Expand All @@ -49,33 +35,24 @@
* @author bbpennel, snluong
*/
public class ImageServerProxyService {
private static final Logger LOG = LoggerFactory.getLogger(LorisContentService.class);

private static final Logger LOG = LoggerFactory.getLogger(ImageServerProxyService.class);
private CloseableHttpClient httpClient;
private HttpClientConnectionManager httpClientConnectionManager;

private String imageServerProxyPath;
private String basePath;
private ObjectMapper iiifMapper = new IiifObjectMapper();

public void setHttpClientConnectionManager(HttpClientConnectionManager manager) {
this.httpClientConnectionManager = manager;

RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(2000)
.setConnectionRequestTimeout(5000)
.build();

this.httpClient = HttpClients.custom()
.setConnectionManager(httpClientConnectionManager)
.setConnectionManager(manager)
.setDefaultRequestConfig(requestConfig)
.build();
}

public void getMetadata(String simplepid, OutputStream outStream, HttpServletResponse response) {
getMetadata(simplepid, outStream, response, 1);
}

public void getMetadata(String simplepid, OutputStream outStream,
HttpServletResponse response, int retryServerError) {

Expand Down Expand Up @@ -122,11 +99,6 @@ public void getMetadata(String simplepid, OutputStream outStream,
LOG.error("Unexpected failure while getting image server proxy path {}: {}", statusLine, path);
}

public void streamJP2(String simplepid, String region, String size, String rotation, String quality,
String format, OutputStream outStream, HttpServletResponse response ) {
this.streamJP2(simplepid, region, size, rotation, quality, format, outStream, response, 1);
}

public void streamJP2(String simplepid, String region, String size, String rotation, String quality,
String format, OutputStream outStream, HttpServletResponse response,
int retryServerError) {
Expand Down Expand Up @@ -166,147 +138,6 @@ public void streamJP2(String simplepid, String region, String size, String rotat
}
}

public String getManifest(HttpServletRequest request, List<ContentObjectRecord> briefObjs)
throws JsonProcessingException {
String manifestBase = getRecordPath(request);
ContentObjectRecord rootObj = briefObjs.get(0);

String title = getTitle(rootObj);

Manifest manifest = new Manifest(URIUtil.join(manifestBase, "manifest"), title);

String abstractText = rootObj.getAbstractText();
List<String> creators = rootObj.getCreator();
List<String> subjects = rootObj.getSubject();
List<String> language = rootObj.getLanguage();

if (abstractText != null) {
manifest.addDescription(abstractText);
}

manifest.addLogo(new ImageContent(URIUtil.join(basePath, "static", "images", "unc-icon.png")));

setMetadataField(manifest, "Creators", creators);
setMetadataField(manifest, "Subjects", subjects);
setMetadataField(manifest, "Languages", language);
manifest.addMetadata("", "<a href=\"" +
URIUtil.join(basePath, "record", rootObj.getId()) + "\">View full record</a>");
String attribution = "University of North Carolina Libraries, Digital Collections Repository";
String collection = rootObj.getParentCollectionName();
if (collection != null) {
attribution += " - Part of " + collection;
}
manifest.addMetadata("Attribution", attribution);

Sequence seq = createSequence(manifestBase, briefObjs);

return iiifMapper.writeValueAsString(manifest.addSequence(seq));
}

public String getSequence(HttpServletRequest request, List<ContentObjectRecord> briefObjs)
throws JsonProcessingException {
String path = getRecordPath(request);
return iiifMapper.writeValueAsString(createSequence(path, briefObjs));
}

public String getCanvas(HttpServletRequest request, ContentObjectRecord briefObj)
throws JsonProcessingException {
String path = getRecordPath(request);
return iiifMapper.writeValueAsString(createCanvas(path, briefObj));
}

private Sequence createSequence(String seqPath, List<ContentObjectRecord> briefObjs) {
Sequence seq = new Sequence(URIUtil.join(seqPath, "sequence", "normal"));

ContentObjectRecord rootObj = briefObjs.get(0);
if (rootObj.getResourceType().equals(ResourceType.Work.name())) {
String rootJp2Id = jp2Pid(rootObj);
briefObjs.remove(0);
// Move the primary object to the beginning of the sequence
if (rootJp2Id != null && !rootJp2Id.equals(rootObj.getId())) {
for (int i = 0; i < briefObjs.size(); i++) {
ContentObjectRecord briefObj = briefObjs.get(i);
if (briefObj.getId().equals(rootJp2Id)) {
if (i != 0) {
briefObjs.remove(i);
briefObjs.add(0, briefObj);
}
break;
}
}
}
}

for (ContentObjectRecord briefObj : briefObjs) {
String datastreamUuid = jp2Pid(briefObj);
if (!StringUtils.isEmpty(datastreamUuid)) {
Canvas canvas = createCanvas(seqPath, briefObj);
seq.addCanvas(canvas);
}
}

return seq;
}

private Canvas createCanvas(String path, ContentObjectRecord briefObj) {
String title = getTitle(briefObj);
String uuid = jp2Pid(briefObj);

Canvas canvas = new Canvas(path, title);
if (StringUtils.isEmpty(uuid)) {
LOG.warn("No jp2 id was found for {}. IIIF canvas is empty.", briefObj.getId());
return canvas;
}

String canvasPath = URIUtil.join(basePath, "jp2Proxy", uuid, "jp2");

Datastream fileDs = briefObj.getDatastreamObject(DatastreamType.ORIGINAL_FILE.getId());
String extent = fileDs.getExtent();
if (extent != null && !extent.equals("")) {
String[] imgDimensions = extent.split("x");
canvas.setHeight(Integer.parseInt(imgDimensions[0]));
canvas.setWidth(Integer.parseInt(imgDimensions[1]));
}

canvas.addIIIFImage(canvasPath, ImageApiProfile.LEVEL_TWO);
ImageContent thumb = new ImageContent(URIUtil.join(basePath,
"services", "api", "thumb", uuid, "large"));
canvas.addImage(thumb);

return canvas;
}

private String getRecordPath(HttpServletRequest request) {
String[] url = request.getRequestURL().toString().split("\\/");
String uuid = url[4];
String datastream = url[5];
return URIUtil.join(basePath, "jp2Proxy", uuid, datastream);
}

private String jp2Pid(ContentObjectRecord briefObj) {
Datastream datastream = briefObj.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId());
if (datastream != null) {
String id = datastream.getOwner();
if (id.equals("")) {
return briefObj.getId();
}
return id;
}

return null;
}

private void setMetadataField(Manifest manifest, String fieldName, List<String> field) {
if (!CollectionUtils.isEmpty(field)) {
manifest.addMetadata(fieldName, String.join(", ", field));
}
}

private String getTitle(ContentObjectRecord briefObj) {
String title = briefObj.getTitle();
return (title != null) ? title : "";
}

public void setImageServerProxyPath(String fullPath) {
this.imageServerProxyPath = fullPath;
}
Expand All @@ -318,8 +149,4 @@ public String getImageServerProxyPath() {
public void setBasePath(String basePath) {
this.basePath = basePath;
}

public String getBasePath() {
return basePath;
}
}

0 comments on commit 4f4215c

Please sign in to comment.