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