From a70b43cde76b4baab82b4ce0d9ff82883f80b8df Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 26 Oct 2011 17:54:07 -0400
Subject: [PATCH] Added status icon

---
 src/com/gitblit/utils/JsonUtils.java |   97 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 89 insertions(+), 8 deletions(-)

diff --git a/src/com/gitblit/utils/JsonUtils.java b/src/com/gitblit/utils/JsonUtils.java
index 1edfc58..5b53bf4 100644
--- a/src/com/gitblit/utils/JsonUtils.java
+++ b/src/com/gitblit/utils/JsonUtils.java
@@ -27,8 +27,14 @@
 import java.security.SecureRandom;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Collection;
+import java.util.Date;
+import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
 
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
@@ -40,11 +46,20 @@
 import org.eclipse.jgit.util.Base64;
 
 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.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.JsonPrimitive;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+import com.google.gson.JsonSyntaxException;
 import com.google.gson.reflect.TypeToken;
 
 /**
@@ -87,8 +102,7 @@
 	 * @return json
 	 */
 	public static String toJsonString(Object o) {
-		Gson gson = new GsonBuilder().setPrettyPrinting().create();
-		String json = gson.toJson(o);
+		String json = gson().toJson(o);
 		return json;
 	}
 
@@ -100,8 +114,7 @@
 	 * @return an object
 	 */
 	public static <X> X fromJsonString(String json, Class<X> clazz) {
-		Gson gson = new Gson();
-		return gson.fromJson(json, clazz);
+		return gson().fromJson(json, clazz);
 	}
 
 	/**
@@ -112,8 +125,7 @@
 	 * @return an object
 	 */
 	public static <X> X fromJsonString(String json, Type type) {
-		Gson gson = new Gson();
-		return gson.fromJson(json, type);
+		return gson().fromJson(json, type);
 	}
 
 	/**
@@ -145,8 +157,26 @@
 		if (StringUtils.isEmpty(json)) {
 			return null;
 		}
-		Gson gson = new Gson();
-		return gson.fromJson(json, type);
+		return gson().fromJson(json, type);
+	}
+
+	/**
+	 * Reads a gson object from the specified url.
+	 * 
+	 * @param url
+	 * @param clazz
+	 * @param username
+	 * @param password
+	 * @return the deserialized object
+	 * @throws {@link IOException}
+	 */
+	public static <X> X retrieveJson(String url, Class<X> clazz, String username, char[] password)
+			throws IOException {
+		String json = retrieveJsonString(url, username, password);
+		if (StringUtils.isEmpty(json)) {
+			return null;
+		}
+		return gson().fromJson(json, clazz);
 	}
 
 	/**
@@ -187,6 +217,12 @@
 			} else if (e.getMessage().indexOf("403") > -1) {
 				// requested url is forbidden by the requesting user
 				throw new ForbiddenException(url);
+			} else if (e.getMessage().indexOf("405") > -1) {
+				// requested url is not allowed by the server
+				throw new NotAllowedException(url);
+			} else if (e.getMessage().indexOf("501") > -1) {
+				// requested url is not recognized by the server
+				throw new UnknownRequestException(url);
 			}
 			throw e;
 		}
@@ -249,6 +285,12 @@
 			} else if (e.getMessage().indexOf("403") > -1) {
 				// requested url is forbidden by the requesting user
 				throw new ForbiddenException(url);
+			} else if (e.getMessage().indexOf("405") > -1) {
+				// requested url is not allowed by the server
+				throw new NotAllowedException(url);
+			} else if (e.getMessage().indexOf("501") > -1) {
+				// requested url is not recognized by the server
+				throw new UnknownRequestException(url);
 			}
 			throw e;
 		}
@@ -263,6 +305,45 @@
 		}
 	}
 
+	// build custom gson instance with GMT date serializer/deserializer
+	// http://code.google.com/p/google-gson/issues/detail?id=281
+	private static Gson gson() {
+		GsonBuilder builder = new GsonBuilder();
+		builder.registerTypeAdapter(Date.class, new GmtDateTypeAdapter());
+		builder.setPrettyPrinting();
+		return builder.create();
+	}
+
+	private static class GmtDateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
+		private final DateFormat dateFormat;
+
+		private GmtDateTypeAdapter() {
+			dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
+			dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+		}
+
+		@Override
+		public synchronized JsonElement serialize(Date date, Type type,
+				JsonSerializationContext jsonSerializationContext) {
+			synchronized (dateFormat) {
+				String dateFormatAsString = dateFormat.format(date);
+				return new JsonPrimitive(dateFormatAsString);
+			}
+		}
+
+		@Override
+		public synchronized Date deserialize(JsonElement jsonElement, Type type,
+				JsonDeserializationContext jsonDeserializationContext) {
+			try {
+				synchronized (dateFormat) {
+					return dateFormat.parse(jsonElement.getAsString());
+				}
+			} catch (ParseException e) {
+				throw new JsonSyntaxException(jsonElement.getAsString(), e);
+			}
+		}
+	}
+
 	/**
 	 * DummyTrustManager trusts all certificates.
 	 */

--
Gitblit v1.9.1