This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3df2ae928d340fc536a531b81ac63d067a6237b5
Author: test <test@localhost>
AuthorDate: Thu Nov 12 10:06:38 2020 +0100
CAMEL-15858: add remote file handling
---
tooling/maven/camel-restdsl-openapi-plugin/pom.xml | 5 +++
.../main/docs/camel-restdsl-openapi-plugin.adoc | 2 +
.../generator/openapi/AbstractGenerateMojo.java | 46 +++++++++++++++++++++-
3 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/tooling/maven/camel-restdsl-openapi-plugin/pom.xml
b/tooling/maven/camel-restdsl-openapi-plugin/pom.xml
index da41363..ce7d551 100644
--- a/tooling/maven/camel-restdsl-openapi-plugin/pom.xml
+++ b/tooling/maven/camel-restdsl-openapi-plugin/pom.xml
@@ -56,6 +56,11 @@
<version>${snakeyaml-version}</version>
</dependency>
<dependency>
+ <groupId>org.openapitools</groupId>
+ <artifactId>openapi-generator</artifactId>
+ <version>4.3.1</version>
+ </dependency>
+ <dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</dependency>
diff --git
a/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc
b/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc
index 2c620a3..b571140 100644
---
a/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc
+++
b/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc
@@ -61,6 +61,7 @@ in the `<configuration>` tag.
| `skip` | `false` | Set to `true` to skip code generation.
| `filterOperation` | | Used for including only the operation ids specified.
Multiple ids can be separated by comma. Wildcards can be used, eg `find*` to
include all operations starting with `find`.
| `specificationUri` | `src/spec/openapi.json` | URI of the OpenApi
specification, supports filesystem paths, HTTP and classpath resources, by
default `src/spec/openapi.json` within the project directory. Supports JSON
and YAML.
+| `auth` | | Adds authorization headers when fetching the OpenApi
specification definitions remotely. Pass in a URL-encoded string of name:header
with a comma separating multiple values.
| `className` | from `title` or `RestDslRoute` | Name of the generated class,
taken from the OpenApi specification title or set to `RestDslRoute` by default
| `packageName` | from `host` or `rest.dsl.generated` | Name of the package
for the generated class, taken from the OpenApi specification host value or
`rest.dsl.generated` by default
| `indent` | `" "` | What identing character(s) to use,
by default four spaces, you can use `\t` to signify tab character
@@ -136,6 +137,7 @@ in the `<configuration>` tag.
| `skip` | `false` | Set to `true` to skip code generation.
| `filterOperation` | | Used for including only the operation ids specified.
Multiple ids can be separated by comma. Wildcards can be used, eg `find*` to
include all operations starting with `find`.
| `specificationUri` | `src/spec/openapi.json` | URI of the OpenApi
specification, by default `src/spec/openapi.json` within the project directory.
Supports JSON and YAML.
+| `auth` | | Adds authorization headers when fetching the OpenApi
specification definitions remotely. Pass in a URL-encoded string of name:header
with a comma separating multiple values.
| `outputDirectory` | `generated-sources/restdsl-openapi` | Where to place the
generated source file, by default `generated-sources/restdsl-openapi` within
the project directory
| `fileName` | `camel-rest.xml` | The name of the XML file as output.
| `blueprint` | `false` | If enabled generates OSGi Blueprint XML instead of
Spring XML.
diff --git
a/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java
b/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java
index 1f25c49..2ed0dc7 100644
---
a/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java
+++
b/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java
@@ -18,11 +18,18 @@ package org.apache.camel.maven.generator.openapi;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
+import java.net.URLConnection;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -33,6 +40,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.openapi.models.OasDocument;
+import io.swagger.v3.parser.core.models.AuthorizationValue;
import org.apache.camel.generator.openapi.DestinationGenerator;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
@@ -44,9 +52,11 @@ import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
+import org.openapitools.codegen.auth.AuthParser;
import org.twdata.maven.mojoexecutor.MojoExecutor;
import org.yaml.snakeyaml.Yaml;
+import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId;
import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo;
@@ -99,6 +109,9 @@ abstract class AbstractGenerateMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.basedir}/src/spec/openapi.json",
required = true)
String specificationUri;
+ @Parameter(name = "auth")
+ String auth;
+
@Parameter(defaultValue = "3.0.19")
String swaggerCodegenMavenPluginVersion;
@@ -282,9 +295,32 @@ abstract class AbstractGenerateMojo extends AbstractMojo {
OasDocument readOpenApiDoc(String specificationUri) throws Exception {
ObjectMapper mapper = new ObjectMapper();
+
+ URL inputSpecRemoteUrl = inputSpecRemoteUrl(specificationUri);
+ File inputSpecTempFile = new File(specificationUri);
+
+ if (inputSpecRemoteUrl != null) {
+ inputSpecTempFile = File.createTempFile("openapi-spec", ".tmp");
+
+ URLConnection conn = inputSpecRemoteUrl.openConnection();
+ if (isNotEmpty(auth)) {
+ List<AuthorizationValue> authList = AuthParser.parse(auth);
+ for (AuthorizationValue a : authList) {
+ conn.setRequestProperty(a.getKeyName(), a.getValue());
+ }
+ }
+ try (ReadableByteChannel readableByteChannel =
Channels.newChannel(conn.getInputStream())) {
+ FileChannel fileChannel;
+ try (FileOutputStream fileOutputStream = new
FileOutputStream(inputSpecTempFile)) {
+ fileChannel = fileOutputStream.getChannel();
+ fileChannel.transferFrom(readableByteChannel, 0,
Long.MAX_VALUE);
+ }
+ }
+ }
+
InputStream is;
try {
- is = new FileInputStream(new File(specificationUri));
+ is = new FileInputStream(inputSpecTempFile);
} catch (Exception ex) {
//use classloader resource stream as fallback
is =
this.getClass().getClassLoader().getResourceAsStream(specificationUri);
@@ -333,4 +369,12 @@ abstract class AbstractGenerateMojo extends AbstractMojo {
return comp;
}
+
+ private URL inputSpecRemoteUrl(String specificationUri) {
+ try {
+ return new URI(specificationUri).toURL();
+ } catch (URISyntaxException | MalformedURLException |
IllegalArgumentException e) {
+ return null;
+ }
+ }
}