Skip to content

Commit

Permalink
Merge pull request #118 from onfido/release-upgrade
Browse files Browse the repository at this point in the history
Refresh onfido-java after onfido-openapi-spec update (e92d340)
  • Loading branch information
dvacca-onfido authored Jun 21, 2024
2 parents 0149a1c + 272564a commit 9d836e2
Show file tree
Hide file tree
Showing 16 changed files with 441 additions and 221 deletions.
4 changes: 2 additions & 2 deletions .release.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"source": {
"repo_url": "https://github.com/onfido/onfido-openapi-spec",
"short_sha": "38a8740",
"long_sha": "38a87401552386cb0b17aae28385c5e2f4af7bfc",
"short_sha": "e92d340",
"long_sha": "e92d340b84690cce6851fa4a32530ac29911d75a",
"version": ""
},
"release": "v4.0.0"
Expand Down
70 changes: 38 additions & 32 deletions src/main/java/com/onfido/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,13 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
} else if (returnType.equals(File.class)) {
// Handle file downloading.
return (T) downloadFileFromResponse(response);
} else if (returnType.equals(FileTransfer.class)) {
try {
String filename = getFilenameFromResponse(response);
return (T) new FileTransfer(response.body().bytes(), filename);
} catch (IOException e) {
throw new ApiException(e);
}
}

String respBody;
Expand Down Expand Up @@ -946,16 +953,7 @@ public File downloadFileFromResponse(Response response) throws ApiException {
* @throws java.io.IOException If fail to prepare file for download
*/
public File prepareDownloadFile(Response response) throws IOException {
String filename = null;
String contentDisposition = response.header("Content-Disposition");
if (contentDisposition != null && !"".equals(contentDisposition)) {
// Get filename from the Content-Disposition header.
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
Matcher matcher = pattern.matcher(contentDisposition);
if (matcher.find()) {
filename = sanitizeFilename(matcher.group(1));
}
}
String filename = getFilenameFromResponse(response);

String prefix = null;
String suffix = null;
Expand Down Expand Up @@ -1339,14 +1337,14 @@ public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams)
public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
MultipartBody.Builder mpBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) {
File file = (File) param.getValue();
if (param.getValue() instanceof FileTransfer) {
FileTransfer file = (FileTransfer) param.getValue();
addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
} else if (param.getValue() instanceof List) {
List list = (List) param.getValue();
for (Object item: list) {
if (item instanceof File) {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), (File) item);
if (item instanceof FileTransfer) {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), (FileTransfer) item);
} else {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), param.getValue());
}
Expand All @@ -1359,31 +1357,39 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
}

/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param file The given file
* @return The guessed Content-Type
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, FileTransfer file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getFilename() + "\"");
MediaType mediaType = MediaType.parse(file.getContentType());

if ( file.getByteArray() != null ) {
mpBuilder.addPart(partHeaders, RequestBody.create(file.getByteArray(), mediaType));
}
else {
mpBuilder.addPart(partHeaders, RequestBody.create(file.getInputFile(), mediaType));
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
* Retrieve filename, once santized, from provided response.
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
private String getFilenameFromResponse(Response response) {
String filename = "";
String contentDisposition = response.header("Content-Disposition");
if (contentDisposition != null && !"".equals(contentDisposition)) {
// Get filename from the Content-Disposition header.
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
Matcher matcher = pattern.matcher(contentDisposition);
if (matcher.find()) {
filename = sanitizeFilename(matcher.group(1));
}
}
return filename;
}

/**
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/com/onfido/FileTransfer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.onfido;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;

public class FileTransfer {

private File inputFile = null;
private byte[] byteArray = null;
private String filename = null;
private String contentType = null;

/**
* Create a new file transfer from a byte array
*
* @param byteArray Byte array to include in file transfer
* @param filename Filename to send together with the transfer
* @throws ApiException
*/
public FileTransfer(byte[] byteArray, String filename) {
this.byteArray = byteArray;

updateMetadata(filename);
}

/**
* Create a new file transfer from a File
*
* @param file File to include in transfer
* @throws ApiException
*/
public FileTransfer(File inputFile) {
this.inputFile = inputFile;

updateMetadata(inputFile.getName());
}

/**
* Create a new file transfer from an InputStream
*
* @param inputStream InputStream to read data from
* @param filename Filename to send together with the transfer
* @throws ApiException
*/
public FileTransfer(InputStream inputStream, String filename) throws ApiException {
try {
this.byteArray = new byte[inputStream.available()];
inputStream.read(byteArray);
} catch (IOException e) {
throw new ApiException(e);
}

updateMetadata(filename);
}

/**
* Return the array of bytes used for this file transfer.
*
* @return array of bytes
*/
public byte[] getByteArray() {
return byteArray;
}

/**
* Return the input stream used for this file transfer
*
* @return the provided input file
*/
public File getInputFile() {
return inputFile;
}

/**
* @return the filename associated with this transfer
*/
public String getFilename() {
return filename;
}

/**
* @return the content-type associated with this transfer
*/
public String getContentType() {
return contentType;
}

private void updateMetadata(String filename) {
this.contentType = URLConnection.guessContentTypeFromName(filename);
this.filename = filename;
}
}
Loading

0 comments on commit 9d836e2

Please sign in to comment.