diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache index f8d7fad3849..1c07a748346 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache @@ -22,6 +22,7 @@ import java.util.List; import java.util.ArrayList; import java.util.Date; import java.util.TimeZone; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.net.URLEncoder; @@ -35,6 +36,9 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; +import okio.BufferedSink; +import okio.Okio; + import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.ApiKeyAuth; @@ -45,6 +49,7 @@ public class ApiClient { private boolean lenientOnJson = false; private boolean debugging = false; private Map defaultHeaderMap = new HashMap(); + private String tempFolderPath = null; private Map authentications; @@ -359,6 +364,22 @@ 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; + } + /** * Format the given parameter object into string. */ @@ -490,6 +511,10 @@ public class ApiClient { if (response == null || returnType == null) return null; + // Handle file downloading. + if (returnType.equals(File.class)) + return (T) downloadFileFromResponse(response); + String respBody; try { if (response.body() != null) @@ -538,6 +563,56 @@ public class ApiClient { } } + /** + * Download file from the given response. + */ + public File downloadFileFromResponse(Response response) throws ApiException { + try { + File file = prepareDownloadFile(response); + BufferedSink sink = Okio.buffer(Okio.sink(file)); + sink.writeAll(response.body().source()); + sink.close(); + return file; + } catch (IOException e) { + throw new ApiException(e); + } + } + + 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 = 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)); + } + /** * @see #execute(Call, Type) */ diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java index 33c009fa0a3..38d6a39cbcc 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.TimeZone; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.net.URLEncoder; @@ -35,6 +36,9 @@ import java.text.SimpleDateFormat; import java.text.ParseException; +import okio.BufferedSink; +import okio.Okio; + import io.swagger.client.auth.Authentication; import io.swagger.client.auth.HttpBasicAuth; import io.swagger.client.auth.ApiKeyAuth; @@ -45,6 +49,7 @@ public class ApiClient { private boolean lenientOnJson = false; private boolean debugging = false; private Map defaultHeaderMap = new HashMap(); + private String tempFolderPath = null; private Map authentications; @@ -358,6 +363,22 @@ public ApiClient setDebugging(boolean debugging) { 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; + } + /** * Format the given parameter object into string. */ @@ -489,6 +510,10 @@ public T deserialize(Response response, Type returnType) throws ApiException if (response == null || returnType == null) return null; + // Handle file downloading. + if (returnType.equals(File.class)) + return (T) downloadFileFromResponse(response); + String respBody; try { if (response.body() != null) @@ -537,6 +562,56 @@ public String serialize(Object obj, String contentType) throws ApiException { } } + /** + * Download file from the given response. + */ + public File downloadFileFromResponse(Response response) throws ApiException { + try { + File file = prepareDownloadFile(response); + BufferedSink sink = Okio.buffer(Okio.sink(file)); + sink.writeAll(response.body().source()); + sink.close(); + return file; + } catch (IOException e) { + throw new ApiException(e); + } + } + + 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 = 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)); + } + /** * @see #execute(Call, Type) */