Search in sources :

Example 36 with HttpResponseException

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

Example 37 with HttpResponseException

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 38 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 39 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 40 with HttpResponseException

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;
}
Also used : BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

HttpResponseException (org.apache.http.client.HttpResponseException)40 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 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 URISyntaxException (java.net.URISyntaxException)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)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 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)2