Skip to content

Commit

Permalink
Re-implemented rest web service using Dropwizard and Swagger document…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
EdDuarte committed Jul 2, 2016
1 parent 624d6e9 commit 5ad3e6f
Show file tree
Hide file tree
Showing 68 changed files with 49,294 additions and 406 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

<targetJdk>1.8</targetJdk>

<jersey.version>2.17</jersey.version>
<jersey.version>2.23.1</jersey.version>
<servlet-api.version>3.0.1</servlet-api.version>
<jackson.version>2.5.3</jackson.version>

Expand All @@ -140,7 +140,7 @@
<swagger-version>1.5.8</swagger-version>
<swagger-ui-version>2.1.4</swagger-ui-version>

<dropwizard.version>0.9.2</dropwizard.version>
<dropwizard.version>0.9.3</dropwizard.version>

</properties>

Expand Down
100 changes: 55 additions & 45 deletions vokter-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,6 @@

<dependencies>

<!-- dropwizard (included here so we have -->
<!-- access to the MediaType.java class -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>


<!-- mutable string -->
<dependency>
<groupId>it.unimi.dsi</groupId>
Expand All @@ -208,17 +199,12 @@
</dependency>


<!-- MediaType class -->
<!--<dependency>-->
<!--<groupId>com.fasterxml.jackson.core</groupId>-->
<!--<artifactId>jackson-databind</artifactId>-->
<!--<version>${jackson.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.glassfish.jersey.containers</groupId>-->
<!--<artifactId>jersey-container-servlet</artifactId>-->
<!--<version>${jersey.version}</version>-->
<!--</dependency>-->
<!-- jersey client and MediaType class -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>


<!-- sentence splitting parsing -->
Expand Down Expand Up @@ -247,6 +233,12 @@
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
Expand All @@ -256,43 +248,61 @@


<!-- other utils -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.glassfish.jersey.bundles.repackaged</groupId>-->
<!--<artifactId>jersey-guava</artifactId>-->
<!--<version>${jersey.version}</version>-->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>com.google.guava</groupId>-->
<!--<artifactId>guava</artifactId>-->
<!--</exclusion>-->
<!--<exclusion>-->
<!--<groupId>org.eclipse.jetty</groupId>-->
<!--<artifactId>jetty-server</artifactId>-->
<!--</exclusion>-->
<!--<exclusion>-->
<!--<groupId>org.eclipse.jetty</groupId>-->
<!--<artifactId>jetty-util</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
<!--</dependency>-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
<!--<dependency>-->
<!--<groupId>commons-codec</groupId>-->
<!--<artifactId>commons-codec</artifactId>-->
<!--<version>1.9</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.commons</groupId>-->
<!--<artifactId>commons-lang3</artifactId>-->
<!--<version>3.0</version>-->
<!--</dependency>-->


<!-- language detector -->
<dependency>
<groupId>com.optimaize.languagedetector</groupId>
<artifactId>language-detector</artifactId>
<version>0.5</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
Expand Down
5 changes: 0 additions & 5 deletions vokter-core/src/main/java/com/edduarte/vokter/diff/Match.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.edduarte.vokter.diff;

import com.edduarte.vokter.keyword.Keyword;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* @author Eduardo Duarte (<a href="mailto:hello@edduarte.com">hello@edduarte.com</a>)
Expand All @@ -13,27 +12,23 @@ public class Match {
/**
* The event of this difference ('added', 'removed' or 'nothing').
*/
@JsonProperty(required = true)
private DiffEvent event;

/**
* The keyword contained within this difference.
*/
@JsonProperty(required = true)
private Keyword keyword;

/**
* The text that contains the keyword above that was either added or
* removed from the document.
*/
@JsonProperty(required = true)
private String text;

/**
* The text that contains the difference text above along with surrounding
* text that contextualizes where this difference occurred.
*/
@JsonProperty(required = true)
private String snippet;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.edduarte.vokter.diff.DiffEvent;
import com.edduarte.vokter.diff.Match;
import com.edduarte.vokter.keyword.Keyword;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.quartz.InterruptableJob;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
Expand Down Expand Up @@ -91,51 +91,46 @@ public void execute(JobExecutionContext context)
String clientContentType = dataMap.getString(CLIENT_CONTENT_TYPE);
String clientToken = dataMap.getString(CLIENT_TOKEN);

try {
ObjectMapper mapper = new ObjectMapper();

boolean hasNewDifferences = dataMap.getBoolean(HAS_NEW_DIFFS);
if (hasNewDifferences) {
dataMap.put(HAS_NEW_DIFFS, false);

List<String> keywords = mapper.readValue(
dataMap.getString(KEYWORDS), ArrayList.class);
List<DiffEvent> events = mapper.readValue(
dataMap.getString(EVENTS), ArrayList.class);
boolean ignoreAdded = !events.contains(DiffEvent.inserted);
boolean ignoreRemoved = !events.contains(DiffEvent.deleted);
boolean filterStopwords = dataMap.getBoolean(FILTER_STOPWORDS);
boolean enableStemming = dataMap.getBoolean(ENABLE_STEMMING);
boolean ignoreCase = dataMap.getBoolean(IGNORE_CASE);
int snippetOffset = dataMap.getInt(SNIPPET_OFFSET);

// build keywords
List<Keyword> kws = keywords.stream()
.map((keywordInput) -> manager.callBuildKeyword(
keywordInput,
filterStopwords,
enableStemming,
ignoreCase
))
.collect(Collectors.toList());

// match them
Set<Match> results = manager.callGetMatchesImpl(
boolean hasNewDifferences = dataMap.getBoolean(HAS_NEW_DIFFS);
if (hasNewDifferences) {
dataMap.put(HAS_NEW_DIFFS, false);

Gson gson = new Gson();
List<String> keywords = gson
.fromJson(dataMap.getString(KEYWORDS), ArrayList.class);
List<DiffEvent> events = gson
.fromJson(dataMap.getString(EVENTS), ArrayList.class);
boolean ignoreAdded = !events.contains(DiffEvent.inserted);
boolean ignoreRemoved = !events.contains(DiffEvent.deleted);
boolean filterStopwords = dataMap.getBoolean(FILTER_STOPWORDS);
boolean enableStemming = dataMap.getBoolean(ENABLE_STEMMING);
boolean ignoreCase = dataMap.getBoolean(IGNORE_CASE);
int snippetOffset = dataMap.getInt(SNIPPET_OFFSET);

// build keywords
List<Keyword> kws = keywords.stream()
.map((keywordInput) -> manager.callBuildKeyword(
keywordInput,
filterStopwords,
enableStemming,
ignoreCase
))
.collect(Collectors.toList());

// match them
Set<Match> results = manager.callGetMatchesImpl(
documentUrl, documentContentType,
kws, filterStopwords, enableStemming, ignoreCase,
ignoreAdded, ignoreRemoved, snippetOffset);
if (!results.isEmpty()) {
boolean wasSuccessful = manager.sendNotificationToClient(
documentUrl, documentContentType,
kws, filterStopwords, enableStemming, ignoreCase,
ignoreAdded, ignoreRemoved, snippetOffset);
if (!results.isEmpty()) {
boolean wasSuccessful = manager.sendNotificationToClient(
documentUrl, documentContentType,
clientUrl, clientContentType, clientToken,
results
);
// TODO: Add fault tolerance so that, if failed 10 times,
// cancel this matching job and send a timeout to the client
}
clientUrl, clientContentType, clientToken,
results
);
// TODO: Add fault tolerance so that, if failed 10 times,
// cancel this matching job and send a timeout to the client
}
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}
}

Expand Down
21 changes: 10 additions & 11 deletions vokter-core/src/main/java/com/edduarte/vokter/job/JobManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import com.edduarte.vokter.persistence.DocumentCollection;
import com.edduarte.vokter.persistence.Session;
import com.edduarte.vokter.persistence.SessionCollection;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.optimaize.langdetect.LanguageDetector;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
Expand Down Expand Up @@ -76,7 +75,7 @@ public class JobManager {

private final String managerName;

private final NotificationHandler handler;
private final JobManagerListener handler;

private final DocumentCollection documentCollection;

Expand All @@ -103,7 +102,7 @@ private JobManager(String managerName,
LanguageDetector langDetector,
boolean ignoreCase,
boolean filterStopwords,
NotificationHandler handler) {
JobManagerListener handler) {
this.managerName = managerName;
this.handler = handler;
this.documentCollection = documentCollection;
Expand All @@ -124,7 +123,7 @@ public static JobManager create(String managerName,
LanguageDetector langDetector,
boolean ignoreCase,
boolean filterStopwords,
NotificationHandler handler) {
JobManagerListener handler) {
JobManager existingManager = get(managerName);
if (existingManager != null) {
existingManager.stop();
Expand Down Expand Up @@ -292,9 +291,9 @@ public Session createJob(
documentUrl, documentContentType);
}

ObjectMapper mapper = new ObjectMapper();
String keywordJson = mapper.writeValueAsString(keywords);
String eventsJson = mapper.writeValueAsString(events);
Gson gson = new Gson();
String keywordJson = gson.toJson(keywords);
String eventsJson = gson.toJson(events);

// attempt to create a new matching job, chained to execute after
// the detection job
Expand Down Expand Up @@ -396,7 +395,7 @@ public Session createJob(

return sessionCollection.add(clientUrl, clientContentType, token);

} catch (SchedulerException | JsonProcessingException ex) {
} catch (SchedulerException ex) {
logger.error(ex.getMessage(), ex);
return null;
}
Expand Down Expand Up @@ -716,7 +715,7 @@ final boolean sendTimeoutToClient(String documentUrl, String documentContentType
String clientUrl, String clientContentType, String clientToken) {
Session session = sessionCollection
.validateToken(clientUrl, clientContentType, clientToken);
return handler.sendTimeoutToClient(
return handler.onTimeout(
documentUrl, documentContentType,
session
);
Expand All @@ -728,7 +727,7 @@ final boolean sendNotificationToClient(String documentUrl, String documentConten
Set<Match> results) {
Session session = sessionCollection
.validateToken(clientUrl, clientContentType, clientToken);
return handler.sendNotificationToClient(
return handler.onNotification(
documentUrl, documentContentType,
session, results
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@
* @version 1.3.2
* @since 1.0.0
*/
public interface NotificationHandler {
public interface JobManagerListener {

/**
* Sends a notification to the client with the specified matched diffs.
* Logic where a notification with the specified matched diffs is sent to
* the client.
*/
boolean sendNotificationToClient(String documentUrl, String documentContentType,
Session session, Set<Match> diffs);
boolean onNotification(String documentUrl, String documentContentType,
Session session, Set<Match> diffs);

/**
* Sends a time-out warning to the client.
* Logic where a time-out warning must be sent to the client.
*/
boolean sendTimeoutToClient(String documentUrl, String documentContentType,
Session session);
boolean onTimeout(String documentUrl, String documentContentType,
Session session);
}
Loading

0 comments on commit 5ad3e6f

Please sign in to comment.