1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 
17 package libcore.net.http;
18 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 
21 import android.annotation.SystemApi;
22 
23 import libcore.api.CorePlatformApi;
24 
25 import java.io.IOException;
26 import java.net.Proxy;
27 import java.net.URL;
28 import java.net.URLConnection;
29 import java.util.concurrent.TimeUnit;
30 
31 import javax.net.SocketFactory;
32 import libcore.util.NonNull;
33 
34 /**
35  * A HttpURLConnectionFactory that supports some configuration on a per-factory or per-connection
36  * basis. The per-factory configuration is <b>optional</b>; if not set, global configuration or
37  * default behavior is used.
38  *
39  * @hide
40  */
41 @SystemApi(client = MODULE_LIBRARIES)
42 @CorePlatformApi(status = CorePlatformApi.Status.STABLE)
43 public class HttpURLConnectionFactory {
44     private final com.android.okhttp.internalandroidapi.HttpURLConnectionFactory mFactory;
45 
46     /**
47      * Create a new {@link HttpURLConnectionFactory} instance.
48      *
49      * @hide
50      */
51     @SystemApi(client = MODULE_LIBRARIES)
52     @CorePlatformApi(status = CorePlatformApi.Status.STABLE)
createInstance()53     @NonNull public static HttpURLConnectionFactory createInstance() {
54         return new HttpURLConnectionFactory();
55     }
56 
HttpURLConnectionFactory()57     HttpURLConnectionFactory() {
58         mFactory = new com.android.okhttp.internalandroidapi.HttpURLConnectionFactory();
59     }
60 
61     /**
62      * Sets a new ConnectionPool, specific to this HttpURLConnectionFactory and for all future
63      * connections created by {@link #openConnection}, with the given configuration.
64      *
65      * @param maxIdleConnections The maximum number of idle connections to each to keep in the pool.
66      * @param keepAliveDuration Time to keep the connection alive in the pool before closing it.
67      * @param timeUnit The time unit of keep alive duration.
68      *
69      * @hide
70      */
71     @SystemApi(client = MODULE_LIBRARIES)
72     @CorePlatformApi(status = CorePlatformApi.Status.STABLE)
setNewConnectionPool(int maxIdleConnections, long keepAliveDuration, @NonNull TimeUnit timeUnit)73     public void setNewConnectionPool(int maxIdleConnections, long keepAliveDuration,
74             @NonNull TimeUnit timeUnit) {
75         mFactory.setNewConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
76     }
77 
78     /**
79      * Sets a new dns resolver.
80      *
81      * @param dns the dns resolver for looking up.
82      *
83      * @hide
84      */
85     @SystemApi(client = MODULE_LIBRARIES)
86     @CorePlatformApi(status = CorePlatformApi.Status.STABLE)
setDns(@onNull Dns dns)87     public void setDns(@NonNull Dns dns) {
88         mFactory.setDns(dns);
89     }
90 
91     /**
92      * Opens a connection using the specified SocketFactory and the specified proxy
93      * settings, overriding any system wide configuration.
94      *
95      * @param url The target URL that connection opens on.
96      * @param socketFactory The socket factory used to create connections.
97      * @param proxy The proxy settings used to create connections.
98      *
99      * @return An {@link java.net.URLConnection} using given SocketFactory, proxy settings and
100      *         configuration.
101      *
102      * @hide
103      */
104     @SystemApi(client = MODULE_LIBRARIES)
105     @CorePlatformApi(status = CorePlatformApi.Status.STABLE)
openConnection(@onNull URL url, @NonNull SocketFactory socketFactory, @NonNull Proxy proxy)106     public URLConnection openConnection(@NonNull URL url, @NonNull SocketFactory socketFactory,
107             @NonNull Proxy proxy) throws IOException {
108         return mFactory.openConnection(url, socketFactory, proxy);
109     }
110 }
111