View Javadoc
1   package io.apicurio.registry.maven;
2   
3   import com.microsoft.kiota.ApiException;
4   import io.apicurio.registry.client.auth.VertXAuthFactory;
5   import io.apicurio.registry.rest.client.RegistryClient;
6   import io.apicurio.registry.rest.client.models.ProblemDetails;
7   import io.apicurio.registry.rest.client.models.RuleViolationProblemDetails;
8   import io.apicurio.registry.types.ContentTypes;
9   import io.kiota.http.vertx.VertXRequestAdapter;
10  import io.vertx.core.Vertx;
11  import io.vertx.core.VertxOptions;
12  import io.vertx.core.file.FileSystemOptions;
13  import io.vertx.ext.web.client.WebClient;
14  import org.apache.maven.plugin.AbstractMojo;
15  import org.apache.maven.plugin.MojoExecutionException;
16  import org.apache.maven.plugin.MojoFailureException;
17  import org.apache.maven.plugins.annotations.Parameter;
18  
19  import java.util.Locale;
20  import java.util.concurrent.ExecutionException;
21  
22  /**
23   * Base class for all Registry Mojo's. It handles RegistryService's (aka client) lifecycle.
24   */
25  public abstract class AbstractRegistryMojo extends AbstractMojo {
26  
27      /**
28       * The registry's url. e.g. http://localhost:8080/apis/registry/v3
29       */
30      @Parameter(required = true, property = "apicurio.url")
31      String registryUrl;
32  
33      /**
34       * The URL of the authentication server (if required).
35       */
36      @Parameter(property = "auth.server.url")
37      String authServerUrl;
38  
39      /**
40       * The client id to use when authenticating to the auth sever.
41       */
42      @Parameter(property = "client.id")
43      String clientId;
44  
45      /**
46       * The client secret to use when authenticating to the auth sever.
47       */
48      @Parameter(property = "client.secret")
49      String clientSecret;
50  
51      /**
52       * The client scope to use when authenticating to the auth sever.
53       */
54      @Parameter(property = "client.scope")
55      String clientScope;
56  
57      /**
58       * Authentication credentials: username
59       */
60      @Parameter(property = "username")
61      String username;
62  
63      /**
64       * Authentication credentials: password
65       */
66      @Parameter(property = "password")
67      String password;
68  
69      protected Vertx createVertx() {
70          var options = new VertxOptions();
71          var fsOpts = new FileSystemOptions();
72          fsOpts.setFileCachingEnabled(false);
73          fsOpts.setClassPathResolvingEnabled(false);
74          options.setFileSystemOptions(fsOpts);
75          return Vertx.vertx(options);
76      }
77  
78      protected RegistryClient createClient(Vertx vertx) {
79          WebClient provider = null;
80          if (authServerUrl != null && clientId != null && clientSecret != null) {
81              provider = VertXAuthFactory.buildOIDCWebClient(vertx, authServerUrl, clientId, clientSecret,
82                      clientScope);
83          } else if (username != null && password != null) {
84              provider = VertXAuthFactory.buildSimpleAuthWebClient(vertx, username, password);
85          } else {
86              provider = WebClient.create(vertx);
87          }
88  
89          var adapter = new VertXRequestAdapter(provider);
90          adapter.setBaseUrl(registryUrl);
91          return new RegistryClient(adapter);
92      }
93  
94      @Override
95      public void execute() throws MojoExecutionException, MojoFailureException {
96          try {
97              executeInternal();
98          } catch (ExecutionException e) {
99              throw new MojoExecutionException(e);
100         } catch (InterruptedException e) {
101             throw new MojoFailureException(e);
102         }
103         closeClients();
104     }
105 
106     private void closeClients() {
107         // TODO: check there are no connection leaks etc...
108     }
109 
110     protected abstract void executeInternal()
111             throws MojoExecutionException, MojoFailureException, ExecutionException, InterruptedException;
112 
113     protected String getContentTypeByExtension(String fileName) {
114         if (fileName == null)
115             return null;
116         String[] temp = fileName.split("[.]");
117         String extension = temp[temp.length - 1];
118         switch (extension.toLowerCase(Locale.ROOT)) {
119             case "avro":
120             case "avsc":
121             case "json":
122                 return ContentTypes.APPLICATION_JSON;
123             case "yml":
124             case "yaml":
125                 return ContentTypes.APPLICATION_YAML;
126             case "graphql":
127                 return ContentTypes.APPLICATION_GRAPHQL;
128             case "proto":
129                 return ContentTypes.APPLICATION_PROTOBUF;
130             case "wsdl":
131             case "xsd":
132             case "xml":
133                 return ContentTypes.APPLICATION_XML;
134         }
135         return null;
136     }
137 
138     public void setRegistryUrl(String registryUrl) {
139         this.registryUrl = registryUrl;
140     }
141 
142     public void setAuthServerUrl(String authServerUrl) {
143         this.authServerUrl = authServerUrl;
144     }
145 
146     public void setClientId(String clientId) {
147         this.clientId = clientId;
148     }
149 
150     public void setClientSecret(String clientSecret) {
151         this.clientSecret = clientSecret;
152     }
153 
154     public void setClientScope(String clientScope) {
155         this.clientScope = clientScope;
156     }
157 
158     public void setUsername(String username) {
159         this.username = username;
160     }
161 
162     public void setPassword(String password) {
163         this.password = password;
164     }
165 
166     protected void logAndThrow(ApiException e) throws MojoExecutionException, MojoFailureException {
167         if (e instanceof RuleViolationProblemDetails) {
168             logAndThrow((RuleViolationProblemDetails) e);
169         }
170         if (e instanceof ProblemDetails) {
171             logAndThrow((ProblemDetails) e);
172         }
173     }
174 
175     protected void logAndThrow(ProblemDetails e) throws MojoExecutionException {
176         getLog().error("---");
177         getLog().error("Error registering artifact: " + e.getName());
178         getLog().error(e.getTitle());
179         getLog().error(e.getDetail());
180         getLog().error("---");
181         throw new MojoExecutionException("Error registering artifact: " + e.getName(), e);
182     }
183 
184     protected void logAndThrow(RuleViolationProblemDetails e) throws MojoFailureException {
185         getLog().error("---");
186         getLog().error("Registry rule validation failure: " + e.getName());
187         getLog().error(e.getTitle());
188         if (e.getCauses() != null) {
189             e.getCauses().forEach(cause -> {
190                 getLog().error("\t-> " + cause.getContext());
191                 getLog().error("\t   " + cause.getDescription());
192             });
193         }
194         getLog().error("---");
195         throw new MojoFailureException("Registry rule validation failure: " + e.getName(), e);
196     }
197 
198 }