Download Modifiying documents with the Talkwalker API

Transcript
 } else if (httpCode == 503) {
// the service is currently unavailable
int secondsToWait = httpConnection.getHeaderFieldInt("Retry-After", 60);
System.out.println("TEMPORARILY UNAVAILABLE");
System.out.println("WAITING " + secondsToWait + "s UNTIL RETRYING");
Thread.sleep(secondsToWait * 1000);
} else {
// when encountering an error, we exit loop
try (InputStream is = httpConnection.getErrorStream()) {
readError(httpConnection, is, httpCode);
} catch (IOException e) {
e.printStackTrace();
} finally {
finished = true;
}
}
} catch (IOException ex) {
// try again
ex.printStackTrace();
// sleep a minute
Thread.sleep(60 * 1000);
}
}
deleteStream();
}
private void readError(HttpURLConnection httpConnection, InputStream errorInputStream, int httpCode)
throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] dataBuf = new byte[1024 * 1024];
// read answer
while (true) {
int read = errorInputStream.read(dataBuf, 0, dataBuf.length);
if (read == -1) {
break;
}
bos.write(dataBuf, 0, read);
}
InputStream is = new ByteArrayInputStream(bos.toByteArray());
if ((httpConnection.getContentEncoding() != null) && (httpConnection.getContentEncoding().equals("gzip")))
{
is = new GZIPInputStream(is);
}
// read json using jackson json (another library may be used here)
JsonFactory factory = new JsonFactory();