Search in sources :

Example 1 with HttpResponseException

use of cz.msebera.android.httpclient.client.HttpResponseException in project android-async-http by loopj.

the class BinaryHttpResponseHandler method sendResponseMessage.

@Override
public final void sendResponseMessage(HttpResponse response) throws IOException {
    StatusLine status = response.getStatusLine();
    Header[] contentTypeHeaders = response.getHeaders(AsyncHttpClient.HEADER_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) {
            AsyncHttpClient.log.e(LOG_TAG, "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 (" + contentTypeHeader.getValue() + ") not allowed!"));
        return;
    }
    super.sendResponseMessage(response);
}
Also used : StatusLine(cz.msebera.android.httpclient.StatusLine) Header(cz.msebera.android.httpclient.Header) HttpResponseException(cz.msebera.android.httpclient.client.HttpResponseException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with HttpResponseException

use of cz.msebera.android.httpclient.client.HttpResponseException in project android-async-http by loopj.

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);
            }
        }
    }
}
Also used : StatusLine(cz.msebera.android.httpclient.StatusLine) HttpResponseException(cz.msebera.android.httpclient.client.HttpResponseException)

Aggregations

StatusLine (cz.msebera.android.httpclient.StatusLine)2 HttpResponseException (cz.msebera.android.httpclient.client.HttpResponseException)2 Header (cz.msebera.android.httpclient.Header)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1