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