001package com.box.sdkgen.box.errors; 002 003import com.box.sdkgen.internal.logging.DataSanitizer; 004import com.box.sdkgen.networking.fetchresponse.FetchResponse; 005import com.fasterxml.jackson.databind.JsonNode; 006import java.util.Map; 007 008public class ResponseInfo { 009 010 public final int statusCode; 011 012 public final Map<String, String> headers; 013 014 public JsonNode body; 015 016 public String rawBody; 017 018 public String code; 019 020 public JsonNode contextInfo; 021 022 public String requestId; 023 024 public String helpUrl; 025 026 public ResponseInfo(int statusCode, Map<String, String> headers) { 027 this.statusCode = statusCode; 028 this.headers = headers; 029 } 030 031 protected ResponseInfo(Builder builder) { 032 this.statusCode = builder.statusCode; 033 this.headers = builder.headers; 034 this.body = builder.body; 035 this.rawBody = builder.rawBody; 036 this.code = builder.code; 037 this.contextInfo = builder.contextInfo; 038 this.requestId = builder.requestId; 039 this.helpUrl = builder.helpUrl; 040 } 041 042 public int getStatusCode() { 043 return statusCode; 044 } 045 046 public Map<String, String> getHeaders() { 047 return headers; 048 } 049 050 public JsonNode getBody() { 051 return body; 052 } 053 054 public String getRawBody() { 055 return rawBody; 056 } 057 058 public String getCode() { 059 return code; 060 } 061 062 public JsonNode getContextInfo() { 063 return contextInfo; 064 } 065 066 public String getRequestId() { 067 return requestId; 068 } 069 070 public String getHelpUrl() { 071 return helpUrl; 072 } 073 074 public static class Builder { 075 076 protected final int statusCode; 077 078 protected final Map<String, String> headers; 079 080 protected JsonNode body; 081 082 protected String rawBody; 083 084 protected String code; 085 086 protected JsonNode contextInfo; 087 088 protected String requestId; 089 090 protected String helpUrl; 091 092 public Builder(int statusCode, Map<String, String> headers) { 093 this.statusCode = statusCode; 094 this.headers = headers; 095 } 096 097 public Builder body(JsonNode body) { 098 this.body = body; 099 return this; 100 } 101 102 public Builder rawBody(String rawBody) { 103 this.rawBody = rawBody; 104 return this; 105 } 106 107 public Builder code(String code) { 108 this.code = code; 109 return this; 110 } 111 112 public Builder contextInfo(JsonNode contextInfo) { 113 this.contextInfo = contextInfo; 114 return this; 115 } 116 117 public Builder requestId(String requestId) { 118 this.requestId = requestId; 119 return this; 120 } 121 122 public Builder helpUrl(String helpUrl) { 123 this.helpUrl = helpUrl; 124 return this; 125 } 126 127 public ResponseInfo build() { 128 return new ResponseInfo(this); 129 } 130 } 131 132 public static ResponseInfo fromResponse(FetchResponse fetchResponse, String rawResponseBody) { 133 Builder builder = new Builder(fetchResponse.getStatus(), fetchResponse.getHeaders()); 134 JsonNode body = fetchResponse.getData(); 135 if (body == null) { 136 builder.rawBody(rawResponseBody); 137 } else { 138 builder 139 .body(body) 140 .rawBody(body.toString()) 141 .code(body.get("code") != null ? body.get("code").asText("") : null) 142 .contextInfo(body.get("context_info") != null ? body.get("context_info") : null) 143 .requestId(body.get("request_id") != null ? body.get("request_id").asText() : null) 144 .helpUrl(body.get("help_url") != null ? body.get("help_url").asText() : null); 145 } 146 147 return builder.build(); 148 } 149 150 String print(DataSanitizer dataSanitizer) { 151 Map<String, String> sanitizedHeaders = 152 dataSanitizer == null ? headers : dataSanitizer.sanitizeHeaders(headers); 153 JsonNode sanitizedBody = dataSanitizer == null ? body : dataSanitizer.sanitizeBody(body); 154 return "ResponseInfo{" 155 + "\n\tstatusCode=" 156 + statusCode 157 + ", \n\theaders=" 158 + sanitizedHeaders 159 + ", \n\tbody=" 160 + sanitizedBody 161 + ", \n\tcode='" 162 + code 163 + '\'' 164 + ", \n\tcontextInfo=" 165 + contextInfo 166 + ", \n\trequestId='" 167 + requestId 168 + '\'' 169 + ", \n\thelpUrl='" 170 + helpUrl 171 + '\'' 172 + '}'; 173 } 174}