diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java index 032ea27..9989706 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java @@ -1,6 +1,5 @@ package meerkat.bulletinboard.httpserver; -import java.io.File; import java.io.IOException; import javax.servlet.ServletConfig; @@ -16,7 +15,7 @@ import meerkat.protobuf.BulletinBoardAPI.*; public class BulletinBoardHttpServer extends HttpServlet { - public final static File DEFAULT_MEERKAT_DB = new File("local-instances/meerkat.db"); + public final static String DEFAULT_MEERKAT_DB = "local-instances/meerkat.db"; /** * Auto-generated UID. diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java index 4cb8f38..b8fc3cd 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java @@ -1,6 +1,5 @@ package meerkat.bulletinboard.sqlserver; -import java.io.File; import java.util.Arrays; import java.util.List; @@ -39,7 +38,7 @@ public abstract class BulletinBoardSQLServer implements BulletinBoardServer{ * 2. Call this procedure */ @Override - public void init(File meerkatDB) throws CommunicationException { + public void init(String meerkatDB) throws CommunicationException { // TODO write signature reading part. digest = new SHA256Digest(); diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java index 95f1948..d4aa881 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java @@ -1,6 +1,5 @@ package meerkat.bulletinboard.sqlserver; -import java.io.File; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -41,10 +40,10 @@ public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { } @Override - public void init(File meerkatDB) throws CommunicationException { + public void init(String meerkatDB) throws CommunicationException { try{ - String dbString = "jdbc:sqlite:" + meerkatDB.getPath(); + String dbString = "jdbc:sqlite:" + meerkatDB; connection = DriverManager.getConnection(dbString); createSchema(); diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java index 1b8883a..69f2ba4 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java @@ -1,12 +1,14 @@ package meerkat.bulletinboard.webapp; -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import meerkat.bulletinboard.BulletinBoardServer; @@ -18,26 +20,55 @@ import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessageList; import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; import meerkat.rest.Constants; -import java.io.File; - @Path("/sqlserver") -public class BulletinBoardWebApp implements BulletinBoardServer { +public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextListener{ + + private static final String BULLETIN_BOARD_ATTRIBUTE_NAME = "bulletinBoard"; + + @Context ServletContext servletContext; BulletinBoardServer bulletinBoard; - @PostConstruct + /** + * This is the servlet init method. + */ + public void init(){ + bulletinBoard = (BulletinBoardServer) servletContext.getAttribute(BULLETIN_BOARD_ATTRIBUTE_NAME); + } + + /** + * This is the BulletinBoard init method. + */ @Override - public void init(File meerkatDB) throws CommunicationException { - bulletinBoard = new SQLiteBulletinBoardServer(); + public void init(String meerkatDB) throws CommunicationException { bulletinBoard.init(meerkatDB); } + @Override + public void contextInitialized(ServletContextEvent servletContextEvent) { + ServletContext servletContext = servletContextEvent.getServletContext(); + String meerkatDB = servletContext.getInitParameter("meerkatdb"); + String dbType = servletContext.getInitParameter("dbtype"); + + if (dbType.compareTo("SQLite") == 0){ + bulletinBoard = new SQLiteBulletinBoardServer(); + } + + try { + init(meerkatDB); + servletContext.setAttribute(BULLETIN_BOARD_ATTRIBUTE_NAME, bulletinBoard); + } catch (CommunicationException e) { + System.err.println(e.getMessage()); + } + } + @Path("postmessage") @POST @Consumes(Constants.MEDIATYPE_PROTOBUF) @Produces(Constants.MEDIATYPE_PROTOBUF) @Override public BoolMsg postMessage(BulletinBoardMessage msg) throws CommunicationException { + init(); return bulletinBoard.postMessage(msg); } @@ -47,13 +78,17 @@ public class BulletinBoardWebApp implements BulletinBoardServer { @Produces(Constants.MEDIATYPE_PROTOBUF) @Override public BulletinBoardMessageList readMessages(MessageFilterList filterList) throws CommunicationException { + init(); return bulletinBoard.readMessages(filterList); } @Override - @PreDestroy - public void close() throws CommunicationException { - bulletinBoard.close(); + public void close(){ + try { + bulletinBoard.close(); + } catch (CommunicationException e) { + System.err.println(e.getMessage()); + } } @GET @@ -62,4 +97,11 @@ public class BulletinBoardWebApp implements BulletinBoardServer { return "This BulletinBoard is up and running!\n Please consult the API documents to perform queries."; } + @Override + public void contextDestroyed(ServletContextEvent servletContextEvent) { + ServletContext servletContext = servletContextEvent.getServletContext(); + bulletinBoard = (BulletinBoardServer) servletContext.getAttribute(BULLETIN_BOARD_ATTRIBUTE_NAME); + close(); + } + } diff --git a/bulletin-board-server/src/main/webapp/WEB-INF/web.xml b/bulletin-board-server/src/main/webapp/WEB-INF/web.xml index cc90843..5f513e9 100644 --- a/bulletin-board-server/src/main/webapp/WEB-INF/web.xml +++ b/bulletin-board-server/src/main/webapp/WEB-INF/web.xml @@ -14,4 +14,13 @@ Jersey Hello World /* + + meerkatdb + meerkatdb + + dbtype + SQLite + + meerkat.bulletinboard.webapp.BulletinBoardWebApp + diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardServerTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardServerTest.java deleted file mode 100644 index e0801af..0000000 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardServerTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package meerkat.bulletinboard; - -import java.io.File; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.Test; - -import meerkat.bulletinboard.sqlserver.SQLiteBulletinBoardServer; - -public class BulletinBoardServerTest { - - @Test - public void testAllServers() throws Exception { - GenericBulletinBoardServerTest bbst = new GenericBulletinBoardServerTest(); - - bbst.init(SQLiteBulletinBoardServer.class); - bbst.bulkTest(); - bbst.close(); - } -} diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java index 36a998b..fb26df8 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java @@ -1,6 +1,5 @@ package meerkat.bulletinboard; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; @@ -8,7 +7,6 @@ import java.security.InvalidKeyException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; import java.security.SignatureException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; @@ -28,36 +26,57 @@ import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; -public class GenericBulletinBoardServerTest { - private BulletinBoardServer bulletinBoardServer; +public abstract class GenericBulletinBoardServerTest { + protected BulletinBoardServer bulletinBoardServer; private ECDSASignature signers[]; private Random random; private static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12"; - private static String KEYFILE_PASSWORD = "secret"; + private static String KEYFILE_EXAMPLE3 = "/certs/enduser-certs/user3-key-with-password-shh.p12"; + + private static String KEYFILE_PASSWORD1 = "secret"; + private static String KEYFILE_PASSWORD3 = "shh"; public static String CERT1_PEM_EXAMPLE = "/certs/enduser-certs/user1.crt"; + public static String CERT3_PEM_EXAMPLE = "/certs/enduser-certs/user3.crt"; -// private static String KEYFILE_EXAMPLE2 = "/certs/enduser-certs/user2-key.pem"; - - public void init(Class cls) throws InstantiationException, IllegalAccessException, CertificateException, KeyStoreException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, CommunicationException{ - bulletinBoardServer = (BulletinBoardServer) cls.newInstance(); - - bulletinBoardServer.init(File.createTempFile("meerkat-test", "db")); + /** + * + * @param cls + * @throws InstantiationException + * @throws IllegalAccessException + * @throws CertificateException + * @throws KeyStoreException + * @throws NoSuchAlgorithmException + * @throws IOException + * @throws UnrecoverableKeyException + * @throws CommunicationException + */ + public void init(String meerkatDB) throws InstantiationException, IllegalAccessException, CertificateException, KeyStoreException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, CommunicationException{ + + bulletinBoardServer.init(meerkatDB); signers = new ECDSASignature[2]; signers[0] = new ECDSASignature(); signers[1] = new ECDSASignature(); InputStream keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE); - char[] password = KEYFILE_PASSWORD.toCharArray(); + char[] password = KEYFILE_PASSWORD1.toCharArray(); - KeyStore.Builder keyStore = signers[0].getPKCS12KeyStoreBuilder(keyStream, password); - signers[0].loadSigningCertificate(keyStore); + KeyStore.Builder keyStoreBuilder = signers[0].getPKCS12KeyStoreBuilder(keyStream, password); + signers[0].loadSigningCertificate(keyStoreBuilder); signers[0].loadVerificationCertificates(getClass().getResourceAsStream(CERT1_PEM_EXAMPLE)); + keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE3); + password = KEYFILE_PASSWORD3.toCharArray(); + + keyStoreBuilder = signers[1].getPKCS12KeyStoreBuilder(keyStream, password); + signers[1].loadSigningCertificate(keyStoreBuilder); + + signers[1].loadVerificationCertificates(getClass().getResourceAsStream(CERT3_PEM_EXAMPLE)); + random = new Random(0); // We use insecure randomness in tests for repeatability } @@ -122,6 +141,11 @@ public class GenericBulletinBoardServerTest { if (i % 2 == 1){ signers[0].updateContent(msgBuilder.getMsg()); msgBuilder.addSig(signers[0].sign()); + + if (i % 4 == 1){ + signers[1].updateContent(msgBuilder.getMsg()); + msgBuilder.addSig(signers[1].sign()); + } } // Post message. @@ -166,6 +190,20 @@ public class GenericBulletinBoardServerTest { signers[0].initVerify(msg.getSig(0)); signers[0].updateContent(msg.getMsg()); assertTrue("Signature did not verify!", signers[0].verify()); + + if (msg.getEntryNum() % 4 == 1){ + signers[1].initVerify(msg.getSig(1)); + signers[1].updateContent(msg.getMsg()); + assertTrue("Signature did not verify!", signers[1].verify()); + + assertThat(msg.getSigCount(), is(2)); + } + else{ + assertThat(msg.getSigCount(), is(1)); + } + } + else{ + assertThat(msg.getSigCount(), is(0)); } } diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/SQLiteServerIntegrationTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/SQLiteServerIntegrationTest.java index e090529..95c916d 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/SQLiteServerIntegrationTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/SQLiteServerIntegrationTest.java @@ -22,7 +22,7 @@ import javax.ws.rs.core.Response; public class SQLiteServerIntegrationTest { private static String PROP_GETTY_URL = "gretty.httpBaseURI"; - private static String DEFAULT_BASE_URL = "localhost:8081"; + private static String DEFAULT_BASE_URL = "http://localhost:8081"; private static String BASE_URL = System.getProperty(PROP_GETTY_URL, DEFAULT_BASE_URL); private static String SQL_SERVER_POST = "sqlserver/postmessage"; private static String SQL_SERVER_GET = "sqlserver/readmessages"; @@ -32,6 +32,7 @@ public class SQLiteServerIntegrationTest { @Before public void setup() throws Exception { + System.err.println("Registering client"); client = ClientBuilder.newClient(); client.register(ProtobufMessageBodyReader.class); client.register(ProtobufMessageBodyWriter.class); @@ -64,7 +65,11 @@ public class SQLiteServerIntegrationTest { // Test writing mechanism System.err.println("******** Testing: " + SQL_SERVER_POST); + System.err.println(BASE_URL); + System.err.println(SQL_SERVER_POST); + System.err.println(client.getConfiguration()); webTarget = client.target(BASE_URL).path(SQL_SERVER_POST); + System.err.println(webTarget.getUri()); msg = BulletinBoardMessage.newBuilder() .setMsg(UnsignedBulletinBoardMessage.newBuilder() diff --git a/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java b/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java index 298c290..da53c1f 100644 --- a/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java +++ b/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java @@ -3,8 +3,6 @@ package meerkat.bulletinboard; import meerkat.comm.CommunicationException; import meerkat.protobuf.BulletinBoardAPI.*; -import java.io.File; - /** * Created by Arbel on 07/11/15. * @@ -19,7 +17,7 @@ public interface BulletinBoardServer{ * It also establishes the connection to the DB. * @throws CommunicationException on DB connection error. */ - public void init(File meerkatDB) throws CommunicationException; + public void init(String meerkatDB) throws CommunicationException; /** * Post a message to bulletin board.