use of org.apache.http.client.HttpResponseException in project ThinkAndroid by white-cat.
the class BinaryHttpResponseHandler method sendResponseMessage.
// Interface to AsyncHttpRequest
@Override
protected 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 (Pattern.matches(anAllowedContentType, 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 sling by apache.
the class SimpleHttpDistributionTransport method deliverPackage.
public void deliverPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionPackage distributionPackage, @Nonnull DistributionTransportContext distributionContext) throws DistributionException {
String hostAndPort = getHostAndPort(distributionEndpoint.getUri());
DistributionPackageInfo info = distributionPackage.getInfo();
URI packageOrigin = info.get(PACKAGE_INFO_PROPERTY_ORIGIN_URI, URI.class);
if (packageOrigin != null && hostAndPort.equals(getHostAndPort(packageOrigin))) {
log.debug("skipping distribution of package {} to same origin {}", distributionPackage.getId(), hostAndPort);
} else {
try {
Executor executor = getExecutor(distributionContext);
Request req = Request.Post(distributionEndpoint.getUri()).connectTimeout(httpConfiguration.getConnectTimeout()).socketTimeout(httpConfiguration.getSocketTimeout()).addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE).useExpectContinue();
// add the message body digest, see https://tools.ietf.org/html/rfc3230#section-4.3.2
if (distributionPackage instanceof AbstractDistributionPackage) {
AbstractDistributionPackage adb = (AbstractDistributionPackage) distributionPackage;
if (adb.getDigestAlgorithm() != null && adb.getDigestMessage() != null) {
req.addHeader(DIGEST_HEADER, String.format("%s=%s", adb.getDigestAlgorithm(), adb.getDigestMessage()));
}
}
InputStream inputStream = null;
try {
inputStream = DistributionPackageUtils.createStreamWithHeader(distributionPackage);
req = req.bodyStream(inputStream, ContentType.APPLICATION_OCTET_STREAM);
Response response = executor.execute(req);
// throws an error if HTTP status is >= 300
response.returnContent();
} finally {
IOUtils.closeQuietly(inputStream);
}
log.debug("delivered packageId={}, endpoint={}", distributionPackage.getId(), distributionEndpoint.getUri());
} catch (HttpHostConnectException e) {
throw new RecoverableDistributionException("endpoint not available " + distributionEndpoint.getUri(), e);
} catch (HttpResponseException e) {
int statusCode = e.getStatusCode();
if (statusCode == 404 || statusCode == 401) {
throw new RecoverableDistributionException("not enough rights for " + distributionEndpoint.getUri(), e);
}
throw new DistributionException(e);
} catch (Exception e) {
throw new DistributionException(e);
}
}
}
use of org.apache.http.client.HttpResponseException in project weixin-java-tools by chanjarster.
the class InputStreamResponseHandler method handleResponse.
public InputStream handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
final StatusLine statusLine = response.getStatusLine();
final HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
EntityUtils.consume(entity);
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
return entity == null ? null : entity.getContent();
}
use of org.apache.http.client.HttpResponseException in project cw-omnibus by commonsguy.
the class ByteArrayResponseHandler method handleResponse.
public byte[] handleResponse(final HttpResponse response) throws IOException, HttpResponseException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
HttpEntity entity = response.getEntity();
if (entity == null) {
return (null);
}
return (EntityUtils.toByteArray(entity));
}
use of org.apache.http.client.HttpResponseException in project Libraries-for-Android-Developers by eoecn.
the class BinaryHttpResponseHandler method sendResponseMessage.
@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);
}
Aggregations