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