Search in sources :

Example 56 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 57 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project SmartAndroidSource by jaychou2012.

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) {
            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 58 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 59 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project openkit-android by OpenKit.

the class OKUserUtilities method checkIfErrorIsUnsubscribedUserError.

public static void checkIfErrorIsUnsubscribedUserError(Throwable e) {
    if (e == null) {
        return;
    }
    if (e instanceof HttpResponseException) {
        HttpResponseException responseException = (HttpResponseException) e;
        if (responseException.getStatusCode() == OKHTTPClient.UNSUBSCRIBED_USER_ERROR_CODE) {
            // Logout current user if we get an unsubscribed user error
            OKLog.v("Unsubscribed user, log out the user, error is: " + e);
            OKManager.INSTANCE.logoutCurrentUserWithoutClearingFB();
        }
    }
}
Also used : HttpResponseException(org.apache.http.client.HttpResponseException)

Example 60 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project stashbot by palantir.

the class JenkinsManager method synchronousTriggerBuild.

public void synchronousTriggerBuild(Repository repo, JobType jobType, PullRequest pullRequest) {
    try {
        String pullRequestId = pullRequest.getId().toString();
        String hashToBuild = pullRequest.getToRef().getLatestChangeset();
        RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
        JobTemplate jt = jtm.getJobTemplate(jobType, rc);
        String jenkinsBuildId = jt.getBuildNameFor(repo);
        String url = jsc.getUrl();
        String user = jsc.getUsername();
        String password = jsc.getPassword();
        log.info("Triggering jenkins build id " + jenkinsBuildId + " on hash " + hashToBuild + " (" + user + "@" + url + " pw: " + password.replaceAll(".", "*") + ")");
        final JenkinsServer js = jenkinsClientManager.getJenkinsServer(jsc, rc);
        Map<String, Job> jobMap = js.getJobs();
        String key = jt.getBuildNameFor(repo);
        if (!jobMap.containsKey(key)) {
            throw new RuntimeException("Build doesn't exist: " + key);
        }
        Builder<String, String> builder = ImmutableMap.builder();
        builder.put("repoId", repo.getId().toString());
        if (pullRequest != null) {
            log.debug("Determined pullRequestId " + pullRequestId);
            builder.put("pullRequestId", pullRequestId);
            // toRef is always present in the repo
            builder.put("buildHead", pullRequest.getToRef().getLatestChangeset().toString());
            // fromRef may be in a different repo
            builder.put("mergeRef", pullRequest.getFromRef().getId());
            builder.put("buildRef", pullRequest.getToRef().getId());
            builder.put("mergeRefUrl", sub.buildCloneUrl(pullRequest.getFromRef().getRepository(), jsc));
            builder.put("mergeHead", pullRequest.getFromRef().getLatestChangeset().toString());
        }
        jobMap.get(key).build(builder.build());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    } catch (HttpResponseException e) {
        // client
        if (e.getStatusCode() == 302) {
            // to some URL after the fact.
            return;
        }
        // For other HTTP errors, log it for easier debugging
        log.error("HTTP Error (resp code " + Integer.toString(e.getStatusCode()) + ")", e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JenkinsServer(com.offbytwo.jenkins.JenkinsServer) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) Job(com.offbytwo.jenkins.model.Job)

Aggregations

HttpResponseException (org.apache.http.client.HttpResponseException)82 IOException (java.io.IOException)32 StatusLine (org.apache.http.StatusLine)30 HttpEntity (org.apache.http.HttpEntity)25 HttpGet (org.apache.http.client.methods.HttpGet)14 HttpResponse (org.apache.http.HttpResponse)13 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)11 Header (org.apache.http.Header)9 ClientProtocolException (org.apache.http.client.ClientProtocolException)9 URISyntaxException (java.net.URISyntaxException)8 ArrayList (java.util.ArrayList)8 URI (java.net.URI)7 Document (org.jsoup.nodes.Document)7 Test (org.junit.Test)7 InputStream (java.io.InputStream)6 Expectations (mockit.Expectations)5 HttpClient (org.apache.http.client.HttpClient)5 Element (org.jsoup.nodes.Element)5 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)4 HttpPost (org.apache.http.client.methods.HttpPost)4