diff --git a/src/main/java/com/bandwidth/http/client/OkClient.java b/src/main/java/com/bandwidth/http/client/OkClient.java index 3b40ba31..547e9df7 100644 --- a/src/main/java/com/bandwidth/http/client/OkClient.java +++ b/src/main/java/com/bandwidth/http/client/OkClient.java @@ -255,8 +255,14 @@ private okhttp3.Request convertRequest(HttpRequest httpRequest) { httpRequest.getHeaders().add("content-type", contentType); } - requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse(contentType), - ((FileWrapper) body).getFile()); + if (file.getFile() != null) { + requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse(contentType), + ((FileWrapper) body).getFile()); + } else { + requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse(contentType), + ((FileWrapper) body).getFileStream()); + } + } else { // set request body if (!httpRequest.getHeaders().has("content-type")) { diff --git a/src/main/java/com/bandwidth/utilities/FileWrapper.java b/src/main/java/com/bandwidth/utilities/FileWrapper.java index 8e8daa15..3a24f1f4 100644 --- a/src/main/java/com/bandwidth/utilities/FileWrapper.java +++ b/src/main/java/com/bandwidth/utilities/FileWrapper.java @@ -8,14 +8,17 @@ import com.fasterxml.jackson.annotation.JsonInclude; import java.io.File; +import java.io.FileInputStream; /** * Class to wrap file and contentType to be sent as part of a HTTP request. */ public class FileWrapper { - @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonInclude(JsonInclude.Include.NON_ABSENT) private File file; + @JsonInclude(JsonInclude.Include.NON_ABSENT) + private byte[] fileStream; @JsonInclude(JsonInclude.Include.NON_NULL) private String contentType; @@ -26,6 +29,17 @@ public class FileWrapper { */ public FileWrapper(File file, String contentType) { this.file = file; + this.fileStream = null; + this.contentType = contentType; + } + /** + * Initialization constructor. + * @param fileStream File Input Stream object to be wrapped + * @param contentType content type of file + */ + public FileWrapper(byte[] fileStream, String contentType) { + this.file = null; + this.fileStream = fileStream; this.contentType = contentType; } @@ -37,13 +51,25 @@ public FileWrapper(File file) { this.file = file; } + /** + * Initialization constructor. + * @param fileStream File object to be wrapped + */ + public FileWrapper(byte[] fileStream) { + this.fileStream = fileStream; + } + /** * Getter for file. * @return File instance */ - public File getFile() { - return file; - } + public File getFile() { return file; } + + /** + * Getter for fileStream. + * @return byte[] instance + */ + public byte[] getFileStream() { return fileStream; } /** * Getter for content type. diff --git a/src/test/java/com/bandwidth/MessagingApiTest.java b/src/test/java/com/bandwidth/MessagingApiTest.java index 96281dec..b9c6d817 100644 --- a/src/test/java/com/bandwidth/MessagingApiTest.java +++ b/src/test/java/com/bandwidth/MessagingApiTest.java @@ -10,6 +10,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.InputStream; import java.nio.file.Files; import java.util.Collections; @@ -108,6 +109,38 @@ public void testUploadDownloadDeleteMedia() throws Exception { assertEquals("Response Code is not 204", 204, deleteMediaApiResponse.getStatusCode()); } + @Test + public void testUploadDownloadDeleteBinaryMedia() throws Exception { + final String fileName = "src/test/resources/mediaUpload.png"; + final String contentType = "image/png"; + + File file = new File(fileName); + byte[] fileContents = Files.readAllBytes(file.toPath()); + FileWrapper body = new FileWrapper(fileContents, contentType); + + final String mediaId = "java-media-binary-test_" + java.util.UUID.randomUUID(); + + ApiResponse uploadMediaApiResponse = controller.uploadMedia(ACCOUNT_ID, mediaId, body, contentType, "no-cache"); + assertEquals("Response Code is not 204", 204, uploadMediaApiResponse.getStatusCode()); + + ApiResponse downloadMediaApiResponse = controller.getMedia(ACCOUNT_ID, mediaId); + assertEquals("Response Code is not 200", 200, downloadMediaApiResponse.getStatusCode()); + + InputStream downloadMediaResponse = downloadMediaApiResponse.getResult(); + + int bRead; + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + while ((bRead = downloadMediaResponse.read()) != -1){ + byteArrayOutputStream.write(bRead); + } + byte[] responseContents = byteArrayOutputStream.toByteArray(); + + assertArrayEquals("Media download not equal to media upload", fileContents, responseContents); + + ApiResponse deleteMediaApiResponse = controller.deleteMedia(ACCOUNT_ID, mediaId); + assertEquals("Response Code is not 204", 204, deleteMediaApiResponse.getStatusCode()); + } + @Test public void testUploadDownloadDeleteMediaAsync() throws Exception { final String fileName = "src/test/resources/mediaUpload.png";