James Moger
2011-10-02 31abc26dd0354bc2dafe27c011c2e54934a89486
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.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.reflect.TypeToken;
/**
 * Utility methods for gson calls to a Gitblit server.
 * Utility methods for json calls to a Gitblit server.
 * 
 * @author James Moger
 * 
@@ -116,11 +121,27 @@
    * 
    * @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,12 +154,15 @@
    * 
    * @param url
    * @return the JSON message as a string
    * @throws Exception
    * @throws {@link IOException}
    */
   public static String retrieveJsonString(String url) throws Exception {
   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) {
@@ -156,6 +180,16 @@
      }
      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;
      }
   }
   /**
@@ -166,14 +200,33 @@
    * @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 {
   public static int sendJsonString(String url, String json) throws IOException {
      return sendJsonString(url, json, null, null);
   }
   /**
    * 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) {
@@ -189,6 +242,25 @@
      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()));
      }
   }
   /**