1 /*
2  * Copyright (C) 2014 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.squareup.okhttp;
17 
18 import com.squareup.okhttp.internal.URLFilter;
19 import com.squareup.okhttp.internal.huc.HttpURLConnectionImpl;
20 import com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl;
21 
22 import java.net.HttpURLConnection;
23 import java.net.Proxy;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import java.net.URLStreamHandler;
27 import java.net.URLStreamHandlerFactory;
28 
29 public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
30   private final OkHttpClient client;
31   private URLFilter urlFilter;
32 
OkUrlFactory(OkHttpClient client)33   public OkUrlFactory(OkHttpClient client) {
34     this.client = client;
35   }
36 
client()37   public OkHttpClient client() {
38     return client;
39   }
40 
setUrlFilter(URLFilter filter)41   void setUrlFilter(URLFilter filter) {
42     urlFilter = filter;
43   }
44 
45   /**
46    * Returns a copy of this stream handler factory that includes a shallow copy
47    * of the internal {@linkplain OkHttpClient HTTP client}.
48    */
clone()49   @Override public OkUrlFactory clone() {
50     return new OkUrlFactory(client.clone());
51   }
52 
open(URL url)53   public HttpURLConnection open(URL url) {
54     return open(url, client.getProxy());
55   }
56 
open(URL url, Proxy proxy)57   HttpURLConnection open(URL url, Proxy proxy) {
58     String protocol = url.getProtocol();
59     OkHttpClient copy = client.copyWithDefaults();
60     copy.setProxy(proxy);
61 
62     if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy, urlFilter);
63     if (protocol.equals("https")) return new HttpsURLConnectionImpl(url, copy, urlFilter);
64     throw new IllegalArgumentException("Unexpected protocol: " + protocol);
65   }
66 
67   /**
68    * Creates a URLStreamHandler as a {@link java.net.URL#setURLStreamHandlerFactory}.
69    *
70    * <p>This code configures OkHttp to handle all HTTP and HTTPS connections
71    * created with {@link java.net.URL#openConnection()}: <pre>   {@code
72    *
73    *   OkHttpClient okHttpClient = new OkHttpClient();
74    *   URL.setURLStreamHandlerFactory(new OkUrlFactory(okHttpClient));
75    * }</pre>
76    */
createURLStreamHandler(final String protocol)77   @Override public URLStreamHandler createURLStreamHandler(final String protocol) {
78     if (!protocol.equals("http") && !protocol.equals("https")) return null;
79 
80     return new URLStreamHandler() {
81       @Override protected URLConnection openConnection(URL url) {
82         return open(url);
83       }
84 
85       @Override protected URLConnection openConnection(URL url, Proxy proxy) {
86         return open(url, proxy);
87       }
88 
89       @Override protected int getDefaultPort() {
90         if (protocol.equals("http")) return 80;
91         if (protocol.equals("https")) return 443;
92         throw new AssertionError();
93       }
94     };
95   }
96 }
97