• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.libraries.entitlement.http;
18 
19 import android.content.res.Resources;
20 import android.net.Network;
21 
22 import androidx.annotation.Nullable;
23 
24 import com.android.libraries.entitlement.utils.UrlConnectionFactory;
25 import com.android.libraries.entitlement.CarrierConfig;
26 
27 import com.google.auto.value.AutoValue;
28 import com.google.common.collect.ImmutableListMultimap;
29 import com.google.common.net.HttpHeaders;
30 import com.google.errorprone.annotations.CanIgnoreReturnValue;
31 
32 import org.json.JSONObject;
33 
34 import java.util.List;
35 
36 /** The parameters of an http request. */
37 @AutoValue
38 public abstract class HttpRequest {
39     /** The URL. */
url()40     public abstract String url();
41 
42     /** The HTTP request method, like "GET" or "POST". */
requestMethod()43     public abstract String requestMethod();
44 
45     /** For "POST" request method, the body of the request in JSON format. */
postData()46     public abstract JSONObject postData();
47 
48     /** HTTP header fields. */
requestProperties()49     public abstract ImmutableListMultimap<String, String> requestProperties();
50 
51     /** The client side timeout, in seconds. See {@link Builder#setTimeoutInSec}. */
timeoutInSec()52     public abstract int timeoutInSec();
53 
54     /** The network used for this HTTP connection. See {@link Builder#setNetwork}. */
55     @Nullable
network()56     public abstract Network network();
57 
58     /**
59      * The {@link UrlConnectionFactory} used for this HTTP connection.
60      * See {@link Builder#setUrlConnectionFactory}.
61      */
62     @Nullable
urlConnectionFactory()63     public abstract UrlConnectionFactory urlConnectionFactory();
64 
65     /** Builder of {@link HttpRequest}. */
66     @AutoValue.Builder
67     public abstract static class Builder {
build()68         public abstract HttpRequest build();
69 
70         /** Sets the URL. */
setUrl(String url)71         public abstract Builder setUrl(String url);
72 
73         /**
74          * Sets the HTTP request method, like "GET" or "POST".
75          *
76          * @see HttpConstants.RequestMethod
77          */
setRequestMethod(String requestMethod)78         public abstract Builder setRequestMethod(String requestMethod);
79 
80         /** For "POST" request method, sets the body of the request in JSON format. */
setPostData(JSONObject postData)81         public abstract Builder setPostData(JSONObject postData);
82 
requestPropertiesBuilder()83         abstract ImmutableListMultimap.Builder<String, String> requestPropertiesBuilder();
84 
85         /** Adds an HTTP header field. */
86         @CanIgnoreReturnValue
addRequestProperty(String key, String value)87         public Builder addRequestProperty(String key, String value) {
88             requestPropertiesBuilder().put(key, value);
89             return this;
90         }
91 
92         /**
93           * Adds an HTTP header field with multiple values. Equivalent to calling
94           * {@link #addRequestProperty(String, String)} multiple times with the same key and
95           * one value at a time.
96           */
97         @CanIgnoreReturnValue
addRequestProperty(String key, List<String> value)98         public Builder addRequestProperty(String key, List<String> value) {
99             requestPropertiesBuilder().putAll(key, value);
100             return this;
101         }
102 
103         /**
104          * Sets the client side timeout for HTTP connection. Default to
105          * {@link com.android.libraries.entitlement.CarrierConfig#DEFAULT_TIMEOUT_IN_SEC}.
106          *
107          * <p>This timeout is used by both {@link java.net.URLConnection#setConnectTimeout} and
108          * {@link java.net.URLConnection#setReadTimeout}.
109          */
setTimeoutInSec(int timeoutInSec)110         public abstract Builder setTimeoutInSec(int timeoutInSec);
111 
112         /**
113          * Sets the network used for this HTTP connection. If not set, the device default network
114          * is used.
115          */
setNetwork(@ullable Network network)116         public abstract Builder setNetwork(@Nullable Network network);
117 
118         /**
119          * If unset, the default Android API {@link java.net.URL#openConnection}
120          * would be used. This allows callers of the lib to choose the HTTP stack.
121          */
setUrlConnectionFactory( @ullable UrlConnectionFactory urlConnectionFactory)122         public abstract Builder setUrlConnectionFactory(
123                 @Nullable UrlConnectionFactory urlConnectionFactory);
124     }
125 
builder()126     public static Builder builder() {
127         return new AutoValue_HttpRequest.Builder()
128                 .setUrl("")
129                 .setRequestMethod("")
130                 .setPostData(new JSONObject())
131                 .setTimeoutInSec(CarrierConfig.DEFAULT_TIMEOUT_IN_SEC)
132                 .addRequestProperty(
133                         HttpHeaders.ACCEPT_LANGUAGE,
134                         Resources.getSystem()
135                                 .getConfiguration()
136                                 .getLocales()
137                                 .get(0)
138                                 .toLanguageTag());
139     }
140 }
141