use of org.apache.http.client.HttpResponseException in project openkit-android by OpenKit.
the class BinaryHttpResponseHandler method sendResponseMessage.
// Interface to AsyncHttpRequest
void sendResponseMessage(HttpResponse response) {
StatusLine status = response.getStatusLine();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
byte[] responseBody = null;
if (contentTypeHeaders.length != 1) {
//malformed/ambiguous HTTP Header, ABORT!
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), responseBody);
return;
}
Header contentTypeHeader = contentTypeHeaders[0];
boolean foundAllowedContentType = false;
for (String anAllowedContentType : mAllowedContentTypes) {
if (anAllowedContentType.equals(contentTypeHeader.getValue())) {
foundAllowedContentType = true;
}
}
if (!foundAllowedContentType) {
//Content-Type not in allowed list, ABORT!
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), responseBody);
return;
}
try {
HttpEntity entity = null;
HttpEntity temp = response.getEntity();
if (temp != null) {
entity = new BufferedHttpEntity(temp);
}
responseBody = EntityUtils.toByteArray(entity);
} catch (IOException e) {
sendFailureMessage(e, (byte[]) null);
}
if (status.getStatusCode() >= 300) {
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
} else {
sendSuccessMessage(status.getStatusCode(), responseBody);
}
}
use of org.apache.http.client.HttpResponseException in project android by JetBrains.
the class GoogleCrash method submit.
@NotNull
@Override
public CompletableFuture<String> submit(@NotNull final HttpEntity requestEntity) {
CompletableFuture<String> future = new CompletableFuture<>();
try {
ourExecutor.submit(() -> {
try {
HttpClient client = HttpClients.createDefault();
HttpEntity entity = requestEntity;
if (!UNIT_TEST_MODE) {
// The test server used in testing doesn't handle gzip compression (netty requires jcraft jzlib for gzip decompression)
entity = new GzipCompressingEntity(requestEntity);
}
HttpPost post = new HttpPost(myCrashUrl);
post.setEntity(entity);
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
future.completeExceptionally(new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()));
return;
}
entity = response.getEntity();
if (entity == null) {
future.completeExceptionally(new NullPointerException("Empty response entity"));
return;
}
String reportId = EntityUtils.toString(entity);
if (DEBUG_BUILD) {
//noinspection UseOfSystemOutOrSystemErr
System.out.println("Report submitted: http://go/crash-staging/" + reportId);
}
future.complete(reportId);
} catch (IOException e) {
future.completeExceptionally(e);
}
});
} catch (RejectedExecutionException ignore) {
// handled by the rejected execution handler associated with ourExecutor
}
return future;
}
use of org.apache.http.client.HttpResponseException in project android by JetBrains.
the class GoogleCrash method submit.
public CompletableFuture<String> submit(@NotNull FlightRecorder flightRecorder, @NotNull String issueText, @NotNull List<Path> logFiles) {
CompletableFuture<String> future = new CompletableFuture<>();
ForkJoinPool.commonPool().submit(() -> {
try {
HttpClient client = HttpClients.createDefault();
HttpResponse response = client.execute(createPost(flightRecorder, issueText, logFiles));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
future.completeExceptionally(new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()));
if (DEBUG_BUILD) {
//noinspection UseOfSystemOutOrSystemErr
System.out.println("Error submitting report: " + statusLine);
}
return;
}
HttpEntity entity = response.getEntity();
if (entity == null) {
future.completeExceptionally(new NullPointerException("Empty response entity"));
return;
}
String reportId = EntityUtils.toString(entity);
if (DEBUG_BUILD) {
//noinspection UseOfSystemOutOrSystemErr
System.out.println("Report submitted: http://go/crash-staging/" + reportId);
}
future.complete(reportId);
} catch (IOException e) {
future.completeExceptionally(e);
}
});
return future;
}
use of org.apache.http.client.HttpResponseException in project afinal by yangfuhai.
the class HttpHandler method handleResponse.
private void handleResponse(HttpResponse response) {
StatusLine status = response.getStatusLine();
if (status.getStatusCode() >= 300) {
String errorMsg = "response status error code:" + status.getStatusCode();
if (status.getStatusCode() == 416 && isResume) {
errorMsg += " \n maybe you have download complete.";
}
publishProgress(UPDATE_FAILURE, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), status.getStatusCode(), errorMsg);
} else {
try {
HttpEntity entity = response.getEntity();
Object responseBody = null;
if (entity != null) {
time = SystemClock.uptimeMillis();
if (targetUrl != null) {
responseBody = mFileEntityHandler.handleEntity(entity, this, targetUrl, isResume);
} else {
responseBody = mStrEntityHandler.handleEntity(entity, this, charset);
}
}
publishProgress(UPDATE_SUCCESS, responseBody);
} catch (IOException e) {
publishProgress(UPDATE_FAILURE, e, 0, e.getMessage());
}
}
}
use of org.apache.http.client.HttpResponseException in project SeaStar by 13120241790.
the class AsyncHttpResponseHandler method sendResponseMessage.
@Override
public void sendResponseMessage(HttpResponse response) throws IOException {
// do not process if request has been cancelled
if (!Thread.currentThread().isInterrupted()) {
StatusLine status = response.getStatusLine();
byte[] responseBody;
responseBody = getResponseData(response.getEntity());
// additional cancellation check as getResponseData() can take non-zero time to process
if (!Thread.currentThread().isInterrupted()) {
if (status.getStatusCode() >= 300) {
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), responseBody, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
} else {
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
}
}
}
}
Aggregations