001package com.box.sdkgen.managers.downloads;
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;
008import static com.box.sdkgen.internal.utils.UtilsManager.writeInputStreamToOutputStream;
009
010import com.box.sdkgen.box.errors.BoxSDKError;
011import com.box.sdkgen.networking.auth.Authentication;
012import com.box.sdkgen.networking.fetchoptions.FetchOptions;
013import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
014import com.box.sdkgen.networking.fetchresponse.FetchResponse;
015import com.box.sdkgen.networking.network.NetworkSession;
016import java.io.InputStream;
017import java.io.OutputStream;
018import java.util.Map;
019
020public class DownloadsManager {
021
022  public Authentication auth;
023
024  public NetworkSession networkSession;
025
026  public DownloadsManager() {
027    this.networkSession = new NetworkSession();
028  }
029
030  protected DownloadsManager(Builder builder) {
031    this.auth = builder.auth;
032    this.networkSession = builder.networkSession;
033  }
034
035  /**
036   * Returns the contents of a file in binary format.
037   *
038   * @param fileId The unique identifier that represents a file.
039   *     <p>The ID for any file can be determined by visiting a file in the web application and
040   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
041   *     `file_id` is `123`. Example: "12345"
042   */
043  public String getDownloadFileUrl(String fileId) {
044    return getDownloadFileUrl(
045        fileId, new GetDownloadFileUrlQueryParams(), new GetDownloadFileUrlHeaders());
046  }
047
048  /**
049   * Returns the contents of a file in binary format.
050   *
051   * @param fileId The unique identifier that represents a file.
052   *     <p>The ID for any file can be determined by visiting a file in the web application and
053   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
054   *     `file_id` is `123`. Example: "12345"
055   * @param queryParams Query parameters of downloadFile method
056   */
057  public String getDownloadFileUrl(String fileId, GetDownloadFileUrlQueryParams queryParams) {
058    return getDownloadFileUrl(fileId, queryParams, new GetDownloadFileUrlHeaders());
059  }
060
061  /**
062   * Returns the contents of a file in binary format.
063   *
064   * @param fileId The unique identifier that represents a file.
065   *     <p>The ID for any file can be determined by visiting a file in the web application and
066   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
067   *     `file_id` is `123`. Example: "12345"
068   * @param headers Headers of downloadFile method
069   */
070  public String getDownloadFileUrl(String fileId, GetDownloadFileUrlHeaders headers) {
071    return getDownloadFileUrl(fileId, new GetDownloadFileUrlQueryParams(), headers);
072  }
073
074  /**
075   * Returns the contents of a file in binary format.
076   *
077   * @param fileId The unique identifier that represents a file.
078   *     <p>The ID for any file can be determined by visiting a file in the web application and
079   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
080   *     `file_id` is `123`. Example: "12345"
081   * @param queryParams Query parameters of downloadFile method
082   * @param headers Headers of downloadFile method
083   */
084  public String getDownloadFileUrl(
085      String fileId, GetDownloadFileUrlQueryParams queryParams, GetDownloadFileUrlHeaders headers) {
086    Map<String, String> queryParamsMap =
087        prepareParams(
088            mapOf(
089                entryOf("version", convertToString(queryParams.getVersion())),
090                entryOf("access_token", convertToString(queryParams.getAccessToken()))));
091    Map<String, String> headersMap =
092        prepareParams(
093            mergeMaps(
094                mapOf(
095                    entryOf("range", convertToString(headers.getRange())),
096                    entryOf("boxapi", convertToString(headers.getBoxapi()))),
097                headers.getExtraHeaders()));
098    FetchResponse response =
099        this.networkSession
100            .getNetworkClient()
101            .fetch(
102                new FetchOptions.Builder(
103                        String.join(
104                            "",
105                            this.networkSession.getBaseUrls().getBaseUrl(),
106                            "/2.0/files/",
107                            convertToString(fileId),
108                            "/content"),
109                        "GET")
110                    .params(queryParamsMap)
111                    .headers(headersMap)
112                    .responseFormat(ResponseFormat.NO_CONTENT)
113                    .auth(this.auth)
114                    .networkSession(this.networkSession)
115                    .followRedirects(false)
116                    .build());
117    if (response.getHeaders().containsKey("location")) {
118      return response.getHeaders().get("location");
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());
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 =
177        prepareParams(
178            mapOf(
179                entryOf("version", convertToString(queryParams.getVersion())),
180                entryOf("access_token", convertToString(queryParams.getAccessToken()))));
181    Map<String, String> headersMap =
182        prepareParams(
183            mergeMaps(
184                mapOf(
185                    entryOf("range", convertToString(headers.getRange())),
186                    entryOf("boxapi", convertToString(headers.getBoxapi()))),
187                headers.getExtraHeaders()));
188    FetchResponse response =
189        this.networkSession
190            .getNetworkClient()
191            .fetch(
192                new FetchOptions.Builder(
193                        String.join(
194                            "",
195                            this.networkSession.getBaseUrls().getBaseUrl(),
196                            "/2.0/files/",
197                            convertToString(fileId),
198                            "/content"),
199                        "GET")
200                    .params(queryParamsMap)
201                    .headers(headersMap)
202                    .responseFormat(ResponseFormat.BINARY)
203                    .auth(this.auth)
204                    .networkSession(this.networkSession)
205                    .build());
206    if (convertToString(response.getStatus()).equals("202")) {
207      return null;
208    }
209    return response.getContent();
210  }
211
212  public void downloadFileToOutputStream(String fileId, OutputStream outputStream) {
213    downloadFileToOutputStream(
214        fileId,
215        outputStream,
216        new DownloadFileToOutputStreamQueryParams(),
217        new DownloadFileToOutputStreamHeaders());
218  }
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 =
238        this.downloadFile(
239            fileId,
240            new DownloadFileQueryParams.Builder()
241                .version(queryParams.getVersion())
242                .accessToken(queryParams.getAccessToken())
243                .build(),
244            new DownloadFileHeaders.Builder()
245                .range(headers.getRange())
246                .boxapi(headers.getBoxapi())
247                .extraHeaders(headers.getExtraHeaders())
248                .build());
249    writeInputStreamToOutputStream(downloadStream, outputStream);
250  }
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() {}
267
268    public Builder auth(Authentication auth) {
269      this.auth = auth;
270      return this;
271    }
272
273    public Builder networkSession(NetworkSession networkSession) {
274      this.networkSession = networkSession;
275      return this;
276    }
277
278    public DownloadsManager build() {
279      if (this.networkSession == null) {
280        this.networkSession = new NetworkSession();
281      }
282      return new DownloadsManager(this);
283    }
284  }
285}