Search in sources :

Example 1 with HttpException

use of com.bumptech.glide.load.HttpException in project DevRing by LJYcoder.

the class OkHttpStreamFetcher method onResponse.

@Override
public void onResponse(Call call, Response response) throws IOException {
    responseBody = response.body();
    if (response.isSuccessful()) {
        long contentLength = responseBody.contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
    } else {
        callback.onLoadFailed(new HttpException(response.message(), response.code()));
    }
}
Also used : HttpException(com.bumptech.glide.load.HttpException)

Example 2 with HttpException

use of com.bumptech.glide.load.HttpException in project Rocket by mozilla-tw.

the class HttpUrlFetcher method loadDataWithRedirects.

private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl, Map<String, String> headers) throws IOException {
    if (redirects >= MAXIMUM_REDIRECTS) {
        throw new HttpException("Too many (> " + MAXIMUM_REDIRECTS + ") redirects!");
    } else {
        // See http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html.
        try {
            if (lastUrl != null && url.toURI().equals(lastUrl.toURI())) {
                throw new HttpException("In re-direct loop");
            }
        } catch (URISyntaxException e) {
        // Do nothing, this is best effort.
        }
    }
    urlConnection = connectionFactory.build(url);
    for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
        urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue());
    }
    urlConnection.setConnectTimeout(timeout);
    urlConnection.setReadTimeout(timeout);
    urlConnection.setUseCaches(false);
    urlConnection.setDoInput(true);
    // Stop the urlConnection instance of HttpUrlConnection from following redirects so that
    // redirects will be handled by recursive calls to this method, loadDataWithRedirects.
    urlConnection.setInstanceFollowRedirects(false);
    // Connect explicitly to avoid errors in decoders if connection fails.
    urlConnection.connect();
    // Set the stream so that it's closed in cleanup to avoid resource leaks. See #2352.
    stream = urlConnection.getInputStream();
    if (isCancelled) {
        return null;
    }
    final int statusCode = urlConnection.getResponseCode();
    if (statusCode / 100 == 2) {
        return getStreamForSuccessfulRequest(urlConnection);
    } else if (statusCode / 100 == 3) {
        String redirectUrlString = urlConnection.getHeaderField("Location");
        if (TextUtils.isEmpty(redirectUrlString)) {
            throw new HttpException("Received empty or null redirect url");
        }
        URL redirectUrl = new URL(url, redirectUrlString);
        // Closing the stream specifically is required to avoid leaking ResponseBodys in addition
        // to disconnecting the url connection below. See #2352.
        cleanup();
        return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers);
    } else if (statusCode == -1) {
        throw new HttpException(statusCode);
    } else {
        throw new HttpException(urlConnection.getResponseMessage(), statusCode);
    }
}
Also used : HttpException(com.bumptech.glide.load.HttpException) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) URL(java.net.URL)

Example 3 with HttpException

use of com.bumptech.glide.load.HttpException in project glide by bumptech.

the class OkHttpStreamFetcher method onResponse.

@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
    responseBody = response.body();
    if (response.isSuccessful()) {
        long contentLength = Preconditions.checkNotNull(responseBody).contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
    } else {
        callback.onLoadFailed(new HttpException(response.message(), response.code()));
    }
}
Also used : HttpException(com.bumptech.glide.load.HttpException)

Example 4 with HttpException

use of com.bumptech.glide.load.HttpException in project glide by bumptech.

the class HttpUrlFetcher method loadDataWithRedirects.

private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl, Map<String, String> headers) throws IOException {
    if (redirects >= MAXIMUM_REDIRECTS) {
        throw new HttpException("Too many (> " + MAXIMUM_REDIRECTS + ") redirects!");
    } else {
        // See http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html.
        try {
            if (lastUrl != null && url.toURI().equals(lastUrl.toURI())) {
                throw new HttpException("In re-direct loop");
            }
        } catch (URISyntaxException e) {
        // Do nothing, this is best effort.
        }
    }
    urlConnection = connectionFactory.build(url);
    for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
        urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue());
    }
    urlConnection.setConnectTimeout(timeout);
    urlConnection.setReadTimeout(timeout);
    urlConnection.setUseCaches(false);
    urlConnection.setDoInput(true);
    // Stop the urlConnection instance of HttpUrlConnection from following redirects so that
    // redirects will be handled by recursive calls to this method, loadDataWithRedirects.
    urlConnection.setInstanceFollowRedirects(false);
    // Connect explicitly to avoid errors in decoders if connection fails.
    urlConnection.connect();
    // Set the stream so that it's closed in cleanup to avoid resource leaks. See #2352.
    stream = urlConnection.getInputStream();
    if (isCancelled) {
        return null;
    }
    final int statusCode = urlConnection.getResponseCode();
    if (isHttpOk(statusCode)) {
        return getStreamForSuccessfulRequest(urlConnection);
    } else if (isHttpRedirect(statusCode)) {
        String redirectUrlString = urlConnection.getHeaderField("Location");
        if (TextUtils.isEmpty(redirectUrlString)) {
            throw new HttpException("Received empty or null redirect url");
        }
        URL redirectUrl = new URL(url, redirectUrlString);
        // Closing the stream specifically is required to avoid leaking ResponseBodys in addition
        // to disconnecting the url connection below. See #2352.
        cleanup();
        return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers);
    } else if (statusCode == INVALID_STATUS_CODE) {
        throw new HttpException(statusCode);
    } else {
        throw new HttpException(urlConnection.getResponseMessage(), statusCode);
    }
}
Also used : HttpException(com.bumptech.glide.load.HttpException) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) URL(java.net.URL)

Example 5 with HttpException

use of com.bumptech.glide.load.HttpException in project CloudReader by youlookwhat.

the class OkHttpStreamFetcher method loadData.

@Override
public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
    Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }
    Request request = requestBuilder.build();
    call = client.newCall(request);
    call.enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "OkHttp failed to obtain result", e);
            }
            callback.onLoadFailed(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            responseBody = response.body();
            if (response.isSuccessful()) {
                long contentLength = responseBody.contentLength();
                stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
                callback.onDataReady(stream);
            } else {
                callback.onLoadFailed(new HttpException(response.message(), response.code()));
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Request(okhttp3.Request) HttpException(com.bumptech.glide.load.HttpException) IOException(java.io.IOException) Map(java.util.Map)

Aggregations

HttpException (com.bumptech.glide.load.HttpException)9 Map (java.util.Map)6 IOException (java.io.IOException)4 Call (okhttp3.Call)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1