From b934894bc52d5139d5a0ce460a9f489201fa43cf Mon Sep 17 00:00:00 2001 From: "arbel.peled" Date: Thu, 5 May 2016 16:55:10 +0300 Subject: [PATCH] Created Polling Station Scanner interface Implemented Web App for the scanner Not tested --- .../PollingStationConstants.java | 15 ++++ .../pollingstation/PollingStationScanner.java | 29 ++++++++ .../main/proto/meerkat/pollingstation.proto | 15 ++++ polling-station/build.gradle | 9 +++ .../PollingStationWebScanner.java | 71 +++++++++++++++++++ .../src/main/webapp/META-INF/jetty-env.xml | 12 ++++ .../src/main/webapp/WEB-INF/web.xml | 20 ++++++ 7 files changed, 171 insertions(+) create mode 100644 meerkat-common/src/main/java/meerkat/pollingstation/PollingStationConstants.java create mode 100644 meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java create mode 100644 meerkat-common/src/main/proto/meerkat/pollingstation.proto create mode 100644 polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java create mode 100644 polling-station/src/main/webapp/META-INF/jetty-env.xml create mode 100644 polling-station/src/main/webapp/WEB-INF/web.xml diff --git a/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationConstants.java b/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationConstants.java new file mode 100644 index 0000000..643a73c --- /dev/null +++ b/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationConstants.java @@ -0,0 +1,15 @@ +package meerkat.pollingstation; + +/** + * Created by Arbel Deutsch Peled on 21-Dec-15. + */ +public interface PollingStationConstants { + + // Relative addresses for Scanner operations + + public static final String POLLING_STATION_WEB_SCANNER_PATH = "/scanner"; + public static final String POLLING_STATION_WEB_SCANNER_SCAN_PATH = "/scan"; + public static final String POLLING_STATION_WEB_SCANNER_ERROR_PATH = "/error"; + + +} diff --git a/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java b/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java new file mode 100644 index 0000000..9bea5b5 --- /dev/null +++ b/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java @@ -0,0 +1,29 @@ +package meerkat.pollingstation; + +import com.google.common.util.concurrent.FutureCallback; +import meerkat.protobuf.PollingStation.*; +/** + * Created by Arbel on 05/05/2016. + * An interface for the scanner used by the Polling Station Committee + */ +public interface PollingStationScanner { + + /** + * Subscribes to new scans + * @param scanCallback is the handler for scanned data + */ + public void subscribe(FutureCallback scanCallback); + + /** + * Sends a scan to all subscribers + * @param scannedData contains the scanned data + */ + public void newScan(ScannedData scannedData); + + /** + * Notifies subscribers about an error that occurred during scan + * @param errorMsg is the error that occurred + */ + public void reportScanError(ErrorMsg errorMsg); + +} \ No newline at end of file diff --git a/meerkat-common/src/main/proto/meerkat/pollingstation.proto b/meerkat-common/src/main/proto/meerkat/pollingstation.proto new file mode 100644 index 0000000..0cdc658 --- /dev/null +++ b/meerkat-common/src/main/proto/meerkat/pollingstation.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package meerkat; + +option java_package = "meerkat.protobuf"; + +// Container for scanned data +message ScannedData { + bytes data = 1; +} + +// Container for error messages +message ErrorMsg { + string msg = 1; +} \ No newline at end of file diff --git a/polling-station/build.gradle b/polling-station/build.gradle index d510d26..036d2e8 100644 --- a/polling-station/build.gradle +++ b/polling-station/build.gradle @@ -2,8 +2,10 @@ plugins { id "us.kirchmeier.capsule" version "1.0.1" id 'com.google.protobuf' version '0.7.0' + id 'org.akhikhl.gretty' version "1.2.4" } +apply plugin: 'org.akhikhl.gretty' apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' @@ -41,6 +43,13 @@ version += "${isSnapshot ? '-SNAPSHOT' : ''}" dependencies { // Meerkat common compile project(':meerkat-common') + compile project(':restful-api-common') + + // Jersey for RESTful API + compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.22.+' + + // Servlets + compile 'javax.servlet:javax.servlet-api:3.0.+' // Logging compile 'org.slf4j:slf4j-api:1.7.7' diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java new file mode 100644 index 0000000..a3aefdd --- /dev/null +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java @@ -0,0 +1,71 @@ +package meerkat.pollingstation; + +import com.google.common.util.concurrent.FutureCallback; +import meerkat.protobuf.PollingStation; +import static meerkat.rest.Constants.*; + +import javax.ws.rs.*; +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +import static meerkat.pollingstation.PollingStationConstants.*; +import meerkat.protobuf.PollingStation.*; + +/** + * Created by Arbel on 05/05/2016. + */ +@Path(POLLING_STATION_WEB_SCANNER_PATH) +public class PollingStationWebScanner implements PollingStationScanner{ + + private final List> callbacks; + + public PollingStationWebScanner() { + callbacks = new LinkedList<>(); + } + + @Override + public void subscribe(FutureCallback scanCallback) { + callbacks.add(scanCallback); + } + + @Path(POLLING_STATION_WEB_SCANNER_SCAN_PATH) + @POST + @Consumes(MEDIATYPE_PROTOBUF) + @Produces(MEDIATYPE_PROTOBUF) + @Override + public void newScan(ScannedData scannedData) { + + if (callbacks.size() <= 0) + throw new RuntimeException("No subscribers to forward scan to!"); + + for (FutureCallback callback : callbacks){ + callback.onSuccess(scannedData); + } + + } + + @Path(POLLING_STATION_WEB_SCANNER_ERROR_PATH) + @POST + @Consumes(MEDIATYPE_PROTOBUF) + @Produces(MEDIATYPE_PROTOBUF) + @Override + public void reportScanError(PollingStation.ErrorMsg errorMsg) { + + if (callbacks.size() <= 0) + throw new RuntimeException("No subscribers to forward error to!"); + + for (FutureCallback callback : callbacks){ + callback.onFailure(new IOException(errorMsg.getMsg())); + } + + } + + @Path("/test") + @GET + public String test(){ + return "test"; + } + + +} diff --git a/polling-station/src/main/webapp/META-INF/jetty-env.xml b/polling-station/src/main/webapp/META-INF/jetty-env.xml new file mode 100644 index 0000000..c4d368f --- /dev/null +++ b/polling-station/src/main/webapp/META-INF/jetty-env.xml @@ -0,0 +1,12 @@ + + + + + org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern + none + + + org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern + none + + \ No newline at end of file diff --git a/polling-station/src/main/webapp/WEB-INF/web.xml b/polling-station/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..0632b0d --- /dev/null +++ b/polling-station/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,20 @@ + + + Jersey Hello World + + org.glassfish.jersey.servlet.ServletContainer + + + jersey.config.server.provider.packages + meerkat + + 1 + + + Jersey Hello World + /* + + + meerkat.pollingstation.PollingStationWebScanner + +