diff --git a/src/main/java/synapticloop/b2/B2ApiClient.java b/src/main/java/synapticloop/b2/B2ApiClient.java index d170024..82e7925 100644 --- a/src/main/java/synapticloop/b2/B2ApiClient.java +++ b/src/main/java/synapticloop/b2/B2ApiClient.java @@ -54,6 +54,7 @@ import synapticloop.b2.response.B2GetUploadUrlResponse; import synapticloop.b2.response.B2HideFileResponse; import synapticloop.b2.response.B2ListFilesResponse; +import synapticloop.b2.util.ChecksumHelper; /** * This is a wrapper class for the underlying calls to the request/response @@ -325,7 +326,8 @@ public B2FileResponse uploadFile(String bucketId, String fileName, HttpEntity en */ public B2FileResponse uploadFile(String bucketId, String fileName, File file, String mimeType, Map fileInfo) throws B2ApiException { B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse(); - return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, mimeType, fileInfo).getResponse(); + return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, + ChecksumHelper.calculateSha1(file), mimeType, fileInfo).getResponse(); } /** @@ -344,7 +346,8 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file, St public B2FileResponse uploadFile(String bucketId, String fileName, File file, Map fileInfo) throws B2ApiException { B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse(); - return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, fileInfo).getResponse(); + return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, + ChecksumHelper.calculateSha1(file), fileInfo).getResponse(); } /** @@ -365,7 +368,8 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file, Ma */ public B2FileResponse uploadFile(String bucketId, String fileName, File file, String mimeType) throws B2ApiException { B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse(); - return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, mimeType).getResponse(); + return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, + ChecksumHelper.calculateSha1(file), mimeType).getResponse(); } /** @@ -383,7 +387,8 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file, St */ public B2FileResponse uploadFile(String bucketId, String fileName, File file) throws B2ApiException { B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse(); - return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file).getResponse(); + return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, + file, ChecksumHelper.calculateSha1(file)).getResponse(); } diff --git a/src/main/java/synapticloop/b2/request/B2ListFileNamesRequest.java b/src/main/java/synapticloop/b2/request/B2ListFileNamesRequest.java index bb70d80..d562ee2 100644 --- a/src/main/java/synapticloop/b2/request/B2ListFileNamesRequest.java +++ b/src/main/java/synapticloop/b2/request/B2ListFileNamesRequest.java @@ -52,10 +52,8 @@ public class B2ListFileNamesRequest extends BaseB2Request { * @param client the HTTP client to use * @param b2AuthorizeAccountResponse the authorize account response * @param bucketId the id of the bucket to list - * @throws B2ApiException if the requested maximum number of files to be - * returned is greater than the allowable limt */ - public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId) throws B2ApiException { + public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId) { this(client, b2AuthorizeAccountResponse, bucketId, null, DEFAULT_MAX_FILE_COUNT); } @@ -67,12 +65,8 @@ public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResp * @param bucketId the id of the bucket to list * @param maxFileCount The maximum number of files to return from this call. * The default value is 100, and the maximum allowed is 1000. - * @throws B2ApiException if the requested maximum number of files to be - * returned is greater than the allowable limt - * - * @throws B2ApiException if there was an error constructing the request */ - public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId, Integer maxFileCount) throws B2ApiException { + public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId, Integer maxFileCount) { this(client, b2AuthorizeAccountResponse, bucketId, null, maxFileCount); } @@ -87,12 +81,8 @@ public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResp * file name after this the first one after this name. * @param maxFileCount The maximum number of files to return from this call. * The default value is 100, and the maximum allowed is 1000. - * @throws B2ApiException if the requested maximum number of files to be - * returned is greater than the allowable limit - * - * @throws B2ApiException if there was an error constructing the request */ - public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId, String startFileName, Integer maxFileCount) throws B2ApiException { + public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId, String startFileName, Integer maxFileCount) { super(client, b2AuthorizeAccountResponse, b2AuthorizeAccountResponse.getApiUrl() + B2_LIST_FILE_NAMES); this.addProperty(B2RequestProperties.KEY_BUCKET_ID, bucketId); @@ -101,10 +91,6 @@ public B2ListFileNamesRequest(CloseableHttpClient client, B2AuthorizeAccountResp this.addProperty(B2RequestProperties.KEY_START_FILE_NAME, URLEncoder.encode(startFileName)); } - if(maxFileCount > MAX_FILE_COUNT_RETURN) { - throw new B2ApiException("Maximum allowed return file count is " + MAX_FILE_COUNT_RETURN); - } - this.addProperty(B2RequestProperties.KEY_MAX_FILE_COUNT, maxFileCount); } diff --git a/src/main/java/synapticloop/b2/request/B2ListFileVersionsRequest.java b/src/main/java/synapticloop/b2/request/B2ListFileVersionsRequest.java index 197d952..761b060 100644 --- a/src/main/java/synapticloop/b2/request/B2ListFileVersionsRequest.java +++ b/src/main/java/synapticloop/b2/request/B2ListFileVersionsRequest.java @@ -50,10 +50,9 @@ public class B2ListFileVersionsRequest extends BaseB2Request { * @param client The HTTP client to use * @param b2AuthorizeAccountResponse the authorize account response * @param bucketId The id of the bucket to look for file names in. - * - * @throws B2ApiException if there was a problem creating the request */ - public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId) throws B2ApiException { + public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, + String bucketId) { this(client, b2AuthorizeAccountResponse, bucketId, DEFAULT_MAX_FILE_COUNT); } @@ -65,10 +64,9 @@ public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountR * @param bucketId The id of the bucket to look for file names in. * @param maxFileCount The maximum number of files to return from this call. * The default value is 100, and the maximum allowed is 1000. - * - * @throws B2ApiException if there was a problem creating the request */ - public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId, Integer maxFileCount) throws B2ApiException { + public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, + String bucketId, Integer maxFileCount) { this(client, b2AuthorizeAccountResponse, bucketId, maxFileCount, null, null); } @@ -89,30 +87,16 @@ public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountR * ID will be first in the list. * @param startFileId The first file ID to return. startFileName must also * be provided if startFileId is specified. (See startFileName.) - * - * @throws B2ApiException if there was an error creating the request */ - public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketId, Integer maxFileCount, String startFileName, String startFileId) throws B2ApiException { + public B2ListFileVersionsRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, + String bucketId, Integer maxFileCount, String startFileName, String startFileId) { super(client, b2AuthorizeAccountResponse, b2AuthorizeAccountResponse.getApiUrl() + B2_LIST_FILE_VERSIONS); - if(null != startFileId) { - if(null == startFileName) { - throw new B2ApiException(String.format("Must include a '%s', if you are also include a '%s'.", - B2RequestProperties.KEY_START_FILE_NAME, B2RequestProperties.KEY_START_FILE_ID)); - } - - } - this.addProperty(B2RequestProperties.KEY_BUCKET_ID, bucketId); - if(maxFileCount > MAX_FILE_COUNT_RETURN) { - throw new B2ApiException(String.format("Maximum return file count is %d", MAX_FILE_COUNT_RETURN)); - } - this.addProperty(B2RequestProperties.KEY_MAX_FILE_COUNT, maxFileCount); if(null != startFileName) { this.addProperty(B2RequestProperties.KEY_START_FILE_NAME, URLEncoder.encode(startFileName)); } - if(null != startFileId) { this.addProperty(B2RequestProperties.KEY_START_FILE_ID, startFileId); } diff --git a/src/main/java/synapticloop/b2/request/B2UploadFileRequest.java b/src/main/java/synapticloop/b2/request/B2UploadFileRequest.java index d4f9952..c94161b 100644 --- a/src/main/java/synapticloop/b2/request/B2UploadFileRequest.java +++ b/src/main/java/synapticloop/b2/request/B2UploadFileRequest.java @@ -51,10 +51,8 @@ */ public class B2UploadFileRequest extends BaseB2Request { private final HttpEntity entity; - private final String mimeType; - private final String fileName; - protected static final String CONTENT_TYPE_VALUE_B2_X_AUTO = "b2/x-auto"; + private static final String CONTENT_TYPE_VALUE_B2_X_AUTO = "b2/x-auto"; /** * Instantiate a upload file request in order to place a file on the B2 bucket, @@ -66,13 +64,11 @@ public class B2UploadFileRequest extends BaseB2Request { * @param fileName the name of the file * @param file the file to upload * @param fileInfo the file info map which are passed through as headers prefixed by "X-Bz-Info-" - * - * @throws B2ApiException if there was an error in the request */ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, - B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, Map fileInfo) throws B2ApiException { + B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String sha1Checksum, Map fileInfo) { - this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, null, fileInfo); + this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, sha1Checksum, null, fileInfo); } /** @@ -91,13 +87,11 @@ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountRespons * to automatically set the stored Content-Type post upload. In the case * where a file extension is absent or the lookup fails, the Content-Type * is set to application/octet-stream. - * - * @throws B2ApiException if there was an error in the request */ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, - B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String mimeType) throws B2ApiException { + B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String sha1Checksum, String mimeType) { - this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, mimeType, null); + this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, sha1Checksum, mimeType, null); } /** @@ -109,13 +103,11 @@ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountRespons * @param b2GetUploadUrlResponse the upload URL for this request * @param fileName the name of the file * @param file the file to upload - * - * @throws B2ApiException if there was an error in the request */ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, - B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file) throws B2ApiException { + B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String sha1Checksum) { - this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, null, null); + this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, sha1Checksum, null, null); } /** @@ -136,19 +128,18 @@ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountRespons * is set to application/octet-stream. * @param fileInfo the file info map which are passed through as headers * prefixed by "X-Bz-Info-" - * - * @throws B2ApiException if there was an error in the request */ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, - B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String mimeType, - Map fileInfo) throws B2ApiException { + B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, + String sha1Checksum, String mimeType, + Map fileInfo) { this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, new FileEntity(file), - ChecksumHelper.calculateSha1(file), + sha1Checksum, mimeType, fileInfo); } @@ -175,32 +166,19 @@ public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountRespons * is set to application/octet-stream. * @param fileInfo A map of information that will be stored with the file. Up * to 10 of keys may be present.. The same file info headers sent with the - * upload will be returned with the download. - * - * @throws B2ApiException If the number of file info headers is greater than the allowable + * upload will be returned with the download. */ - public B2UploadFileRequest(CloseableHttpClient client, - B2AuthorizeAccountResponse b2AuthorizeAccountResponse, + public B2UploadFileRequest(CloseableHttpClient client, B2AuthorizeAccountResponse b2AuthorizeAccountResponse, B2GetUploadUrlResponse b2GetUploadUrlResponse, - String fileName, - HttpEntity entity, - String sha1Checksum, - String mimeType, - Map fileInfo) throws B2ApiException { + String fileName, HttpEntity entity, String sha1Checksum, String mimeType, Map fileInfo) { super(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse.getUploadUrl()); - this.fileName = fileName; this.entity = entity; - this.mimeType = mimeType; - // now go through and add in the 'X-Bz-Info-*' headers + // Add 'X-Bz-Info-*' headers if(null != fileInfo) { int fileInfoSize = fileInfo.size(); - if(fileInfoSize > MAX_FILE_INFO_HEADERS) { - throw new B2ApiException(String.format("The maximum allowable file info size is '%d', you sent '%d'", MAX_FILE_INFO_HEADERS, fileInfoSize)); - } - for(final String key : fileInfo.keySet()) { this.addHeader(B2ResponseHeaders.HEADER_X_BZ_INFO_PREFIX + URLEncoder.encode(key), URLEncoder.encode(fileInfo.get(key))); } diff --git a/src/main/java/synapticloop/b2/request/BaseB2Request.java b/src/main/java/synapticloop/b2/request/BaseB2Request.java index 827380a..fc37dfe 100644 --- a/src/main/java/synapticloop/b2/request/BaseB2Request.java +++ b/src/main/java/synapticloop/b2/request/BaseB2Request.java @@ -54,9 +54,6 @@ public abstract class BaseB2Request { public static final String VALUE_APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; public static final String VALUE_UTF_8 = "UTF-8"; - public static final int MAX_FILE_COUNT_RETURN = 1000; - public static final int MAX_FILE_INFO_HEADERS = 10; - private final Map requestHeaders = new HashMap<>(); /** diff --git a/src/test/java/synapticloop/b2/helper/B2TestHelper.java b/src/test/java/synapticloop/b2/helper/B2TestHelper.java index d6933b2..5ed1fcd 100644 --- a/src/test/java/synapticloop/b2/helper/B2TestHelper.java +++ b/src/test/java/synapticloop/b2/helper/B2TestHelper.java @@ -1,27 +1,18 @@ package synapticloop.b2.helper; +import org.apache.http.impl.client.HttpClients; +import synapticloop.b2.BucketType; +import synapticloop.b2.exception.B2ApiException; +import synapticloop.b2.request.*; +import synapticloop.b2.response.*; +import synapticloop.b2.util.ChecksumHelper; + import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Map; import java.util.UUID; -import org.apache.http.impl.client.HttpClients; - -import synapticloop.b2.BucketType; -import synapticloop.b2.exception.B2ApiException; -import synapticloop.b2.request.B2AuthorizeAccountRequest; -import synapticloop.b2.request.B2CreateBucketRequest; -import synapticloop.b2.request.B2DeleteBucketRequest; -import synapticloop.b2.request.B2DeleteFileVersionRequest; -import synapticloop.b2.request.B2GetUploadUrlRequest; -import synapticloop.b2.request.B2UploadFileRequest; -import synapticloop.b2.response.B2AuthorizeAccountResponse; -import synapticloop.b2.response.B2BucketResponse; -import synapticloop.b2.response.B2DeleteFileVersionResponse; -import synapticloop.b2.response.B2FileResponse; -import synapticloop.b2.response.B2GetUploadUrlResponse; - public class B2TestHelper { public static final String B2_BUCKET_PREFIX = "b2api-test-"; public static final String B2_ACCOUNT_ID = "B2_ACCOUNT_ID"; @@ -129,7 +120,8 @@ public static B2FileResponse uploadTemporaryFileToBucket(String bucketId) throws } catch(IOException ioex) { throw new B2ApiException("Could not create temporary file", ioex); } - return(new B2UploadFileRequest(HttpClients.createDefault(), getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, file.getName(), file).getResponse()); + return(new B2UploadFileRequest(HttpClients.createDefault(), getB2AuthorizeAccountResponse(), + b2GetUploadUrlResponse, file.getName(), file, ChecksumHelper.calculateSha1(file)).getResponse()); } // public static B2FileResponse uploadTemporaryFileToBucketWithPath(String bucketId) throws B2Exception { @@ -170,7 +162,8 @@ public static B2FileResponse uploadTemporaryFileToBucket(String bucketId, Map