Skip to content

Commit

Permalink
Merge pull request square#1312 from nfuller/PullOutAndroidSupport
Browse files Browse the repository at this point in the history
Create a new maven artifact to hold things needed for Android embedding
  • Loading branch information
swankjesse committed Jan 13, 2015
2 parents ef4e9b9 + c9b0262 commit 2f50252
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 22 deletions.
50 changes: 50 additions & 0 deletions okhttp-android-support/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup.okhttp</groupId>
<artifactId>parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
</parent>

<artifactId>okhttp-android-support</artifactId>
<name>OkHttp Android Platform Support</name>
<description>Classes to support the Android platform's use of OkHttp (not required for most developers).</description>

<dependencies>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp-urlconnection</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>mockwebserver</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<excludePackageNames>com.squareup.okhttp.internal.*</excludePackageNames>
<links>
<link>http://square.github.io/okhttp/javadoc/</link>
</links>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.okhttp;

import com.squareup.okhttp.internal.InternalCache;
import com.squareup.okhttp.internal.huc.CacheAdapter;

import java.net.ResponseCache;

/**
* A class for back doors for Android's use of OkHttp within the Android platform.
*/
public class AndroidInternal {

private AndroidInternal() {
}

/** Sets the response cache to be used to read and write cached responses. */
public static void setResponseCache(OkUrlFactory okUrlFactory, ResponseCache responseCache) {
okUrlFactory.client().setInternalCache(
responseCache != null ? new CacheAdapter(responseCache) : null);
}

public static ResponseCache getResponseCache(OkUrlFactory okUrlFactory) {
InternalCache cache = okUrlFactory.client().internalCache();
return cache instanceof CacheAdapter ? ((CacheAdapter) cache).getDelegate() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,8 @@ private void assertHeadersContainsMapping(Map<String, List<String>> headers, Str
javaResponseHeaders.put("key2", Arrays.asList("value2"));
assertEquals("StatusLine", JavaApiConverter.extractStatusLine(javaResponseHeaders));

assertNull(JavaApiConverter.extractStatusLine(Collections.<String, List<String>>emptyMap()));
assertNull(
JavaApiConverter.extractStatusLine(Collections.<String, List<String>>emptyMap()));
}

private URL configureServer(MockResponse mockResponse) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
package com.squareup.okhttp;

import com.squareup.okhttp.internal.InternalCache;
import com.squareup.okhttp.internal.huc.CacheAdapter;
import com.squareup.okhttp.internal.huc.HttpURLConnectionImpl;
import com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl;

import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.ResponseCache;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
Expand All @@ -38,17 +36,6 @@ public OkHttpClient client() {
return client;
}

/** Sets the response cache to be used to read and write cached responses. */
OkUrlFactory setResponseCache(ResponseCache responseCache) {
client.setInternalCache(responseCache != null ? new CacheAdapter(responseCache) : null);
return this;
}

ResponseCache getResponseCache() {
InternalCache cache = client.internalCache();
return cache instanceof CacheAdapter ? ((CacheAdapter) cache).getDelegate() : null;
}

/**
* Returns a copy of this stream handler factory that includes a shallow copy
* of the internal {@linkplain OkHttpClient HTTP client}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public final class UrlConnectionCacheTest {

@Test public void responseCacheAccessWithOkHttpMember() throws IOException {
assertSame(cache, client.client().getCache());
assertNull(client.getResponseCache());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

package com.squareup.okhttp.internal.huc;

import com.squareup.okhttp.AbstractResponseCache;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.InternalCache;
import com.squareup.okhttp.internal.http.CacheRequest;
import com.squareup.okhttp.internal.http.CacheStrategy;

import java.io.IOException;
import java.net.CacheResponse;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -124,13 +129,38 @@ private URI backdoorUrlToUri(URL url) throws Exception {
final AtomicReference<URI> uriReference = new AtomicReference<>();

OkHttpClient client = new OkHttpClient();
Internal.instance.setCache(client, new CacheAdapter(new AbstractResponseCache() {
@Override public CacheResponse get(URI uri, String requestMethod,
Map<String, List<String>> requestHeaders) throws IOException {
uriReference.set(uri);
Internal.instance.setCache(client, new InternalCache() {
@Override
public Response get(Request request) throws IOException {
uriReference.set(request.uri());
throw new UnsupportedOperationException();
}
}));

@Override
public CacheRequest put(Response response) throws IOException {
return null;
}

@Override
public void remove(Request request) throws IOException {

}

@Override
public void update(Response cached, Response network) throws IOException {

}

@Override
public void trackConditionalCacheHit() {

}

@Override
public void trackResponse(CacheStrategy cacheStrategy) {

}
});

try {
HttpURLConnection connection = new OkUrlFactory(client).open(url);
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<module>okhttp-apache</module>
<module>okhttp-tests</module>
<module>okhttp-urlconnection</module>
<module>okhttp-android-support</module>
<module>okcurl</module>
<module>mockwebserver</module>
<module>samples</module>
Expand Down

0 comments on commit 2f50252

Please sign in to comment.