in src/main/java/com/epam/dep/esp/common/web/WebUtils.java [119:184]
public String perform(int i, int time) throws HttpException {
HttpURLConnection conn = null;
try {
if (i == 0) throw new HttpException("Unable to perform operation");
//TODO add multi address support
//TODO add PORT support
conn = getConnection(10);
int responseCode = conn.getResponseCode();
logger.info("{} \t Response {}", conn.getURL(), responseCode);
switch (responseCode) {
case 401:
throw new HttpException(conn.getURL().getPath());
case 500:
logger.info("Retrying in {} sec...", time);
Thread.sleep(time * 1000);
conn.disconnect();
return perform(i - 1, time * 2);
}
String encoding = conn.getContentEncoding();
// allow both GZip and Deflate (ZLib) encodings
InputStream inStr = null;
// create the appropriate stream wrapper based on
// the encoding type
if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
inStr = new GZIPInputStream(conn.getInputStream());
} else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
inStr = new InflaterInputStream(conn.getInputStream(),
new Inflater(true));
} else {
inStr = conn.getInputStream();
}
} else {
/* error from server */
inStr = conn.getErrorStream();
}
InputStreamReader isr = new InputStreamReader(inStr);
int numCharsRead;
char[] charArray = new char[1024];
StringBuilder sb = new StringBuilder();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
throw new HttpException("Response code is " + responseCode + " message: " + result);
}
return result;
} catch (MalformedURLException | URISyntaxException e) {
throw new HttpException("Can't find path: " + path);
} catch (IOException e) {
throw new HttpException("Server is offline: " + path + " " + e.getMessage(), e);
} catch (InterruptedException e) {
throw new HttpException("InterruptedException", e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}