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 f8d7fad384..43d8f86637 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; @@ -490,6 +494,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 +546,53 @@ 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-"; + } + + return File.createTempFile(prefix, suffix); + } + /** * @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 33c009fa0a..777e413e20 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.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 io.swagger.client.auth.Authentication; import io.swagger.client.auth.HttpBasicAuth; import io.swagger.client.auth.ApiKeyAuth; @@ -489,6 +493,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) @@ -537,6 +545,53 @@ 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-"; + } + + return File.createTempFile(prefix, suffix); + } + /** * @see #execute(Call, Type) */