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 * Builder of {@link HttpResponse}. 49 */ 50 @AutoValue.Builder 51 public abstract static class Builder { build()52 public abstract HttpResponse build(); 53 setContentType(int contentType)54 public abstract Builder setContentType(int contentType); 55 setBody(String body)56 public abstract Builder setBody(String body); 57 setResponseCode(int responseCode)58 public abstract Builder setResponseCode(int responseCode); 59 setResponseMessage(String responseMessage)60 public abstract Builder setResponseMessage(String responseMessage); 61 62 /** 63 * Sets the content of the "Set-Cookie" response headers. 64 */ setCookies(List<String> cookies)65 public abstract Builder setCookies(List<String> cookies); 66 } 67 builder()68 public static Builder builder() { 69 return new AutoValue_HttpResponse.Builder() 70 .setContentType(ContentType.UNKNOWN) 71 .setBody("") 72 .setResponseCode(0) 73 .setResponseMessage("") 74 .setCookies(ImmutableList.of()); 75 } 76 77 /** 78 * Returns a short string representation for debugging purposes. Doesn't include the cookie or 79 * full body to prevent leaking sensitive data. 80 */ toShortDebugString()81 public String toShortDebugString() { 82 return new StringBuilder("HttpResponse{") 83 .append("contentType=") 84 .append(contentType()) 85 .append(" body=(") 86 .append(body().length()) 87 .append(" characters)") 88 .append(" responseCode=") 89 .append(responseCode()) 90 .append(" responseMessage=") 91 .append(responseMessage()) 92 .append(" cookies=[") 93 .append(cookies().size()) 94 .append(" cookies]}") 95 .toString(); 96 } 97 } 98