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

Commit af67e3c

Browse files
author
synapticloop
committed
Merge branch 'master' of ssh://github.com/synapticloop/backblaze-b2-java-api
2 parents 3f2e5a6 + 981aa57 commit af67e3c

10 files changed

Lines changed: 377 additions & 116 deletions

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

Lines changed: 167 additions & 45 deletions
Large diffs are not rendered by default.

src/main/java/synapticloop/b2/io/HttpMethodReleaseInputStream.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
* this source code or binaries.
1717
*/
1818

19-
import java.io.IOException;
20-
import java.util.logging.Level;
21-
import java.util.logging.Logger;
22-
2319
import org.apache.commons.io.input.CountingInputStream;
2420
import org.apache.http.HttpConnection;
2521
import org.apache.http.HttpResponse;
2622
import org.apache.http.client.methods.CloseableHttpResponse;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.io.IOException;
2727

2828
public class HttpMethodReleaseInputStream extends CountingInputStream {
29-
private static final Logger LOGGER = Logger.getLogger(HttpMethodReleaseInputStream.class.getName());
29+
private static final Logger LOGGER = LoggerFactory.getLogger(HttpMethodReleaseInputStream.class);
3030

3131
private HttpResponse response;
3232

@@ -58,10 +58,7 @@ public void close() throws IOException {
5858
// Fully consumed
5959
super.close();
6060
} else {
61-
if(LOGGER.isLoggable(Level.WARNING)) {
62-
LOGGER.warning(String.format("Abort connection for response '{}'", response));
63-
}
64-
61+
LOGGER.warn("Abort connection for response '{}'", response);
6562
// Close an HTTP response as quickly as possible, avoiding consuming
6663
// response data unnecessarily though at the expense of making underlying
6764
// connections unavailable for reuse.

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,15 @@ public B2FileInfoResponse(final JSONObject response) throws B2ApiException {
5858
this.contentLength = this.readLong(B2ResponseProperties.KEY_CONTENT_LENGTH);
5959
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
6060
this.contentSha1 = this.readString(B2ResponseProperties.KEY_CONTENT_SHA1);
61-
this.fileInfo = new HashMap<String, String>();
62-
63-
JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
64-
if(null != fileInfoObject) {
65-
Iterator keys = fileInfoObject.keys();
66-
while (keys.hasNext()) {
67-
String key = (String) keys.next();
68-
fileInfo.put(key, this.readString(fileInfoObject, key));
69-
}
70-
}
71-
61+
this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
7262
String action = this.readString(B2ResponseProperties.KEY_ACTION);
7363
if(null != action) {
7464
try {
7565
this.action = Action.valueOf(action);
7666
}
7767
catch(IllegalArgumentException e) {
7868
LOGGER.warn("Unknown action value " + action);
79-
this.action = null;
8069
}
81-
} else {
82-
// Default
83-
this.action = Action.upload;
8470
}
8571

8672
this.size = this.readLong(B2ResponseProperties.KEY_SIZE);

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

27+
import synapticloop.b2.Action;
2728
import synapticloop.b2.exception.B2ApiException;
2829

2930
public class B2FileResponse extends BaseB2Response {
@@ -37,6 +38,7 @@ public class B2FileResponse extends BaseB2Response {
3738
private final String contentSha1;
3839
private final String contentType;
3940
private final Map<String, String> fileInfo;
41+
private Action action;
4042

4143
/**
4244
* Instantiate a file response with the JSON response as a string from
@@ -57,15 +59,15 @@ public B2FileResponse(String json) throws B2ApiException {
5759
this.contentLength = this.readLong(B2ResponseProperties.KEY_CONTENT_LENGTH);
5860
this.contentSha1 = this.readString(B2ResponseProperties.KEY_CONTENT_SHA1);
5961
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
62+
this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
6063

61-
this.fileInfo = new HashMap<String, String>();
62-
63-
JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
64-
if(null != fileInfoObject) {
65-
Iterator keys = fileInfoObject.keys();
66-
while (keys.hasNext()) {
67-
String key = (String) keys.next();
68-
fileInfo.put(key, this.readString(fileInfoObject, key));
64+
String action = this.readString(B2ResponseProperties.KEY_ACTION);
65+
if(null != action) {
66+
try {
67+
this.action = Action.valueOf(action);
68+
}
69+
catch(IllegalArgumentException e) {
70+
LOGGER.warn("Unknown action value " + action);
6971
}
7072
}
7173

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class B2FinishLargeFileResponse extends BaseB2Response {
3838
private final String contentType;
3939

4040
private final Map<String, String> fileInfo;
41-
private final Action action;
41+
private Action action;
4242

4343
public B2FinishLargeFileResponse(final String json) throws B2ApiException {
4444
super(json);
@@ -50,23 +50,15 @@ public B2FinishLargeFileResponse(final String json) throws B2ApiException {
5050
this.contentLength = this.readLong(B2ResponseProperties.KEY_CONTENT_LENGTH);
5151
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
5252
this.contentSha1 = this.readString(B2ResponseProperties.KEY_CONTENT_SHA1);
53-
this.fileInfo = new HashMap<String, String>();
54-
55-
JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
56-
if(null != fileInfoObject) {
57-
Iterator keys = fileInfoObject.keys();
58-
while (keys.hasNext()) {
59-
String key = (String) keys.next();
60-
fileInfo.put(key, this.readString(fileInfoObject, key));
61-
}
62-
}
63-
53+
this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
6454
String action = this.readString(B2ResponseProperties.KEY_ACTION);
6555
if(null != action) {
66-
this.action = Action.valueOf(action);
67-
} else {
68-
// Default
69-
this.action = Action.upload;
56+
try {
57+
this.action = Action.valueOf(action);
58+
}
59+
catch(IllegalArgumentException e) {
60+
LOGGER.warn("Unknown action value " + action);
61+
}
7062
}
7163

7264
this.warnOnMissedKeys();

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class B2StartLargeFileResponse extends BaseB2Response {
3333
private final String accountId;
3434
private final String bucketId;
3535
private final String contentType;
36-
private final Map<String, Object> fileInfo;
36+
private final Map<String, String> fileInfo;
3737

3838
public B2StartLargeFileResponse(String json) throws B2ApiException {
3939
super(json);
@@ -43,15 +43,7 @@ public B2StartLargeFileResponse(String json) throws B2ApiException {
4343
this.accountId = this.readString(B2ResponseProperties.KEY_ACCOUNT_ID);
4444
this.bucketId = this.readString(B2ResponseProperties.KEY_BUCKET_ID);
4545
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
46-
this.fileInfo = new HashMap<String, Object>();
47-
JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
48-
if (null != fileInfoObject) {
49-
Iterator keys = fileInfoObject.keys();
50-
while (keys.hasNext()) {
51-
String key = (String) keys.next();
52-
fileInfo.put(key, fileInfoObject.opt(key));
53-
}
54-
}
46+
this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
5547

5648
this.warnOnMissedKeys();
5749
}
@@ -66,7 +58,7 @@ public B2StartLargeFileResponse(String json) throws B2ApiException {
6658

6759
public String getContentType() { return this.contentType; }
6860

69-
public Map<String, Object> getFileInfo() { return this.fileInfo; }
61+
public Map<String, String> getFileInfo() { return this.fileInfo; }
7062

7163
@Override
7264
protected Logger getLogger() { return LOGGER; }

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
/*
44
* Copyright (c) 2016 Synapticloop.
5-
*
5+
*
66
* All rights reserved.
7-
*
8-
* This code may contain contributions from other parties which, where
9-
* applicable, will be listed in the default build file for the project
7+
*
8+
* This code may contain contributions from other parties which, where
9+
* applicable, will be listed in the default build file for the project
1010
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
11-
*
12-
* This source code and any derived binaries are covered by the terms and
13-
* conditions of the Licence agreement ("the Licence"). You may not use this
14-
* source code or any derived binaries except in compliance with the Licence.
15-
* A copy of the Licence is available in the file named LICENSE.txt shipped with
11+
*
12+
* This source code and any derived binaries are covered by the terms and
13+
* conditions of the Licence agreement ("the Licence"). You may not use this
14+
* source code or any derived binaries except in compliance with the Licence.
15+
* A copy of the Licence is available in the file named LICENSE.txt shipped with
1616
* this source code or binaries.
1717
*/
1818

@@ -22,9 +22,12 @@
2222
import org.json.JSONException;
2323
import org.json.JSONObject;
2424
import org.slf4j.Logger;
25-
2625
import synapticloop.b2.exception.B2ApiException;
2726

27+
import java.util.HashMap;
28+
import java.util.Iterator;
29+
import java.util.Map;
30+
2831
public abstract class BaseB2Response {
2932
private final JSONObject response;
3033

@@ -170,6 +173,25 @@ protected JSONArray readObjects(String key) {
170173
return value instanceof JSONArray ? (JSONArray) value : null;
171174
}
172175

176+
/**
177+
* Read and remove JSONObject with key from JSON object
178+
*
179+
* @param key the key to read as a JSONObject and put keys and values into map
180+
*
181+
* @return the read keys and values (or null if it doesn't exist)
182+
*/
183+
protected Map<String, String> readMap(String key) {
184+
final Map<String, String> map = new HashMap<String, String>();
185+
JSONObject value = this.readObject(key);
186+
if (null == value || JSONObject.NULL == value) {
187+
getLogger().warn("No field for key {}", key);
188+
return null;
189+
}
190+
for (String k: value.keySet().toArray(new String[value.keySet().size()])) {
191+
map.put(k, this.readString(value, k));
192+
}
193+
return map;
194+
}
173195

174196
/**
175197
* Parse through the expected keys to determine whether any of the keys in

src/test/java/synapticloop/b2/request/B2StartLargeFileRequestTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
public class B2StartLargeFileRequestTest {
2323

24-
// this is expected until the large file support goes live
25-
@Test(expected=B2ApiException.class)
24+
@Test
2625
public void getResponse() throws Exception {
2726
B2AuthorizeAccountResponse b2AuthorizeAccountResponse = B2TestHelper.getB2AuthorizeAccountResponse();
2827

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package synapticloop.b2.response;
2+
3+
import org.json.JSONObject;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class B2FileInfoResponseTest {
9+
10+
@Test
11+
public void testGetFileInfo() throws Exception {
12+
final String json =
13+
" {\n" +
14+
" \"action\": \"upload\",\n" +
15+
" \"contentSha1\": \"f66ccfc5920c4527a3473cac8418e759fc991a1c\",\n" +
16+
" \"contentType\": \"image/jpeg\",\n" +
17+
" \"fileId\": \"4_zd866c2e0ac0d7d4855110b15_f10583db69f768e88_d20160122_m203004_c000_v0001016_t0021\",\n" +
18+
" \"fileInfo\": {\n" +
19+
" \"meta1\": \"This is some text for meta1.\",\n" +
20+
" \"meta2\": \"This is some more text for meta2.\",\n" +
21+
" \"unicorns-and-rainbows\": \"test header\"\n" +
22+
" },\n" +
23+
" \"fileName\": \"IMG_4666.jpg\",\n" +
24+
" \"size\": 1828156,\n" +
25+
" \"uploadTimestamp\": 1453494604000\n" +
26+
" }";
27+
final B2FileInfoResponse response = new B2FileInfoResponse(new JSONObject(json));
28+
assertNotNull(response);
29+
assertNotNull(response.getFileId());
30+
assertNull(response.getContentLength());
31+
assertNotNull(response.getAction());
32+
assertNotNull(response.getFileName());
33+
assertNotNull(response.getContentSha1());
34+
assertNotNull(response.getSize());
35+
assertNotNull(response.getFileInfo());
36+
assertFalse(response.getFileInfo().isEmpty());
37+
assertNotNull(response.getFileInfo().get("meta1"));
38+
assertNotNull(response.getFileInfo().get("meta1"));
39+
assertNotNull(response.getFileInfo().get("meta2"));
40+
assertNotNull(response.getFileInfo().get("unicorns-and-rainbows"));
41+
}
42+
}

0 commit comments

Comments
 (0)