Skip to content

Commit

Permalink
Merge pull request #64 from TAMULib/b03880-b03881-b03882-listen-to-cap
Browse files Browse the repository at this point in the history
B03880 b03881 b03882 Using messaging broker to listen for updates
  • Loading branch information
rladdusaw committed May 1, 2019
2 parents c613871 + e6c25c1 commit aa6aae5
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 34 deletions.
32 changes: 25 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
<description>An Institutional Repository IIIF manifest generation service.</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.x-MESSAGING-MODULE-SNAPSHOT</version>
</parent>

<properties>
Expand All @@ -28,8 +27,28 @@
<jena-libs.version>3.2.0</jena-libs.version>
<iiif-presentation.version>3.2.5</iiif-presentation.version>
</properties>

<repositories>

<repository>
<id>tamu-releases</id>
<url>https://maven.library.tamu.edu/content/repositories/releases</url>
</repository>

<repository>
<id>tamu-snapshots</id>
<url>https://maven.library.tamu.edu/content/repositories/snapshots</url>
</repository>

</repositories>

<dependencies>

<dependency>
<groupId>edu.tamu.weaver</groupId>
<artifactId>messaging</artifactId>
<version>2.x-MESSAGING-MODULE-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -126,6 +145,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand All @@ -138,14 +158,14 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<quiet>true</quiet>
<instrumentation>
Expand All @@ -164,7 +184,6 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand All @@ -177,7 +196,6 @@
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
<configuration>
<repoToken></repoToken>
</configuration>
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/edu/tamu/iiif/config/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package edu.tamu.iiif.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
Expand All @@ -22,7 +20,7 @@ public class CorsConfig {
* @return
*/
@Bean
public FilterRegistrationBean corsFilter() {
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
Expand All @@ -31,9 +29,7 @@ public FilterRegistrationBean corsFilter() {
config.addAllowedMethod("GET");
config.setMaxAge(3600L);
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new CorsFilter(source));
filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return filterRegistrationBean;
return new CorsFilter(source);
}

}
11 changes: 11 additions & 0 deletions src/main/java/edu/tamu/iiif/controller/ManifestRequest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package edu.tamu.iiif.controller;

import static edu.tamu.iiif.utility.StringUtility.decode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import edu.tamu.iiif.model.RedisManifest;

public class ManifestRequest {

private final String context;
Expand Down Expand Up @@ -44,4 +49,10 @@ public static ManifestRequest of(String path, boolean update) {
return new ManifestRequest(path, update, new ArrayList<String>(), new ArrayList<String>());
}

public static ManifestRequest of(RedisManifest manifest) {
List<String> allowedList = Arrays.asList(manifest.getAllowed().split(","));
List<String> disallowedList = Arrays.asList(manifest.getDisallowed().split(","));
return new ManifestRequest(decode(manifest.getPath()), true, allowedList, disallowedList);
}

}
3 changes: 3 additions & 0 deletions src/main/java/edu/tamu/iiif/model/repo/RedisManifestRepo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.tamu.iiif.model.repo;

import java.util.List;
import java.util.Optional;

import org.springframework.data.repository.CrudRepository;
Expand All @@ -11,4 +12,6 @@ public interface RedisManifestRepo extends CrudRepository<RedisManifest, String>

public Optional<RedisManifest> findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(String path, ManifestType type, String repository, String allowed, String disallowed);

public List<RedisManifest> findByPath(String contextPath);

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import edu.tamu.iiif.controller.ManifestRequest;
import edu.tamu.iiif.exception.InvalidUrlException;
import edu.tamu.iiif.exception.NotFoundException;
import edu.tamu.iiif.model.ManifestType;
import edu.tamu.iiif.model.RedisManifest;
import edu.tamu.iiif.model.rdf.RdfResource;
import edu.tamu.iiif.model.repo.RedisManifestRepo;
Expand Down Expand Up @@ -116,7 +115,7 @@ public String getManifest(ManifestRequest request) throws IOException, URISyntax
} else {
logger.info("Generating new manifest.");
manifest = generateManifest(request);
redisManifestRepo.save(new RedisManifest(encode(path), getManifestType(), getRepositoryType(), request.getAllowed(), request.getDisallowed(), manifest));
redisManifestRepo.save(new RedisManifest(encode(path), getManifestType(), getRepository(), request.getAllowed(), request.getDisallowed(), manifest));
update = false;
}

Expand Down Expand Up @@ -346,10 +345,6 @@ protected List<Metadata> getMetadata(RdfResource rdfResource) {

protected abstract String getIiifImageServiceName();

protected abstract String getRepositoryType();

protected abstract ManifestType getManifestType();

protected abstract String getRepositoryContextIdentifier(String url);

protected abstract String getRepositoryPath(String url);
Expand All @@ -369,7 +364,7 @@ private Service getService(RdfResource rdfResource, String name) throws InvalidU
}

private Optional<RedisManifest> getRedisManifest(ManifestRequest request) {
return redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(encode(request.getContext()), getManifestType(), getRepositoryType(), request.getAllowed(), request.getDisallowed());
return redisManifestRepo.findByPathAndTypeAndRepositoryAndAllowedAndDisallowed(encode(request.getContext()), getManifestType(), getRepository(), request.getAllowed(), request.getDisallowed());
}

private List<Metadata> getMetadata(RdfResource rdfResource, String prefix) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/edu/tamu/iiif/service/ManifestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import java.net.URISyntaxException;

import edu.tamu.iiif.controller.ManifestRequest;
import edu.tamu.iiif.model.ManifestType;

public interface ManifestService {

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

public String getRepository();

public ManifestType getManifestType();

}
70 changes: 70 additions & 0 deletions src/main/java/edu/tamu/iiif/service/MessageListenerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package edu.tamu.iiif.service;

import static edu.tamu.iiif.utility.StringUtility.encode;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import edu.tamu.iiif.controller.ManifestRequest;
import edu.tamu.iiif.model.RedisManifest;
import edu.tamu.iiif.model.repo.RedisManifestRepo;
import edu.tamu.weaver.messaging.annotation.WeaverMessageListener;
import edu.tamu.weaver.messaging.config.MessagingConfig;
import edu.tamu.weaver.messaging.model.MessageAction;

@Service
@Profile("weaver-messaging")
@Import(MessagingConfig.class)
public class MessageListenerService {

private static final Logger logger = LoggerFactory.getLogger(MessageListenerService.class);

@Autowired
private RedisManifestRepo manifestRepo;

@Autowired
private List<ManifestService> manifestServices;

@WeaverMessageListener(destination = "${messaging.channels.cap:cap}", containerFactory = "topicContainerFactory")
private void update(Map<String, String> message) {
MessageAction action = MessageAction.valueOf(message.get("action"));
switch (action) {
case CREATE:
case DELETE:
case REFRESH:
case UPDATE:
updateManifest(message.get("id"));
break;
case READ:
default:
break;
}

}

private void updateManifest(String id) {
List<RedisManifest> manifests = manifestRepo.findByPath(encode(id));
manifests.stream().forEach(manifest -> {
manifestServices.stream().filter(manifestService -> manifestService.getManifestType().equals(manifest.getType()) && manifestService.getRepository().equals(manifest.getRepository())).forEach(manifestService -> {
try {
manifestService.getManifest(ManifestRequest.of(manifest));
} catch (IOException | URISyntaxException e) {
if (logger.isDebugEnabled()) {
e.printStackTrace();
}
logger.info(String.format("Unable to get %s %s manifest for %s!", manifest.getRepository(), manifest.getType(), manifest.getPath()));
}
});
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected String getRepositoryPath(String url) {
}

@Override
protected String getRepositoryType() {
public String getRepository() {
return config.getIdentifier();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private String extractHandle(String context) {
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return CANVAS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private List<CollectionReference> getCollections(RdfResource rdfResource) throws
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return COLLECTION;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String generateManifest(ManifestRequest request) throws IOException, URIS
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return IMAGE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private List<Sequence> getSequencesByPredicate(ManifestRequest request, RdfResou
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return PRESENTATION;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String generateManifest(ManifestRequest request) throws IOException, URIS
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return SEQUENCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected String getRepositoryPath(String url) {
}

@Override
protected String getRepositoryType() {
public String getRepository() {
return config.getIdentifier();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String generateManifest(ManifestRequest request) throws IOException, URIS
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return CANVAS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private List<CollectionReference> getSubcollections(RdfResource rdfResource) thr
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return COLLECTION;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String generateManifest(ManifestRequest request) throws IOException, URIS
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return IMAGE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private List<Sequence> getSequences(ManifestRequest request, RdfResource rdfReso
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return PRESENTATION;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String generateManifest(ManifestRequest request) throws IOException, URIS
}

@Override
protected ManifestType getManifestType() {
public ManifestType getManifestType() {
return SEQUENCE;
}

Expand Down
Loading

0 comments on commit aa6aae5

Please sign in to comment.