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

Commit

Permalink
Merge branch 'iterate-ch-feature/b2_get_download_authorization'
Browse files Browse the repository at this point in the history
  • Loading branch information
synapticloop committed Apr 22, 2017
2 parents 4355a3c + 9ef5507 commit 4eece66
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/main/java/synapticloop/b2/B2ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 4eece66

Please sign in to comment.