Skip to content

Commit

Permalink
Merge pull request bumptech#856 from swankjesse/jwilson_0102_okhttp_3
Browse files Browse the repository at this point in the history
Upgrade to OkHttp 3.
  • Loading branch information
sjudd committed Jan 3, 2016
2 parents 3557f4b + 1311e2b commit 734c54c
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 23 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ [email protected]
SUPPORT_V4_VERSION=22.2.0
SUPPORT_V7_VERSION=22.2.0
VOLLEY_VERSION=1.0.16
OK_HTTP_VERSION=2.3.0
OK_HTTP_VERSION=3.0.0-RC1
# TODO: use this in library/build.gradle.
ANDROID_GRADLE_VERSION=1.2.3

Expand All @@ -30,7 +30,7 @@ COVERALLS_GRADLE_VERSION=2.4.0
JUNIT_VERSION=4.12
MOCKITO_VERSION=1.10.19
ROBOLECTRIC_VERSION=3.0-rc3
MOCKWEBSERVER_VERSION=2.3.0
MOCKWEBSERVER_VERSION=3.0.0-RC1
TRUTH_VERSION=0.26

FINDBUGS_VERSION=3.0.0
Expand Down
2 changes: 1 addition & 1 deletion integration/okhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repositories {
dependencies {
compile project(':glide')

compile "com.squareup.okhttp:okhttp:${OK_HTTP_VERSION}"
compile "com.squareup.okhttp:okhttp:2.7.1"
}

android {
Expand Down
2 changes: 1 addition & 1 deletion integration/okhttp/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ VERSION_MINOR=0
VERSION_PATCH=0
VERSION_CODE=8

POM_DESCRIPTION=An integration library to use OkHttp to fetch data over http/https in Glide
POM_DESCRIPTION=An integration library to use OkHttp 2.x to fetch data over http/https in Glide

# Prefix and postfix for source and javadoc jars.
JAR_PREFIX=glide-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
* <p> If you're using gradle, you can include this module simply by depending on the aar, the
* module will be merged in by manifest merger. For other build systems or for more more
* information, see {@link com.bumptech.glide.module.GlideModule}. </p>
*
* @deprecated replaced with com.bumptech.glide.integration.okhttp3.OkHttpGlideModule.
*/
@Deprecated
public class OkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

/**
* Fetches an {@link InputStream} using the okhttp library.
*
* @deprecated replaced with com.bumptech.glide.integration.okhttp3.OkHttpStreamFetcher.
*/
@Deprecated
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private static final String TAG = "OkHttpFetcher";
private final OkHttpClient client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

/**
* A simple model loader for fetching media over http/https using OkHttp.
*
* @deprecated replaced with com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader.
*/
@Deprecated
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {

private final OkHttpClient client;
Expand Down
31 changes: 31 additions & 0 deletions integration/okhttp3/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.library'

repositories {
mavenCentral()
}

dependencies {
compile project(':glide')

compile "com.squareup.okhttp3:okhttp:${OK_HTTP_VERSION}"
}

android {
compileSdkVersion COMPILE_SDK_VERSION as int
buildToolsVersion BUILD_TOOLS_VERSION as String

defaultConfig {
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int

versionCode VERSION_CODE as int
versionName VERSION_NAME as String
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

apply from: "$rootProject.projectDir/scripts/upload.gradle"
15 changes: 15 additions & 0 deletions integration/okhttp3/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
POM_NAME=Glide OkHttp 3.x Integration
POM_ARTIFACT_ID=okhttp3-integration
POM_PACKAGING=aar

VERSION_NAME=2.0.0-SNAPSHOT
VERSION_MAJOR=2
VERSION_MINOR=0
VERSION_PATCH=0
VERSION_CODE=8

POM_DESCRIPTION=An integration library to use OkHttp 3.x to fetch data over http/https in Glide

# Prefix and postfix for source and javadoc jars.
JAR_PREFIX=glide-
JAR_POSTFIX=-integration
8 changes: 8 additions & 0 deletions integration/okhttp3/lint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="AllowBackup" severity="ignore"/>
<!-- See https://github.com/square/okio/issues/58 -->
<issue id="InvalidPackage" severity="ignore">
<ignore regexp="okio-1.0.0.jar"/>
</issue>
</lint>
9 changes: 9 additions & 0 deletions integration/okhttp3/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bumptech.glide.integration.okhttp">

<application>
<meta-data
android:name="com.bumptech.glide.integration.okhttp3.OkHttpGlideModule"
android:value="GlideModule"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.bumptech.glide.integration.okhttp3;

import android.content.Context;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.GlideModule;

import java.io.InputStream;

/**
* A {@link com.bumptech.glide.module.GlideModule} implementation to replace Glide's default
* {@link java.net.HttpURLConnection} based {@link com.bumptech.glide.load.model.ModelLoader}
* with an OkHttp based {@link com.bumptech.glide.load.model.ModelLoader}.
*
* <p> If you're using gradle, you can include this module simply by depending on the aar, the
* module will be merged in by manifest merger. For other build systems or for more more
* information, see {@link com.bumptech.glide.module.GlideModule}. </p>
*/
public class OkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// Do nothing.
}

@Override
public void registerComponents(Context context, Registry registry) {
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.bumptech.glide.integration.okhttp3;

import android.util.Log;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
* Fetches an {@link InputStream} using the okhttp library.
*/
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private static final String TAG = "OkHttpFetcher";
private final Call.Factory client;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;

public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) {
this.client = client;
this.url = url;
}

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

client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Request request, IOException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "OkHttp failed to obtain result", e);
}
callback.onLoadFailed(e);
}

@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
long contentLength = response.body().contentLength();
responseBody = response.body();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
} else if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "OkHttp got error response: " + response.code() + ", " + response.message());
}
callback.onDataReady(stream);
}
});
}

@Override
public void cleanup() {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
// Ignored
}
if (responseBody != null) {
responseBody.close();
}
}

@Override
public void cancel() {
// TODO: call cancel on the client when this method is called on a background thread. See #257
}

@Override
public Class<InputStream> getDataClass() {
return InputStream.class;
}

@Override
public DataSource getDataSource() {
return DataSource.REMOTE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.bumptech.glide.integration.okhttp3;

import android.content.Context;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import okhttp3.Call;
import okhttp3.OkHttpClient;

import java.io.InputStream;

/**
* A simple model loader for fetching media over http/https using OkHttp.
*/
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {

private final Call.Factory client;

public OkHttpUrlLoader(Call.Factory client) {
this.client = client;
}

@Override
public boolean handles(GlideUrl url) {
return true;
}

@Override
public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height,
Options options) {
return new LoadData<>(model, new OkHttpStreamFetcher(client, model));
}

/**
* The default factory for {@link OkHttpUrlLoader}s.
*/
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
private static volatile Call.Factory internalClient;
private Call.Factory client;

private static Call.Factory getInternalClient() {
if (internalClient == null) {
synchronized (Factory.class) {
if (internalClient == null) {
internalClient = new OkHttpClient();
}
}
}
return internalClient;
}

/**
* Constructor for a new Factory that runs requests using a static singleton client.
*/
public Factory() {
this(getInternalClient());
}

/**
* Constructor for a new Factory that runs requests using given client.
*
* @param client this is typically an instance of {@code OkHttpClient}.
*/
public Factory(Call.Factory client) {
this.client = client;
}

@Override
public ModelLoader<GlideUrl, InputStream> build(Context context,
MultiModelLoaderFactory multiFactory) {
return new OkHttpUrlLoader(client);
}

@Override
public void teardown() {
// Do nothing, this instance doesn't own the client.
}
}
}
2 changes: 1 addition & 1 deletion integration/volley/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
testCompile "junit:junit:${JUNIT_VERSION}"
testCompile "org.mockito:mockito-core:${MOCKITO_VERSION}"
testCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}"
testCompile "com.squareup.okhttp:mockwebserver:${MOCKWEBSERVER_VERSION}"
testCompile "com.squareup.okhttp3:mockwebserver:${MOCKWEBSERVER_VERSION}"
}

android {
Expand Down
Loading

0 comments on commit 734c54c

Please sign in to comment.