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