This is an automated email from the git hooks/post-receive script. unknown user pushed a commit to branch devel in repository Pollen. commit 819759fe8a3e49185a21a6de1be845b82d33b6a1 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon May 19 15:57:46 2014 +0200 Reuse Gson + fix vote choices order --- pollen-rest-api/pom.xml | 22 +------ .../org/chorem/pollen/rest/api/PollenRender.java | 21 +------ .../rest/api/PollenRestApiApplicationListener.java | 4 + .../chorem/pollen/rest/api/PollenRestApiUtil.java | 65 ++++++++++++++------ .../pollen/rest/api/converter/DateConverter.java | 35 +++++++++++ .../rest/api/converter/JsonArrayConverter.java | 19 ++---- .../pollen/rest/api/converter/JsonConverter.java | 46 ++------------ .../org/chorem/pollen/services/bean/VoteBean.java | 3 +- .../pollen/services/service/VoteServiceTest.java | 20 ++++++ pom.xml | 30 +-------- 10 files changed, 127 insertions(+), 138 deletions(-) diff --git a/pollen-rest-api/pom.xml b/pollen-rest-api/pom.xml index c4cb8fc..4680c26 100644 --- a/pollen-rest-api/pom.xml +++ b/pollen-rest-api/pom.xml @@ -78,11 +78,6 @@ </dependency> <dependency> - <groupId>javax</groupId> - <artifactId>javaee-api</artifactId> - </dependency> - - <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </dependency> @@ -108,24 +103,9 @@ <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> - <!--dependency> + <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> - </dependency--> - - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - </dependency> - - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-core</artifactId> - </dependency> - - <dependency> - <groupId>com.fasterxml.jackson.module</groupId> - <artifactId>jackson-module-afterburner</artifactId> </dependency> <dependency> diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java index 50b2811..e2ee51a 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java @@ -23,9 +23,8 @@ package org.chorem.pollen.rest.api; * #L% */ -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; import com.google.common.collect.Multimap; +import com.google.gson.Gson; import org.chorem.pollen.services.service.InvalidFormException; import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.call.HttpContext; @@ -62,34 +61,20 @@ public class PollenRender<T> extends Render { Object map; - ObjectMapper mapper; - if (model instanceof InvalidFormException) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } - mapper = PollenRestApiUtil.newSimpleMapper(); - map = toMap(model); PollenRestApiApplicationContext applicationContext = PollenRestApiApplicationContext.getApplicationContext(context.getServletContext()); boolean devMode = applicationContext.getApplicationConfig().isDevMode(); - String json; - - if (devMode) { - - ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter(); - json = objectWriter.writeValueAsString(map); - - } else { - - json = mapper.writeValueAsString(map); - - } + Gson gson = PollenRestApiUtil.newGson(devMode); + String json = gson.toJson(map); PrintWriter out = context.getOut(); out.print(json); diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java index 494dd4a..d5be352 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiApplicationListener.java @@ -24,6 +24,7 @@ package org.chorem.pollen.rest.api; */ import com.google.common.collect.Sets; +import org.chorem.pollen.rest.api.converter.DateConverter; import org.chorem.pollen.rest.api.converter.JsonArrayConverter; import org.chorem.pollen.rest.api.converter.JsonConverter; import org.chorem.pollen.services.PollenService; @@ -43,6 +44,7 @@ import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler; import org.debux.webmotion.server.mapping.Mapping; import java.lang.reflect.Type; +import java.util.Date; import java.util.Set; /** @@ -70,6 +72,8 @@ public class PollenRestApiApplicationListener implements WebMotionServerListener // --- init converters --- // + serverContext.addConverter(new DateConverter(), Date.class); + for (Class<?> beanType : BEAN_TYPES) { serverContext.addConverter(JsonConverter.newConverter(beanType), beanType); diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java index 6d5b6a6..5a9c6ae 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiUtil.java @@ -1,15 +1,22 @@ package org.chorem.pollen.rest.api; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.module.afterburner.AfterburnerModule; import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; import org.apache.commons.lang3.StringUtils; import org.debux.webmotion.server.call.HttpContext; import javax.servlet.http.HttpServletResponse; +import java.lang.reflect.Type; +import java.util.Date; import java.util.List; /** @@ -35,20 +42,9 @@ public class PollenRestApiUtil { return list; } - public static ObjectMapper newSimpleMapper() { + public static String entitiesToString(Object o) { - ObjectMapper mapper = new ObjectMapper() - .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true) - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) //FIXME Could be better ? - .registerModule(new AfterburnerModule()); - - return mapper; - - } - - public static String entitiesToString(Object o) throws JsonProcessingException { - - return newSimpleMapper().writeValueAsString(o); + return newGson(true).toJson(o); } @@ -88,4 +84,37 @@ public class PollenRestApiUtil { context.getResponse().addHeader(HEADER_ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders); } } + + public static Gson newGson(boolean prettyPrint) { + + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { + + @Override + public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { + if (src == null) { + return JsonNull.INSTANCE; + } + return new JsonPrimitive(src.getTime()); + } + + }); + gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { + + @Override + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsLong()); + } + + }); + if (prettyPrint) { + + gsonBuilder.setPrettyPrinting(); + + } + + Gson gson = gsonBuilder.create(); + return gson; + + } } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/DateConverter.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/DateConverter.java new file mode 100644 index 0000000..590bc91 --- /dev/null +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/DateConverter.java @@ -0,0 +1,35 @@ +package org.chorem.pollen.rest.api.converter; + +import org.apache.commons.beanutils.converters.AbstractConverter; + +import java.util.Date; + +/** + * Created on 5/19/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class DateConverter extends AbstractConverter { + + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + Date result = null; + if (value != null) { + if (value.getClass().isAssignableFrom(Date.class)) { + result = (Date) value; + } else { + Object o = ((Object[]) value)[0]; + String sTime = o.toString(); + Long time = Long.parseLong(sTime); + result = new Date(time); + } + } + return (T) result; + } + + @Override + protected Class<?> getDefaultType() { + return Date.class; + } +} diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java index 63abb75..6dac6ca 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonArrayConverter.java @@ -1,11 +1,9 @@ package org.chorem.pollen.rest.api.converter; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; import org.apache.commons.beanutils.converters.AbstractConverter; import org.chorem.pollen.rest.api.PollenRestApiUtil; -import org.chorem.pollen.services.PollenTechnicalException; -import java.io.IOException; import java.lang.reflect.Array; /** @@ -18,7 +16,7 @@ public class JsonArrayConverter<O> extends AbstractConverter { protected final Class<O[]> arrayType; - protected final ObjectMapper mapper; + protected final Gson gson; public static <O> JsonArrayConverter<O> newConverter(Class<O> objectType) { return new JsonArrayConverter<>(objectType); @@ -26,7 +24,7 @@ public class JsonArrayConverter<O> extends AbstractConverter { public JsonArrayConverter(Class<O> entityType) { this.arrayType = getArrayClass(entityType); - this.mapper = PollenRestApiUtil.newSimpleMapper(); + this.gson = PollenRestApiUtil.newGson(false); } <T> Class<T[]> getArrayClass(Class<T> clazz) { @@ -48,14 +46,9 @@ public class JsonArrayConverter<O> extends AbstractConverter { } - try { - - O[] values = mapper.readValue(stringValue, this.arrayType); - return (T) values; - - } catch (IOException e) { - throw new PollenTechnicalException("Could not convert " + stringValue + " to " + type.getName(), e); - } + O[] values = gson.fromJson(stringValue, this.arrayType); +// O[] values = mapper.readValue(stringValue, this.arrayType); + return (T) values; } diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java index c649ba2..abe3f4d 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/converter/JsonConverter.java @@ -21,16 +21,12 @@ package org.chorem.pollen.rest.api.converter; * #L% */ -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; +import com.google.gson.Gson; import org.apache.commons.beanutils.converters.AbstractConverter; import org.chorem.pollen.persistence.PollenEntityEnum; import org.chorem.pollen.rest.api.PollenRestApiUtil; -import org.chorem.pollen.services.PollenTechnicalException; import org.nuiton.topia.persistence.TopiaEntity; -import java.io.IOException; - /** * Created on 5/6/14. * @@ -43,7 +39,7 @@ public class JsonConverter<O> extends AbstractConverter { protected Class<O> implementationClass; - protected ObjectMapper mapper; + protected final Gson gson; public static <O> JsonConverter<O> newConverter(Class<O> objectType) { return newConverter(objectType, objectType); @@ -61,38 +57,14 @@ public class JsonConverter<O> extends AbstractConverter { public JsonConverter(Class<O> objectType, Class<O> implementationClass) { this.objectType = objectType; - + this.gson = PollenRestApiUtil.newGson(false); this.implementationClass = implementationClass; - - SimpleModule module = new SimpleModule(); - - if (objectType != implementationClass) { - - module.addAbstractTypeMapping(objectType, implementationClass); - - } - - for (Class<? extends TopiaEntity> entityClass : PollenEntityEnum.getContractClasses()) { - - Class concreteClass = PollenEntityEnum.getImplementationClass(entityClass); - module.addAbstractTypeMapping(entityClass, concreteClass); - - } - - mapper = PollenRestApiUtil.newSimpleMapper(); - - } - - public ObjectMapper getMapper() { - - return mapper; - } @Override protected String convertToString(Object value) throws Throwable { - String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); + String result = gson.toJson(value); return result; } @@ -112,15 +84,9 @@ public class JsonConverter<O> extends AbstractConverter { } - try { - - T result = (T) mapper.readValue(stringValue, implementationClass); - - return result; + T result = (T) gson.fromJson(stringValue, implementationClass); - } catch (IOException e) { - throw new PollenTechnicalException("Could not convert " + stringValue + " to " + type.getName(), e); - } + return result; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java index 3e7a7a9..e5d99f2 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/VoteBean.java @@ -9,6 +9,7 @@ import org.chorem.pollen.persistence.entity.VoteToChoices; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -30,7 +31,7 @@ public class VoteBean extends PollenBean<Vote> { protected Boolean anonymous; - protected Set<VoteToChoiceBean> choice = new HashSet<>(); + protected Set<VoteToChoiceBean> choice = new LinkedHashSet<>(); @Override public void fromEntity(Vote vote) { diff --git a/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteServiceTest.java b/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteServiceTest.java new file mode 100644 index 0000000..78e69bc --- /dev/null +++ b/pollen-services/src/test/java/org/chorem/pollen/services/service/VoteServiceTest.java @@ -0,0 +1,20 @@ +package org.chorem.pollen.services.service; + +import org.chorem.pollen.services.AbstractPollenServiceTest; +import org.chorem.pollen.services.service.security.SecurityService; +import org.junit.Test; + +public class VoteServiceTest extends AbstractPollenServiceTest { + + protected PollService pollService; + + protected VoteService service; + + protected SecurityService securityService; + + @Test + public void addVote() throws Exception { + + //TODO + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index f23caff..e159774 100644 --- a/pom.xml +++ b/pom.xml @@ -410,34 +410,10 @@ <version>${httpCommonsHttpclientVersion}</version> </dependency> - <!--dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpcore</artifactId> - <version>${httpCommonsHttpclientVersion}</version> - </dependency--> - - <!--dependency> + <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> - </dependency--> - - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <version>2.3.3</version> - </dependency> - - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-core</artifactId> - <version>2.3.3</version> - </dependency> - - <dependency> - <groupId>com.fasterxml.jackson.module</groupId> - <artifactId>jackson-module-afterburner</artifactId> - <version>2.3.3</version> </dependency> <dependency> @@ -636,12 +612,12 @@ <plugins> <plugin> <groupId>org.nuiton</groupId> - <artifactId>nuiton-utils-maven-report-plugin</artifactId> + <artifactId>nuiton-maven-report-plugin</artifactId> <version>3.0-rc-1</version> <reportSets> <reportSet> <reports> - <report>aggregate-application-config-report</report> + <report>aggregate-config-report</report> </reports> </reportSet> </reportSets> -- To stop receiving notification emails like this one, please contact Chorem.org SCM administrator <admin+scm@chorem.org>.