From 6aeb3e1d634ee74c37d4b75dac62ad7d0241ccc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Sat, 15 Nov 2014 14:43:20 -0200 Subject: [PATCH 1/6] Using modern Java APIs Hashtable is too old and its prefered to use HashMap instead Hashtable. Has no reason to use Hashtable here. And GsonDeserializer can use Charset instead of String charset name, that is preferable in Java 6+. --- .../java/br/com/caelum/vraptor/http/VRaptorRequest.java | 3 +-- .../vraptor/serialization/gson/GsonDeserialization.java | 3 ++- .../br/com/caelum/vraptor/http/VRaptorRequestTest.java | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/http/VRaptorRequest.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/http/VRaptorRequest.java index d29f1f020..cbfc6af76 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/http/VRaptorRequest.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/http/VRaptorRequest.java @@ -22,7 +22,6 @@ import java.util.Enumeration; import java.util.HashMap; -import java.util.Hashtable; import java.util.Map; import javax.enterprise.inject.Vetoed; @@ -43,7 +42,7 @@ public class VRaptorRequest extends HttpServletRequestWrapper implements Mutable private static final Logger logger = LoggerFactory.getLogger(VRaptorRequest.class); - private final Hashtable extraParameters = new Hashtable<>(); + private final Map extraParameters = new HashMap<>(); public VRaptorRequest(HttpServletRequest request) { super(request); diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java index 3be5eccd0..4c51078c4 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java @@ -23,6 +23,7 @@ import java.io.InputStreamReader; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.nio.charset.Charset; import javax.enterprise.inject.Instance; import javax.inject.Inject; @@ -156,7 +157,7 @@ public Object[] deserialize(InputStream inputStream, ControllerMethod method) { } private String getContentOfStream(InputStream input) throws IOException { - String charset = getRequestCharset(); + Charset charset = Charset.forName(getRequestCharset()); logger.debug("Using charset {}", charset); return CharStreams.toString(new InputStreamReader(input, charset)); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/VRaptorRequestTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/VRaptorRequestTest.java index fdfd1b2b9..e63eb4590 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/VRaptorRequestTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/VRaptorRequestTest.java @@ -17,6 +17,7 @@ package br.com.caelum.vraptor.http; +import static java.util.Collections.enumeration; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -24,7 +25,7 @@ import static org.mockito.Mockito.when; import java.util.Enumeration; -import java.util.Hashtable; +import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -43,11 +44,11 @@ public class VRaptorRequestTest { public void setup() { MockitoAnnotations.initMocks(this); - final Hashtable t = new Hashtable<>(); + final Map t = new HashMap<>(); t.put("name", new String[] { "guilherme" }); t.put("age", new String[] { "27" }); - when(request.getParameterNames()).thenReturn(t.keys()); + when(request.getParameterNames()).thenReturn(enumeration(t.keySet())); when(request.getParameterMap()).thenReturn(t); when(request.getParameter("name")).thenReturn("guilherme"); when(request.getParameter("minimum")).thenReturn(null); From eb32ae2b16af631d86977f5500a32081dbf95878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Sat, 15 Nov 2014 20:54:00 -0200 Subject: [PATCH 2/6] Adding serialization and equals/hashcode to comparators. According JLS all comparators should implements Serializable and overriding equals and hashcode methods. --- .../DefaultRepresentationResult.java | 7 +++++-- .../view/DefaultAcceptHeaderToFormat.java | 20 +++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/DefaultRepresentationResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/DefaultRepresentationResult.java index 4fee2fd7e..6160fcf93 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/DefaultRepresentationResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/DefaultRepresentationResult.java @@ -19,6 +19,7 @@ import static com.google.common.collect.Lists.newArrayList; import static java.util.Collections.sort; +import java.io.Serializable; import java.util.Comparator; import java.util.List; @@ -102,12 +103,14 @@ public Serializer from(T object, String alias) { * @author A.C de Souza * @since 3.4.0 */ - static final class ApplicationPackageFirst implements Comparator { + static final class ApplicationPackageFirst implements Comparator, Serializable { + + public static final long serialVersionUID = 1L; private static final String VRAPTOR_PACKAGE = "br.com.caelum.vraptor.serialization"; private int priority(Serialization s) { - return s.getClass().getPackage().getName().startsWith(VRAPTOR_PACKAGE)? 1 : 0; + return s.getClass().getPackage().getName().startsWith(VRAPTOR_PACKAGE) ? 1 : 0; } @Override diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultAcceptHeaderToFormat.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultAcceptHeaderToFormat.java index 9c9c0afd8..7d9c847a4 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultAcceptHeaderToFormat.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultAcceptHeaderToFormat.java @@ -17,6 +17,7 @@ package br.com.caelum.vraptor.view; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; @@ -99,8 +100,8 @@ private String chooseMimeType(String acceptHeader) { } private static class MimeType implements Comparable { - String type; - double qualifier; + private final String type; + private final double qualifier; public MimeType(String type, double qualifier) { this.type = type; @@ -113,6 +114,21 @@ public int compareTo(MimeType mime) { return Double.compare(mime.qualifier, this.qualifier); } + @Override + public boolean equals(Object obj) { + if (obj == null || getClass() != obj.getClass()) { + return false; + } + + MimeType other = (MimeType) obj; + return Objects.equals(type, other.type) && Objects.equals(qualifier, other.qualifier); + } + + @Override + public int hashCode() { + return Objects.hash(type, qualifier); + } + public String getType() { return type; } From bd1afeac052db397cdd33f8b8d30582342095d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Sat, 15 Nov 2014 20:56:59 -0200 Subject: [PATCH 3/6] Using Arrays.toString to print values instead of object reference --- .../caelum/vraptor/http/route/DefaultParametersControl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/http/route/DefaultParametersControl.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/http/route/DefaultParametersControl.java index e8ab2d069..2c25fe0e3 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/http/route/DefaultParametersControl.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/http/route/DefaultParametersControl.java @@ -96,7 +96,9 @@ private Pattern compilePattern(String originalPattern, Map param @Override public String fillUri(Parameter[] paramNames, Object... paramValues) { if (paramNames.length != paramValues.length) { - throw new IllegalArgumentException("paramNames must have the same length as paramValues. Names: " + paramNames + " Values: " + Arrays.toString(paramValues)); + String message = String.format("paramNames must have the same length as paramValues. Names: %s Values: %s", + Arrays.toString(paramNames), Arrays.toString(paramValues)); + throw new IllegalArgumentException(message); } String[] splittedPatterns = StringUtils.extractParameters(originalPattern); From b95115218c271cb44251c7e1c6447b9d3f21dbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Sat, 15 Nov 2014 21:24:11 -0200 Subject: [PATCH 4/6] SortByArgumentsLengthDesc should be a static inner class and Serializable --- .../main/java/br/com/caelum/vraptor/view/LinkToHandler.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/LinkToHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/LinkToHandler.java index 089cadf6d..da9cada06 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/LinkToHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/LinkToHandler.java @@ -20,6 +20,7 @@ import static java.util.Collections.sort; import static javassist.CtNewMethod.abstractMethod; +import java.io.Serializable; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; @@ -210,7 +211,9 @@ private List getMethods(Class controller) { return methods; } - private final class SortByArgumentsLengthDesc implements Comparator { + private static final class SortByArgumentsLengthDesc implements Comparator, Serializable { + public static final long serialVersionUID = 1L; + @Override public int compare(Method o1, Method o2) { return Integer.compare(o2.getParameterTypes().length, o1.getParameterTypes().length); From d769992c3fabc2f49099eced589166259cdc8b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Sat, 15 Nov 2014 21:35:08 -0200 Subject: [PATCH 5/6] Closing streams to avoid Eclipse warnings --- .../vraptor/environment/DefaultEnvironment.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/environment/DefaultEnvironment.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/environment/DefaultEnvironment.java index 64126c6aa..36403c12f 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/environment/DefaultEnvironment.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/environment/DefaultEnvironment.java @@ -63,14 +63,16 @@ public DefaultEnvironment(EnvironmentType environmentType) throws IOException { private void loadAndPut(String environment) throws IOException { String name = "/" + environment + ".properties"; - InputStream stream = DefaultEnvironment.class.getResourceAsStream(name); - Properties properties = new Properties(); - if (stream != null) { - properties.load(stream); - this.properties.putAll(properties); - } else { - LOG.warn("Could not find the file '{}.properties' to load.", environment); + try (InputStream stream = DefaultEnvironment.class.getResourceAsStream(name)) { + Properties properties = new Properties(); + + if (stream != null) { + properties.load(stream); + this.properties.putAll(properties); + } else { + LOG.warn("Could not find the file '{}.properties' to load.", environment); + } } } From 986d8179976221a9c7cab4b5fcf72151ecf13e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Sat, 15 Nov 2014 21:38:41 -0200 Subject: [PATCH 6/6] I18nParam must be Serializable Because I18nParam class is stored in a serializable class, so must be serializable too. --- .../java/br/com/caelum/vraptor/validator/I18nParam.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/I18nParam.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/I18nParam.java index 20724ff54..8891e546e 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/I18nParam.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/I18nParam.java @@ -15,6 +15,7 @@ */ package br.com.caelum.vraptor.validator; +import java.io.Serializable; import java.util.ResourceBundle; import javax.enterprise.inject.Vetoed; @@ -29,8 +30,10 @@ * @since 3.4.0 */ @Vetoed -public class I18nParam { - +public class I18nParam implements Serializable { + + public static final long serialVersionUID = 1L; + private final String key; public I18nParam(String key) {