summaryrefslogtreecommitdiffstats
path: root/src/com/gitblit/utils/JsonUtils.java
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2011-10-02 15:37:24 -0400
committerJames Moger <james.moger@gitblit.com>2011-10-02 15:37:24 -0400
commit31abc26dd0354bc2dafe27c011c2e54934a89486 (patch)
tree0bcd3046123eb1a3ccfc095e56a01c80c3e98b19 /src/com/gitblit/utils/JsonUtils.java
parentca9d0f3cb462e3ff9daa676c9f5e81407fbb79d6 (diff)
downloadgitblit-31abc26dd0354bc2dafe27c011c2e54934a89486.tar.gz
gitblit-31abc26dd0354bc2dafe27c011c2e54934a89486.zip
Fairly complete json rpc interface to view/control Gitblit data objects.
Diffstat (limited to 'src/com/gitblit/utils/JsonUtils.java')
-rw-r--r--src/com/gitblit/utils/JsonUtils.java166
1 files changed, 119 insertions, 47 deletions
diff --git a/src/com/gitblit/utils/JsonUtils.java b/src/com/gitblit/utils/JsonUtils.java
index a697b7a5..1edfc583 100644
--- a/src/com/gitblit/utils/JsonUtils.java
+++ b/src/com/gitblit/utils/JsonUtils.java
@@ -16,6 +16,7 @@
package com.gitblit.utils;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
@@ -36,6 +37,10 @@ import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
+import org.eclipse.jgit.util.Base64;
+
+import com.gitblit.GitBlitException.ForbiddenException;
+import com.gitblit.GitBlitException.UnauthorizedException;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.google.gson.Gson;
@@ -43,7 +48,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
/**
- * Utility methods for gson calls to a Gitblit server.
+ * Utility methods for json calls to a Gitblit server.
*
* @author James Moger
*
@@ -98,7 +103,7 @@ public class JsonUtils {
Gson gson = new Gson();
return gson.fromJson(json, clazz);
}
-
+
/**
* Convert a json string to an object of the specified type.
*
@@ -116,11 +121,27 @@ public class JsonUtils {
*
* @param url
* @param type
- * @return
- * @throws Exception
+ * @return the deserialized object
+ * @throws {@link IOException}
*/
- public static <X> X retrieveJson(String url, Type type) throws Exception {
- String json = retrieveJsonString(url);
+ public static <X> X retrieveJson(String url, Type type) throws IOException,
+ UnauthorizedException {
+ return retrieveJson(url, type, null, null);
+ }
+
+ /**
+ * Reads a gson object from the specified url.
+ *
+ * @param url
+ * @param type
+ * @param username
+ * @param password
+ * @return the deserialized object
+ * @throws {@link IOException}
+ */
+ public static <X> X retrieveJson(String url, Type type, String username, char[] password)
+ throws IOException {
+ String json = retrieveJsonString(url, username, password);
if (StringUtils.isEmpty(json)) {
return null;
}
@@ -133,29 +154,42 @@ public class JsonUtils {
*
* @param url
* @return the JSON message as a string
- * @throws Exception
+ * @throws {@link IOException}
*/
- public static String retrieveJsonString(String url) throws Exception {
- URL urlObject = new URL(url);
- URLConnection conn = urlObject.openConnection();
- conn.setRequestProperty("Accept-Charset", CHARSET);
- conn.setUseCaches(false);
- conn.setDoInput(true);
- if (conn instanceof HttpsURLConnection) {
- HttpsURLConnection secureConn = (HttpsURLConnection) conn;
- secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());
- secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);
- }
- InputStream is = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(is, CHARSET));
- StringBuilder json = new StringBuilder();
- char[] buffer = new char[4096];
- int len = 0;
- while ((len = reader.read(buffer)) > -1) {
- json.append(buffer, 0, len);
+ public static String retrieveJsonString(String url, String username, char[] password)
+ throws IOException {
+ try {
+ URL urlObject = new URL(url);
+ URLConnection conn = urlObject.openConnection();
+ conn.setRequestProperty("Accept-Charset", CHARSET);
+ setAuthorization(conn, username, password);
+ conn.setUseCaches(false);
+ conn.setDoInput(true);
+ if (conn instanceof HttpsURLConnection) {
+ HttpsURLConnection secureConn = (HttpsURLConnection) conn;
+ secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());
+ secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);
+ }
+ InputStream is = conn.getInputStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is, CHARSET));
+ StringBuilder json = new StringBuilder();
+ char[] buffer = new char[4096];
+ int len = 0;
+ while ((len = reader.read(buffer)) > -1) {
+ json.append(buffer, 0, len);
+ }
+ is.close();
+ return json.toString();
+ } catch (IOException e) {
+ if (e.getMessage().indexOf("401") > -1) {
+ // unauthorized
+ throw new UnauthorizedException(url);
+ } else if (e.getMessage().indexOf("403") > -1) {
+ // requested url is forbidden by the requesting user
+ throw new ForbiddenException(url);
+ }
+ throw e;
}
- is.close();
- return json.toString();
}
/**
@@ -166,29 +200,67 @@ public class JsonUtils {
* @param json
* the json message to send
* @return the http request result code
- * @throws Exception
+ * @throws {@link IOException}
*/
- public static int sendJsonString(String url, String json) throws Exception {
- byte[] jsonBytes = json.getBytes(CHARSET);
- URL urlObject = new URL(url);
- URLConnection conn = urlObject.openConnection();
- conn.setRequestProperty("Content-Type", "text/plain;charset=" + CHARSET);
- conn.setRequestProperty("Content-Length", "" + jsonBytes.length);
- conn.setUseCaches(false);
- conn.setDoOutput(true);
- if (conn instanceof HttpsURLConnection) {
- HttpsURLConnection secureConn = (HttpsURLConnection) conn;
- secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());
- secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);
- }
+ public static int sendJsonString(String url, String json) throws IOException {
+ return sendJsonString(url, json, null, null);
+ }
- // write json body
- OutputStream os = conn.getOutputStream();
- os.write(jsonBytes);
- os.close();
+ /**
+ * Sends a JSON message.
+ *
+ * @param url
+ * the url to write to
+ * @param json
+ * the json message to send
+ * @param username
+ * @param password
+ * @return the http request result code
+ * @throws {@link IOException}
+ */
+ public static int sendJsonString(String url, String json, String username, char[] password)
+ throws IOException {
+ try {
+ byte[] jsonBytes = json.getBytes(CHARSET);
+ URL urlObject = new URL(url);
+ URLConnection conn = urlObject.openConnection();
+ conn.setRequestProperty("Content-Type", "text/plain;charset=" + CHARSET);
+ conn.setRequestProperty("Content-Length", "" + jsonBytes.length);
+ setAuthorization(conn, username, password);
+ conn.setUseCaches(false);
+ conn.setDoOutput(true);
+ if (conn instanceof HttpsURLConnection) {
+ HttpsURLConnection secureConn = (HttpsURLConnection) conn;
+ secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());
+ secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);
+ }
- int status = ((HttpURLConnection) conn).getResponseCode();
- return status;
+ // write json body
+ OutputStream os = conn.getOutputStream();
+ os.write(jsonBytes);
+ os.close();
+
+ int status = ((HttpURLConnection) conn).getResponseCode();
+ return status;
+ } catch (IOException e) {
+ if (e.getMessage().indexOf("401") > -1) {
+ // unauthorized
+ throw new UnauthorizedException(url);
+ } else if (e.getMessage().indexOf("403") > -1) {
+ // requested url is forbidden by the requesting user
+ throw new ForbiddenException(url);
+ }
+ throw e;
+ }
+ }
+
+ private static void setAuthorization(URLConnection conn, String username, char[] password) {
+ if (!StringUtils.isEmpty(username) && (password != null && password.length > 0)) {
+ conn.setRequestProperty(
+ "Authorization",
+ "Basic "
+ + Base64.encodeBytes((username + ":" + new String(password)).getBytes()));
+ }
}
/**