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

Commit 981aa57

Browse files
authored
Merge pull request #27 from iterate-ch/bugfix/concurrent-modification
Bugfix/concurrent modification
2 parents 7c8c934 + 73adbde commit 981aa57

7 files changed

Lines changed: 176 additions & 44 deletions

File tree

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,7 @@ 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 {

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,7 @@ public B2FileResponse(String json) throws B2ApiException {
5959
this.contentLength = this.readLong(B2ResponseProperties.KEY_CONTENT_LENGTH);
6060
this.contentSha1 = this.readString(B2ResponseProperties.KEY_CONTENT_SHA1);
6161
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
62-
63-
this.fileInfo = new HashMap<String, String>();
64-
65-
JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
66-
if(null != fileInfoObject) {
67-
Iterator keys = fileInfoObject.keys();
68-
while (keys.hasNext()) {
69-
String key = (String) keys.next();
70-
fileInfo.put(key, this.readString(fileInfoObject, key));
71-
}
72-
}
62+
this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
7363

7464
String action = this.readString(B2ResponseProperties.KEY_ACTION);
7565
if(null != action) {

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,7 @@ 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) {
6656
try {

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import org.slf4j.Logger;
2323
import synapticloop.b2.exception.B2ApiException;
2424

25+
import java.util.HashMap;
2526
import java.util.Iterator;
27+
import java.util.Map;
2628

2729
public abstract class BaseB2Response {
2830
private final JSONObject response;
@@ -169,6 +171,25 @@ protected JSONArray readObjects(String key) {
169171
return value instanceof JSONArray ? (JSONArray) value : null;
170172
}
171173

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

173194
/**
174195
* Parse through the expected keys to determine whether any of the keys in
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+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package synapticloop.b2.response;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class B2ListFilesResponseTest {
8+
9+
@Test
10+
public void testGetFiles() throws Exception {
11+
final String json = "{\n" +
12+
" \"files\": [\n" +
13+
" {\n" +
14+
" \"action\": \"upload\",\n" +
15+
" \"contentSha1\": \"e73f8339c3e731e3fd9b0bec46222bd0016f1afa\",\n" +
16+
" \"contentType\": \"image/jpeg\",\n" +
17+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f112de56cdcbb09e8_d20160120_m172133_c000_v0001010_t0006\",\n" +
18+
" \"fileInfo\": {\n" +
19+
" \"src_last_modified_millis\": \"0\"\n" +
20+
" },\n" +
21+
" \"fileName\": \"IMG_5066.jpg\",\n" +
22+
" \"size\": 180903,\n" +
23+
" \"uploadTimestamp\": 1453310493000\n" +
24+
" },\n" +
25+
" {\n" +
26+
" \"action\": \"upload\",\n" +
27+
" \"contentSha1\": \"89d2b373a7b26dbec209fe2db5c0ca6557cb1a8d\",\n" +
28+
" \"contentType\": \"video/mp4\",\n" +
29+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f1092070825b2ec14_d20151219_m191222_c000_v0001014_t0038\",\n" +
30+
" \"fileInfo\": {},\n" +
31+
" \"fileName\": \"adele/BBC.Music.Presents.Adele.at.the.BBC.2015.HDTV.x264-NoGRP.mp4\",\n" +
32+
" \"size\": 536107168,\n" +
33+
" \"uploadTimestamp\": 1450552342000\n" +
34+
" },\n" +
35+
" {\n" +
36+
" \"action\": \"upload\",\n" +
37+
" \"contentSha1\": \"7201a995b6841a84fe23086b33d50e49a627fabe\",\n" +
38+
" \"contentType\": \"application/octet-stream\",\n" +
39+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f109c8fe9ff88885f_d20160106_m005102_c000_v0001014_t0041\",\n" +
40+
" \"fileInfo\": {},\n" +
41+
" \"fileName\": \"b2sync.tar.gz\",\n" +
42+
" \"size\": 1515462,\n" +
43+
" \"uploadTimestamp\": 1452041462000\n" +
44+
" },\n" +
45+
" {\n" +
46+
" \"action\": \"upload\",\n" +
47+
" \"contentSha1\": \"0a9d332d09376d28cf04726e146aeedc546b09cf\",\n" +
48+
" \"contentType\": \"image/png\",\n" +
49+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f10695c6a45107f47_d20151227_m200935_c000_v0001014_t0026\",\n" +
50+
" \"fileInfo\": {},\n" +
51+
" \"fileName\": \"logo/selligy-icon-square_360.png\",\n" +
52+
" \"size\": 7583,\n" +
53+
" \"uploadTimestamp\": 1451246975000\n" +
54+
" },\n" +
55+
" {\n" +
56+
" \"action\": \"upload\",\n" +
57+
" \"contentSha1\": \"f2cf229c6657ca2b17afb9af22090c92cf9a7d2f\",\n" +
58+
" \"contentType\": \"application/octet-stream\",\n" +
59+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f1189dc406f02d4d9_d20160104_m040533_c000_v0001014_t0004\",\n" +
60+
" \"fileInfo\": {},\n" +
61+
" \"fileName\": \"uploads-large.tar.gz\",\n" +
62+
" \"size\": 236324399,\n" +
63+
" \"uploadTimestamp\": 1451880333000\n" +
64+
" },\n" +
65+
" {\n" +
66+
" \"action\": \"upload\",\n" +
67+
" \"contentSha1\": \"7d9133df91610fd610d817b316db01c68b988dfd\",\n" +
68+
" \"contentType\": \"application/octet-stream\",\n" +
69+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f1189dc406f02d4d7_d20160104_m040413_c000_v0001014_t0004\",\n" +
70+
" \"fileInfo\": {},\n" +
71+
" \"fileName\": \"uploads-small.tar.gz\",\n" +
72+
" \"size\": 1216666,\n" +
73+
" \"uploadTimestamp\": 1451880253000\n" +
74+
" },\n" +
75+
" {\n" +
76+
" \"action\": \"upload\",\n" +
77+
" \"contentSha1\": \"07ecb3b3f48025118b314f5c7e169b0ca96bc3f1\",\n" +
78+
" \"contentType\": \"application/octet-stream\",\n" +
79+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f1189dc406f02d4d8_d20160104_m040432_c000_v0001014_t0004\",\n" +
80+
" \"fileInfo\": {},\n" +
81+
" \"fileName\": \"uploads-smallmedium.tar.gz\",\n" +
82+
" \"size\": 40941493,\n" +
83+
" \"uploadTimestamp\": 1451880272000\n" +
84+
" },\n" +
85+
" {\n" +
86+
" \"action\": \"upload\",\n" +
87+
" \"contentSha1\": \"b63f91f826e87887f4ce5c28ee6988cab8a0f3f4\",\n" +
88+
" \"contentType\": \"application/octet-stream\",\n" +
89+
" \"fileId\": \"4_z181632c04c2ddde855010b15_f1189dc406f02d4db_d20160104_m043616_c000_v0001014_t0004\",\n" +
90+
" \"fileInfo\": {},\n" +
91+
" \"fileName\": \"uploads-tiny.tar.gz\",\n" +
92+
" \"size\": 1130,\n" +
93+
" \"uploadTimestamp\": 1451882176000\n" +
94+
" }\n" +
95+
" ],\n" +
96+
" \"nextFileId\": null,\n" +
97+
" \"nextFileName\": null\n" +
98+
"}";
99+
final B2ListFilesResponse response = new B2ListFilesResponse(json);
100+
assertNotNull(response);
101+
assertNotNull(response.getFiles());
102+
assertFalse(response.getFiles().isEmpty());
103+
assertEquals(8, response.getFiles().size());
104+
assertNull(response.getNextFileId());
105+
assertNull(response.getNextFileName());
106+
}
107+
}

0 commit comments

Comments
 (0)