Skip to content

Commit

Permalink
Merge pull request #60 from Bandwidth/DX-2456
Browse files Browse the repository at this point in the history
DX-2456 Overload `FileWrapper` class to accept a `bytes[]` object
  • Loading branch information
ajrice6713 authored Mar 4, 2022
2 parents 054a399 + ef6c112 commit dc8c848
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/bandwidth/http/client/OkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/com/bandwidth/utilities/FileWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/bandwidth/MessagingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Void> uploadMediaApiResponse = controller.uploadMedia(ACCOUNT_ID, mediaId, body, contentType, "no-cache");
assertEquals("Response Code is not 204", 204, uploadMediaApiResponse.getStatusCode());

ApiResponse<InputStream> 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<Void> 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";
Expand Down

0 comments on commit dc8c848

Please sign in to comment.