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

Commit

Permalink
Merge branch 'pr/12'
Browse files Browse the repository at this point in the history
  • Loading branch information
Synapticloop authored and Synapticloop committed Mar 7, 2016
2 parents 0929d7e + b8b3bcc commit 9b26201
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 124 deletions.
13 changes: 9 additions & 4 deletions src/main/java/synapticloop/b2/B2ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, String> 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();
}

/**
Expand All @@ -344,7 +346,8 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file, St

public B2FileResponse uploadFile(String bucketId, String fileName, File file, Map<String, String> 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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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();
}


Expand Down
20 changes: 3 additions & 17 deletions src/main/java/synapticloop/b2/request/B2ListFileNamesRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down
52 changes: 15 additions & 37 deletions src/main/java/synapticloop/b2/request/B2UploadFileRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String, String> fileInfo) throws B2ApiException {
B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String sha1Checksum, Map<String, String> fileInfo) {

this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, null, fileInfo);
this(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file, sha1Checksum, null, fileInfo);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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<String, String> fileInfo) throws B2ApiException {
B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file,
String sha1Checksum, String mimeType,
Map<String, String> fileInfo) {

this(client,
b2AuthorizeAccountResponse,
b2GetUploadUrlResponse,
fileName,
new FileEntity(file),
ChecksumHelper.calculateSha1(file),
sha1Checksum,
mimeType, fileInfo);
}

Expand All @@ -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<String, String> fileInfo) throws B2ApiException {
String fileName, HttpEntity entity, String sha1Checksum, String mimeType, Map<String, String> 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)));
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/synapticloop/b2/request/BaseB2Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> requestHeaders = new HashMap<>();

/**
Expand Down
29 changes: 11 additions & 18 deletions src/test/java/synapticloop/b2/helper/B2TestHelper.java
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -170,7 +162,8 @@ public static B2FileResponse uploadTemporaryFileToBucket(String bucketId, Map<St
} catch(IOException ioex) {
throw new B2ApiException("Could not create temporary file", ioex);
}
return(new B2UploadFileRequest(HttpClients.createDefault(), getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, file.getName(), file, fileInfo).getResponse());
return(new B2UploadFileRequest(HttpClients.createDefault(), getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, file.getName(), file,
ChecksumHelper.calculateSha1(file), fileInfo).getResponse());
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package synapticloop.b2.request;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;

import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.helper.B2TestHelper;
import synapticloop.b2.response.B2BucketResponse;
import synapticloop.b2.response.B2DeleteFileVersionResponse;
import synapticloop.b2.response.B2DownloadFileResponse;
import synapticloop.b2.response.B2FileResponse;
import synapticloop.b2.response.B2GetUploadUrlResponse;
import synapticloop.b2.response.*;
import synapticloop.b2.util.ChecksumHelper;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class B2DownloadFileRequestTest {
private B2BucketResponse randomPrivateBucket = null;
Expand All @@ -36,7 +32,7 @@ public void testDownloadByFilePath() throws Exception {
B2FileResponse b2UploadFileResponse = new B2UploadFileRequest(HttpClients.createDefault(),
B2TestHelper.getB2AuthorizeAccountResponse(),
b2GetUploadUrlResponse,
testFileName, file).getResponse();
testFileName, file, ChecksumHelper.calculateSha1(file)).getResponse();

String fileName = b2UploadFileResponse.getFileName();
String fileId = b2UploadFileResponse.getFileId();
Expand Down
Loading

0 comments on commit 9b26201

Please sign in to comment.