1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-25 16:01:38 +03:00

Merge pull request #213 from square/jwilson/URLStreamHandlerFactory

Tweak the API for URLStreamHandlerFactory
This commit is contained in:
Marcelo Cortes
2013-06-15 08:34:48 -07:00
2 changed files with 35 additions and 79 deletions

View File

@@ -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<String> 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}.
*
* <p>This code configures OkHttp to handle all HTTP and HTTPS connections
* created with {@link URL#openConnection()}: <pre> {@code
*
* OkHttpClient okHttpClient = new OkHttpClient();
* URL.setURLStreamHandlerFactory(okHttpClient);
* }</pre>
*/
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();
}
};
}
}

View File

@@ -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:
* <pre> {@code
* OkHttpClient okHttpClient = new OkHttpClient();
* URL.setURLStreamHandlerFactory(new OkHttpURLStreamHandlerFactory(okHttpClient));
* }</pre>
*
*/
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;
}
}