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              clientOptions.oauth2(authServerUrl, clientId, clientSecret);
81          } else if (username != null && password != null) {
82              clientOptions.basicAuth(username, password);
83          }
84          return RegistryClientFactory.create(clientOptions.retry());
85      }
86  
87      @Override
88      public void execute() throws MojoExecutionException, MojoFailureException {
89          try {
90              executeInternal();
91          } catch (ExecutionException e) {
92              throw new MojoExecutionException(e);
93          } catch (InterruptedException e) {
94              throw new MojoFailureException(e);
95          }
96          closeClients();
97      }
98  
99      private void closeClients() {
100         // TODO: check there are no connection leaks etc...
101     }
102 
103     protected abstract void executeInternal()
104             throws MojoExecutionException, MojoFailureException, ExecutionException, InterruptedException;
105 
106     protected String getContentTypeByExtension(String fileName) {
107         if (fileName == null)
108             return null;
109         String[] temp = fileName.split("[.]");
110         String extension = temp[temp.length - 1];
111         switch (extension.toLowerCase(Locale.ROOT)) {
112             case "avro":
113             case "avsc":
114             case "json":
115                 return ContentTypes.APPLICATION_JSON;
116             case "yml":
117             case "yaml":
118                 return ContentTypes.APPLICATION_YAML;
119             case "graphql":
120                 return ContentTypes.APPLICATION_GRAPHQL;
121             case "proto":
122                 return ContentTypes.APPLICATION_PROTOBUF;
123             case "wsdl":
124             case "xsd":
125             case "xml":
126                 return ContentTypes.APPLICATION_XML;
127         }
128         return null;
129     }
130 
131     public void setRegistryUrl(String registryUrl) {
132         this.registryUrl = registryUrl;
133     }
134 
135     public void setAuthServerUrl(String authServerUrl) {
136         this.authServerUrl = authServerUrl;
137     }
138 
139     public void setClientId(String clientId) {
140         this.clientId = clientId;
141     }
142 
143     public void setClientSecret(String clientSecret) {
144         this.clientSecret = clientSecret;
145     }
146 
147     public void setClientScope(String clientScope) {
148         this.clientScope = clientScope;
149     }
150 
151     public void setUsername(String username) {
152         this.username = username;
153     }
154 
155     public void setPassword(String password) {
156         this.password = password;
157     }
158 
159     protected void logAndThrow(ApiException e) throws MojoExecutionException, MojoFailureException {
160         if (e instanceof RuleViolationProblemDetails) {
161             logAndThrow((RuleViolationProblemDetails) e);
162         }
163         if (e instanceof ProblemDetails) {
164             logAndThrow((ProblemDetails) e);
165         }
166     }
167 
168     protected void logAndThrow(ProblemDetails e) throws MojoExecutionException {
169         getLog().error("---");
170         getLog().error("Error registering artifact: " + e.getName());
171         getLog().error(e.getTitle());
172         getLog().error(e.getDetail());
173         getLog().error("---");
174         throw new MojoExecutionException("Error registering artifact: " + e.getName(), e);
175     }
176 
177     protected void logAndThrow(RuleViolationProblemDetails e) throws MojoFailureException {
178         getLog().error("---");
179         getLog().error("Registry rule validation failure: " + e.getName());
180         getLog().error(e.getTitle());
181         if (e.getCauses() != null) {
182             e.getCauses().forEach(cause -> {
183                 getLog().error("\t-> " + cause.getContext());
184                 getLog().error("\t   " + cause.getDescription());
185             });
186         }
187         getLog().error("---");
188         throw new MojoFailureException("Registry rule validation failure: " + e.getName(), e);
189     }
190 
191 }