From 07a97dc250bcb7402246f8bd48ac6d7806ef99e3 Mon Sep 17 00:00:00 2001 From: jwilson Date: Sat, 15 Jun 2013 09:36:04 -0400 Subject: [PATCH] Tweak the API for URLStreamHandlerFactory This follows up on candrews committed change. --- .../com/squareup/okhttp/OkHttpClient.java | 36 ++++++++- .../okhttp/OkHttpURLStreamHandlerFactory.java | 78 ------------------- 2 files changed, 35 insertions(+), 79 deletions(-) delete mode 100644 okhttp/src/main/java/com/squareup/okhttp/OkHttpURLStreamHandlerFactory.java diff --git a/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java b/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java index f347d653c..f466c4b01 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java +++ b/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java @@ -22,12 +22,16 @@ import com.squareup.okhttp.internal.http.HttpsURLConnectionImpl; import com.squareup.okhttp.internal.http.OkResponseCache; import com.squareup.okhttp.internal.http.OkResponseCacheAdapter; import com.squareup.okhttp.internal.tls.OkHostnameVerifier; +import java.io.IOException; import java.net.CookieHandler; import java.net.HttpURLConnection; import java.net.Proxy; import java.net.ProxySelector; import java.net.ResponseCache; import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; @@ -38,7 +42,7 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; /** Configures and creates HTTP connections. */ -public final class OkHttpClient { +public final class OkHttpClient implements URLStreamHandlerFactory { private static final List DEFAULT_TRANSPORTS = Util.immutableList(Arrays.asList("spdy/3", "http/1.1")); @@ -306,4 +310,34 @@ public final class OkHttpClient { result.transports = transports != null ? transports : DEFAULT_TRANSPORTS; return result; } + + /** + * Creates a URLStreamHandler as a {@link URL#setURLStreamHandlerFactory}. + * + *

This code configures OkHttp to handle all HTTP and HTTPS connections + * created with {@link URL#openConnection()}:

   {@code
+   *
+   *   OkHttpClient okHttpClient = new OkHttpClient();
+   *   URL.setURLStreamHandlerFactory(okHttpClient);
+   * }
+ */ + public URLStreamHandler createURLStreamHandler(final String protocol) { + if (!protocol.equals("http") && !protocol.equals("https")) return null; + + return new URLStreamHandler() { + @Override protected URLConnection openConnection(URL url) { + return open(url); + } + + @Override protected URLConnection openConnection(URL url, Proxy proxy) { + return open(url, proxy); + } + + @Override protected int getDefaultPort() { + if (protocol.equals("http")) return 80; + if (protocol.equals("https")) return 443; + throw new AssertionError(); + } + }; + } } diff --git a/okhttp/src/main/java/com/squareup/okhttp/OkHttpURLStreamHandlerFactory.java b/okhttp/src/main/java/com/squareup/okhttp/OkHttpURLStreamHandlerFactory.java deleted file mode 100644 index 309cf8e41..000000000 --- a/okhttp/src/main/java/com/squareup/okhttp/OkHttpURLStreamHandlerFactory.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2013 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 java.io.IOException; -import java.net.Proxy; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLStreamHandler; -import java.net.URLStreamHandlerFactory; - -/** - * Provides a URLStreamHandlerFactory implementation for use with - * {@link java.net.URL#setURLStreamHandlerFactory}. - * - * Using this factory will ensure that all HTTP/HTTPS connections opened using {@link java.net.URL}, - * such as {@link java.net.URL#openConnection()}, will be handled by the given {@link OkHttpClient}. - * - * Example of how to use this factory: - *
   {@code
- *     OkHttpClient okHttpClient = new OkHttpClient();
- *     URL.setURLStreamHandlerFactory(new OkHttpURLStreamHandlerFactory(okHttpClient));
- * }
- * - */ -public class OkHttpURLStreamHandlerFactory implements URLStreamHandlerFactory { - - protected static class OkHttpClientHandler extends URLStreamHandler { - - private final OkHttpClient okHttpClient; - private final int defaultPort; - - public OkHttpClientHandler(OkHttpClient okHttpClient, int defaultPort) { - this.okHttpClient = okHttpClient; - this.defaultPort = defaultPort; - } - - @Override protected URLConnection openConnection(URL url) throws IOException { - return okHttpClient.open(url); - } - - @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException { - return okHttpClient.open(url, proxy); - } - - @Override protected int getDefaultPort() { - return defaultPort; - } - } - - private final OkHttpClient okHttpClient; - - public OkHttpURLStreamHandlerFactory(OkHttpClient okHttpClient) { - this.okHttpClient = okHttpClient; - } - - public URLStreamHandler createURLStreamHandler(String protocol) { - if (protocol.equals("http")) { - return new OkHttpClientHandler(okHttpClient, 80); - } else if (protocol.equals("https")) { - return new OkHttpClientHandler(okHttpClient, 443); - } - return null; - } -}