use of com.gitblit.GitBlitException.UnauthorizedException 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;
}
}
use of com.gitblit.GitBlitException.UnauthorizedException 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;
}
}
Aggregations