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

box / box-java-sdk / #5076

07 Oct 2025 12:35PM UTC coverage: 37.132% (+0.007%) from 37.125%
#5076

push

github

web-flow
test: Change `Event.additionalDetails` field assertion in events test (box/box-codegen#858) (#1491)

18454 of 49699 relevant lines covered (37.13%)

0.37 hits per line

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

82.47
/src/main/java/com/box/sdkgen/managers/downloads/DownloadsManager.java
1
package com.box.sdkgen.managers.downloads;
2

3
import static com.box.sdkgen.internal.utils.UtilsManager.convertToString;
4
import static com.box.sdkgen.internal.utils.UtilsManager.entryOf;
5
import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
6
import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps;
7
import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
8
import static com.box.sdkgen.internal.utils.UtilsManager.writeInputStreamToOutputStream;
9

10
import com.box.sdkgen.box.errors.BoxSDKError;
11
import com.box.sdkgen.networking.auth.Authentication;
12
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
13
import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
14
import com.box.sdkgen.networking.fetchresponse.FetchResponse;
15
import com.box.sdkgen.networking.network.NetworkSession;
16
import java.io.InputStream;
17
import java.io.OutputStream;
18
import java.util.Map;
19

20
public class DownloadsManager {
21

22
  public Authentication auth;
23

24
  public NetworkSession networkSession;
25

26
  public DownloadsManager() {
×
27
    this.networkSession = new NetworkSession();
×
28
  }
×
29

30
  protected DownloadsManager(Builder builder) {
1✔
31
    this.auth = builder.auth;
1✔
32
    this.networkSession = builder.networkSession;
1✔
33
  }
1✔
34

35
  /**
36
   * Returns the contents of a file in binary format.
37
   *
38
   * @param fileId The unique identifier that represents a file.
39
   *     <p>The ID for any file can be determined by visiting a file in the web application and
40
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
41
   *     `file_id` is `123`. Example: "12345"
42
   */
43
  public String getDownloadFileUrl(String fileId) {
44
    return getDownloadFileUrl(
1✔
45
        fileId, new GetDownloadFileUrlQueryParams(), new GetDownloadFileUrlHeaders());
46
  }
47

48
  /**
49
   * Returns the contents of a file in binary format.
50
   *
51
   * @param fileId The unique identifier that represents a file.
52
   *     <p>The ID for any file can be determined by visiting a file in the web application and
53
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
54
   *     `file_id` is `123`. Example: "12345"
55
   * @param queryParams Query parameters of downloadFile method
56
   */
57
  public String getDownloadFileUrl(String fileId, GetDownloadFileUrlQueryParams queryParams) {
58
    return getDownloadFileUrl(fileId, queryParams, new GetDownloadFileUrlHeaders());
×
59
  }
60

61
  /**
62
   * Returns the contents of a file in binary format.
63
   *
64
   * @param fileId The unique identifier that represents a file.
65
   *     <p>The ID for any file can be determined by visiting a file in the web application and
66
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
67
   *     `file_id` is `123`. Example: "12345"
68
   * @param headers Headers of downloadFile method
69
   */
70
  public String getDownloadFileUrl(String fileId, GetDownloadFileUrlHeaders headers) {
71
    return getDownloadFileUrl(fileId, new GetDownloadFileUrlQueryParams(), headers);
×
72
  }
73

74
  /**
75
   * Returns the contents of a file in binary format.
76
   *
77
   * @param fileId The unique identifier that represents a file.
78
   *     <p>The ID for any file can be determined by visiting a file in the web application and
79
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
80
   *     `file_id` is `123`. Example: "12345"
81
   * @param queryParams Query parameters of downloadFile method
82
   * @param headers Headers of downloadFile method
83
   */
84
  public String getDownloadFileUrl(
85
      String fileId, GetDownloadFileUrlQueryParams queryParams, GetDownloadFileUrlHeaders headers) {
86
    Map<String, String> queryParamsMap =
1✔
87
        prepareParams(
1✔
88
            mapOf(
1✔
89
                entryOf("version", convertToString(queryParams.getVersion())),
1✔
90
                entryOf("access_token", convertToString(queryParams.getAccessToken()))));
1✔
91
    Map<String, String> headersMap =
1✔
92
        prepareParams(
1✔
93
            mergeMaps(
1✔
94
                mapOf(
1✔
95
                    entryOf("range", convertToString(headers.getRange())),
1✔
96
                    entryOf("boxapi", convertToString(headers.getBoxapi()))),
1✔
97
                headers.getExtraHeaders()));
1✔
98
    FetchResponse response =
1✔
99
        this.networkSession
100
            .getNetworkClient()
1✔
101
            .fetch(
1✔
102
                new FetchOptions.Builder(
103
                        String.join(
1✔
104
                            "",
105
                            this.networkSession.getBaseUrls().getBaseUrl(),
1✔
106
                            "/2.0/files/",
107
                            convertToString(fileId),
1✔
108
                            "/content"),
109
                        "GET")
110
                    .params(queryParamsMap)
1✔
111
                    .headers(headersMap)
1✔
112
                    .responseFormat(ResponseFormat.NO_CONTENT)
1✔
113
                    .auth(this.auth)
1✔
114
                    .networkSession(this.networkSession)
1✔
115
                    .followRedirects(false)
1✔
116
                    .build());
1✔
117
    if (response.getHeaders().containsKey("location")) {
1✔
118
      return response.getHeaders().get("location");
1✔
119
    }
120
    if (response.getHeaders().containsKey("Location")) {
×
121
      return response.getHeaders().get("Location");
×
122
    }
123
    throw new BoxSDKError("No location header in response");
×
124
  }
125

126
  /**
127
   * Returns the contents of a file in binary format.
128
   *
129
   * @param fileId The unique identifier that represents a file.
130
   *     <p>The ID for any file can be determined by visiting a file in the web application and
131
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
132
   *     `file_id` is `123`. Example: "12345"
133
   */
134
  public InputStream downloadFile(String fileId) {
135
    return downloadFile(fileId, new DownloadFileQueryParams(), new DownloadFileHeaders());
1✔
136
  }
137

138
  /**
139
   * Returns the contents of a file in binary format.
140
   *
141
   * @param fileId The unique identifier that represents a file.
142
   *     <p>The ID for any file can be determined by visiting a file in the web application and
143
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
144
   *     `file_id` is `123`. Example: "12345"
145
   * @param queryParams Query parameters of downloadFile method
146
   */
147
  public InputStream downloadFile(String fileId, DownloadFileQueryParams queryParams) {
148
    return downloadFile(fileId, queryParams, new DownloadFileHeaders());
×
149
  }
150

151
  /**
152
   * Returns the contents of a file in binary format.
153
   *
154
   * @param fileId The unique identifier that represents a file.
155
   *     <p>The ID for any file can be determined by visiting a file in the web application and
156
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
157
   *     `file_id` is `123`. Example: "12345"
158
   * @param headers Headers of downloadFile method
159
   */
160
  public InputStream downloadFile(String fileId, DownloadFileHeaders headers) {
161
    return downloadFile(fileId, new DownloadFileQueryParams(), headers);
×
162
  }
163

164
  /**
165
   * Returns the contents of a file in binary format.
166
   *
167
   * @param fileId The unique identifier that represents a file.
168
   *     <p>The ID for any file can be determined by visiting a file in the web application and
169
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
170
   *     `file_id` is `123`. Example: "12345"
171
   * @param queryParams Query parameters of downloadFile method
172
   * @param headers Headers of downloadFile method
173
   */
174
  public InputStream downloadFile(
175
      String fileId, DownloadFileQueryParams queryParams, DownloadFileHeaders headers) {
176
    Map<String, String> queryParamsMap =
1✔
177
        prepareParams(
1✔
178
            mapOf(
1✔
179
                entryOf("version", convertToString(queryParams.getVersion())),
1✔
180
                entryOf("access_token", convertToString(queryParams.getAccessToken()))));
1✔
181
    Map<String, String> headersMap =
1✔
182
        prepareParams(
1✔
183
            mergeMaps(
1✔
184
                mapOf(
1✔
185
                    entryOf("range", convertToString(headers.getRange())),
1✔
186
                    entryOf("boxapi", convertToString(headers.getBoxapi()))),
1✔
187
                headers.getExtraHeaders()));
1✔
188
    FetchResponse response =
1✔
189
        this.networkSession
190
            .getNetworkClient()
1✔
191
            .fetch(
1✔
192
                new FetchOptions.Builder(
193
                        String.join(
1✔
194
                            "",
195
                            this.networkSession.getBaseUrls().getBaseUrl(),
1✔
196
                            "/2.0/files/",
197
                            convertToString(fileId),
1✔
198
                            "/content"),
199
                        "GET")
200
                    .params(queryParamsMap)
1✔
201
                    .headers(headersMap)
1✔
202
                    .responseFormat(ResponseFormat.BINARY)
1✔
203
                    .auth(this.auth)
1✔
204
                    .networkSession(this.networkSession)
1✔
205
                    .build());
1✔
206
    if (convertToString(response.getStatus()).equals("202")) {
1✔
207
      return null;
×
208
    }
209
    return response.getContent();
1✔
210
  }
211

212
  public void downloadFileToOutputStream(String fileId, OutputStream outputStream) {
213
    downloadFileToOutputStream(
1✔
214
        fileId,
215
        outputStream,
216
        new DownloadFileToOutputStreamQueryParams(),
217
        new DownloadFileToOutputStreamHeaders());
218
  }
1✔
219

220
  public void downloadFileToOutputStream(
221
      String fileId, OutputStream outputStream, DownloadFileToOutputStreamQueryParams queryParams) {
222
    downloadFileToOutputStream(
×
223
        fileId, outputStream, queryParams, new DownloadFileToOutputStreamHeaders());
224
  }
×
225

226
  public void downloadFileToOutputStream(
227
      String fileId, OutputStream outputStream, DownloadFileToOutputStreamHeaders headers) {
228
    downloadFileToOutputStream(
×
229
        fileId, outputStream, new DownloadFileToOutputStreamQueryParams(), headers);
230
  }
×
231

232
  public void downloadFileToOutputStream(
233
      String fileId,
234
      OutputStream outputStream,
235
      DownloadFileToOutputStreamQueryParams queryParams,
236
      DownloadFileToOutputStreamHeaders headers) {
237
    InputStream downloadStream =
1✔
238
        this.downloadFile(
1✔
239
            fileId,
240
            new DownloadFileQueryParams.Builder()
241
                .version(queryParams.getVersion())
1✔
242
                .accessToken(queryParams.getAccessToken())
1✔
243
                .build(),
1✔
244
            new DownloadFileHeaders.Builder()
245
                .range(headers.getRange())
1✔
246
                .boxapi(headers.getBoxapi())
1✔
247
                .extraHeaders(headers.getExtraHeaders())
1✔
248
                .build());
1✔
249
    writeInputStreamToOutputStream(downloadStream, outputStream);
1✔
250
  }
1✔
251

252
  public Authentication getAuth() {
253
    return auth;
×
254
  }
255

256
  public NetworkSession getNetworkSession() {
257
    return networkSession;
×
258
  }
259

260
  public static class Builder {
261

262
    protected Authentication auth;
263

264
    protected NetworkSession networkSession;
265

266
    public Builder() {
1✔
267
      this.networkSession = new NetworkSession();
1✔
268
    }
1✔
269

270
    public Builder auth(Authentication auth) {
271
      this.auth = auth;
1✔
272
      return this;
1✔
273
    }
274

275
    public Builder networkSession(NetworkSession networkSession) {
276
      this.networkSession = networkSession;
1✔
277
      return this;
1✔
278
    }
279

280
    public DownloadsManager build() {
281
      return new DownloadsManager(this);
1✔
282
    }
283
  }
284
}
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

© 2026 Coveralls, Inc