diff --git a/ma-impl.sublime-workspace b/ma-impl.sublime-workspace index 2bda71b97b7ed733b41d92a0e3b140a1256e24b1..1a77f6f7f3582700b4728bd95cd8c734a513e573 100644 --- a/ma-impl.sublime-workspace +++ b/ma-impl.sublime-workspace @@ -451,11 +451,15 @@ [ "/home/eike/Repositories/fu/ss15/ma/impl", "/home/eike/Repositories/fu/ss15/ma/impl/vm", - "/home/eike/Repositories/fu/ss15/ma/impl/vm/config" + "/home/eike/Repositories/fu/ss15/ma/impl/vm/config", + "/home/eike/Repositories/fu/ss15/ma/impl/vm/data" ], "file_history": [ + "/home/eike/Repositories/fu/ss15/ma/impl/vm/data/data.json", "/home/eike/Repositories/fu/ss15/ma/impl/vm/bootstrap.sh", + "/home/eike/Repositories/fu/ss15/ma/impl/vm/config/environment", + "/home/eike/Repositories/fu/ss15/ma/impl/vm/config/disable-transparent-hugepages", "/home/eike/Repositories/fu/ss15/ma/impl/Vagrantfile", "/home/eike/Repositories/fu/ss15/ma/impl/vm/config/spark-env.sh", "/home/eike/Repositories/fu/ss15/ma/impl/vm/rc.sh", @@ -463,7 +467,6 @@ "/home/eike/Repositories/fu/ss15/ma/impl/vm/env.sh", "/home/eike/Repositories/fu/ss15/ma/impl/ui/backend/pom.xml", "/home/eike/Repositories/fu/ss15/ma/impl/vm/config/mongo-express.config.js", - "/home/eike/Repositories/fu/ss15/ma/impl/vm/config/disable-transparent-hugepages", "/home/eike/Repositories/fu/ss15/ma/impl/data/data.json", "/home/eike/Repositories/fu/ss15/ma/impl/vm/Vagrantfile", "/home/eike/Repositories/fu/ss15/ma/impl/data/data.sql", @@ -564,6 +567,9 @@ "case_sensitive": false, "find_history": [ + "\"id\":.*?\\n ", + "\"id\":.*?\\n", + "\"_id\":", "000+00\"", "\\$date\": ([^,]+),", "\"id\"", @@ -688,17 +694,16 @@ "images", "url(\"../images/", "fa-var", - "account.name", - "category.name", - "t('", - ".name" + "account.name" ], "highlight": true, "in_selection": false, "preserve_case": false, - "regex": false, + "regex": true, "replace_history": [ + "", + "\"id\":", "000+0000\"", "date\": { \"\\$date\": $1 },", "\"_id\"", @@ -824,9 +829,7 @@ "/assets", "Config::get", "Lang::t", - "@__", - "@T", - "'" + "@__" ], "reverse": false, "show_context": true, diff --git a/tmbs-rest-backend/.classpath b/tmbs-rest-backend/.classpath index c1c6abd4fca0e1d10e70b4bb4a5c076bb8cd2b83..0ea5b457813ab8b17b1513a6956425e2739c552c 100644 --- a/tmbs-rest-backend/.classpath +++ b/tmbs-rest-backend/.classpath @@ -1,16 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="src" output="target/classes" path="src/main/java"> - <attributes> - <attribute name="optional" value="true"/> - <attribute name="maven.pomderived" value="true"/> - </attributes> - </classpathentry> - <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> - <attributes> - <attribute name="maven.pomderived" value="true"/> - </attributes> - </classpathentry> + <classpathentry kind="src" path="src/main/java"/> + <classpathentry kind="src" path="src/main/resources"/> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> @@ -19,7 +10,7 @@ </classpathentry> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <attributes> - <attribute name="maven.pomderived" value="true"/> + <attribute name="owner.project.facets" value="java"/> </attributes> </classpathentry> <classpathentry kind="output" path="target/classes"/> diff --git a/tmbs-rest-backend/.settings/org.eclipse.wst.common.component b/tmbs-rest-backend/.settings/org.eclipse.wst.common.component index cf0a78dc927fe987c3bf2efc0a5187da3afcf058..7ffe805eac95f76fb915c35104d8610d42660284 100644 --- a/tmbs-rest-backend/.settings/org.eclipse.wst.common.component +++ b/tmbs-rest-backend/.settings/org.eclipse.wst.common.component @@ -5,6 +5,6 @@ <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/> <property name="java-output-path" value="/tmbs-rest-backend/target/classes"/> - <property name="context-root" value="tmbs-rest-backend"/> + <property name="context-root" value="tmbs"/> </wb-module> </project-modules> diff --git a/tmbs-rest-backend/.settings/org.eclipse.wst.common.project.facet.core.xml b/tmbs-rest-backend/.settings/org.eclipse.wst.common.project.facet.core.xml index 4816bb83da0f206866f376f9328b61057dd0ee48..d59fb58d14dd57692727e9b2e695dd99cd5929ce 100644 --- a/tmbs-rest-backend/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/tmbs-rest-backend/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -2,7 +2,7 @@ <faceted-project> <fixed facet="wst.jsdt.web"/> <installed facet="wst.jsdt.web" version="1.0"/> - <installed facet="java" version="1.8"/> <installed facet="jst.jaxrs" version="2.0"/> <installed facet="jst.web" version="3.1"/> + <installed facet="java" version="1.8"/> </faceted-project> diff --git a/tmbs-rest-backend/pom.xml b/tmbs-rest-backend/pom.xml index 9a69b2aadda00942b5619662bdfe45f1da4818bc..8841b573f1a81c37941db19cfbf47431435227ab 100644 --- a/tmbs-rest-backend/pom.xml +++ b/tmbs-rest-backend/pom.xml @@ -31,5 +31,10 @@ <artifactId>log4j-core</artifactId> <version>2.4.1</version> </dependency> + <dependency> + <groupId>org.mongodb</groupId> + <artifactId>mongodb-driver</artifactId> + <version>3.0.4</version> + </dependency> </dependencies> </project> diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/Application.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/Application.java deleted file mode 100644 index 38fc8eb7f6df596aab8ca5e1a6f79fc890229fd9..0000000000000000000000000000000000000000 --- a/tmbs-rest-backend/src/main/java/de/cochu/backend/Application.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.cochu.backend; - -import org.glassfish.jersey.filter.LoggingFilter; -import org.glassfish.jersey.server.ResourceConfig; - -public class Application extends ResourceConfig { - - public Application() { - packages("de.cochu.backend"); - //register(LoggingFilter.class); - } - -} diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/Test.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/Test.java deleted file mode 100644 index 61988a1189fdf69f53b5c361f2e9a6bffe29295f..0000000000000000000000000000000000000000 --- a/tmbs-rest-backend/src/main/java/de/cochu/backend/Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package de.cochu.backend; - -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -@Path("/") -public class Test { - - static Logger log = LogManager.getLogger(Test.class); - - @GET - @Path("/test") - @Produces(MediaType.TEXT_PLAIN) - public String test() { - return "hello world!"; - } - - @GET - @Path("/test2") - @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) - public TestModel test2() { - TestModel tm = new TestModel(); - tm.setName("Testname"); - return tm; - } - -} diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/TestModel.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/TestModel.java deleted file mode 100644 index 489697aad85cdb6d5b91652049f66a9c72b985be..0000000000000000000000000000000000000000 --- a/tmbs-rest-backend/src/main/java/de/cochu/backend/TestModel.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.cochu.backend; - -public class TestModel { - - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/Application.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/Application.java new file mode 100644 index 0000000000000000000000000000000000000000..44869d6ad9776efa6a2295035a20f51bd737ba80 --- /dev/null +++ b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/Application.java @@ -0,0 +1,11 @@ +package de.cochu.backend.rest; + +import org.glassfish.jersey.server.ResourceConfig; + +public class Application extends ResourceConfig { + + public Application() { + packages("de.cochu.backend.rest"); + } + +} diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/CORSFilter.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/CORSFilter.java similarity index 96% rename from tmbs-rest-backend/src/main/java/de/cochu/backend/CORSFilter.java rename to tmbs-rest-backend/src/main/java/de/cochu/backend/rest/CORSFilter.java index 2bdd870e58ca2af961b48a110c444c9093ea0c56..37f105d967c20dd9512f6c6c421ef9e744e6c792 100644 --- a/tmbs-rest-backend/src/main/java/de/cochu/backend/CORSFilter.java +++ b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/CORSFilter.java @@ -1,4 +1,4 @@ -package de.cochu.backend; +package de.cochu.backend.rest; import java.io.IOException; diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/domain/Article.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/domain/Article.java new file mode 100644 index 0000000000000000000000000000000000000000..f7cb9dc42ac20c8e1d18350bb63e3f330c8ac6f9 --- /dev/null +++ b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/domain/Article.java @@ -0,0 +1,78 @@ +package de.cochu.backend.rest.domain; + +import java.util.ArrayList; +import java.util.Date; + +import javax.xml.bind.annotation.XmlRootElement; + +import org.bson.Document; +import org.bson.types.ObjectId; + +@XmlRootElement +public class Article extends Domain { + + private String title; + private String text; + private String url; + private Date date; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + @Override + public Document toDocument() { + Document doc = new Document("title", title).append("text", text).append("url", url).append("date", date); + if (id != null) { + doc.append("_id", new ObjectId(id)); + } + return doc; + } + + public static Article fromDocument(final Document doc) { + Article article = new Article(); + article.id = doc.getObjectId("_id").toString(); + article.title = doc.getString("title"); + article.text = doc.getString("text"); + article.url = doc.getString("url"); + article.date = doc.getDate("date"); + return article; + } + + public static ArrayList<Article> fromDocuments(final ArrayList<Document> docs) { + ArrayList<Article> articles = new ArrayList<Article>(docs.size()); + for (Document doc : docs) { + articles.add(fromDocument(doc)); + } + return articles; + } + +} diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/domain/Domain.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/domain/Domain.java new file mode 100644 index 0000000000000000000000000000000000000000..931db095799915ad63f763e81f983b46e5187507 --- /dev/null +++ b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/domain/Domain.java @@ -0,0 +1,30 @@ +package de.cochu.backend.rest.domain; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; + +import javax.xml.bind.annotation.XmlRootElement; + +import org.bson.Document; + +@XmlRootElement +public abstract class Domain { + + protected String id; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public URI getURI(URI base) throws URISyntaxException, MalformedURLException { + return new URI(base.toString() + "/" + id); + } + + public abstract Document toDocument(); + +} diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/resource/Articles.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/resource/Articles.java new file mode 100644 index 0000000000000000000000000000000000000000..2c76d5c5be77f054324f8db62d4fdb87bc3fd56c --- /dev/null +++ b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/resource/Articles.java @@ -0,0 +1,97 @@ +package de.cochu.backend.rest.resource; + +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import javax.ws.rs.core.GenericEntity; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bson.Document; +import org.bson.types.ObjectId; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Filters; + +import static com.mongodb.client.model.Sorts.*; + +import de.cochu.backend.rest.domain.Article; + +@Path("articles") +public class Articles { + + static Logger log = LogManager.getLogger(Articles.class); + + @Context + UriInfo uri; + + MongoClient mongo = new MongoClient(); + MongoDatabase db = mongo.getDatabase("test"); + MongoCollection<Document> articles = db.getCollection("articles"); + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getArticles(@QueryParam("skip") @DefaultValue("0") int skip, + @QueryParam("limit") @DefaultValue("0") int limit, + @QueryParam("sortby") @DefaultValue("date") String sortBy, + @QueryParam("order") @DefaultValue("desc") String order) { + ArrayList<Document> result = articles.find().skip(skip).limit(limit) + .sort(order.equals("desc") ? descending(sortBy) : ascending(sortBy)).into(new ArrayList<Document>()); + ArrayList<Article> articles = Article.fromDocuments(result); + return Response.ok(new GenericEntity<List<Article>>(articles) { + }).build(); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("{id}") + public Response getArticle(@PathParam("id") String id) { + if (id == null || id.trim().length() == 0) { + return Response.status(Response.Status.BAD_REQUEST) + .entity(String.format(Messages.BAD_REQUEST, "id cannot be empty")).build(); + } + ObjectId objectId = new ObjectId(id); + ArrayList<Document> result = articles.find(Filters.eq("_id", objectId)).into(new ArrayList<Document>()); + if (result.size() == 1) { + Article article = Article.fromDocument(result.get(0)); + return Response.ok(article).build(); + } + return Response.status(Response.Status.NOT_FOUND).entity(String.format(Messages.NOT_FOUND, id)).build(); + } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response createArticle(Article article) throws MalformedURLException, URISyntaxException { + Document doc = new Document(article.toDocument()); + articles.insertOne(doc); + article = Article.fromDocument(doc); + return Response.created(article.getURI(uri.getAbsolutePath())).entity(article).build(); + } + + @DELETE + @Path("{id}") + public Response deleteArticle(@PathParam("id") String id) { + ObjectId objectId = new ObjectId(id); + articles.deleteOne(Filters.eq("_id", objectId)); + return Response.noContent().build(); + } + +} diff --git a/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/resource/Messages.java b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/resource/Messages.java new file mode 100644 index 0000000000000000000000000000000000000000..cce2773740c362b57dedbb5c50af18c7e02e7f59 --- /dev/null +++ b/tmbs-rest-backend/src/main/java/de/cochu/backend/rest/resource/Messages.java @@ -0,0 +1,8 @@ +package de.cochu.backend.rest.resource; + +public class Messages { + + public static final String NOT_FOUND = "Entity not found for id %1"; + public static final String BAD_REQUEST = "Bad request: %1"; + +} diff --git a/tmbs-rest-backend/src/main/webapp/WEB-INF/web.xml b/tmbs-rest-backend/src/main/webapp/WEB-INF/web.xml index 7a8fa34d0ce08434ce99f5f661fb3be2fa319241..6bef82bfc8d499e3228c3deb1fa80f45e18c938d 100644 --- a/tmbs-rest-backend/src/main/webapp/WEB-INF/web.xml +++ b/tmbs-rest-backend/src/main/webapp/WEB-INF/web.xml @@ -1,11 +1,14 @@ <?xml version="1.0" encoding="UTF-8"?> -<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:web="http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" + version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"> <servlet> <servlet-name>jersey</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> - <param-value>de.cochu.backend.Application</param-value> + <param-value>de.cochu.backend.rest.Application</param-value> </init-param> </servlet> <servlet-mapping> diff --git a/tmbs-rest-backend/target/m2e-wtp/web-resources/META-INF/maven/de.cochu/backend/pom.properties b/tmbs-rest-backend/target/m2e-wtp/web-resources/META-INF/maven/de.cochu/backend/pom.properties deleted file mode 100644 index 2fcd807cdfbaba407fe9193e0e45be4f89421abb..0000000000000000000000000000000000000000 --- a/tmbs-rest-backend/target/m2e-wtp/web-resources/META-INF/maven/de.cochu/backend/pom.properties +++ /dev/null @@ -1,7 +0,0 @@ -#Generated by Maven Integration for Eclipse -#Sat Dec 05 14:53:18 CET 2015 -version=0.0.1-SNAPSHOT -groupId=de.cochu -m2e.projectName=tmbs-rest-backend -m2e.projectLocation=/home/eike/Repositories/fu/ss15/ma/impl/ui/backend -artifactId=backend diff --git a/tmbs-rest-backend/target/m2e-wtp/web-resources/META-INF/maven/de.cochu/backend/pom.xml b/tmbs-rest-backend/target/m2e-wtp/web-resources/META-INF/maven/de.cochu/backend/pom.xml deleted file mode 100644 index c8b5d4de25d4e5023ceb9f9c7f636b6d3dd7a8fb..0000000000000000000000000000000000000000 --- a/tmbs-rest-backend/target/m2e-wtp/web-resources/META-INF/maven/de.cochu/backend/pom.xml +++ /dev/null @@ -1,35 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <groupId>de.cochu</groupId> - <artifactId>backend</artifactId> - <packaging>war</packaging> - <version>0.0.1-SNAPSHOT</version> - <name>backend Maven Webapp</name> - <properties> - <maven.compiler.target>1.8</maven.compiler.target> - <maven.compiler.source>1.8</maven.compiler.source> - </properties> - <dependencies> - <dependency> - <groupId>org.glassfish.jersey.containers</groupId> - <artifactId>jersey-container-servlet</artifactId> - <version>2.22.1</version> - </dependency> - <dependency> - <groupId>org.glassfish.jersey.media</groupId> - <artifactId>jersey-media-moxy</artifactId> - <version>2.22.1</version> - </dependency> - <dependency> - <groupId>javax.servlet</groupId> - <artifactId>javax.servlet-api</artifactId> - <version>3.1.0</version> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-core</artifactId> - <version>2.4.1</version> - </dependency> - </dependencies> -</project>