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

Commit

Permalink
Merge branch 'master' of ssh://github.com/synapticloop/backblaze-b2-j…
Browse files Browse the repository at this point in the history
…ava-api
  • Loading branch information
synapticloop committed Jul 2, 2016
2 parents 3f2e5a6 + 981aa57 commit af67e3c
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 116 deletions.
212 changes: 167 additions & 45 deletions src/main/java/synapticloop/b2/B2ApiClient.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
* this source code or binaries.
*/

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.input.CountingInputStream;
import org.apache.http.HttpConnection;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class HttpMethodReleaseInputStream extends CountingInputStream {
private static final Logger LOGGER = Logger.getLogger(HttpMethodReleaseInputStream.class.getName());
private static final Logger LOGGER = LoggerFactory.getLogger(HttpMethodReleaseInputStream.class);

private HttpResponse response;

Expand Down Expand Up @@ -58,10 +58,7 @@ public void close() throws IOException {
// Fully consumed
super.close();
} else {
if(LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning(String.format("Abort connection for response '{}'", response));
}

LOGGER.warn("Abort connection for response '{}'", response);
// Close an HTTP response as quickly as possible, avoiding consuming
// response data unnecessarily though at the expense of making underlying
// connections unavailable for reuse.
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/synapticloop/b2/response/B2FileInfoResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,15 @@ public B2FileInfoResponse(final JSONObject response) throws B2ApiException {
this.contentLength = this.readLong(B2ResponseProperties.KEY_CONTENT_LENGTH);
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
this.contentSha1 = this.readString(B2ResponseProperties.KEY_CONTENT_SHA1);
this.fileInfo = new HashMap<String, String>();

JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
if(null != fileInfoObject) {
Iterator keys = fileInfoObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
fileInfo.put(key, this.readString(fileInfoObject, key));
}
}

this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
String action = this.readString(B2ResponseProperties.KEY_ACTION);
if(null != action) {
try {
this.action = Action.valueOf(action);
}
catch(IllegalArgumentException e) {
LOGGER.warn("Unknown action value " + action);
this.action = null;
}
} else {
// Default
this.action = Action.upload;
}

this.size = this.readLong(B2ResponseProperties.KEY_SIZE);
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/synapticloop/b2/response/B2FileResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import synapticloop.b2.Action;
import synapticloop.b2.exception.B2ApiException;

public class B2FileResponse extends BaseB2Response {
Expand All @@ -37,6 +38,7 @@ public class B2FileResponse extends BaseB2Response {
private final String contentSha1;
private final String contentType;
private final Map<String, String> fileInfo;
private Action action;

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

this.fileInfo = new HashMap<String, String>();

JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
if(null != fileInfoObject) {
Iterator keys = fileInfoObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
fileInfo.put(key, this.readString(fileInfoObject, key));
String action = this.readString(B2ResponseProperties.KEY_ACTION);
if(null != action) {
try {
this.action = Action.valueOf(action);
}
catch(IllegalArgumentException e) {
LOGGER.warn("Unknown action value " + action);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class B2FinishLargeFileResponse extends BaseB2Response {
private final String contentType;

private final Map<String, String> fileInfo;
private final Action action;
private Action action;

public B2FinishLargeFileResponse(final String json) throws B2ApiException {
super(json);
Expand All @@ -50,23 +50,15 @@ public B2FinishLargeFileResponse(final String json) throws B2ApiException {
this.contentLength = this.readLong(B2ResponseProperties.KEY_CONTENT_LENGTH);
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
this.contentSha1 = this.readString(B2ResponseProperties.KEY_CONTENT_SHA1);
this.fileInfo = new HashMap<String, String>();

JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
if(null != fileInfoObject) {
Iterator keys = fileInfoObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
fileInfo.put(key, this.readString(fileInfoObject, key));
}
}

this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
String action = this.readString(B2ResponseProperties.KEY_ACTION);
if(null != action) {
this.action = Action.valueOf(action);
} else {
// Default
this.action = Action.upload;
try {
this.action = Action.valueOf(action);
}
catch(IllegalArgumentException e) {
LOGGER.warn("Unknown action value " + action);
}
}

this.warnOnMissedKeys();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class B2StartLargeFileResponse extends BaseB2Response {
private final String accountId;
private final String bucketId;
private final String contentType;
private final Map<String, Object> fileInfo;
private final Map<String, String> fileInfo;

public B2StartLargeFileResponse(String json) throws B2ApiException {
super(json);
Expand All @@ -43,15 +43,7 @@ public B2StartLargeFileResponse(String json) throws B2ApiException {
this.accountId = this.readString(B2ResponseProperties.KEY_ACCOUNT_ID);
this.bucketId = this.readString(B2ResponseProperties.KEY_BUCKET_ID);
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
this.fileInfo = new HashMap<String, Object>();
JSONObject fileInfoObject = this.readObject(B2ResponseProperties.KEY_FILE_INFO);
if (null != fileInfoObject) {
Iterator keys = fileInfoObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
fileInfo.put(key, fileInfoObject.opt(key));
}
}
this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);

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

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

public Map<String, Object> getFileInfo() { return this.fileInfo; }
public Map<String, String> getFileInfo() { return this.fileInfo; }

@Override
protected Logger getLogger() { return LOGGER; }
Expand Down
42 changes: 32 additions & 10 deletions src/main/java/synapticloop/b2/response/BaseB2Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

/*
* Copyright (c) 2016 Synapticloop.
*
*
* All rights reserved.
*
* This code may contain contributions from other parties which, where
* applicable, will be listed in the default build file for the project
*
* This code may contain contributions from other parties which, where
* applicable, will be listed in the default build file for the project
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENSE.txt shipped with
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENSE.txt shipped with
* this source code or binaries.
*/

Expand All @@ -22,9 +22,12 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;

import synapticloop.b2.exception.B2ApiException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public abstract class BaseB2Response {
private final JSONObject response;

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

/**
* Read and remove JSONObject with key from JSON object
*
* @param key the key to read as a JSONObject and put keys and values into map
*
* @return the read keys and values (or null if it doesn't exist)
*/
protected Map<String, String> readMap(String key) {
final Map<String, String> map = new HashMap<String, String>();
JSONObject value = this.readObject(key);
if (null == value || JSONObject.NULL == value) {
getLogger().warn("No field for key {}", key);
return null;
}
for (String k: value.keySet().toArray(new String[value.keySet().size()])) {
map.put(k, this.readString(value, k));
}
return map;
}

/**
* Parse through the expected keys to determine whether any of the keys in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

public class B2StartLargeFileRequestTest {

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

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/synapticloop/b2/response/B2FileInfoResponseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package synapticloop.b2.response;

import org.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.*;

public class B2FileInfoResponseTest {

@Test
public void testGetFileInfo() throws Exception {
final String json =
" {\n" +
" \"action\": \"upload\",\n" +
" \"contentSha1\": \"f66ccfc5920c4527a3473cac8418e759fc991a1c\",\n" +
" \"contentType\": \"image/jpeg\",\n" +
" \"fileId\": \"4_zd866c2e0ac0d7d4855110b15_f10583db69f768e88_d20160122_m203004_c000_v0001016_t0021\",\n" +
" \"fileInfo\": {\n" +
" \"meta1\": \"This is some text for meta1.\",\n" +
" \"meta2\": \"This is some more text for meta2.\",\n" +
" \"unicorns-and-rainbows\": \"test header\"\n" +
" },\n" +
" \"fileName\": \"IMG_4666.jpg\",\n" +
" \"size\": 1828156,\n" +
" \"uploadTimestamp\": 1453494604000\n" +
" }";
final B2FileInfoResponse response = new B2FileInfoResponse(new JSONObject(json));
assertNotNull(response);
assertNotNull(response.getFileId());
assertNull(response.getContentLength());
assertNotNull(response.getAction());
assertNotNull(response.getFileName());
assertNotNull(response.getContentSha1());
assertNotNull(response.getSize());
assertNotNull(response.getFileInfo());
assertFalse(response.getFileInfo().isEmpty());
assertNotNull(response.getFileInfo().get("meta1"));
assertNotNull(response.getFileInfo().get("meta1"));
assertNotNull(response.getFileInfo().get("meta2"));
assertNotNull(response.getFileInfo().get("unicorns-and-rainbows"));
}
}
Loading

0 comments on commit af67e3c

Please sign in to comment.