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

Commit be1f92d

Browse files
SynapticloopSynapticloop
authored andcommitted
downloading of files
1 parent da48e19 commit be1f92d

5 files changed

Lines changed: 212 additions & 8 deletions

File tree

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package synapticloop.b2.request;
22

3+
import java.io.File;
4+
5+
import synapticloop.b2.exception.B2ApiException;
36
import synapticloop.b2.response.B2AuthorizeAccountResponse;
7+
import synapticloop.b2.response.B2DownloadFileResponse;
48

59
/**
610
* <p>Downloads one file from B2.</p>
@@ -16,15 +20,21 @@
1620
*/
1721

1822
public class B2DownloadFileByIdRequest extends BaseB2Request {
19-
private static final String B2_DOWNLOAD_FILE_BY_ID = BASE_API_VERSION + "b2_delete_file_version";
23+
private static final String B2_DOWNLOAD_FILE_BY_ID = BASE_API_VERSION + "b2_download_file_by_id";
24+
private File fileTo = null;
2025

2126
public B2DownloadFileByIdRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileId) {
2227
super(b2AuthorizeAccountResponse);
2328
url = b2AuthorizeAccountResponse.getDownloadUrl() + B2_DOWNLOAD_FILE_BY_ID;
2429
stringData.put(KEY_FILE_ID, fileId);
2530
}
26-
27-
public void getResponse() {
28-
//new B2Do
31+
32+
public B2DownloadFileByIdRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileId, File fileTo) {
33+
this(b2AuthorizeAccountResponse, fileId);
34+
this.fileTo = fileTo;
35+
}
36+
37+
public B2DownloadFileResponse getResponse() throws B2ApiException {
38+
return(new B2DownloadFileResponse(executePostWithData(), fileTo));
2939
}
3040
}

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

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

3+
import java.io.File;
4+
5+
import synapticloop.b2.exception.B2ApiException;
6+
import synapticloop.b2.response.B2AuthorizeAccountResponse;
7+
import synapticloop.b2.response.B2DownloadFileResponse;
8+
39
/**
410
* <p>Downloads one file by providing the name of the bucket and the name of the file.</p>
511
*
@@ -14,9 +20,23 @@
1420
* @author synapticloop
1521
*/
1622

17-
public class B2DownloadFileByName {
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";
26+
private File fileTo = null;
27+
28+
public B2DownloadFileByName(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileName) {
29+
super(b2AuthorizeAccountResponse);
30+
url = b2AuthorizeAccountResponse.getDownloadUrl() + B2_DOWNLOAD_FILE_BY_ID;
31+
stringData.put(KEY_FILE_ID, fileName);
32+
}
33+
34+
public B2DownloadFileByName(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, String fileName, File fileTo) {
35+
this(b2AuthorizeAccountResponse, fileName);
36+
this.fileTo = fileTo;
37+
}
1838

19-
public B2DownloadFileByName() {
20-
39+
public B2DownloadFileResponse getResponse() throws B2ApiException {
40+
return(new B2DownloadFileResponse(executePostWithData(), fileTo));
2141
}
2242
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import java.io.File;
44
import java.io.UnsupportedEncodingException;
55
import java.net.URLEncoder;
6+
import java.util.Iterator;
7+
import java.util.Map;
68

79
import synapticloop.b2.exception.B2ApiException;
810
import synapticloop.b2.response.B2AuthorizeAccountResponse;
9-
import synapticloop.b2.response.B2GetUploadUrlResponse;
1011
import synapticloop.b2.response.B2FileResponse;
12+
import synapticloop.b2.response.B2GetUploadUrlResponse;
1113
import synapticloop.b2.util.Helper;
1214

1315
/**
@@ -26,20 +28,40 @@ public class B2UploadFileRequest extends BaseB2Request {
2628
private String fileName = null;
2729
private String mimeType = null;
2830
private String authorizationToken = null;
31+
private Map<String, String> fileInfo = null;
2932

3033
public B2UploadFileRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, B2GetUploadUrlResponse b2GetUploadUrlResponse, File file) {
3134
super(b2AuthorizeAccountResponse);
3235
this.url = b2GetUploadUrlResponse.getUploadUrl();
3336
this.authorizationToken = b2GetUploadUrlResponse.getAuthorizationToken();
3437
this.file = file;
3538
this.fileName = file.getName();
39+
40+
// now go through and add in the 'X-Bz-Info-*' headers
41+
if(null != fileInfo) {
42+
Iterator<String> iterator = fileInfo.keySet().iterator();
43+
while (iterator.hasNext()) {
44+
String key = (String) iterator.next();
45+
headers.put("X-Bz-Info-" + key, fileInfo.get(key));
46+
}
47+
}
48+
}
49+
50+
51+
public B2UploadFileRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, B2GetUploadUrlResponse b2GetUploadUrlResponse, File file, Map<String, String> fileInfo) {
52+
super(b2AuthorizeAccountResponse);
53+
this.fileInfo = fileInfo;
3654
}
3755

3856
public B2UploadFileRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, B2GetUploadUrlResponse b2GetUploadUrlResponse, File file, String mimeType) {
3957
this(b2AuthorizeAccountResponse, b2GetUploadUrlResponse, file);
4058
this.mimeType = mimeType;
4159
}
4260

61+
public B2UploadFileRequest(B2AuthorizeAccountResponse b2AuthorizeAccountResponse, B2GetUploadUrlResponse b2GetUploadUrlResponse, File file, String mimeType, Map<String, String> fileInfo) {
62+
this(b2AuthorizeAccountResponse, b2GetUploadUrlResponse, file, fileInfo);
63+
}
64+
4365
public B2FileResponse getResponse() throws B2ApiException {
4466
if(null == mimeType) {
4567
headers.put(HEADER_CONTENT_TYPE, VALUE_B2_X_AUTO);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,32 @@ protected String executePost(File file) throws B2ApiException {
182182
}
183183
}
184184

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+
185211
/**
186212
* Set the headers safely, go through the headers Map and add them to the http
187213
* request. If they already exist on the http request, it will be ignored.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package synapticloop.b2.response;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import org.apache.commons.io.FileUtils;
10+
import org.apache.http.Header;
11+
import org.apache.http.client.methods.CloseableHttpResponse;
12+
13+
import synapticloop.b2.exception.B2ApiException;
14+
15+
public class B2DownloadFileResponse extends BaseB2Response {
16+
private static final String HEADER_X_BZ_INFO_PREFIX = "X-Bz-Info-";
17+
private static final int HEADER_CONTENT_LENGTH = 0;
18+
private static final int HEADER_CONTENT_TYPE = 1;
19+
private static final int HEADER_X_BZ_FILE_ID = 2;
20+
private static final int HEADER_X_BZ_FILE_NAME = 3;
21+
private static final int HEADER_X_BZ_CONTENT_SHA1 = 4;
22+
23+
private static final Map<String, Integer> headerLookup = new HashMap<String, Integer>();
24+
static {
25+
headerLookup.put("Content-Length", HEADER_CONTENT_LENGTH);
26+
headerLookup.put("Content-Type", HEADER_CONTENT_TYPE);
27+
headerLookup.put("X-Bz-File-Id", HEADER_X_BZ_FILE_ID);
28+
headerLookup.put("X-Bz-File-Name", HEADER_X_BZ_FILE_NAME);
29+
headerLookup.put("X-Bz-Content-Sha1", HEADER_X_BZ_CONTENT_SHA1);
30+
}
31+
32+
private InputStream content = null;
33+
private File fileTo = null;
34+
private Integer contentLength = null;
35+
private String contentType = null;
36+
private String fileId = null;
37+
private String fileName = null;
38+
private String contentSha1 = null;
39+
40+
private Map<String, String> fileInfo = new HashMap<String, String>();
41+
42+
public B2DownloadFileResponse(CloseableHttpResponse closeableHttpResponse, File fileTo) throws B2ApiException {
43+
this.fileTo = fileTo;
44+
45+
try {
46+
this.content = closeableHttpResponse.getEntity().getContent();
47+
parseHeaders(closeableHttpResponse);
48+
49+
if(null != fileTo) {
50+
// write the contents to the file
51+
FileUtils.copyInputStreamToFile(content, fileTo);
52+
content.close();
53+
content = null;
54+
}
55+
} catch (IllegalStateException | IOException ex) {
56+
throw new B2ApiException("Could not retrieve response", ex);
57+
}
58+
}
59+
60+
private void parseHeaders(CloseableHttpResponse closeableHttpResponse) throws B2ApiException {
61+
Header[] allHeaders = closeableHttpResponse.getAllHeaders();
62+
for (Header header : allHeaders) {
63+
String headerName = header.getName();
64+
String headerValue = header.getValue();
65+
if(headerLookup.containsKey(headerName)) {
66+
switch (headerLookup.get(headerName)) {
67+
case HEADER_CONTENT_LENGTH:
68+
contentLength = Integer.parseInt(headerValue);
69+
break;
70+
case HEADER_CONTENT_TYPE:
71+
contentType = headerValue;
72+
break;
73+
case HEADER_X_BZ_CONTENT_SHA1:
74+
contentSha1 = headerValue;
75+
break;
76+
case HEADER_X_BZ_FILE_ID:
77+
fileId = headerValue;
78+
break;
79+
case HEADER_X_BZ_FILE_NAME:
80+
fileName = headerValue;
81+
break;
82+
default:
83+
throw new B2ApiException("Unknown header for lookup '" + headerName + "'");
84+
}
85+
} else {
86+
// could not find it in the lookup, need to only look for 'X-Bz-Info-*'
87+
// headers
88+
if(headerName.startsWith(HEADER_X_BZ_INFO_PREFIX)) {
89+
fileInfo.put(headerName.substring(HEADER_X_BZ_INFO_PREFIX.length()), headerValue);
90+
}
91+
}
92+
}
93+
}
94+
95+
public InputStream getContent() {
96+
return this.content;
97+
}
98+
99+
public File getFileTo() {
100+
return this.fileTo;
101+
}
102+
103+
public Integer getContentLength() {
104+
return this.contentLength;
105+
}
106+
107+
public String getContentType() {
108+
return this.contentType;
109+
}
110+
111+
public String getFileId() {
112+
return this.fileId;
113+
}
114+
115+
public String getFileName() {
116+
return this.fileName;
117+
}
118+
119+
public String getContentSha1() {
120+
return this.contentSha1;
121+
}
122+
123+
public Map<String, String> getFileInfo() {
124+
return this.fileInfo;
125+
}
126+
}

0 commit comments

Comments
 (0)