• 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

90.14
/src/main/java/com/box/sdkgen/managers/uploads/UploadsManager.java
1
package com.box.sdkgen.managers.uploads;
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

9
import com.box.sdkgen.box.errors.BoxSDKError;
10
import com.box.sdkgen.networking.auth.Authentication;
11
import com.box.sdkgen.networking.fetchoptions.FetchOptions;
12
import com.box.sdkgen.networking.fetchoptions.MultipartItem;
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 com.box.sdkgen.schemas.files.Files;
17
import com.box.sdkgen.schemas.uploadurl.UploadUrl;
18
import com.box.sdkgen.serialization.json.JsonManager;
19
import java.util.Arrays;
20
import java.util.Map;
21

22
public class UploadsManager {
23

24
  public Authentication auth;
25

26
  public NetworkSession networkSession;
27

28
  public UploadsManager() {
×
29
    this.networkSession = new NetworkSession();
×
30
  }
×
31

32
  protected UploadsManager(Builder builder) {
1✔
33
    this.auth = builder.auth;
1✔
34
    this.networkSession = builder.networkSession;
1✔
35
  }
1✔
36

37
  /**
38
   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
39
   *
40
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
41
   * follow this format when uploading the file will receive a HTTP `400` error with a
42
   * `metadata_after_file_contents` error code.
43
   *
44
   * @param fileId The unique identifier that represents a file.
45
   *     <p>The ID for any file can be determined by visiting a file in the web application and
46
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
47
   *     `file_id` is `123`. Example: "12345"
48
   * @param requestBody Request body of uploadFileVersion method
49
   */
50
  public Files uploadFileVersion(String fileId, UploadFileVersionRequestBody requestBody) {
51
    return uploadFileVersion(
1✔
52
        fileId, requestBody, new UploadFileVersionQueryParams(), new UploadFileVersionHeaders());
53
  }
54

55
  /**
56
   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
57
   *
58
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
59
   * follow this format when uploading the file will receive a HTTP `400` error with a
60
   * `metadata_after_file_contents` error code.
61
   *
62
   * @param fileId The unique identifier that represents a file.
63
   *     <p>The ID for any file can be determined by visiting a file in the web application and
64
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
65
   *     `file_id` is `123`. Example: "12345"
66
   * @param requestBody Request body of uploadFileVersion method
67
   * @param queryParams Query parameters of uploadFileVersion method
68
   */
69
  public Files uploadFileVersion(
70
      String fileId,
71
      UploadFileVersionRequestBody requestBody,
72
      UploadFileVersionQueryParams queryParams) {
73
    return uploadFileVersion(fileId, requestBody, queryParams, new UploadFileVersionHeaders());
×
74
  }
75

76
  /**
77
   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
78
   *
79
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
80
   * follow this format when uploading the file will receive a HTTP `400` error with a
81
   * `metadata_after_file_contents` error code.
82
   *
83
   * @param fileId The unique identifier that represents a file.
84
   *     <p>The ID for any file can be determined by visiting a file in the web application and
85
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
86
   *     `file_id` is `123`. Example: "12345"
87
   * @param requestBody Request body of uploadFileVersion method
88
   * @param headers Headers of uploadFileVersion method
89
   */
90
  public Files uploadFileVersion(
91
      String fileId, UploadFileVersionRequestBody requestBody, UploadFileVersionHeaders headers) {
92
    return uploadFileVersion(fileId, requestBody, new UploadFileVersionQueryParams(), headers);
×
93
  }
94

95
  /**
96
   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
97
   *
98
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
99
   * follow this format when uploading the file will receive a HTTP `400` error with a
100
   * `metadata_after_file_contents` error code.
101
   *
102
   * @param fileId The unique identifier that represents a file.
103
   *     <p>The ID for any file can be determined by visiting a file in the web application and
104
   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
105
   *     `file_id` is `123`. Example: "12345"
106
   * @param requestBody Request body of uploadFileVersion method
107
   * @param queryParams Query parameters of uploadFileVersion method
108
   * @param headers Headers of uploadFileVersion method
109
   */
110
  public Files uploadFileVersion(
111
      String fileId,
112
      UploadFileVersionRequestBody requestBody,
113
      UploadFileVersionQueryParams queryParams,
114
      UploadFileVersionHeaders headers) {
115
    Map<String, String> queryParamsMap =
1✔
116
        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
1✔
117
    Map<String, String> headersMap =
1✔
118
        prepareParams(
1✔
119
            mergeMaps(
1✔
120
                mapOf(
1✔
121
                    entryOf("if-match", convertToString(headers.getIfMatch())),
1✔
122
                    entryOf("content-md5", convertToString(headers.getContentMd5()))),
1✔
123
                headers.getExtraHeaders()));
1✔
124
    FetchResponse response =
1✔
125
        this.networkSession
126
            .getNetworkClient()
1✔
127
            .fetch(
1✔
128
                new FetchOptions.Builder(
129
                        String.join(
1✔
130
                            "",
131
                            this.networkSession.getBaseUrls().getUploadUrl(),
1✔
132
                            "/2.0/files/",
133
                            convertToString(fileId),
1✔
134
                            "/content"),
135
                        "POST")
136
                    .params(queryParamsMap)
1✔
137
                    .headers(headersMap)
1✔
138
                    .multipartData(
1✔
139
                        Arrays.asList(
1✔
140
                            new MultipartItem.Builder("attributes")
141
                                .data(JsonManager.serialize(requestBody.getAttributes()))
1✔
142
                                .build(),
1✔
143
                            new MultipartItem.Builder("file")
144
                                .fileStream(requestBody.getFile())
1✔
145
                                .fileName(requestBody.getFileFileName())
1✔
146
                                .contentType(requestBody.getFileContentType())
1✔
147
                                .build()))
1✔
148
                    .contentType("multipart/form-data")
1✔
149
                    .responseFormat(ResponseFormat.JSON)
1✔
150
                    .auth(this.auth)
1✔
151
                    .networkSession(this.networkSession)
1✔
152
                    .build());
1✔
153
    return JsonManager.deserialize(response.getData(), Files.class);
1✔
154
  }
155

156
  /**
157
   * Performs a check to verify that a file will be accepted by Box before you upload the entire
158
   * file.
159
   */
160
  public UploadUrl preflightFileUploadCheck() {
161
    return preflightFileUploadCheck(
×
162
        new PreflightFileUploadCheckRequestBody(), new PreflightFileUploadCheckHeaders());
163
  }
164

165
  /**
166
   * Performs a check to verify that a file will be accepted by Box before you upload the entire
167
   * file.
168
   *
169
   * @param requestBody Request body of preflightFileUploadCheck method
170
   */
171
  public UploadUrl preflightFileUploadCheck(PreflightFileUploadCheckRequestBody requestBody) {
172
    return preflightFileUploadCheck(requestBody, new PreflightFileUploadCheckHeaders());
1✔
173
  }
174

175
  /**
176
   * Performs a check to verify that a file will be accepted by Box before you upload the entire
177
   * file.
178
   *
179
   * @param headers Headers of preflightFileUploadCheck method
180
   */
181
  public UploadUrl preflightFileUploadCheck(PreflightFileUploadCheckHeaders headers) {
182
    return preflightFileUploadCheck(new PreflightFileUploadCheckRequestBody(), headers);
×
183
  }
184

185
  /**
186
   * Performs a check to verify that a file will be accepted by Box before you upload the entire
187
   * file.
188
   *
189
   * @param requestBody Request body of preflightFileUploadCheck method
190
   * @param headers Headers of preflightFileUploadCheck method
191
   */
192
  public UploadUrl preflightFileUploadCheck(
193
      PreflightFileUploadCheckRequestBody requestBody, PreflightFileUploadCheckHeaders headers) {
194
    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
1✔
195
    FetchResponse response =
1✔
196
        this.networkSession
197
            .getNetworkClient()
1✔
198
            .fetch(
1✔
199
                new FetchOptions.Builder(
200
                        String.join(
1✔
201
                            "",
202
                            this.networkSession.getBaseUrls().getBaseUrl(),
1✔
203
                            "/2.0/files/content"),
204
                        "OPTIONS")
205
                    .headers(headersMap)
1✔
206
                    .data(JsonManager.serialize(requestBody))
1✔
207
                    .contentType("application/json")
1✔
208
                    .responseFormat(ResponseFormat.JSON)
1✔
209
                    .auth(this.auth)
1✔
210
                    .networkSession(this.networkSession)
1✔
211
                    .build());
1✔
212
    return JsonManager.deserialize(response.getData(), UploadUrl.class);
1✔
213
  }
214

215
  /**
216
   * Uploads a small file to Box. For file sizes over 50MB we recommend using the Chunk Upload APIs.
217
   *
218
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
219
   * follow this format when uploading the file will receive a HTTP `400` error with a
220
   * `metadata_after_file_contents` error code.
221
   *
222
   * @param requestBody Request body of uploadFile method
223
   */
224
  public Files uploadFile(UploadFileRequestBody requestBody) {
225
    return uploadFile(requestBody, new UploadFileQueryParams(), new UploadFileHeaders());
1✔
226
  }
227

228
  /**
229
   * Uploads a small file to Box. For file sizes over 50MB we recommend using the Chunk Upload APIs.
230
   *
231
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
232
   * follow this format when uploading the file will receive a HTTP `400` error with a
233
   * `metadata_after_file_contents` error code.
234
   *
235
   * @param requestBody Request body of uploadFile method
236
   * @param queryParams Query parameters of uploadFile method
237
   */
238
  public Files uploadFile(UploadFileRequestBody requestBody, UploadFileQueryParams queryParams) {
239
    return uploadFile(requestBody, queryParams, new UploadFileHeaders());
×
240
  }
241

242
  /**
243
   * Uploads a small file to Box. For file sizes over 50MB we recommend using the Chunk Upload APIs.
244
   *
245
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
246
   * follow this format when uploading the file will receive a HTTP `400` error with a
247
   * `metadata_after_file_contents` error code.
248
   *
249
   * @param requestBody Request body of uploadFile method
250
   * @param headers Headers of uploadFile method
251
   */
252
  public Files uploadFile(UploadFileRequestBody requestBody, UploadFileHeaders headers) {
253
    return uploadFile(requestBody, new UploadFileQueryParams(), headers);
×
254
  }
255

256
  /**
257
   * Uploads a small file to Box. For file sizes over 50MB we recommend using the Chunk Upload APIs.
258
   *
259
   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
260
   * follow this format when uploading the file will receive a HTTP `400` error with a
261
   * `metadata_after_file_contents` error code.
262
   *
263
   * @param requestBody Request body of uploadFile method
264
   * @param queryParams Query parameters of uploadFile method
265
   * @param headers Headers of uploadFile method
266
   */
267
  public Files uploadFile(
268
      UploadFileRequestBody requestBody,
269
      UploadFileQueryParams queryParams,
270
      UploadFileHeaders headers) {
271
    Map<String, String> queryParamsMap =
1✔
272
        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
1✔
273
    Map<String, String> headersMap =
1✔
274
        prepareParams(
1✔
275
            mergeMaps(
1✔
276
                mapOf(entryOf("content-md5", convertToString(headers.getContentMd5()))),
1✔
277
                headers.getExtraHeaders()));
1✔
278
    FetchResponse response =
1✔
279
        this.networkSession
280
            .getNetworkClient()
1✔
281
            .fetch(
1✔
282
                new FetchOptions.Builder(
283
                        String.join(
1✔
284
                            "",
285
                            this.networkSession.getBaseUrls().getUploadUrl(),
1✔
286
                            "/2.0/files/content"),
287
                        "POST")
288
                    .params(queryParamsMap)
1✔
289
                    .headers(headersMap)
1✔
290
                    .multipartData(
1✔
291
                        Arrays.asList(
1✔
292
                            new MultipartItem.Builder("attributes")
293
                                .data(JsonManager.serialize(requestBody.getAttributes()))
1✔
294
                                .build(),
1✔
295
                            new MultipartItem.Builder("file")
296
                                .fileStream(requestBody.getFile())
1✔
297
                                .fileName(requestBody.getFileFileName())
1✔
298
                                .contentType(requestBody.getFileContentType())
1✔
299
                                .build()))
1✔
300
                    .contentType("multipart/form-data")
1✔
301
                    .responseFormat(ResponseFormat.JSON)
1✔
302
                    .auth(this.auth)
1✔
303
                    .networkSession(this.networkSession)
1✔
304
                    .build());
1✔
305
    return JsonManager.deserialize(response.getData(), Files.class);
1✔
306
  }
307

308
  /**
309
   * Upload a file with a preflight check
310
   *
311
   * @param requestBody The requestBody parameter
312
   */
313
  public Files uploadWithPreflightCheck(UploadWithPreflightCheckRequestBody requestBody) {
314
    return uploadWithPreflightCheck(
1✔
315
        requestBody,
316
        new UploadWithPreflightCheckQueryParams(),
317
        new UploadWithPreflightCheckHeaders());
318
  }
319

320
  /**
321
   * Upload a file with a preflight check
322
   *
323
   * @param requestBody The requestBody parameter
324
   * @param queryParams Query parameters of uploadFile method
325
   */
326
  public Files uploadWithPreflightCheck(
327
      UploadWithPreflightCheckRequestBody requestBody,
328
      UploadWithPreflightCheckQueryParams queryParams) {
329
    return uploadWithPreflightCheck(
×
330
        requestBody, queryParams, new UploadWithPreflightCheckHeaders());
331
  }
332

333
  /**
334
   * Upload a file with a preflight check
335
   *
336
   * @param requestBody The requestBody parameter
337
   * @param headers Headers of uploadFile method
338
   */
339
  public Files uploadWithPreflightCheck(
340
      UploadWithPreflightCheckRequestBody requestBody, UploadWithPreflightCheckHeaders headers) {
341
    return uploadWithPreflightCheck(
×
342
        requestBody, new UploadWithPreflightCheckQueryParams(), headers);
343
  }
344

345
  /**
346
   * Upload a file with a preflight check
347
   *
348
   * @param requestBody The requestBody parameter
349
   * @param queryParams Query parameters of uploadFile method
350
   * @param headers Headers of uploadFile method
351
   */
352
  public Files uploadWithPreflightCheck(
353
      UploadWithPreflightCheckRequestBody requestBody,
354
      UploadWithPreflightCheckQueryParams queryParams,
355
      UploadWithPreflightCheckHeaders headers) {
356
    Map<String, String> queryParamsMap =
1✔
357
        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
1✔
358
    Map<String, String> headersMap =
1✔
359
        prepareParams(
1✔
360
            mergeMaps(
1✔
361
                mapOf(entryOf("content-md5", convertToString(headers.getContentMd5()))),
1✔
362
                headers.getExtraHeaders()));
1✔
363
    UploadUrl preflightUploadUrl =
1✔
364
        this.preflightFileUploadCheck(
1✔
365
            new PreflightFileUploadCheckRequestBody.Builder()
366
                .name(requestBody.getAttributes().getName())
1✔
367
                .size(requestBody.getAttributes().getSize())
1✔
368
                .parent(
1✔
369
                    new PreflightFileUploadCheckRequestBodyParentField.Builder()
370
                        .id(requestBody.getAttributes().getParent().getId())
1✔
371
                        .build())
1✔
372
                .build(),
1✔
373
            new PreflightFileUploadCheckHeaders.Builder()
374
                .extraHeaders(headers.getExtraHeaders())
1✔
375
                .build());
1✔
376
    if (preflightUploadUrl.getUploadUrl() == null
1✔
377
        || !(preflightUploadUrl.getUploadUrl().contains("http"))) {
1✔
378
      throw new BoxSDKError("Unable to get preflight upload URL");
×
379
    }
380
    FetchResponse response =
1✔
381
        this.networkSession
382
            .getNetworkClient()
1✔
383
            .fetch(
1✔
384
                new FetchOptions.Builder(preflightUploadUrl.getUploadUrl(), "POST")
1✔
385
                    .params(queryParamsMap)
1✔
386
                    .headers(headersMap)
1✔
387
                    .multipartData(
1✔
388
                        Arrays.asList(
1✔
389
                            new MultipartItem.Builder("attributes")
390
                                .data(JsonManager.serialize(requestBody.getAttributes()))
1✔
391
                                .build(),
1✔
392
                            new MultipartItem.Builder("file")
393
                                .fileStream(requestBody.getFile())
1✔
394
                                .fileName(requestBody.getFileFileName())
1✔
395
                                .contentType(requestBody.getFileContentType())
1✔
396
                                .build()))
1✔
397
                    .contentType("multipart/form-data")
1✔
398
                    .responseFormat(ResponseFormat.JSON)
1✔
399
                    .auth(this.auth)
1✔
400
                    .networkSession(this.networkSession)
1✔
401
                    .build());
1✔
402
    return JsonManager.deserialize(response.getData(), Files.class);
1✔
403
  }
404

405
  public Authentication getAuth() {
406
    return auth;
×
407
  }
408

409
  public NetworkSession getNetworkSession() {
410
    return networkSession;
×
411
  }
412

413
  public static class Builder {
414

415
    protected Authentication auth;
416

417
    protected NetworkSession networkSession;
418

419
    public Builder() {
1✔
420
      this.networkSession = new NetworkSession();
1✔
421
    }
1✔
422

423
    public Builder auth(Authentication auth) {
424
      this.auth = auth;
1✔
425
      return this;
1✔
426
    }
427

428
    public Builder networkSession(NetworkSession networkSession) {
429
      this.networkSession = networkSession;
1✔
430
      return this;
1✔
431
    }
432

433
    public UploadsManager build() {
434
      return new UploadsManager(this);
1✔
435
    }
436
  }
437
}
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