From 3e063a21bec94ccb33b86a8e75ac4fbfa4b3f868 Mon Sep 17 00:00:00 2001 From: Rafael Cavazin <rafaelcavazin@gmail.com> Date: Thu, 06 Dec 2012 15:07:55 -0500 Subject: [PATCH] Some language adaptions on pt-br properties --- src/com/gitblit/utils/JsonUtils.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/com/gitblit/utils/JsonUtils.java b/src/com/gitblit/utils/JsonUtils.java index da9c99d..24f4ecb 100644 --- a/src/com/gitblit/utils/JsonUtils.java +++ b/src/com/gitblit/utils/JsonUtils.java @@ -32,12 +32,15 @@ import java.util.Map; import java.util.TimeZone; +import com.gitblit.Constants.AccessPermission; import com.gitblit.GitBlitException.ForbiddenException; import com.gitblit.GitBlitException.NotAllowedException; import com.gitblit.GitBlitException.UnauthorizedException; import com.gitblit.GitBlitException.UnknownRequestException; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; @@ -108,7 +111,7 @@ UnauthorizedException { return retrieveJson(url, type, null, null); } - + /** * Reads a gson object from the specified url. * @@ -169,10 +172,11 @@ */ public static String retrieveJsonString(String url, String username, char[] password) throws IOException { - try { + try { URLConnection conn = ConnectionUtils.openReadConnection(url, username, password); InputStream is = conn.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(is, ConnectionUtils.CHARSET)); + BufferedReader reader = new BufferedReader(new InputStreamReader(is, + ConnectionUtils.CHARSET)); StringBuilder json = new StringBuilder(); char[] buffer = new char[4096]; int len = 0; @@ -260,10 +264,14 @@ // build custom gson instance with GMT date serializer/deserializer // http://code.google.com/p/google-gson/issues/detail?id=281 - private static Gson gson() { + public static Gson gson(ExclusionStrategy... strategies) { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new GmtDateTypeAdapter()); + builder.registerTypeAdapter(AccessPermission.class, new AccessPermissionTypeAdapter()); builder.setPrettyPrinting(); + if (!ArrayUtils.isEmpty(strategies)) { + builder.setExclusionStrategies(strategies); + } return builder.create(); } @@ -289,11 +297,50 @@ JsonDeserializationContext jsonDeserializationContext) { try { synchronized (dateFormat) { - return dateFormat.parse(jsonElement.getAsString()); + Date date = dateFormat.parse(jsonElement.getAsString()); + return new Date((date.getTime() / 1000) * 1000); } } catch (ParseException e) { throw new JsonSyntaxException(jsonElement.getAsString(), e); } } } + + private static class AccessPermissionTypeAdapter implements JsonSerializer<AccessPermission>, JsonDeserializer<AccessPermission> { + + private AccessPermissionTypeAdapter() { + } + + @Override + public synchronized JsonElement serialize(AccessPermission permission, Type type, + JsonSerializationContext jsonSerializationContext) { + return new JsonPrimitive(permission.code); + } + + @Override + public synchronized AccessPermission deserialize(JsonElement jsonElement, Type type, + JsonDeserializationContext jsonDeserializationContext) { + return AccessPermission.fromCode(jsonElement.getAsString()); + } + } + + public static class ExcludeField implements ExclusionStrategy { + + private Class<?> c; + private String fieldName; + + public ExcludeField(String fqfn) throws SecurityException, NoSuchFieldException, + ClassNotFoundException { + this.c = Class.forName(fqfn.substring(0, fqfn.lastIndexOf("."))); + this.fieldName = fqfn.substring(fqfn.lastIndexOf(".") + 1); + } + + public boolean shouldSkipClass(Class<?> arg0) { + return false; + } + + public boolean shouldSkipField(FieldAttributes f) { + return (f.getDeclaringClass() == c && f.getName().equals(fieldName)); + } + } } -- Gitblit v1.9.1