Skip to content
Snippets Groups Projects
Commit e6c2e4f0 authored by Eike Cochu's avatar Eike Cochu
Browse files

updated rest interface to implement json api v1.0

parent 429674af
Branches master
No related tags found
No related merge requests found
package de.vipra.rest.resource;
import static com.mongodb.client.model.Sorts.*;
import java.util.ArrayList;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
public class Helper {
public static Bson getSorts(String sortBy) {
String[] sortKeys = sortBy.split(",");
ArrayList<Bson> sorts = new ArrayList<>(sortKeys.length);
for (String sort : sortKeys) {
if (sort.startsWith("-")) {
sorts.add(descending(sort.substring(1)));
} else if (sort.startsWith("+")) {
sort = sort.substring(1);
}
sorts.add(ascending(sort));
}
return orderBy(sorts);
}
public static ObjectId objectId(String id) {
try {
return new ObjectId(id);
} catch (IllegalArgumentException e) {
return null;
}
}
}
package de.vipra.rest.serializer;
import de.vipra.rest.model.Articles;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class ArticlesSerializer extends JsonSerializer<Articles> {
@Override
public void serialize(Articles value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeStringField("id", value.getId());
gen.writeStringField("type", value.getType());
gen.writeObjectFieldStart("attributes");
if (value.getTitle() != null)
gen.writeStringField("title", value.getTitle());
if (value.getText() != null)
gen.writeStringField("text", value.getText());
if (value.getUrl() != null)
gen.writeStringField("url", value.getUrl());
if (value.getDate() != null)
gen.writeStringField("date", value.getDate().toString());
gen.writeEndObject();
if (value.getLinks() != null)
gen.writeObjectField("links", value.getLinks());
gen.writeEndObject();
}
}
package de.vipra.rest.serializer;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import de.vipra.rest.model.Link;
public class LinkSerializer extends JsonSerializer<Link> {
@Override
public void serialize(Link value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
if (value.getMeta() != null) {
gen.writeStartObject();
gen.writeStringField("href", value.getHref());
gen.writeObjectField("meta", value.getMeta());
gen.writeEndObject();
} else {
gen.writeString(value.getHref());
}
}
}
package de.vipra.rest.service; package de.vipra.rest.service;
import static com.mongodb.client.model.Sorts.ascending; import java.net.URI;
import static com.mongodb.client.model.Sorts.descending;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
...@@ -15,50 +11,56 @@ import com.mongodb.client.model.Filters; ...@@ -15,50 +11,56 @@ import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult; import com.mongodb.client.result.UpdateResult;
import de.vipra.rest.dao.Article; import de.vipra.rest.model.Articles;
import static de.vipra.rest.resource.Helper.*;
public class ArticleService { public class ArticlesService {
final MongoCollection<Document> articles; final MongoCollection<Document> articles;
public ArticleService(MongoDatabase db) { public ArticlesService(MongoDatabase db) {
articles = db.getCollection("articles"); articles = db.getCollection("articles");
} }
public Article getArticle(String id) { public Articles getArticle(URI base, String id) {
ObjectId objectId = new ObjectId(id); ArrayList<Document> result = articles.find(Filters.eq("_id", objectId(id))).into(new ArrayList<Document>());
ArrayList<Document> result = articles.find(Filters.eq("_id", objectId)).into(new ArrayList<Document>());
if (result.size() == 1) { if (result.size() == 1) {
return new Article(result.get(0)); Articles article = new Articles(result.get(0));
article.setSelf(base);
return article;
} else { } else {
return null; return null;
} }
} }
public List<Article> getArticles(int skip, int limit, String sortBy, String order) { public ArrayList<Articles> getArticles(URI base, int skip, int limit, String sortBy) {
ArrayList<Document> docs = articles.find().skip(skip).limit(limit)
.sort(order.equals("desc") ? descending(sortBy) : ascending(sortBy)).into(new ArrayList<Document>()); ArrayList<Document> docs = articles.find().skip(skip).limit(limit).sort(getSorts(sortBy))
ArrayList<Article> result = Article.fromDocuments(docs); .into(new ArrayList<Document>());
for (Article article : result) { ArrayList<Articles> result = Articles.fromDocuments(docs);
for (Articles article : result) {
article.setText(null); article.setText(null);
article.setSelf(base);
} }
return result; return result;
} }
public Article createArticle(Article article) { public Articles createArticle(URI base, Articles article) {
Document doc = new Document(article.toDocument()); Document doc = new Document(article.toDocument());
articles.insertOne(doc); articles.insertOne(doc);
return new Article(doc); article = new Articles(doc);
article.setSelf(base);
return article;
} }
public long deleteArticle(String id) { public long deleteArticle(String id) {
ObjectId objectId = new ObjectId(id); DeleteResult result = articles.deleteOne(Filters.eq("_id", objectId(id)));
DeleteResult result = articles.deleteOne(Filters.eq("_id", objectId));
return result.getDeletedCount(); return result.getDeletedCount();
} }
public long updateArticle(Article article) { public long updateArticle(URI base, Articles article) {
Document docOld = new Document("_id", new ObjectId(article.getId())); Document docOld = new Document("_id", objectId(article.getId()));
Document docNew = article.toDocument(); Document docNew = article.toDocument();
UpdateResult result = articles.replaceOne(docOld, docNew); UpdateResult result = articles.replaceOne(docOld, docNew);
return result.getModifiedCount(); return result.getModifiedCount();
......
...@@ -3,18 +3,6 @@ ...@@ -3,18 +3,6 @@
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="3.1"> version="3.1">
<context-param>
<param-name>MONGODB_HOST</param-name>
<param-value>localhost</param-value>
</context-param>
<context-param>
<param-name>MONGODB_PORT</param-name>
<param-value>27017</param-value>
</context-param>
<context-param>
<param-name>MONGODB_DATABASE</param-name>
<param-value>test</param-value>
</context-param>
<servlet> <servlet>
<servlet-name>jersey</servlet-name> <servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
...@@ -27,7 +15,4 @@ ...@@ -27,7 +15,4 @@
<servlet-name>jersey</servlet-name> <servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern> <url-pattern>/rest/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<listener>
<listener-class>de.vipra.rest.MongoDBContextListener</listener-class>
</listener>
</web-app> </web-app>
\ No newline at end of file
...@@ -9,13 +9,13 @@ import org.glassfish.jersey.server.ResourceConfig; ...@@ -9,13 +9,13 @@ import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test; import org.junit.Test;
import de.vipra.rest.resource.ArticleResource; import de.vipra.rest.resource.ArticlesResource;
public class ArticleResourceTest extends JerseyTest { public class ArticleResourceTest extends JerseyTest {
@Override @Override
protected Application configure() { protected Application configure() {
return new ResourceConfig(ArticleResource.class); return new ResourceConfig(ArticlesResource.class);
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment