001package com.box.sdkgen.box.errors; 002 003import com.box.sdkgen.internal.logging.DataSanitizer; 004import java.util.Map; 005import java.util.stream.Collectors; 006import okhttp3.Request; 007import okhttp3.RequestBody; 008import okio.Buffer; 009 010public class RequestInfo { 011 012 public final String method; 013 014 public final String url; 015 016 public final Map<String, String> queryParams; 017 018 public final Map<String, String> headers; 019 020 public String body; 021 022 public RequestInfo( 023 String method, String url, Map<String, String> queryParams, Map<String, String> headers) { 024 this.method = method; 025 this.url = url; 026 this.queryParams = queryParams; 027 this.headers = headers; 028 } 029 030 protected RequestInfo(Builder builder) { 031 this.method = builder.method; 032 this.url = builder.url; 033 this.queryParams = builder.queryParams; 034 this.headers = builder.headers; 035 this.body = builder.body; 036 } 037 038 public String getMethod() { 039 return method; 040 } 041 042 public String getUrl() { 043 return url; 044 } 045 046 public Map<String, String> getQueryParams() { 047 return queryParams; 048 } 049 050 public Map<String, String> getHeaders() { 051 return headers; 052 } 053 054 public String getBody() { 055 return body; 056 } 057 058 public static class Builder { 059 060 protected final String method; 061 062 protected final String url; 063 064 protected final Map<String, String> queryParams; 065 066 protected final Map<String, String> headers; 067 068 protected String body; 069 070 public Builder( 071 String method, String url, Map<String, String> queryParams, Map<String, String> headers) { 072 this.method = method; 073 this.url = url; 074 this.queryParams = queryParams; 075 this.headers = headers; 076 } 077 078 public Builder body(String body) { 079 this.body = body; 080 return this; 081 } 082 083 public RequestInfo build() { 084 return new RequestInfo(this); 085 } 086 } 087 088 public static RequestInfo fromRequest(Request request) { 089 Builder requestInfoBuilder = 090 new Builder( 091 request.method(), 092 request.url().toString(), 093 request.url().queryParameterNames().stream() 094 .collect( 095 Collectors.toMap(name -> name, name -> request.url().queryParameter(name))), 096 request.headers().toMultimap().entrySet().stream() 097 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)))) 098 .body(getRequestBodyAsString(request.body())); 099 100 return new RequestInfo(requestInfoBuilder); 101 } 102 103 private static String getRequestBodyAsString(RequestBody requestBody) { 104 try { 105 if (requestBody != null) { 106 Buffer buffer = new Buffer(); 107 requestBody.writeTo(buffer); 108 return buffer.readUtf8(); 109 } 110 } catch (Exception e) { 111 return null; 112 } 113 return null; 114 } 115 116 String print(DataSanitizer dataSanitizer) { 117 Map<String, String> sanitizedHeaders = 118 dataSanitizer == null ? headers : dataSanitizer.sanitizeHeaders(headers); 119 return "RequestInfo{" 120 + "\n\tmethod='" 121 + method 122 + '\'' 123 + ", \n\turl='" 124 + url 125 + '\'' 126 + ", \n\tqueryParams=" 127 + queryParams 128 + ", \n\theaders=" 129 + sanitizedHeaders 130 + ", \n\tbody='" 131 + body 132 + '\'' 133 + '}'; 134 } 135}