use of org.apache.http.client.HttpResponseException in project XobotOS by xamarin.
the class BasicResponseHandler method handleResponse.
/**
* Returns the response body as a String if the response was successful (a
* 2xx status code). If no response body exists, this returns null. If the
* response was unsuccessful (>= 300 status code), throws an
* {@link HttpResponseException}.
*/
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
HttpEntity entity = response.getEntity();
return entity == null ? null : EntityUtils.toString(entity);
}
use of org.apache.http.client.HttpResponseException in project infoarchive-sip-sdk by Enterprise-Content-Management.
the class ApacheHttpClient method execute.
@SuppressWarnings("PMD.AvoidRethrowingException")
protected <T> T execute(HttpRequestBase request, ResponseFactory<T> factory) throws IOException {
CloseableHttpResponse httpResponse;
try {
httpResponse = client.execute(request);
} catch (HttpResponseException e) {
throw new HttpException(e.getStatusCode(), e);
} catch (HttpException e) {
throw e;
} catch (IOException e) {
throw new HttpException(500, e);
}
Runnable closeResponse = () -> {
IOUtils.closeQuietly(httpResponse);
request.releaseConnection();
};
boolean shouldCloseResponse = true;
try {
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (!isOk(statusCode)) {
HttpEntity entity = httpResponse.getEntity();
String body = toString(entity);
String method = request.getMethod();
URI uri = request.getURI();
String reasonPhrase = statusLine.getReasonPhrase();
throw new HttpException(statusCode, String.format("%n%s %s%n==> %d %s%n%s", method, uri, statusCode, reasonPhrase, body));
}
T result = factory.create(new ApacheResponse(httpResponse), closeResponse);
shouldCloseResponse = false;
return result;
} catch (IOException e) {
throw new RuntimeIoException(e);
} finally {
if (shouldCloseResponse) {
closeResponse.run();
}
}
}
use of org.apache.http.client.HttpResponseException in project c4sg-services by Code4SocialGood.
the class StringResponseHandler method handleResponse.
@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
final StatusLine statusLine = response.getStatusLine();
int status = statusLine.getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
}
use of org.apache.http.client.HttpResponseException in project SeaStar by 13120241790.
the class BreakpointHttpResponseHandler method sendResponseMessage.
@Override
public void sendResponseMessage(HttpResponse response) {
if (!Thread.currentThread().isInterrupted() && !interrupt) {
Throwable error = null;
InputStream instream = null;
StatusLine status = response.getStatusLine();
HttpEntity entity = response.getEntity();
try {
if (entity == null) {
throw new IOException("Fail download. entity is null.");
}
// 处理contentLength
long contentLength = entity.getContentLength();
if (contentLength == -1) {
contentLength = entity.getContent().available();
}
//如果临时文件存在,得到之前下载的大小
if (tempFile.exists()) {
previousFileSize = tempFile.length();
}
// 得到总大小,包括之前已经下载的
totalSize = contentLength + previousFileSize;
if (targetFile.exists() && totalSize == targetFile.length()) {
NLog.e(tag, "Output file already exists. Skipping download.");
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), "success".getBytes());
return;
}
//获取当前下载文件流
instream = entity.getContent();
if (instream == null) {
throw new IOException("Fail download. instream is null.");
}
randomAccessFile = new RandomAccessFile(tempFile, "rw");
randomAccessFile.seek(randomAccessFile.length());
byte[] buffer = new byte[BUFFER_SIZE];
int length, count = 0;
while ((length = instream.read(buffer)) != -1 && !Thread.currentThread().isInterrupted() && !interrupt) {
count += length;
downloadSize = count + previousFileSize;
randomAccessFile.write(buffer, 0, length);
sendProgressMessage((int) downloadSize, (int) totalSize);
}
//判断下载大小与总大小不一致
if (!Thread.currentThread().isInterrupted() && !interrupt) {
if (downloadSize != totalSize && totalSize != -1) {
throw new IOException("Fail download. totalSize not eq downloadSize.");
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
error = e;
} catch (FileNotFoundException e) {
e.printStackTrace();
error = e;
} catch (IOException e) {
e.printStackTrace();
error = e;
} finally {
try {
if (instream != null)
instream.close();
if (randomAccessFile != null)
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
error = e;
}
}
// additional cancellation check as getResponseData() can take non-zero time to process
if (!Thread.currentThread().isInterrupted() && !interrupt) {
if (status.getStatusCode() >= 300 || error != null) {
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), error.getMessage().getBytes(), new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
} else {
tempFile.renameTo(targetFile);
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), "success".getBytes());
}
}
}
}
use of org.apache.http.client.HttpResponseException in project platform_external_apache-http by android.
the class BasicResponseHandler method handleResponse.
/**
* Returns the response body as a String if the response was successful (a
* 2xx status code). If no response body exists, this returns null. If the
* response was unsuccessful (>= 300 status code), throws an
* {@link HttpResponseException}.
*/
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
HttpEntity entity = response.getEntity();
return entity == null ? null : EntityUtils.toString(entity);
}
Aggregations