Skip to content

Commit 96f4a3a

Browse files
committed
Merge pull request #1226 from xhh/java-okhttp-gson-file-downloading
[Java okhttp-gson] Add support of file downloading
2 parents 48bd888 + 1ecb8a7 commit 96f4a3a

File tree

2 files changed

+150
-0
lines changed
  • modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson
  • samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client

2 files changed

+150
-0
lines changed

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.util.List;
2222
import java.util.ArrayList;
2323
import java.util.Date;
2424
import java.util.TimeZone;
25+
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
2627

2728
import java.net.URLEncoder;
@@ -35,6 +36,9 @@ import java.text.DateFormat;
3536
import java.text.SimpleDateFormat;
3637
import java.text.ParseException;
3738

39+
import okio.BufferedSink;
40+
import okio.Okio;
41+
3842
import {{invokerPackage}}.auth.Authentication;
3943
import {{invokerPackage}}.auth.HttpBasicAuth;
4044
import {{invokerPackage}}.auth.ApiKeyAuth;
@@ -45,6 +49,7 @@ public class ApiClient {
4549
private boolean lenientOnJson = false;
4650
private boolean debugging = false;
4751
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
52+
private String tempFolderPath = null;
4853
4954
private Map<String, Authentication> authentications;
5055
@@ -359,6 +364,22 @@ public class ApiClient {
359364
return this;
360365
}
361366

367+
/**
368+
* The path of temporary folder used to store downloaded files from endpoints
369+
* with file response. The default value is <code>null</code>, i.e. using
370+
* the system's default tempopary folder.
371+
*
372+
* @see https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)
373+
*/
374+
public String getTempFolderPath() {
375+
return tempFolderPath;
376+
}
377+
378+
public ApiClient setTempFolderPath(String tempFolderPath) {
379+
this.tempFolderPath = tempFolderPath;
380+
return this;
381+
}
382+
362383
/**
363384
* Format the given parameter object into string.
364385
*/
@@ -490,6 +511,10 @@ public class ApiClient {
490511
if (response == null || returnType == null)
491512
return null;
492513
514+
// Handle file downloading.
515+
if (returnType.equals(File.class))
516+
return (T) downloadFileFromResponse(response);
517+
493518
String respBody;
494519
try {
495520
if (response.body() != null)
@@ -538,6 +563,56 @@ public class ApiClient {
538563
}
539564
}
540565

566+
/**
567+
* Download file from the given response.
568+
*/
569+
public File downloadFileFromResponse(Response response) throws ApiException {
570+
try {
571+
File file = prepareDownloadFile(response);
572+
BufferedSink sink = Okio.buffer(Okio.sink(file));
573+
sink.writeAll(response.body().source());
574+
sink.close();
575+
return file;
576+
} catch (IOException e) {
577+
throw new ApiException(e);
578+
}
579+
}
580+
581+
public File prepareDownloadFile(Response response) throws IOException {
582+
String filename = null;
583+
String contentDisposition = response.header("Content-Disposition");
584+
if (contentDisposition != null && !"".equals(contentDisposition)) {
585+
// Get filename from the Content-Disposition header.
586+
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
587+
Matcher matcher = pattern.matcher(contentDisposition);
588+
if (matcher.find())
589+
filename = matcher.group(1);
590+
}
591+
592+
String prefix = null;
593+
String suffix = null;
594+
if (filename == null) {
595+
prefix = "download-";
596+
suffix = "";
597+
} else {
598+
int pos = filename.lastIndexOf(".");
599+
if (pos == -1) {
600+
prefix = filename + "-";
601+
} else {
602+
prefix = filename.substring(0, pos) + "-";
603+
suffix = filename.substring(pos);
604+
}
605+
// File.createTempFile requires the prefix to be at least three characters long
606+
if (prefix.length() < 3)
607+
prefix = "download-";
608+
}
609+
610+
if (tempFolderPath == null)
611+
return File.createTempFile(prefix, suffix);
612+
else
613+
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
614+
}
615+
541616
/**
542617
* @see #execute(Call, Type)
543618
*/

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Date;
2424
import java.util.TimeZone;
25+
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
2627

2728
import java.net.URLEncoder;
@@ -35,6 +36,9 @@
3536
import java.text.SimpleDateFormat;
3637
import java.text.ParseException;
3738

39+
import okio.BufferedSink;
40+
import okio.Okio;
41+
3842
import io.swagger.client.auth.Authentication;
3943
import io.swagger.client.auth.HttpBasicAuth;
4044
import io.swagger.client.auth.ApiKeyAuth;
@@ -45,6 +49,7 @@ public class ApiClient {
4549
private boolean lenientOnJson = false;
4650
private boolean debugging = false;
4751
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
52+
private String tempFolderPath = null;
4853

4954
private Map<String, Authentication> authentications;
5055

@@ -358,6 +363,22 @@ public ApiClient setDebugging(boolean debugging) {
358363
return this;
359364
}
360365

366+
/**
367+
* The path of temporary folder used to store downloaded files from endpoints
368+
* with file response. The default value is <code>null</code>, i.e. using
369+
* the system's default tempopary folder.
370+
*
371+
* @see https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)
372+
*/
373+
public String getTempFolderPath() {
374+
return tempFolderPath;
375+
}
376+
377+
public ApiClient setTempFolderPath(String tempFolderPath) {
378+
this.tempFolderPath = tempFolderPath;
379+
return this;
380+
}
381+
361382
/**
362383
* Format the given parameter object into string.
363384
*/
@@ -489,6 +510,10 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
489510
if (response == null || returnType == null)
490511
return null;
491512

513+
// Handle file downloading.
514+
if (returnType.equals(File.class))
515+
return (T) downloadFileFromResponse(response);
516+
492517
String respBody;
493518
try {
494519
if (response.body() != null)
@@ -537,6 +562,56 @@ public String serialize(Object obj, String contentType) throws ApiException {
537562
}
538563
}
539564

565+
/**
566+
* Download file from the given response.
567+
*/
568+
public File downloadFileFromResponse(Response response) throws ApiException {
569+
try {
570+
File file = prepareDownloadFile(response);
571+
BufferedSink sink = Okio.buffer(Okio.sink(file));
572+
sink.writeAll(response.body().source());
573+
sink.close();
574+
return file;
575+
} catch (IOException e) {
576+
throw new ApiException(e);
577+
}
578+
}
579+
580+
public File prepareDownloadFile(Response response) throws IOException {
581+
String filename = null;
582+
String contentDisposition = response.header("Content-Disposition");
583+
if (contentDisposition != null && !"".equals(contentDisposition)) {
584+
// Get filename from the Content-Disposition header.
585+
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
586+
Matcher matcher = pattern.matcher(contentDisposition);
587+
if (matcher.find())
588+
filename = matcher.group(1);
589+
}
590+
591+
String prefix = null;
592+
String suffix = null;
593+
if (filename == null) {
594+
prefix = "download-";
595+
suffix = "";
596+
} else {
597+
int pos = filename.lastIndexOf(".");
598+
if (pos == -1) {
599+
prefix = filename + "-";
600+
} else {
601+
prefix = filename.substring(0, pos) + "-";
602+
suffix = filename.substring(pos);
603+
}
604+
// File.createTempFile requires the prefix to be at least three characters long
605+
if (prefix.length() < 3)
606+
prefix = "download-";
607+
}
608+
609+
if (tempFolderPath == null)
610+
return File.createTempFile(prefix, suffix);
611+
else
612+
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
613+
}
614+
540615
/**
541616
* @see #execute(Call, Type)
542617
*/

0 commit comments

Comments
 (0)