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 String contentType;
023
024  public RequestInfo(
025      String method, String url, Map<String, String> queryParams, Map<String, String> headers) {
026    this.method = method;
027    this.url = url;
028    this.queryParams = queryParams;
029    this.headers = headers;
030  }
031
032  protected RequestInfo(Builder builder) {
033    this.method = builder.method;
034    this.url = builder.url;
035    this.queryParams = builder.queryParams;
036    this.headers = builder.headers;
037    this.body = builder.body;
038    this.contentType = builder.contentType;
039  }
040
041  public String getMethod() {
042    return method;
043  }
044
045  public String getUrl() {
046    return url;
047  }
048
049  public Map<String, String> getQueryParams() {
050    return queryParams;
051  }
052
053  public Map<String, String> getHeaders() {
054    return headers;
055  }
056
057  public String getBody() {
058    return body;
059  }
060
061  public String getContentType() {
062    return contentType;
063  }
064
065  public static class Builder {
066
067    protected final String method;
068
069    protected final String url;
070
071    protected final Map<String, String> queryParams;
072
073    protected final Map<String, String> headers;
074
075    protected String body;
076
077    protected String contentType;
078
079    public Builder(
080        String method, String url, Map<String, String> queryParams, Map<String, String> headers) {
081      this.method = method;
082      this.url = url;
083      this.queryParams = queryParams;
084      this.headers = headers;
085    }
086
087    public Builder body(String body) {
088      this.body = body;
089      return this;
090    }
091
092    public Builder contentType(String contentType) {
093      this.contentType = contentType;
094      return this;
095    }
096
097    public RequestInfo build() {
098      return new RequestInfo(this);
099    }
100  }
101
102  public static RequestInfo fromRequest(Request request) {
103    return fromRequest(request, null);
104  }
105
106  public static RequestInfo fromRequest(Request request, String contentType) {
107    Builder requestInfoBuilder =
108        new Builder(
109                request.method(),
110                request.url().toString(),
111                request.url().queryParameterNames().stream()
112                    .collect(
113                        Collectors.toMap(name -> name, name -> request.url().queryParameter(name))),
114                request.headers().toMultimap().entrySet().stream()
115                    .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0))))
116            .body(getRequestBodyAsString(request.body()))
117            .contentType(contentType);
118
119    return new RequestInfo(requestInfoBuilder);
120  }
121
122  private static String getRequestBodyAsString(RequestBody requestBody) {
123    try {
124      if (requestBody != null) {
125        Buffer buffer = new Buffer();
126        requestBody.writeTo(buffer);
127        return buffer.readUtf8();
128      }
129    } catch (Exception e) {
130      return null;
131    }
132    return null;
133  }
134
135  String print(DataSanitizer dataSanitizer) {
136    Map<String, String> sanitizedHeaders =
137        dataSanitizer == null ? headers : dataSanitizer.sanitizeHeaders(headers);
138    String sanitizedBody;
139    if (dataSanitizer == null || body == null) {
140      sanitizedBody = body;
141    } else {
142      sanitizedBody = dataSanitizer.sanitizeStringBody(body, contentType);
143    }
144    return "RequestInfo{"
145        + "\n\tmethod='"
146        + method
147        + '\''
148        + ", \n\turl='"
149        + url
150        + '\''
151        + ", \n\tqueryParams="
152        + queryParams
153        + ", \n\theaders="
154        + sanitizedHeaders
155        + ", \n\tbody='"
156        + sanitizedBody
157        + '\''
158        + '}';
159  }
160}