Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,54 @@ public class {{classname}} {
return operationId + " call failed with: " + statusCode + " - " + body;
}

/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}

/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}

{{#operation}}
{{#vendorExtensions.x-group-parameters}}
{{#hasParams}}
Expand Down Expand Up @@ -291,13 +339,23 @@ public class {{classname}} {
);
}

{{^isResponseFile}}{{#isResponseBinary}}
Byte[] responseValue = localVarResponse.body().readAllBytes();
{{/isResponseBinary}}{{/isResponseFile}}
{{#isResponseFile}}
// Handle file downloading.
File responseValue = downloadFileFromResponse(localVarResponse);
{{/isResponseFile}}
{{^isResponseBinary}}{{^isResponseFile}}
String responseBody = new String(localVarResponse.body().readAllBytes());
{{{returnType}}} responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {});
{{/isResponseFile}}{{/isResponseBinary}}
localVarResponse.body().close();

return new ApiResponse<{{{returnType}}}>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {})
responseValue
);
{{/returnType}}
{{^returnType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ private String formatExceptionMessage(String operationId, int statusCode, String
return operationId + " call failed with: " + statusCode + " - " + body;
}

/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}

/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}

/**
* To test HTTP basic authentication
* To test HTTP basic authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,54 @@ private String formatExceptionMessage(String operationId, int statusCode, String
return operationId + " call failed with: " + statusCode + " - " + body;
}

/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}

/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}

/**
* Test binary (gif) response body
* Test binary (gif) response body
Expand Down Expand Up @@ -129,13 +177,16 @@ public ApiResponse<File> testBinaryGifWithHttpInfo() throws ApiException {
);
}

String responseBody = new String(localVarResponse.body().readAllBytes());

// Handle file downloading.
File responseValue = downloadFileFromResponse(localVarResponse);

localVarResponse.body().close();

return new ApiResponse<File>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<File>() {})
responseValue
);
} finally {
}
Expand Down Expand Up @@ -514,13 +565,17 @@ public ApiResponse<Pet> testEchoBodyAllOfPetWithHttpInfo(@javax.annotation.Nulla
);
}



String responseBody = new String(localVarResponse.body().readAllBytes());
Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {});

localVarResponse.body().close();

return new ApiResponse<Pet>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {})
responseValue
);
} finally {
}
Expand Down Expand Up @@ -681,13 +736,17 @@ public ApiResponse<Pet> testEchoBodyPetWithHttpInfo(@javax.annotation.Nullable P
);
}



String responseBody = new String(localVarResponse.body().readAllBytes());
Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {});

localVarResponse.body().close();

return new ApiResponse<Pet>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {})
responseValue
);
} finally {
}
Expand Down Expand Up @@ -848,13 +907,17 @@ public ApiResponse<StringEnumRef> testEchoBodyStringEnumWithHttpInfo(@javax.anno
);
}



String responseBody = new String(localVarResponse.body().readAllBytes());
StringEnumRef responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<StringEnumRef>() {});

localVarResponse.body().close();

return new ApiResponse<StringEnumRef>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<StringEnumRef>() {})
responseValue
);
} finally {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,54 @@ private String formatExceptionMessage(String operationId, int statusCode, String
return operationId + " call failed with: " + statusCode + " - " + body;
}

/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}

/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}

/**
* Test form parameter(s)
* Test form parameter(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,54 @@ private String formatExceptionMessage(String operationId, int statusCode, String
return operationId + " call failed with: " + statusCode + " - " + body;
}

/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}

/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}

/**
* Test header parameter(s)
* Test header parameter(s)
Expand Down
Loading
Loading