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.huc.HttpURLConnectionImpl;
19import com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl;
20
21import java.net.HttpURLConnection;
22import java.net.Proxy;
23import java.net.URL;
24import java.net.URLConnection;
25import java.net.URLStreamHandler;
26import java.net.URLStreamHandlerFactory;
27
28public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
29  private final OkHttpClient client;
30
31  public OkUrlFactory(OkHttpClient client) {
32    this.client = client;
33  }
34
35  public OkHttpClient client() {
36    return client;
37  }
38
39  /**
40   * Returns a copy of this stream handler factory that includes a shallow copy
41   * of the internal {@linkplain OkHttpClient HTTP client}.
42   */
43  @Override public OkUrlFactory clone() {
44    return new OkUrlFactory(client.clone());
45  }
46
47  public HttpURLConnection open(URL url) {
48    return open(url, client.getProxy());
49  }
50
51  HttpURLConnection open(URL url, Proxy proxy) {
52    String protocol = url.getProtocol();
53    OkHttpClient copy = client.copyWithDefaults();
54    copy.setProxy(proxy);
55
56    if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy);
57    if (protocol.equals("https")) return new HttpsURLConnectionImpl(url, copy);
58    throw new IllegalArgumentException("Unexpected protocol: " + protocol);
59  }
60
61  /**
62   * Creates a URLStreamHandler as a {@link java.net.URL#setURLStreamHandlerFactory}.
63   *
64   * <p>This code configures OkHttp to handle all HTTP and HTTPS connections
65   * created with {@link java.net.URL#openConnection()}: <pre>   {@code
66   *
67   *   OkHttpClient okHttpClient = new OkHttpClient();
68   *   URL.setURLStreamHandlerFactory(new OkUrlFactory(okHttpClient));
69   * }</pre>
70   */
71  @Override public URLStreamHandler createURLStreamHandler(final String protocol) {
72    if (!protocol.equals("http") && !protocol.equals("https")) return null;
73
74    return new URLStreamHandler() {
75      @Override protected URLConnection openConnection(URL url) {
76        return open(url);
77      }
78
79      @Override protected URLConnection openConnection(URL url, Proxy proxy) {
80        return open(url, proxy);
81      }
82
83      @Override protected int getDefaultPort() {
84        if (protocol.equals("http")) return 80;
85        if (protocol.equals("https")) return 443;
86        throw new AssertionError();
87      }
88    };
89  }
90}
91