From c68eba84d2f27c30131c7c4665750ee860848b9b Mon Sep 17 00:00:00 2001 From: Arbel Deutsch Peled Date: Fri, 20 Nov 2015 21:59:45 +0200 Subject: [PATCH] Refactored into WebApp and Server --- .../sqlserver/SQLiteBulletinBoardServer.java | 28 +-------- .../webapp/BulletinBoardWebApp.java | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java 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 8be9a2d..abc87a2 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 @@ -7,25 +7,13 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.List; -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -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.MediaType; - import com.google.protobuf.InvalidProtocolBufferException; import meerkat.protobuf.BulletinBoardAPI.*; import meerkat.protobuf.Crypto.Signature; -import meerkat.rest.Constants; import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer; import meerkat.comm.CommunicationException; - -@Path("/sqlserver") public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { protected static final int TIMEOUT = 20; @@ -35,7 +23,7 @@ public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { * 1. The database connection * 2. The database tables (if they do not yet exist). */ - @PostConstruct + @Override public void init() throws CommunicationException { @@ -65,7 +53,6 @@ public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { } - @PreDestroy public void close() throws CommunicationException{ try{ @@ -78,7 +65,6 @@ public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { } - @Override protected void insertNewTags(String[] tags) throws SQLException { @@ -104,17 +90,11 @@ public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { } - @Path("postmessage") - @POST @Override public BoolMsg postMessage(BulletinBoardMessage msg) throws CommunicationException { return super.postMessage(msg); } - @Path("readmessages") - @POST - @Consumes(Constants.MEDIATYPE_PROTOBUF) - @Produces(Constants.MEDIATYPE_PROTOBUF) @Override public BulletinBoardMessageList readMessages(MessageFilterList filterList) throws CommunicationException{ @@ -278,11 +258,5 @@ public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { return resultListBuilder.build(); } - - @GET - @Produces(MediaType.TEXT_PLAIN) - public String test() { - return "Hello World"; - } } 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 new file mode 100644 index 0000000..ed162e1 --- /dev/null +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java @@ -0,0 +1,63 @@ +package meerkat.bulletinboard.webapp; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +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.MediaType; + +import meerkat.bulletinboard.BulletinBoardServer; +import meerkat.bulletinboard.sqlserver.SQLiteBulletinBoardServer; +import meerkat.comm.CommunicationException; +import meerkat.protobuf.BulletinBoardAPI.BoolMsg; +import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; +import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessageList; +import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; +import meerkat.rest.Constants; + +@Path("/sqlserver") +public class BulletinBoardWebApp implements BulletinBoardServer{ + + BulletinBoardServer bulletinBoard; + + @PostConstruct + @Override + public void init() throws CommunicationException { + bulletinBoard = new SQLiteBulletinBoardServer(); + bulletinBoard.init(); + } + + @Path("postmessage") + @POST + @Consumes(Constants.MEDIATYPE_PROTOBUF) + @Produces(Constants.MEDIATYPE_PROTOBUF) + @Override + public BoolMsg postMessage(BulletinBoardMessage msg) throws CommunicationException { + return bulletinBoard.postMessage(msg); + } + + @Path("readmessages") + @POST + @Consumes(Constants.MEDIATYPE_PROTOBUF) + @Produces(Constants.MEDIATYPE_PROTOBUF) + @Override + public BulletinBoardMessageList readMessages(MessageFilterList filterList) throws CommunicationException { + return bulletinBoard.readMessages(filterList); + } + + @Override + @PreDestroy + public void close() throws CommunicationException { + bulletinBoard.close(); + } + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String test() { + return "This BulletinBoard is up and running!\n Please consult the API documents to perform queries."; + } + +}