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

Commit 35c04d9

Browse files
SynapticloopSynapticloop
authored andcommitted
refactoring and file downloading
1 parent be1f92d commit 35c04d9

18 files changed

Lines changed: 171 additions & 70 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package synapticloop.b2;
2+
3+
public enum ActionType {
4+
HIDE("hide"),
5+
UPLOAD("upload");
6+
7+
private final String actionType;
8+
9+
ActionType(String actionType) {
10+
this.actionType = actionType;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return(actionType);
16+
}
17+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import synapticloop.b2.request.B2ListFileVersionsRequest;
1616
import synapticloop.b2.request.B2UpdateBucketRequest;
1717
import synapticloop.b2.request.B2UploadFileRequest;
18-
import synapticloop.b2.request.BucketType;
1918
import synapticloop.b2.response.B2AuthorizeAccountResponse;
2019
import synapticloop.b2.response.B2BucketResponse;
2120
import synapticloop.b2.response.B2DeleteFileVersionResponse;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package synapticloop.b2;
2+
3+
public enum BucketType {
4+
ALL_PUBLIC("allPublic"),
5+
ALL_PRIVATE("allPrivate");
6+
7+
private final String bucketType;
8+
9+
BucketType(String bucketType) {
10+
this.bucketType = bucketType;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return(bucketType);
16+
}
17+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package synapticloop.b2.request;
22

3+
import synapticloop.b2.BucketType;
34
import synapticloop.b2.exception.B2ApiException;
45
import synapticloop.b2.response.B2AuthorizeAccountResponse;
56
import synapticloop.b2.response.B2BucketResponse;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class B2DownloadFileByIdRequest extends BaseB2Request {
2626
public B2DownloadFileByIdRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileId) {
2727
super(b2AuthorizeAccountResponse);
2828
url = b2AuthorizeAccountResponse.getDownloadUrl() + B2_DOWNLOAD_FILE_BY_ID;
29-
stringData.put(KEY_FILE_ID, fileId);
29+
parameters.put(KEY_FILE_ID, fileId);
3030
}
3131

3232
public B2DownloadFileByIdRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileId, File fileTo) {
@@ -35,6 +35,6 @@ public B2DownloadFileByIdRequest(B2AuthorizeAccountResponse b2AuthorizeAccountRe
3535
}
3636

3737
public B2DownloadFileResponse getResponse() throws B2ApiException {
38-
return(new B2DownloadFileResponse(executePostWithData(), fileTo));
38+
return(new B2DownloadFileResponse(executeGetWithData(), fileTo));
3939
}
4040
}

src/main/java/synapticloop/b2/request/B2DownloadFileByName.java renamed to src/main/java/synapticloop/b2/request/B2DownloadFileByNameRequest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,20 @@
2020
* @author synapticloop
2121
*/
2222

23-
public class B2DownloadFileByName extends BaseB2Request {
24-
25-
private static final String B2_DOWNLOAD_FILE_BY_ID = BASE_API_VERSION + "b2_download_file_by_id";
23+
public class B2DownloadFileByNameRequest extends BaseB2Request {
2624
private File fileTo = null;
2725

28-
public B2DownloadFileByName(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileName) {
26+
public B2DownloadFileByNameRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketName, String fileName) {
2927
super(b2AuthorizeAccountResponse);
30-
url = b2AuthorizeAccountResponse.getDownloadUrl() + B2_DOWNLOAD_FILE_BY_ID;
31-
stringData.put(KEY_FILE_ID, fileName);
28+
url = b2AuthorizeAccountResponse.getDownloadUrl() + "/file/" + bucketName + "/" + fileName;
3229
}
3330

34-
public B2DownloadFileByName(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileName, File fileTo) {
35-
this(b2AuthorizeAccountResponse, fileName);
31+
public B2DownloadFileByNameRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String bucketName, String fileName, File fileTo) {
32+
this(b2AuthorizeAccountResponse, bucketName, fileName);
3633
this.fileTo = fileTo;
3734
}
3835

3936
public B2DownloadFileResponse getResponse() throws B2ApiException {
40-
return(new B2DownloadFileResponse(executePostWithData(), fileTo));
37+
return(new B2DownloadFileResponse(executeGetWithData(), fileTo));
4138
}
4239
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package synapticloop.b2.request;
22

3+
import synapticloop.b2.BucketType;
34
import synapticloop.b2.exception.B2ApiException;
45
import synapticloop.b2.response.B2AuthorizeAccountResponse;
56
import synapticloop.b2.response.B2BucketResponse;

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
57
import java.util.HashMap;
68
import java.util.Iterator;
79
import java.util.Map;
@@ -11,6 +13,7 @@
1113
import org.apache.http.client.methods.HttpGet;
1214
import org.apache.http.client.methods.HttpPost;
1315
import org.apache.http.client.methods.HttpRequestBase;
16+
import org.apache.http.client.utils.URIBuilder;
1417
import org.apache.http.entity.FileEntity;
1518
import org.apache.http.entity.StringEntity;
1619
import org.apache.http.impl.client.CloseableHttpClient;
@@ -59,6 +62,7 @@ public class BaseB2Request {
5962

6063
protected String url = null;
6164
protected Map<String, String> headers = new HashMap<String, String>();
65+
protected Map<String, String> parameters = new HashMap<String, String>();
6266

6367
protected Map<String, String> stringData = new HashMap<String, String>();
6468
protected Map<String, Integer> integerData = new HashMap<String, Integer>();
@@ -120,7 +124,40 @@ protected String executeGet() throws B2ApiException {
120124
} catch (IOException ex) {
121125
throw new B2ApiException(ex);
122126
}
127+
}
128+
129+
protected CloseableHttpResponse executeGetWithData() throws B2ApiException {
130+
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
131+
132+
133+
try {
134+
URI uri = new URIBuilder(url).build();
135+
LOG.debug("GET request to URL '{}'", url);
136+
137+
Iterator<String> iterator = parameters.keySet().iterator();
138+
while (iterator.hasNext()) {
139+
String key = (String) iterator.next();
140+
// @TODO - this is kind of wasteful - refactor
141+
uri = new URIBuilder(uri).addParameter(key,parameters.get(key)).build();
142+
}
143+
144+
HttpGet httpGet = new HttpGet(uri);
145+
setHeaders(httpGet);
146+
147+
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet);
148+
int statusCode = httpResponse.getStatusLine().getStatusCode();
149+
String response = EntityUtils.toString(httpResponse.getEntity());
123150

151+
LOG.debug("Received status code of:{}, for GET request to url '{}'", statusCode, url);
152+
153+
if(statusCode != 200) {
154+
throw new B2ApiException(response);
155+
} else {
156+
return(httpResponse);
157+
}
158+
} catch (IOException | URISyntaxException ex) {
159+
throw new B2ApiException(ex);
160+
}
124161
}
125162

126163
protected String executePost() throws B2ApiException {
@@ -182,32 +219,6 @@ protected String executePost(File file) throws B2ApiException {
182219
}
183220
}
184221

185-
protected CloseableHttpResponse executePostWithData() throws B2ApiException {
186-
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
187-
HttpPost httpPost = new HttpPost(url);
188-
setHeaders(httpPost);
189-
190-
String postData = getPostData();
191-
LOG.debug("POST request to URL '{}', with data of '{}'", url, postData);
192-
193-
try {
194-
httpPost.setEntity(new StringEntity(postData));
195-
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpPost);
196-
int statusCode = httpResponse.getStatusLine().getStatusCode();
197-
String response = EntityUtils.toString(httpResponse.getEntity());
198-
199-
LOG.debug("Received status code of:{}, for POST request to url '{}'", statusCode, url);
200-
201-
if(statusCode != 200) {
202-
throw new B2ApiException(response);
203-
} else {
204-
return(httpResponse);
205-
}
206-
} catch (IOException ex) {
207-
throw new B2ApiException(ex);
208-
}
209-
}
210-
211222
/**
212223
* Set the headers safely, go through the headers Map and add them to the http
213224
* request. If they already exist on the http request, it will be ignored.
@@ -219,6 +230,7 @@ private void setHeaders(HttpRequestBase httpRequestBase) {
219230
Set<String> headerKeySet = headers.keySet();
220231
for (String headerKey : headerKeySet) {
221232
if(!httpRequestBase.containsHeader(headerKey)) {
233+
LOG.trace("Setting header '" + headerKey + "'.");
222234
httpRequestBase.setHeader(headerKey, headers.get(headerKey));
223235
}
224236
}

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Map;
88

99
import org.apache.commons.io.FileUtils;
10+
import org.apache.commons.io.IOUtils;
1011
import org.apache.http.Header;
1112
import org.apache.http.client.methods.CloseableHttpResponse;
1213

@@ -29,7 +30,7 @@ public class B2DownloadFileResponse extends BaseB2Response {
2930
headerLookup.put("X-Bz-Content-Sha1", HEADER_X_BZ_CONTENT_SHA1);
3031
}
3132

32-
private InputStream content = null;
33+
private byte[] content = null;
3334
private File fileTo = null;
3435
private Integer contentLength = null;
3536
private String contentType = null;
@@ -42,18 +43,21 @@ public class B2DownloadFileResponse extends BaseB2Response {
4243
public B2DownloadFileResponse(CloseableHttpResponse closeableHttpResponse, File fileTo) throws B2ApiException {
4344
this.fileTo = fileTo;
4445

46+
InputStream inputStream = null;
4547
try {
46-
this.content = closeableHttpResponse.getEntity().getContent();
48+
inputStream = closeableHttpResponse.getEntity().getContent();
4749
parseHeaders(closeableHttpResponse);
4850

4951
if(null != fileTo) {
5052
// write the contents to the file
51-
FileUtils.copyInputStreamToFile(content, fileTo);
52-
content.close();
53-
content = null;
53+
FileUtils.copyInputStreamToFile(inputStream, fileTo);
54+
} else {
55+
content = IOUtils.toByteArray(inputStream);
5456
}
5557
} catch (IllegalStateException | IOException ex) {
5658
throw new B2ApiException("Could not retrieve response", ex);
59+
} finally {
60+
IOUtils.closeQuietly(inputStream);
5761
}
5862
}
5963

@@ -92,7 +96,7 @@ private void parseHeaders(CloseableHttpResponse closeableHttpResponse) throws B2
9296
}
9397
}
9498

95-
public InputStream getContent() {
99+
public byte[] getContent() {
96100
return this.content;
97101
}
98102

0 commit comments

Comments
 (0)