• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / box-java-sdk / #4498

26 Feb 2025 11:16AM UTC coverage: 72.059% (+0.05%) from 72.007%
#4498

push

github

web-flow
fix: Improve logging for API Request and API Response (#1295)

41 of 43 new or added lines in 4 files covered. (95.35%)

2 existing lines in 2 files now uncovered.

8214 of 11399 relevant lines covered (72.06%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.0
/src/main/java/com/box/sdk/BoxJSONResponse.java
1
package com.box.sdk;
2

3
import com.box.sdk.http.ContentType;
4
import com.eclipsesource.json.Json;
5
import com.eclipsesource.json.JsonObject;
6
import com.eclipsesource.json.ParseException;
7
import java.io.IOException;
8
import java.io.InputStreamReader;
9
import java.util.List;
10
import java.util.Map;
11

12
/**
13
 * Used to read HTTP responses containing JSON from the Box API.
14
 *
15
 * <p>This request type extends BoxAPIResponse to provide additional functionality for handling JSON strings. It reads
16
 * the response body into a string and allows the JSON in the response to be logged.</p>
17
 */
18
public class BoxJSONResponse extends BoxAPIResponse {
19
    private static final int BUFFER_SIZE = 8192;
20
    private JsonObject jsonObject;
21

22
    /**
23
     * Constructs a BoxJSONResponse without an associated HttpURLConnection.
24
     */
25
    public BoxJSONResponse() {
26
        super();
1✔
27
    }
1✔
28

29
    BoxJSONResponse(BoxAPIResponse response) {
30
        this(
1✔
31
            response.getResponseCode(),
1✔
32
            response.getRequestMethod(),
1✔
33
            response.getRequestUrl(),
1✔
34
            response.getHeaders(),
1✔
35
            new JsonObject()
36
        );
37
    }
1✔
38

39
    /**
40
     * Constructs a BoxAPIResponse with an http response code and response body.
41
     *
42
     * @param responseCode http response code
43
     * @param headers  map of http headers
44
     * @param body         response body as Json Object
45
     */
46
    public BoxJSONResponse(int responseCode,
47
                           String requestMethod,
48
                           String requestUrl,
49
                           Map<String, List<String>> headers,
50
                           JsonObject body
51
    ) {
52
        super(responseCode, requestMethod, requestUrl, headers, body.toString(), ContentType.APPLICATION_JSON);
1✔
53
        this.jsonObject = body;
1✔
54
    }
1✔
55

56
    /**
57
     * Get response as Json Object.
58
     *
59
     * @return response as JsonObject
60
     */
61
    public JsonObject getJsonObject() {
62
        if (this.jsonObject != null) {
1✔
63
            return this.jsonObject;
1✔
64
        } else {
65
            return Json.parse(this.getJSON()).asObject();
×
66
        }
67
    }
68

69
    /**
70
     * Gets the body of the response as a JSON string. When this method is called, the response's body will be read and
71
     * the response will be disconnected, meaning that the stream returned by {@link #getBody} can no longer be used.
72
     *
73
     * @return the body of the response as a JSON string.
74
     */
75
    public String getJSON() {
76
        if (this.jsonObject != null) {
1✔
77
            return this.jsonObject.toString();
1✔
78
        } else if (this.getBody() == null) {
×
79
            return null;
×
80
        } else {
81
            InputStreamReader reader = new InputStreamReader(this.getBody(), StandardCharsets.UTF_8);
×
82
            StringBuilder builder = new StringBuilder();
×
83
            char[] buffer = new char[BUFFER_SIZE];
×
84

85
            try {
86
                int read = reader.read(buffer, 0, BUFFER_SIZE);
×
87
                while (read != -1) {
×
88
                    builder.append(buffer, 0, read);
×
89
                    read = reader.read(buffer, 0, BUFFER_SIZE);
×
90
                }
91

92
                this.close();
×
93
                reader.close();
×
94
            } catch (IOException e) {
×
95
                throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
×
96
            }
×
97
            String jsonAsString = builder.toString();
×
98
            try {
99
                this.jsonObject = Json.parse(jsonAsString).asObject();
×
100
            } catch (ParseException e) {
×
101
                throw new RuntimeException("Error parsing JSON:\n" + jsonAsString, e);
×
102
            }
×
103
            return jsonAsString;
×
104
        }
105
    }
106

107
    @Override
108
    protected String bodyToString() {
109
        String bodyString = super.bodyToString();
1✔
110
        if (bodyString == null) {
1✔
UNCOV
111
            return this.getJSON();
×
112
        } else {
113
            return bodyString;
1✔
114
        }
115
    }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc