Test downloaded file name (FINERACT-1218)
This commit is contained in:
committed by
Aleksandar Vidakovic
parent
e380df187d
commit
26cd903301
+1
-1
@@ -23,7 +23,7 @@ import retrofit2.Call;
|
||||
import retrofit2.Response;
|
||||
|
||||
/**
|
||||
* Exception thrown by {@link Calls#ok(Call)} when {@link Call}s fail.
|
||||
* Exception thrown by {@link Calls} utility when {@link Call}s fail.
|
||||
*
|
||||
* @author Michael Vorburger.ch
|
||||
*/
|
||||
|
||||
@@ -32,7 +32,8 @@ public final class Calls {
|
||||
private Calls() {}
|
||||
|
||||
/**
|
||||
* Execute a Call, expecting success.
|
||||
* Execute a Call, expecting success, returning strongly typed body. This covers the most typical use and is thus
|
||||
* most used.
|
||||
*
|
||||
* @param call
|
||||
* the Call to execute
|
||||
@@ -42,13 +43,38 @@ public final class Calls {
|
||||
* [200..300) successful
|
||||
*/
|
||||
public static <T> T ok(Call<T> call) throws CallFailedRuntimeException {
|
||||
return okR(call).body();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a Call, expecting success, returning Response. This is rarely used, and only useful if you need access to
|
||||
* e.g. the headers of the response.
|
||||
*
|
||||
* @param call
|
||||
* the Call to execute
|
||||
* @return the Response of the successful call (never null)
|
||||
* @throws CallFailedRuntimeException
|
||||
* thrown either if a problem occurred talking to the server, or the HTTP response code was not
|
||||
* [200..300) successful
|
||||
*/
|
||||
public static <T> Response<T> okR(Call<T> call) throws CallFailedRuntimeException {
|
||||
Response<T> response = executeU(call);
|
||||
if (response.isSuccessful()) {
|
||||
return response.body();
|
||||
return response;
|
||||
}
|
||||
throw new CallFailedRuntimeException(call, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Call#execute()} mapping {@link IOException} to {@link CallFailedRuntimeException}. This can be useful for
|
||||
* code which would like to handle non-success HTTP responses, notably tests asserting non-200 results.
|
||||
*
|
||||
* @param call
|
||||
* the Call to execute
|
||||
* @return the Response, which may or may not have been successful.
|
||||
* @throws CallFailedRuntimeException
|
||||
* thrown if a problem occurred talking to the server
|
||||
*/
|
||||
public static <T> Response<T> executeU(Call<T> call) throws CallFailedRuntimeException {
|
||||
try {
|
||||
return call.execute();
|
||||
|
||||
@@ -19,13 +19,22 @@
|
||||
package org.apache.fineract.client.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Optional;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody.Part;
|
||||
import okhttp3.RequestBody;
|
||||
import org.apache.fineract.client.services.DocumentsApiFixed;
|
||||
import org.apache.fineract.client.services.ImagesApi;
|
||||
import retrofit2.Response;
|
||||
|
||||
/**
|
||||
* Convenience Factory for {@link Part} (including {@link RequestBody}).
|
||||
*
|
||||
* {@link Part} is the argument of operations for binary uploads like
|
||||
* {@link DocumentsApiFixed#createDocument(String, Long, Part, String, String)},
|
||||
* {@link DocumentsApiFixed#updateDocument(String, Long, Long, Part, String, String)} and
|
||||
* {@link ImagesApi#create(String, Long, Part)} and {@link ImagesApi#update(String, Long, Part)}.
|
||||
*
|
||||
* @author Michael Vorburger.ch
|
||||
*/
|
||||
public final class Parts {
|
||||
@@ -85,4 +94,16 @@ public final class Parts {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<String> fileName(Response<?> response) {
|
||||
String contentDisposition = response.headers().get("Content-Disposition");
|
||||
if (contentDisposition == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
int i = contentDisposition.indexOf("; filename=\"");
|
||||
if (i == -1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(contentDisposition.substring(i + "; filename=\"".length(), contentDisposition.length() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user