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 */
16package com.squareup.okhttp;
17
18import com.squareup.okhttp.internal.URLFilter;
19import com.squareup.okhttp.internal.huc.HttpURLConnectionImpl;
20import com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl;
21
22import java.net.HttpURLConnection;
23import java.net.Proxy;
24import java.net.URL;
25import java.net.URLConnection;
26import java.net.URLStreamHandler;
27import java.net.URLStreamHandlerFactory;
28
29public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
30  private final OkHttpClient client;
31  private URLFilter urlFilter;
32
33  public OkUrlFactory(OkHttpClient client) {
34    this.client = client;
35  }
36
37  public OkHttpClient client() {
38    return client;
39  }
40
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   */
49  @Override public OkUrlFactory clone() {
50    return new OkUrlFactory(client.clone());
51  }
52
53  public HttpURLConnection open(URL url) {
54    return open(url, client.getProxy());
55  }
56
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   */
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