View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cling.invoker.mvnenc.goals;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.function.Consumer;
24  
25  import org.apache.maven.api.services.MessageBuilderFactory;
26  import org.apache.maven.cling.invoker.mvnenc.EncryptContext;
27  import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
28  
29  import static org.apache.maven.cling.invoker.mvnenc.EncryptInvoker.ERROR;
30  
31  /**
32   * The support class for goal implementations that requires valid/workable config.
33   */
34  public abstract class ConfiguredGoalSupport extends GoalSupport {
35      protected ConfiguredGoalSupport(MessageBuilderFactory messageBuilderFactory, SecDispatcher secDispatcher) {
36          super(messageBuilderFactory, secDispatcher);
37      }
38  
39      @Override
40      public int execute(EncryptContext context) throws Exception {
41          if (!validateConfiguration(context)) {
42              context.terminal
43                      .writer()
44                      .println(messageBuilderFactory
45                              .builder()
46                              .error("Maven Encryption is not configured, run `mvnenc init` first.")
47                              .build());
48              return ERROR;
49          }
50          return doExecute(context);
51      }
52  
53      protected boolean validateConfiguration(EncryptContext context) {
54          SecDispatcher.ValidationResponse response = secDispatcher.validateConfiguration();
55          if (!response.isValid() || context.invokerRequest.options().verbose().orElse(false)) {
56              dumpResponse(context, "", response);
57          }
58          return response.isValid();
59      }
60  
61      protected void dumpResponse(EncryptContext context, String indent, SecDispatcher.ValidationResponse response) {
62          context.terminal
63                  .writer()
64                  .println(messageBuilderFactory
65                          .builder()
66                          .format(
67                                  response.isValid()
68                                          ? messageBuilderFactory
69                                                  .builder()
70                                                  .success("%sConfiguration validation of %s: %s")
71                                                  .build()
72                                          : messageBuilderFactory
73                                                  .builder()
74                                                  .failure("%sConfiguration validation of %s: %s")
75                                                  .build(),
76                                  indent,
77                                  response.getSource(),
78                                  response.isValid() ? "VALID" : "INVALID"));
79          for (Map.Entry<SecDispatcher.ValidationResponse.Level, List<String>> entry :
80                  response.getReport().entrySet()) {
81              Consumer<String> consumer = s -> context.terminal
82                      .writer()
83                      .println(messageBuilderFactory.builder().info(s).build());
84              if (entry.getKey() == SecDispatcher.ValidationResponse.Level.ERROR) {
85                  consumer = s -> context.terminal
86                          .writer()
87                          .println(messageBuilderFactory.builder().error(s).build());
88              } else if (entry.getKey() == SecDispatcher.ValidationResponse.Level.WARNING) {
89                  consumer = s -> context.terminal
90                          .writer()
91                          .println(messageBuilderFactory.builder().warning(s).build());
92              }
93              for (String line : entry.getValue()) {
94                  consumer.accept(indent + "  " + line);
95              }
96          }
97          for (SecDispatcher.ValidationResponse subsystem : response.getSubsystems()) {
98              dumpResponse(context, indent + "  ", subsystem);
99          }
100     }
101 
102     protected abstract int doExecute(EncryptContext context) throws Exception;
103 }