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

moved Article model into utils project for sharing

readded de-/serializer for article class because of removed attributes subobject
parent 8f84dd10
No related branches found
No related tags found
No related merge requests found
Showing
with 586 additions and 57 deletions
......@@ -34,7 +34,13 @@
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.1.0</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
......
package de.vipra.rest.model;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.bson.Document;
import org.bson.types.ObjectId;
public class Article extends Model {
public class Article extends de.vipra.util.model.Article {
private String title;
private String text;
private String url;
private Date date;
private Map<String, String> links;
public Article() {
super(Article.class);
}
public Article() {}
public Article(Document document) {
this();
fromDocument(document);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
super(document);
}
public void setText(String text) {
this.text = text;
public Map<String, String> getLinks() {
return links;
}
public String getUrl() {
return url;
public void setLinks(Map<String, String> links) {
this.links = links;
}
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));
public void addLink(String name, String link) {
if (getLinks() == null) {
setLinks(new HashMap<String, String>());
}
return doc;
links.put(name, link);
}
@Override
public void fromDocument(Document document) {
id = document.getObjectId("_id").toString();
title = document.getString("title");
text = document.getString("text");
url = document.getString("url");
date = document.getDate("date");
public void setSelf(URI base) {
URI self = uri(base);
if (self != null) {
addLink("self", self.toString());
}
}
public static ArrayList<Article> fromDocuments(final ArrayList<Document> docs) {
......
......@@ -11,6 +11,11 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import de.vipra.rest.serializer.ArticleDeserializer;
import de.vipra.rest.serializer.ArticleSerializer;
import de.vipra.rest.model.Article;
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
......@@ -29,10 +34,15 @@ public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
}
public static ObjectMapper createDefaultMapper() {
SimpleModule module = new SimpleModule();
module.addSerializer(Article.class, new ArticleSerializer());
module.addDeserializer(Article.class, new ArticleDeserializer());
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
mapper.registerModule(module);
return mapper;
}
......
......@@ -81,7 +81,7 @@ public class ArticleResource {
public Response createArticle(Article article) {
article = service.createArticle(uri.getAbsolutePath(), article);
ResponseWrapper<Article> res = new ResponseWrapper<>(article);
return Response.created(article.getURI(uri.getAbsolutePath())).entity(res).build();
return Response.created(article.uri(uri.getAbsolutePath())).entity(res).build();
}
@DELETE
......
package de.vipra.rest.serializer;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import de.vipra.rest.model.Article;
import static de.vipra.rest.serializer.Helper.*;
public class ArticleDeserializer extends JsonDeserializer<Article> {
@Override
public Article deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Article article = null;
JsonNode node = p.readValueAsTree();
if (node != null) {
article = new Article();
if (node.has("id"))
article.setId(getString(node, "id"));
if (node.has("attributes")) {
JsonNode attrs = node.get("attributes");
if (attrs.has("title"))
article.setTitle(getString(attrs, "title"));
if (attrs.has("text"))
article.setText(getString(attrs, "text"));
if (attrs.has("url"))
article.setUrl(getString(attrs, "url"));
if (attrs.has("date"))
article.setDate(stringToDate(getString(attrs, "date")));
}
}
return article;
}
}
\ No newline at end of file
package de.vipra.rest.serializer;
import de.vipra.rest.model.Article;
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 static de.vipra.rest.serializer.Helper.*;
public class ArticleSerializer extends JsonSerializer<Article> {
@Override
public void serialize(Article value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeStringField("id", value.getId());
gen.writeStringField("type", value.getType());
if (value.getLinks() != null)
gen.writeObjectField("links", value.getLinks());
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", dateToString(value.getDate()));
gen.writeEndObject();
gen.writeEndObject();
}
}
\ No newline at end of file
package de.vipra.rest.serializer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.databind.JsonNode;
public class Helper {
public static <T> T get(JsonNode node, String name, T defaultValue, Class<T> type) {
if (node == null) {
return defaultValue;
}
node = node.get(name);
if (node == null) {
return defaultValue;
}
switch (type.getSimpleName()) {
case "String":
return type.cast(node.asText());
case "Integer":
return type.cast(node.asInt());
case "Long":
return type.cast(node.asLong());
}
return null;
}
public static String getString(JsonNode node, String name, String defaultValue) {
return get(node, name, defaultValue, String.class);
}
public static String getString(JsonNode node, String name) {
return getString(node, name, null);
}
public static long getLong(JsonNode node, String name, long defaultValue) {
return get(node, name, defaultValue, Long.class);
}
public static long getLong(JsonNode node, String name) {
return getLong(node, name, 0L);
}
public static String dateToString(Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
return df.format(date);
}
public static Date stringToDate(String source) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
return df.parse(source);
} catch (ParseException e) {
return null;
}
}
}
\ No newline at end of file
This diff is collapsed.
eclipse.preferences.version=1
formatter_profile=_vipra
formatter_settings_version=12
......@@ -9,7 +9,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4jVersion>2.4.1</log4jVersion>
</properties>
<dependencies>
<!-- Logging -->
<dependency>
......@@ -34,8 +34,15 @@
<artifactId>mongodb-driver</artifactId>
<version>3.0.4</version>
</dependency>
<!-- ElasticSearch Adapter -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
......
package de.vipra.util.model;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class Article extends Model {
private String title;
private String text;
private String url;
private Date date;
public Article() {
super(Article.class);
}
public Article(Document document) {
this();
fromDocument(document);
}
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;
}
public Document toDocument() {
Document doc = new Document("title", title).append("text", text).append("url", url).append("date", date);
if (getId() != null) {
doc.append("_id", new ObjectId(getId()));
}
return doc;
}
public void fromDocument(Document document) {
setId(document.getObjectId("_id").toString());
setTitle(document.getString("title"));
setText(document.getString("text"));
setUrl(document.getString("url"));
setDate(document.getDate("date"));
}
}
\ No newline at end of file
package de.vipra.rest.model;
package de.vipra.util.model;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import org.bson.Document;
public abstract class Model {
protected String id;
protected String type;
protected Map<String, String> links;
public Model() {}
private String id;
private final String type;
public Model(Class<?> clazz) {
this.type = clazz.getSimpleName().toLowerCase();
......@@ -31,32 +24,9 @@ public abstract class Model {
return type;
}
public Map<String, String> getLinks() {
return links;
}
public void setLinks(Map<String, String> links) {
this.links = links;
}
public void addLink(String name, String link) {
if (links == null) {
links = new HashMap<>();
}
links.put(name, link);
}
public void setSelf(URI base) {
addLink("self", getURI(base).toString());
}
abstract Document toDocument();
abstract void fromDocument(Document document);
public URI getURI(URI base) {
public URI uri(URI base) {
try {
return new URI(base.toString() + "/" + id);
return new URI(base.toString() + "/" + getId());
} catch (URISyntaxException e) {
return null;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment