001package com.box.sdkgen.managers.uploads;
002
003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString;
004import static com.box.sdkgen.internal.utils.UtilsManager.entryOf;
005import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
006import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps;
007import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
008
009import com.box.sdkgen.box.errors.BoxSDKError;
010import com.box.sdkgen.networking.auth.Authentication;
011import com.box.sdkgen.networking.fetchoptions.FetchOptions;
012import com.box.sdkgen.networking.fetchoptions.MultipartItem;
013import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
014import com.box.sdkgen.networking.fetchresponse.FetchResponse;
015import com.box.sdkgen.networking.network.NetworkSession;
016import com.box.sdkgen.schemas.files.Files;
017import com.box.sdkgen.schemas.uploadurl.UploadUrl;
018import com.box.sdkgen.serialization.json.JsonManager;
019import java.util.Arrays;
020import java.util.Map;
021
022public class UploadsManager {
023
024  public Authentication auth;
025
026  public NetworkSession networkSession;
027
028  public UploadsManager() {
029    this.networkSession = new NetworkSession();
030  }
031
032  protected UploadsManager(Builder builder) {
033    this.auth = builder.auth;
034    this.networkSession = builder.networkSession;
035  }
036
037  /**
038   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
039   *
040   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
041   * follow this format when uploading the file will receive a HTTP `400` error with a
042   * `metadata_after_file_contents` error code.
043   *
044   * @param fileId The unique identifier that represents a file.
045   *     <p>The ID for any file can be determined by visiting a file in the web application and
046   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
047   *     `file_id` is `123`. Example: "12345"
048   * @param requestBody Request body of uploadFileVersion method
049   */
050  public Files uploadFileVersion(String fileId, UploadFileVersionRequestBody requestBody) {
051    return uploadFileVersion(
052        fileId, requestBody, new UploadFileVersionQueryParams(), new UploadFileVersionHeaders());
053  }
054
055  /**
056   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
057   *
058   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
059   * follow this format when uploading the file will receive a HTTP `400` error with a
060   * `metadata_after_file_contents` error code.
061   *
062   * @param fileId The unique identifier that represents a file.
063   *     <p>The ID for any file can be determined by visiting a file in the web application and
064   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
065   *     `file_id` is `123`. Example: "12345"
066   * @param requestBody Request body of uploadFileVersion method
067   * @param queryParams Query parameters of uploadFileVersion method
068   */
069  public Files uploadFileVersion(
070      String fileId,
071      UploadFileVersionRequestBody requestBody,
072      UploadFileVersionQueryParams queryParams) {
073    return uploadFileVersion(fileId, requestBody, queryParams, new UploadFileVersionHeaders());
074  }
075
076  /**
077   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
078   *
079   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
080   * follow this format when uploading the file will receive a HTTP `400` error with a
081   * `metadata_after_file_contents` error code.
082   *
083   * @param fileId The unique identifier that represents a file.
084   *     <p>The ID for any file can be determined by visiting a file in the web application and
085   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
086   *     `file_id` is `123`. Example: "12345"
087   * @param requestBody Request body of uploadFileVersion method
088   * @param headers Headers of uploadFileVersion method
089   */
090  public Files uploadFileVersion(
091      String fileId, UploadFileVersionRequestBody requestBody, UploadFileVersionHeaders headers) {
092    return uploadFileVersion(fileId, requestBody, new UploadFileVersionQueryParams(), headers);
093  }
094
095  /**
096   * Update a file's content. For file sizes over 50MB we recommend using the Chunk Upload APIs.
097   *
098   * <p>The `attributes` part of the body must come **before** the `file` part. Requests that do not
099   * 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 =
116        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
117    Map<String, String> headersMap =
118        prepareParams(
119            mergeMaps(
120                mapOf(
121                    entryOf("if-match", convertToString(headers.getIfMatch())),
122                    entryOf("content-md5", convertToString(headers.getContentMd5()))),
123                headers.getExtraHeaders()));
124    FetchResponse response =
125        this.networkSession
126            .getNetworkClient()
127            .fetch(
128                new FetchOptions.Builder(
129                        String.join(
130                            "",
131                            this.networkSession.getBaseUrls().getUploadUrl(),
132                            "/2.0/files/",
133                            convertToString(fileId),
134                            "/content"),
135                        "POST")
136                    .params(queryParamsMap)
137                    .headers(headersMap)
138                    .multipartData(
139                        Arrays.asList(
140                            new MultipartItem.Builder("attributes")
141                                .data(JsonManager.serialize(requestBody.getAttributes()))
142                                .build(),
143                            new MultipartItem.Builder("file")
144                                .fileStream(requestBody.getFile())
145                                .fileName(requestBody.getFileFileName())
146                                .contentType(requestBody.getFileContentType())
147                                .build()))
148                    .contentType("multipart/form-data")
149                    .responseFormat(ResponseFormat.JSON)
150                    .auth(this.auth)
151                    .networkSession(this.networkSession)
152                    .build());
153    return JsonManager.deserialize(response.getData(), Files.class);
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());
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()));
195    FetchResponse response =
196        this.networkSession
197            .getNetworkClient()
198            .fetch(
199                new FetchOptions.Builder(
200                        String.join(
201                            "",
202                            this.networkSession.getBaseUrls().getBaseUrl(),
203                            "/2.0/files/content"),
204                        "OPTIONS")
205                    .headers(headersMap)
206                    .data(JsonManager.serialize(requestBody))
207                    .contentType("application/json")
208                    .responseFormat(ResponseFormat.JSON)
209                    .auth(this.auth)
210                    .networkSession(this.networkSession)
211                    .build());
212    return JsonManager.deserialize(response.getData(), UploadUrl.class);
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());
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 =
272        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
273    Map<String, String> headersMap =
274        prepareParams(
275            mergeMaps(
276                mapOf(entryOf("content-md5", convertToString(headers.getContentMd5()))),
277                headers.getExtraHeaders()));
278    FetchResponse response =
279        this.networkSession
280            .getNetworkClient()
281            .fetch(
282                new FetchOptions.Builder(
283                        String.join(
284                            "",
285                            this.networkSession.getBaseUrls().getUploadUrl(),
286                            "/2.0/files/content"),
287                        "POST")
288                    .params(queryParamsMap)
289                    .headers(headersMap)
290                    .multipartData(
291                        Arrays.asList(
292                            new MultipartItem.Builder("attributes")
293                                .data(JsonManager.serialize(requestBody.getAttributes()))
294                                .build(),
295                            new MultipartItem.Builder("file")
296                                .fileStream(requestBody.getFile())
297                                .fileName(requestBody.getFileFileName())
298                                .contentType(requestBody.getFileContentType())
299                                .build()))
300                    .contentType("multipart/form-data")
301                    .responseFormat(ResponseFormat.JSON)
302                    .auth(this.auth)
303                    .networkSession(this.networkSession)
304                    .build());
305    return JsonManager.deserialize(response.getData(), Files.class);
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(
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 =
357        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
358    Map<String, String> headersMap =
359        prepareParams(
360            mergeMaps(
361                mapOf(entryOf("content-md5", convertToString(headers.getContentMd5()))),
362                headers.getExtraHeaders()));
363    UploadUrl preflightUploadUrl =
364        this.preflightFileUploadCheck(
365            new PreflightFileUploadCheckRequestBody.Builder()
366                .name(requestBody.getAttributes().getName())
367                .size(requestBody.getAttributes().getSize())
368                .parent(
369                    new PreflightFileUploadCheckRequestBodyParentField.Builder()
370                        .id(requestBody.getAttributes().getParent().getId())
371                        .build())
372                .build(),
373            new PreflightFileUploadCheckHeaders.Builder()
374                .extraHeaders(headers.getExtraHeaders())
375                .build());
376    if (preflightUploadUrl.getUploadUrl() == null
377        || !(preflightUploadUrl.getUploadUrl().contains("http"))) {
378      throw new BoxSDKError("Unable to get preflight upload URL");
379    }
380    FetchResponse response =
381        this.networkSession
382            .getNetworkClient()
383            .fetch(
384                new FetchOptions.Builder(preflightUploadUrl.getUploadUrl(), "POST")
385                    .params(queryParamsMap)
386                    .headers(headersMap)
387                    .multipartData(
388                        Arrays.asList(
389                            new MultipartItem.Builder("attributes")
390                                .data(JsonManager.serialize(requestBody.getAttributes()))
391                                .build(),
392                            new MultipartItem.Builder("file")
393                                .fileStream(requestBody.getFile())
394                                .fileName(requestBody.getFileFileName())
395                                .contentType(requestBody.getFileContentType())
396                                .build()))
397                    .contentType("multipart/form-data")
398                    .responseFormat(ResponseFormat.JSON)
399                    .auth(this.auth)
400                    .networkSession(this.networkSession)
401                    .build());
402    return JsonManager.deserialize(response.getData(), Files.class);
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() {}
420
421    public Builder auth(Authentication auth) {
422      this.auth = auth;
423      return this;
424    }
425
426    public Builder networkSession(NetworkSession networkSession) {
427      this.networkSession = networkSession;
428      return this;
429    }
430
431    public UploadsManager build() {
432      if (this.networkSession == null) {
433        this.networkSession = new NetworkSession();
434      }
435      return new UploadsManager(this);
436    }
437  }
438}