Search in sources :

Example 31 with HttpResponseException

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);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 32 with HttpResponseException

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) GzipCompressingEntity(org.apache.http.client.entity.GzipCompressingEntity) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) StatusLine(org.apache.http.StatusLine) HttpClient(org.apache.http.client.HttpClient) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with HttpResponseException

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;
}
Also used : StatusLine(org.apache.http.StatusLine) CompletableFuture(java.util.concurrent.CompletableFuture) HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 34 with HttpResponseException

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());
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 35 with HttpResponseException

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);
            }
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpResponseException(org.apache.http.client.HttpResponseException)

Aggregations

HttpResponseException (org.apache.http.client.HttpResponseException)41 StatusLine (org.apache.http.StatusLine)23 HttpEntity (org.apache.http.HttpEntity)17 IOException (java.io.IOException)16 Header (org.apache.http.Header)5 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)5 URISyntaxException (java.net.URISyntaxException)4 HttpClient (org.apache.http.client.HttpClient)4 HttpGet (org.apache.http.client.methods.HttpGet)4 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)4 Test (org.junit.Test)4 URI (java.net.URI)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 HttpResponse (org.apache.http.HttpResponse)3 SharedPreferences (android.content.SharedPreferences)2 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)2 TextMessage (com.nyaruka.androidrelay.data.TextMessage)2 TextMessageHelper (com.nyaruka.androidrelay.data.TextMessageHelper)2 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)2 Job (com.offbytwo.jenkins.model.Job)2