Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 4eece66

Browse files
author
synapticloop
committed
Merge branch 'iterate-ch-feature/b2_get_download_authorization'
2 parents 4355a3c + 9ef5507 commit 4eece66

File tree

5 files changed

+145
-22
lines changed

5 files changed

+145
-22
lines changed

src/main/java/synapticloop/b2/B2ApiClient.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,7 @@
2828
import org.apache.http.impl.client.HttpClients;
2929

3030
import synapticloop.b2.exception.B2ApiException;
31-
import synapticloop.b2.request.B2AuthorizeAccountRequest;
32-
import synapticloop.b2.request.B2CancelLargeFileRequest;
33-
import synapticloop.b2.request.B2CreateBucketRequest;
34-
import synapticloop.b2.request.B2DeleteBucketRequest;
35-
import synapticloop.b2.request.B2DeleteFileVersionRequest;
36-
import synapticloop.b2.request.B2DownloadFileByIdRequest;
37-
import synapticloop.b2.request.B2DownloadFileByNameRequest;
38-
import synapticloop.b2.request.B2FinishLargeFileRequest;
39-
import synapticloop.b2.request.B2GetFileInfoRequest;
40-
import synapticloop.b2.request.B2GetUploadPartUrlRequest;
41-
import synapticloop.b2.request.B2GetUploadUrlRequest;
42-
import synapticloop.b2.request.B2HeadFileByIdRequest;
43-
import synapticloop.b2.request.B2HideFileRequest;
44-
import synapticloop.b2.request.B2ListBucketsRequest;
45-
import synapticloop.b2.request.B2ListFileNamesRequest;
46-
import synapticloop.b2.request.B2ListFileVersionsRequest;
47-
import synapticloop.b2.request.B2ListPartsRequest;
48-
import synapticloop.b2.request.B2ListUnfinishedLargeFilesRequest;
49-
import synapticloop.b2.request.B2StartLargeFileRequest;
50-
import synapticloop.b2.request.B2UpdateBucketRequest;
51-
import synapticloop.b2.request.B2UploadFileRequest;
52-
import synapticloop.b2.request.B2UploadPartRequest;
31+
import synapticloop.b2.request.*;
5332
import synapticloop.b2.response.B2AuthorizeAccountResponse;
5433
import synapticloop.b2.response.B2BucketResponse;
5534
import synapticloop.b2.response.B2DeleteFileVersionResponse;
@@ -769,6 +748,25 @@ public B2ListFilesResponse listFileVersions(String bucketId, String startFileNam
769748
*
770749
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
771750

751+
/**
752+
* Used to generate an authorization token that can be used to download files with the specified prefix
753+
* from a private B2 bucket. Returns an authorization token that can be passed to b2_download_file_by_name
754+
* in the Authorization header or as an Authorization parameter.
755+
*
756+
* @param bucketId the id of the bucket
757+
* @param fileNamePrefix The file name prefix of files the download authorization token will allow b2_download_file_by_name to access.
758+
* @param validDurationInSeconds The number of seconds before the authorization token will expire.
759+
* The maximum value is 604800 which is one week in seconds
760+
* @return The authorization token that can be passed in the Authorization header or as an Authorization
761+
* parameter to b2_download_file_by_name to access files beginning with the file name prefix.
762+
*
763+
* @throws B2ApiException if there was an error with the call
764+
* @throws IOException if there was an error communicating with the API service
765+
*/
766+
public String getDownloadAuthorization(String bucketId, String fileNamePrefix, Integer validDurationInSeconds) throws B2ApiException, IOException {
767+
return new B2GetDownloadAuthorizationRequest(client, b2AuthorizeAccountResponse, bucketId, fileNamePrefix, validDurationInSeconds).getResponse().getAuthorizationToken();
768+
}
769+
772770
/**
773771
* Download a named file from a named bucket to an output file. This is a
774772
* utility method which will automatically write the content to the file.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package synapticloop.b2.request;
2+
3+
/*
4+
* Copyright (c) 2017 iterate GmbH.
5+
*
6+
* All rights reserved.
7+
*
8+
* This code may contain contributions from other parties which, where
9+
* applicable, will be listed in the default build file for the project
10+
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
11+
*
12+
* This source code and any derived binaries are covered by the terms and
13+
* conditions of the Licence agreement ("the Licence"). You may not use this
14+
* source code or any derived binaries except in compliance with the Licence.
15+
* A copy of the Licence is available in the file named LICENSE.txt shipped with
16+
* this source code or binaries.
17+
*/
18+
19+
import org.apache.http.impl.client.CloseableHttpClient;
20+
import org.apache.http.util.EntityUtils;
21+
22+
import java.io.IOException;
23+
24+
import synapticloop.b2.exception.B2ApiException;
25+
import synapticloop.b2.response.B2AuthorizeAccountResponse;
26+
import synapticloop.b2.response.B2GetDownloadAuthorizationResponse;
27+
28+
public class B2GetDownloadAuthorizationRequest extends BaseB2Request {
29+
private static final String B2_GET_DOWNLOAD_AUTHORIZATION = BASE_API_VERSION + "b2_get_download_authorization";
30+
31+
/**
32+
* @param client The http client to use
33+
* @param b2AuthorizeAccountResponse the authorize account response
34+
* @param bucketId The identifier for the bucket.
35+
* @param fileNamePrefix The file name prefix of files the download authorization token will allow b2_download_file_by_name to access.
36+
* @param validDurationInSeconds The number of seconds before the authorization token will expire.
37+
* The maximum value is 604800 which is one week in seconds.
38+
*/
39+
public B2GetDownloadAuthorizationRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse,
40+
String bucketId, String fileNamePrefix, Integer validDurationInSeconds) {
41+
super(client, b2AuthorizeAccountResponse, b2AuthorizeAccountResponse.getApiUrl() + B2_GET_DOWNLOAD_AUTHORIZATION);
42+
43+
this.addProperty(B2RequestProperties.KEY_BUCKET_ID, bucketId);
44+
this.addProperty(B2RequestProperties.KEY_FILE_NAME_PREFIX, fileNamePrefix);
45+
this.addProperty(B2RequestProperties.KEY_VALID_DURATION_INSECONDS, validDurationInSeconds);
46+
}
47+
48+
public B2GetDownloadAuthorizationResponse getResponse() throws B2ApiException, IOException {
49+
return new B2GetDownloadAuthorizationResponse(EntityUtils.toString(executePost().getEntity()));
50+
}
51+
}

src/main/java/synapticloop/b2/request/B2RequestProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public final class B2RequestProperties {
2323
public static final String KEY_BUCKET_TYPE = "bucketType";
2424
public static final String KEY_FILE_ID = "fileId";
2525
public static final String KEY_FILE_NAME = "fileName";
26+
public static final String KEY_FILE_NAME_PREFIX = "fileNamePrefix";
27+
public static final String KEY_VALID_DURATION_INSECONDS = "validDurationInSeconds";
2628
public static final String KEY_CONTENT_TYPE = "contentType";
2729
public static final String KEY_MAX_FILE_COUNT = "maxFileCount";
2830
public static final String KEY_START_FILE_ID = "startFileId";
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package synapticloop.b2.response;
2+
3+
/*
4+
* Copyright (c) 2017 iterate GmbH.
5+
*
6+
* All rights reserved.
7+
*
8+
* This code may contain contributions from other parties which, where
9+
* applicable, will be listed in the default build file for the project
10+
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
11+
*
12+
* This source code and any derived binaries are covered by the terms and
13+
* conditions of the Licence agreement ("the Licence"). You may not use this
14+
* source code or any derived binaries except in compliance with the Licence.
15+
* A copy of the Licence is available in the file named LICENSE.txt shipped with
16+
* this source code or binaries.
17+
*/
18+
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
import synapticloop.b2.exception.B2ApiException;
23+
24+
public class B2GetDownloadAuthorizationResponse extends BaseB2Response {
25+
private static final Logger LOGGER = LoggerFactory.getLogger(B2AuthorizeAccountResponse.class);
26+
27+
private final String bucketId;
28+
private final String fileNamePrefix;
29+
private final String authorizationToken;
30+
31+
public B2GetDownloadAuthorizationResponse(String json) throws B2ApiException {
32+
super(json);
33+
34+
this.bucketId = this.readString(B2ResponseProperties.KEY_BUCKET_ID);
35+
this.fileNamePrefix = this.readString(B2ResponseProperties.KEY_FILE_NAME);
36+
this.authorizationToken = this.readString(B2ResponseProperties.KEY_AUTHORIZATION_TOKEN);
37+
38+
this.warnOnMissedKeys();
39+
}
40+
41+
/**
42+
* @return The identifier for the bucket.
43+
*/
44+
public String getBucketId() { return bucketId; }
45+
46+
/**
47+
* @return The prefix for files the authorization token will allow b2_download_file_by_name to access.
48+
*/
49+
public String getFileNamePrefix() { return fileNamePrefix; }
50+
51+
/**
52+
* @return The authorization token that can be passed in the Authorization header or as an Authorization
53+
* parameter to b2_download_file_by_name to access files beginning with the file name prefix.
54+
*/
55+
public String getAuthorizationToken() { return authorizationToken; }
56+
57+
@Override
58+
protected Logger getLogger() {
59+
return LOGGER;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
final StringBuilder sb = new StringBuilder("B2GetDownloadAuthorizationResponse{");
65+
sb.append("bucketId='").append(bucketId).append('\'');
66+
sb.append(", fileNamePrefix='").append(fileNamePrefix).append('\'');
67+
sb.append(", authorizationToken='").append(authorizationToken).append('\'');
68+
sb.append('}');
69+
return sb.toString();
70+
}
71+
}

src/main/java/synapticloop/b2/response/B2ResponseProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public final class B2ResponseProperties {
3333
public static final String KEY_FILE_ID = "fileId";
3434
public static final String KEY_FILE_INFO = "fileInfo";
3535
public static final String KEY_FILE_NAME = "fileName";
36+
public static final String KEY_FILE_NAME_PREFIX = "fileNamePrefix";
3637
public static final String KEY_FILES = "files";
3738
public static final String KEY_LIFECYCLE_RULES = "lifecycleRules";
3839
public static final String KEY_MINIMUM_PART_SIZE = "minimumPartSize";

0 commit comments

Comments
 (0)