Search in sources :

Example 1 with UnknownRequestException

use of com.gitblit.GitBlitException.UnknownRequestException in project gitblit by gitblit.

the class JsonUtils method retrieveJsonString.

/**
	 * Retrieves a JSON message.
	 *
	 * @param url
	 * @return the JSON message as a string
	 * @throws {@link IOException}
	 */
public static String retrieveJsonString(String url, String username, char[] password) throws IOException {
    try {
        URLConnection conn = ConnectionUtils.openReadConnection(url, username, password);
        InputStream is = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, ConnectionUtils.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);
        } 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;
    }
}
Also used : ForbiddenException(com.gitblit.GitBlitException.ForbiddenException) InputStreamReader(java.io.InputStreamReader) NotAllowedException(com.gitblit.GitBlitException.NotAllowedException) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) UnauthorizedException(com.gitblit.GitBlitException.UnauthorizedException) UnknownRequestException(com.gitblit.GitBlitException.UnknownRequestException) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 2 with UnknownRequestException

use of com.gitblit.GitBlitException.UnknownRequestException in project gitblit by gitblit.

the class JsonUtils method sendJsonString.

/**
	 * 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(ConnectionUtils.CHARSET);
        URLConnection conn = ConnectionUtils.openConnection(url, username, password);
        conn.setRequestProperty("Content-Type", "text/plain;charset=" + ConnectionUtils.CHARSET);
        conn.setRequestProperty("Content-Length", "" + jsonBytes.length);
        // 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);
        } 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;
    }
}
Also used : ForbiddenException(com.gitblit.GitBlitException.ForbiddenException) HttpURLConnection(java.net.HttpURLConnection) NotAllowedException(com.gitblit.GitBlitException.NotAllowedException) OutputStream(java.io.OutputStream) UnauthorizedException(com.gitblit.GitBlitException.UnauthorizedException) UnknownRequestException(com.gitblit.GitBlitException.UnknownRequestException) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Aggregations

ForbiddenException (com.gitblit.GitBlitException.ForbiddenException)2 NotAllowedException (com.gitblit.GitBlitException.NotAllowedException)2 UnauthorizedException (com.gitblit.GitBlitException.UnauthorizedException)2 UnknownRequestException (com.gitblit.GitBlitException.UnknownRequestException)2 IOException (java.io.IOException)2 HttpURLConnection (java.net.HttpURLConnection)2 URLConnection (java.net.URLConnection)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1