use of org.apache.http.client.HttpResponseException in project SeaStar by 13120241790.
the class BinaryHttpResponseHandler method sendResponseMessage.
//
// Pre-processing of messages (in original calling thread, typically the UI thread)
//
// Interface to AsyncHttpRequest
@Override
public final void sendResponseMessage(HttpResponse response) throws IOException {
StatusLine status = response.getStatusLine();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
if (contentTypeHeaders.length != 1) {
//malformed/ambiguous HTTP Header, ABORT!
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"));
return;
}
Header contentTypeHeader = contentTypeHeaders[0];
boolean foundAllowedContentType = false;
for (String anAllowedContentType : getAllowedContentTypes()) {
try {
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
foundAllowedContentType = true;
}
} catch (PatternSyntaxException e) {
Log.e("BinaryHttpResponseHandler", "Given pattern is not valid: " + anAllowedContentType, e);
}
}
if (!foundAllowedContentType) {
//Content-Type not in allowed list, ABORT!
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"));
return;
}
super.sendResponseMessage(response);
}
use of org.apache.http.client.HttpResponseException in project Asqatasun by Asqatasun.
the class DownloaderImpl method download.
private String download(String url) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
httpclient.getParams().setParameter("http.socket.timeout", Integer.valueOf(10000));
httpclient.getParams().setParameter("http.connection.timeout", Integer.valueOf(10000));
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody;
try {
responseBody = httpclient.execute(httpget, responseHandler);
} catch (HttpResponseException ex) {
LOGGER.warn(ex.getMessage() + " " + url);
return "";
} catch (UnknownHostException ex) {
LOGGER.warn(ex.getMessage() + " " + url);
return "";
} catch (SSLPeerUnverifiedException ex) {
LOGGER.warn(ex.getMessage() + " " + url);
return "";
} catch (IOException ex) {
LOGGER.warn(ex.getMessage() + " " + url);
return "";
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
return 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 cloudstack by apache.
the class SspClient method executeMethod.
private String executeMethod(HttpRequestBase req, String path) {
try {
URI base = new URI(apiUrl);
req.setURI(new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), path, null, null));
} catch (URISyntaxException e) {
s_logger.error("invalid API URL " + apiUrl + " path " + path, e);
return null;
}
try {
String content = null;
try {
content = getHttpClient().execute(req, new BasicResponseHandler());
s_logger.info("ssp api call: " + req);
} catch (HttpResponseException e) {
s_logger.info("ssp api call failed: " + req, e);
if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) {
req.reset();
content = getHttpClient().execute(req, new BasicResponseHandler());
s_logger.info("ssp api retry call: " + req);
}
}
return content;
} catch (ClientProtocolException e) {
// includes HttpResponseException
s_logger.error("ssp api call failed: " + req, e);
} catch (IOException e) {
s_logger.error("ssp api call failed: " + req, e);
}
return null;
}
Aggregations