diff --git a/src/main/java/synapticloop/b2/B2ApiClient.java b/src/main/java/synapticloop/b2/B2ApiClient.java index 7fe9391..f65f469 100644 --- a/src/main/java/synapticloop/b2/B2ApiClient.java +++ b/src/main/java/synapticloop/b2/B2ApiClient.java @@ -28,28 +28,7 @@ import org.apache.http.impl.client.HttpClients; import synapticloop.b2.exception.B2ApiException; -import synapticloop.b2.request.B2AuthorizeAccountRequest; -import synapticloop.b2.request.B2CancelLargeFileRequest; -import synapticloop.b2.request.B2CreateBucketRequest; -import synapticloop.b2.request.B2DeleteBucketRequest; -import synapticloop.b2.request.B2DeleteFileVersionRequest; -import synapticloop.b2.request.B2DownloadFileByIdRequest; -import synapticloop.b2.request.B2DownloadFileByNameRequest; -import synapticloop.b2.request.B2FinishLargeFileRequest; -import synapticloop.b2.request.B2GetFileInfoRequest; -import synapticloop.b2.request.B2GetUploadPartUrlRequest; -import synapticloop.b2.request.B2GetUploadUrlRequest; -import synapticloop.b2.request.B2HeadFileByIdRequest; -import synapticloop.b2.request.B2HideFileRequest; -import synapticloop.b2.request.B2ListBucketsRequest; -import synapticloop.b2.request.B2ListFileNamesRequest; -import synapticloop.b2.request.B2ListFileVersionsRequest; -import synapticloop.b2.request.B2ListPartsRequest; -import synapticloop.b2.request.B2ListUnfinishedLargeFilesRequest; -import synapticloop.b2.request.B2StartLargeFileRequest; -import synapticloop.b2.request.B2UpdateBucketRequest; -import synapticloop.b2.request.B2UploadFileRequest; -import synapticloop.b2.request.B2UploadPartRequest; +import synapticloop.b2.request.*; import synapticloop.b2.response.B2AuthorizeAccountResponse; import synapticloop.b2.response.B2BucketResponse; import synapticloop.b2.response.B2DeleteFileVersionResponse; @@ -769,6 +748,25 @@ public B2ListFilesResponse listFileVersions(String bucketId, String startFileNam * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + /** + * Used to generate an authorization token that can be used to download files with the specified prefix + * from a private B2 bucket. Returns an authorization token that can be passed to b2_download_file_by_name + * in the Authorization header or as an Authorization parameter. + * + * @param bucketId the id of the bucket + * @param fileNamePrefix The file name prefix of files the download authorization token will allow b2_download_file_by_name to access. + * @param validDurationInSeconds The number of seconds before the authorization token will expire. + * The maximum value is 604800 which is one week in seconds + * @return The authorization token that can be passed in the Authorization header or as an Authorization + * parameter to b2_download_file_by_name to access files beginning with the file name prefix. + * + * @throws B2ApiException if there was an error with the call + * @throws IOException if there was an error communicating with the API service + */ + public String getDownloadAuthorization(String bucketId, String fileNamePrefix, Integer validDurationInSeconds) throws B2ApiException, IOException { + return new B2GetDownloadAuthorizationRequest(client, b2AuthorizeAccountResponse, bucketId, fileNamePrefix, validDurationInSeconds).getResponse().getAuthorizationToken(); + } + /** * Download a named file from a named bucket to an output file. This is a * utility method which will automatically write the content to the file. diff --git a/src/main/java/synapticloop/b2/request/B2GetDownloadAuthorizationRequest.java b/src/main/java/synapticloop/b2/request/B2GetDownloadAuthorizationRequest.java new file mode 100644 index 0000000..9bb2cb2 --- /dev/null +++ b/src/main/java/synapticloop/b2/request/B2GetDownloadAuthorizationRequest.java @@ -0,0 +1,51 @@ +package synapticloop.b2.request; + +/* + * Copyright (c) 2017 iterate GmbH. + * + * All rights reserved. + * + * This code may contain contributions from other parties which, where + * applicable, will be listed in the default build file for the project + * ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project. + * + * This source code and any derived binaries are covered by the terms and + * conditions of the Licence agreement ("the Licence"). You may not use this + * source code or any derived binaries except in compliance with the Licence. + * A copy of the Licence is available in the file named LICENSE.txt shipped with + * this source code or binaries. + */ + +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +import synapticloop.b2.exception.B2ApiException; +import synapticloop.b2.response.B2AuthorizeAccountResponse; +import synapticloop.b2.response.B2GetDownloadAuthorizationResponse; + +public class B2GetDownloadAuthorizationRequest extends BaseB2Request { + private static final String B2_GET_DOWNLOAD_AUTHORIZATION = BASE_API_VERSION + "b2_get_download_authorization"; + + /** + * @param client The http client to use + * @param b2AuthorizeAccountResponse the authorize account response + * @param bucketId The identifier for the bucket. + * @param fileNamePrefix The file name prefix of files the download authorization token will allow b2_download_file_by_name to access. + * @param validDurationInSeconds The number of seconds before the authorization token will expire. + * The maximum value is 604800 which is one week in seconds. + */ + public B2GetDownloadAuthorizationRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, + String bucketId, String fileNamePrefix, Integer validDurationInSeconds) { + super(client, b2AuthorizeAccountResponse, b2AuthorizeAccountResponse.getApiUrl() + B2_GET_DOWNLOAD_AUTHORIZATION); + + this.addProperty(B2RequestProperties.KEY_BUCKET_ID, bucketId); + this.addProperty(B2RequestProperties.KEY_FILE_NAME_PREFIX, fileNamePrefix); + this.addProperty(B2RequestProperties.KEY_VALID_DURATION_INSECONDS, validDurationInSeconds); + } + + public B2GetDownloadAuthorizationResponse getResponse() throws B2ApiException, IOException { + return new B2GetDownloadAuthorizationResponse(EntityUtils.toString(executePost().getEntity())); + } +} diff --git a/src/main/java/synapticloop/b2/request/B2RequestProperties.java b/src/main/java/synapticloop/b2/request/B2RequestProperties.java index d08bd3a..f7d49ed 100644 --- a/src/main/java/synapticloop/b2/request/B2RequestProperties.java +++ b/src/main/java/synapticloop/b2/request/B2RequestProperties.java @@ -23,6 +23,8 @@ public final class B2RequestProperties { public static final String KEY_BUCKET_TYPE = "bucketType"; public static final String KEY_FILE_ID = "fileId"; public static final String KEY_FILE_NAME = "fileName"; + public static final String KEY_FILE_NAME_PREFIX = "fileNamePrefix"; + public static final String KEY_VALID_DURATION_INSECONDS = "validDurationInSeconds"; public static final String KEY_CONTENT_TYPE = "contentType"; public static final String KEY_MAX_FILE_COUNT = "maxFileCount"; public static final String KEY_START_FILE_ID = "startFileId"; diff --git a/src/main/java/synapticloop/b2/response/B2GetDownloadAuthorizationResponse.java b/src/main/java/synapticloop/b2/response/B2GetDownloadAuthorizationResponse.java new file mode 100644 index 0000000..8af64ee --- /dev/null +++ b/src/main/java/synapticloop/b2/response/B2GetDownloadAuthorizationResponse.java @@ -0,0 +1,71 @@ +package synapticloop.b2.response; + +/* + * Copyright (c) 2017 iterate GmbH. + * + * All rights reserved. + * + * This code may contain contributions from other parties which, where + * applicable, will be listed in the default build file for the project + * ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project. + * + * This source code and any derived binaries are covered by the terms and + * conditions of the Licence agreement ("the Licence"). You may not use this + * source code or any derived binaries except in compliance with the Licence. + * A copy of the Licence is available in the file named LICENSE.txt shipped with + * this source code or binaries. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import synapticloop.b2.exception.B2ApiException; + +public class B2GetDownloadAuthorizationResponse extends BaseB2Response { + private static final Logger LOGGER = LoggerFactory.getLogger(B2AuthorizeAccountResponse.class); + + private final String bucketId; + private final String fileNamePrefix; + private final String authorizationToken; + + public B2GetDownloadAuthorizationResponse(String json) throws B2ApiException { + super(json); + + this.bucketId = this.readString(B2ResponseProperties.KEY_BUCKET_ID); + this.fileNamePrefix = this.readString(B2ResponseProperties.KEY_FILE_NAME); + this.authorizationToken = this.readString(B2ResponseProperties.KEY_AUTHORIZATION_TOKEN); + + this.warnOnMissedKeys(); + } + + /** + * @return The identifier for the bucket. + */ + public String getBucketId() { return bucketId; } + + /** + * @return The prefix for files the authorization token will allow b2_download_file_by_name to access. + */ + public String getFileNamePrefix() { return fileNamePrefix; } + + /** + * @return The authorization token that can be passed in the Authorization header or as an Authorization + * parameter to b2_download_file_by_name to access files beginning with the file name prefix. + */ + public String getAuthorizationToken() { return authorizationToken; } + + @Override + protected Logger getLogger() { + return LOGGER; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("B2GetDownloadAuthorizationResponse{"); + sb.append("bucketId='").append(bucketId).append('\''); + sb.append(", fileNamePrefix='").append(fileNamePrefix).append('\''); + sb.append(", authorizationToken='").append(authorizationToken).append('\''); + sb.append('}'); + return sb.toString(); + } +} diff --git a/src/main/java/synapticloop/b2/response/B2ResponseProperties.java b/src/main/java/synapticloop/b2/response/B2ResponseProperties.java index 7c1daf1..5c15167 100644 --- a/src/main/java/synapticloop/b2/response/B2ResponseProperties.java +++ b/src/main/java/synapticloop/b2/response/B2ResponseProperties.java @@ -33,6 +33,7 @@ public final class B2ResponseProperties { public static final String KEY_FILE_ID = "fileId"; public static final String KEY_FILE_INFO = "fileInfo"; public static final String KEY_FILE_NAME = "fileName"; + public static final String KEY_FILE_NAME_PREFIX = "fileNamePrefix"; public static final String KEY_FILES = "files"; public static final String KEY_LIFECYCLE_RULES = "lifecycleRules"; public static final String KEY_MINIMUM_PART_SIZE = "minimumPartSize";