Search in sources :

Example 11 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project XobotOS by xamarin.

the class BasicResponseHandler method handleResponse.

/**
     * Returns the response body as a String if the response was successful (a
     * 2xx status code). If no response body exists, this returns null. If the
     * response was unsuccessful (>= 300 status code), throws an
     * {@link HttpResponseException}.
     */
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    HttpEntity entity = response.getEntity();
    return entity == null ? null : EntityUtils.toString(entity);
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 12 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class ApacheHttpClient method execute.

@SuppressWarnings("PMD.AvoidRethrowingException")
protected <T> T execute(HttpRequestBase request, ResponseFactory<T> factory) throws IOException {
    CloseableHttpResponse httpResponse;
    try {
        httpResponse = client.execute(request);
    } catch (HttpResponseException e) {
        throw new HttpException(e.getStatusCode(), e);
    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw new HttpException(500, e);
    }
    Runnable closeResponse = () -> {
        IOUtils.closeQuietly(httpResponse);
        request.releaseConnection();
    };
    boolean shouldCloseResponse = true;
    try {
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (!isOk(statusCode)) {
            HttpEntity entity = httpResponse.getEntity();
            String body = toString(entity);
            String method = request.getMethod();
            URI uri = request.getURI();
            String reasonPhrase = statusLine.getReasonPhrase();
            throw new HttpException(statusCode, String.format("%n%s %s%n==> %d %s%n%s", method, uri, statusCode, reasonPhrase, body));
        }
        T result = factory.create(new ApacheResponse(httpResponse), closeResponse);
        shouldCloseResponse = false;
        return result;
    } catch (IOException e) {
        throw new RuntimeIoException(e);
    } finally {
        if (shouldCloseResponse) {
            closeResponse.run();
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) URI(java.net.URI) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException) StatusLine(org.apache.http.StatusLine)

Example 13 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project c4sg-services by Code4SocialGood.

the class StringResponseHandler method handleResponse.

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    final StatusLine statusLine = response.getStatusLine();
    int status = statusLine.getStatusCode();
    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 14 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project SeaStar by 13120241790.

the class BreakpointHttpResponseHandler method sendResponseMessage.

@Override
public void sendResponseMessage(HttpResponse response) {
    if (!Thread.currentThread().isInterrupted() && !interrupt) {
        Throwable error = null;
        InputStream instream = null;
        StatusLine status = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        try {
            if (entity == null) {
                throw new IOException("Fail download. entity is null.");
            }
            // 处理contentLength
            long contentLength = entity.getContentLength();
            if (contentLength == -1) {
                contentLength = entity.getContent().available();
            }
            //如果临时文件存在,得到之前下载的大小
            if (tempFile.exists()) {
                previousFileSize = tempFile.length();
            }
            // 得到总大小,包括之前已经下载的
            totalSize = contentLength + previousFileSize;
            if (targetFile.exists() && totalSize == targetFile.length()) {
                NLog.e(tag, "Output file already exists. Skipping download.");
                sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), "success".getBytes());
                return;
            }
            //获取当前下载文件流
            instream = entity.getContent();
            if (instream == null) {
                throw new IOException("Fail download. instream is null.");
            }
            randomAccessFile = new RandomAccessFile(tempFile, "rw");
            randomAccessFile.seek(randomAccessFile.length());
            byte[] buffer = new byte[BUFFER_SIZE];
            int length, count = 0;
            while ((length = instream.read(buffer)) != -1 && !Thread.currentThread().isInterrupted() && !interrupt) {
                count += length;
                downloadSize = count + previousFileSize;
                randomAccessFile.write(buffer, 0, length);
                sendProgressMessage((int) downloadSize, (int) totalSize);
            }
            //判断下载大小与总大小不一致
            if (!Thread.currentThread().isInterrupted() && !interrupt) {
                if (downloadSize != totalSize && totalSize != -1) {
                    throw new IOException("Fail download. totalSize not eq downloadSize.");
                }
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
            error = e;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            error = e;
        } catch (IOException e) {
            e.printStackTrace();
            error = e;
        } finally {
            try {
                if (instream != null)
                    instream.close();
                if (randomAccessFile != null)
                    randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
                error = e;
            }
        }
        // additional cancellation check as getResponseData() can take non-zero time to process
        if (!Thread.currentThread().isInterrupted() && !interrupt) {
            if (status.getStatusCode() >= 300 || error != null) {
                sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), error.getMessage().getBytes(), new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
            } else {
                tempFile.renameTo(targetFile);
                sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), "success".getBytes());
            }
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) RandomAccessFile(java.io.RandomAccessFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 15 with HttpResponseException

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

the class BasicResponseHandler method handleResponse.

/**
     * Returns the response body as a String if the response was successful (a
     * 2xx status code). If no response body exists, this returns null. If the
     * response was unsuccessful (>= 300 status code), throws an
     * {@link HttpResponseException}.
     */
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    HttpEntity entity = response.getEntity();
    return entity == null ? null : EntityUtils.toString(entity);
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException)

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