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 com.android.libraries.entitlement.http.HttpConstants.ContentType;
20 
21 import com.google.auto.value.AutoValue;
22 import com.google.common.collect.ImmutableList;
23 
24 import java.util.List;
25 
26 /**
27  * The response of the http request.
28  */
29 @AutoValue
30 public abstract class HttpResponse {
31     /**
32      * Content type of the response.
33      */
contentType()34     public abstract int contentType();
35 
body()36     public abstract String body();
37 
responseCode()38     public abstract int responseCode();
39 
responseMessage()40     public abstract String responseMessage();
41 
42     /**
43      * Content of the "Set-Cookie" response header.
44      */
cookies()45     public abstract ImmutableList<String> cookies();
46 
47     /**
48      * Content of the "Location" response header.
49      */
location()50     public abstract String location();
51 
52     /**
53      * Builder of {@link HttpResponse}.
54      */
55     @AutoValue.Builder
56     public abstract static class Builder {
build()57         public abstract HttpResponse build();
58 
setContentType(int contentType)59         public abstract Builder setContentType(int contentType);
60 
setBody(String body)61         public abstract Builder setBody(String body);
62 
setResponseCode(int responseCode)63         public abstract Builder setResponseCode(int responseCode);
64 
setResponseMessage(String responseMessage)65         public abstract Builder setResponseMessage(String responseMessage);
66 
67         /**
68          * Sets the content of the "Set-Cookie" response headers.
69          */
setCookies(List<String> cookies)70         public abstract Builder setCookies(List<String> cookies);
71 
72         /**
73          * Sets the content of the "Location" response header.
74          */
setLocation(String location)75         public abstract Builder setLocation(String location);
76     }
77 
builder()78     public static Builder builder() {
79         return new AutoValue_HttpResponse.Builder()
80                 .setContentType(ContentType.UNKNOWN)
81                 .setBody("")
82                 .setResponseCode(0)
83                 .setResponseMessage("")
84                 .setCookies(ImmutableList.of())
85                 .setLocation("");
86     }
87 
88     /**
89      * Returns a short string representation for debugging purposes. Doesn't include the cookie or
90      * full body to prevent leaking sensitive data.
91      */
toShortDebugString()92     public String toShortDebugString() {
93         return new StringBuilder("HttpResponse{")
94                 .append("contentType=")
95                 .append(contentType())
96                 .append(" body=(")
97                 .append(body().length())
98                 .append(" characters)")
99                 .append(" responseCode=")
100                 .append(responseCode())
101                 .append(" responseMessage=")
102                 .append(responseMessage())
103                 .append(" cookies=[")
104                 .append(cookies().size())
105                 .append(" cookies]")
106                 .append(" location=")
107                 .append(location())
108                 .toString();
109     }
110 }
111