Skip to content

Commit dc8c848

Browse files
authored
Merge pull request #60 from Bandwidth/DX-2456
DX-2456 Overload `FileWrapper` class to accept a `bytes[]` object
2 parents 054a399 + ef6c112 commit dc8c848

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

src/main/java/com/bandwidth/http/client/OkClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,14 @@ private okhttp3.Request convertRequest(HttpRequest httpRequest) {
255255
httpRequest.getHeaders().add("content-type", contentType);
256256
}
257257

258-
requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse(contentType),
259-
((FileWrapper) body).getFile());
258+
if (file.getFile() != null) {
259+
requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse(contentType),
260+
((FileWrapper) body).getFile());
261+
} else {
262+
requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse(contentType),
263+
((FileWrapper) body).getFileStream());
264+
}
265+
260266
} else {
261267
// set request body
262268
if (!httpRequest.getHeaders().has("content-type")) {

src/main/java/com/bandwidth/utilities/FileWrapper.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88

99
import com.fasterxml.jackson.annotation.JsonInclude;
1010
import java.io.File;
11+
import java.io.FileInputStream;
1112

1213
/**
1314
* Class to wrap file and contentType to be sent as part of a HTTP request.
1415
*/
1516
public class FileWrapper {
1617

17-
@JsonInclude(JsonInclude.Include.NON_NULL)
18+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1819
private File file;
20+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
21+
private byte[] fileStream;
1922
@JsonInclude(JsonInclude.Include.NON_NULL)
2023
private String contentType;
2124

@@ -26,6 +29,17 @@ public class FileWrapper {
2629
*/
2730
public FileWrapper(File file, String contentType) {
2831
this.file = file;
32+
this.fileStream = null;
33+
this.contentType = contentType;
34+
}
35+
/**
36+
* Initialization constructor.
37+
* @param fileStream File Input Stream object to be wrapped
38+
* @param contentType content type of file
39+
*/
40+
public FileWrapper(byte[] fileStream, String contentType) {
41+
this.file = null;
42+
this.fileStream = fileStream;
2943
this.contentType = contentType;
3044
}
3145

@@ -37,13 +51,25 @@ public FileWrapper(File file) {
3751
this.file = file;
3852
}
3953

54+
/**
55+
* Initialization constructor.
56+
* @param fileStream File object to be wrapped
57+
*/
58+
public FileWrapper(byte[] fileStream) {
59+
this.fileStream = fileStream;
60+
}
61+
4062
/**
4163
* Getter for file.
4264
* @return File instance
4365
*/
44-
public File getFile() {
45-
return file;
46-
}
66+
public File getFile() { return file; }
67+
68+
/**
69+
* Getter for fileStream.
70+
* @return byte[] instance
71+
*/
72+
public byte[] getFileStream() { return fileStream; }
4773

4874
/**
4975
* Getter for content type.

src/test/java/com/bandwidth/MessagingApiTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.io.ByteArrayOutputStream;
1212
import java.io.File;
13+
import java.io.FileInputStream;
1314
import java.io.InputStream;
1415
import java.nio.file.Files;
1516
import java.util.Collections;
@@ -108,6 +109,38 @@ public void testUploadDownloadDeleteMedia() throws Exception {
108109
assertEquals("Response Code is not 204", 204, deleteMediaApiResponse.getStatusCode());
109110
}
110111

112+
@Test
113+
public void testUploadDownloadDeleteBinaryMedia() throws Exception {
114+
final String fileName = "src/test/resources/mediaUpload.png";
115+
final String contentType = "image/png";
116+
117+
File file = new File(fileName);
118+
byte[] fileContents = Files.readAllBytes(file.toPath());
119+
FileWrapper body = new FileWrapper(fileContents, contentType);
120+
121+
final String mediaId = "java-media-binary-test_" + java.util.UUID.randomUUID();
122+
123+
ApiResponse<Void> uploadMediaApiResponse = controller.uploadMedia(ACCOUNT_ID, mediaId, body, contentType, "no-cache");
124+
assertEquals("Response Code is not 204", 204, uploadMediaApiResponse.getStatusCode());
125+
126+
ApiResponse<InputStream> downloadMediaApiResponse = controller.getMedia(ACCOUNT_ID, mediaId);
127+
assertEquals("Response Code is not 200", 200, downloadMediaApiResponse.getStatusCode());
128+
129+
InputStream downloadMediaResponse = downloadMediaApiResponse.getResult();
130+
131+
int bRead;
132+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
133+
while ((bRead = downloadMediaResponse.read()) != -1){
134+
byteArrayOutputStream.write(bRead);
135+
}
136+
byte[] responseContents = byteArrayOutputStream.toByteArray();
137+
138+
assertArrayEquals("Media download not equal to media upload", fileContents, responseContents);
139+
140+
ApiResponse<Void> deleteMediaApiResponse = controller.deleteMedia(ACCOUNT_ID, mediaId);
141+
assertEquals("Response Code is not 204", 204, deleteMediaApiResponse.getStatusCode());
142+
}
143+
111144
@Test
112145
public void testUploadDownloadDeleteMediaAsync() throws Exception {
113146
final String fileName = "src/test/resources/mediaUpload.png";

0 commit comments

Comments
 (0)