diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index ec3c7a7f7c..c37880c7ee 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -20,6 +20,9 @@ import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.MultiPart; import org.glassfish.jersey.media.multipart.MultiPartFeature; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; import java.util.Collection; import java.util.Collections; import java.util.Map; @@ -37,6 +40,8 @@ import java.io.UnsupportedEncodingException; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.HttpBasicAuth; @@ -52,6 +57,7 @@ public class ApiClient { private Client httpClient; private JSON json; + private String tempFolderPath = null; private Map authentications; @@ -235,9 +241,25 @@ public class ApiClient { return this; } + /** + * The path of temporary folder used to store downloaded files from endpoints + * with file response. The default value is null, i.e. using + * the system's default tempopary folder. + * + * @see https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File) + */ + public String getTempFolderPath() { + return tempFolderPath; + } + + public ApiClient setTempFolderPath(String tempFolderPath) { + this.tempFolderPath = tempFolderPath; + return this; + } + /** * Connect timeout (in milliseconds). - */ + */ public int getConnectTimeout() { return connectionTimeout; } @@ -250,8 +272,8 @@ public class ApiClient { public ApiClient setConnectTimeout(int connectionTimeout) { this.connectionTimeout = connectionTimeout; httpClient.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout); - return this; - } + return this; + } /** * Get the date format used to parse/format date parameters. @@ -467,6 +489,13 @@ public class ApiClient { * Deserialize response body to Java object according to the Content-Type. */ public T deserialize(Response response, GenericType returnType) throws ApiException { + // Handle file downloading. + if (returnType.equals(File.class)) { + @SuppressWarnings("unchecked") + T file = (T) downloadFileFromResponse(response); + return file; + } + String contentType = null; List contentTypes = response.getHeaders().get("Content-Type"); if (contentTypes != null && !contentTypes.isEmpty()) @@ -477,6 +506,55 @@ public class ApiClient { return response.readEntity(returnType); } + /** + * Download file from the given response. + * @throws ApiException If fail to read file content from response and write to disk + */ + public File downloadFileFromResponse(Response response) throws ApiException { + try { + File file = prepareDownloadFile(response); + Files.copy(response.readEntity(InputStream.class), file.toPath()); + return file; + } catch (IOException e) { + throw new ApiException(e); + } + } + + public File prepareDownloadFile(Response response) throws IOException { + String filename = null; + String contentDisposition = (String) response.getHeaders().getFirst("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 = matcher.group(1); + } + + String prefix = null; + String suffix = null; + if (filename == null) { + prefix = "download-"; + suffix = ""; + } else { + int pos = filename.lastIndexOf("."); + if (pos == -1) { + prefix = filename + "-"; + } else { + prefix = filename.substring(0, pos) + "-"; + suffix = filename.substring(pos); + } + // File.createTempFile requires the prefix to be at least three characters long + if (prefix.length() < 3) + prefix = "download-"; + } + + if (tempFolderPath == null) + return File.createTempFile(prefix, suffix); + else + return File.createTempFile(prefix, suffix, new File(tempFolderPath)); + } + /** * Invoke API by sending HTTP request with the given options. *