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.DefaultEncryptInvoker;
27  import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
28  
29  import static org.apache.maven.cling.invoker.mvnenc.DefaultEncryptInvoker.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(DefaultEncryptInvoker.LocalContext 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(DefaultEncryptInvoker.LocalContext 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(
62              DefaultEncryptInvoker.LocalContext context, String indent, SecDispatcher.ValidationResponse response) {
63          context.terminal
64                  .writer()
65                  .println(messageBuilderFactory
66                          .builder()
67                          .format(
68                                  response.isValid()
69                                          ? messageBuilderFactory
70                                                  .builder()
71                                                  .success("%sConfiguration validation of %s: %s")
72                                                  .build()
73                                          : messageBuilderFactory
74                                                  .builder()
75                                                  .failure("%sConfiguration validation of %s: %s")
76                                                  .build(),
77                                  indent,
78                                  response.getSource(),
79                                  response.isValid() ? "VALID" : "INVALID"));
80          for (Map.Entry<SecDispatcher.ValidationResponse.Level, List<String>> entry :
81                  response.getReport().entrySet()) {
82              Consumer<String> consumer = s -> context.terminal
83                      .writer()
84                      .println(messageBuilderFactory.builder().info(s).build());
85              if (entry.getKey() == SecDispatcher.ValidationResponse.Level.ERROR) {
86                  consumer = s -> context.terminal
87                          .writer()
88                          .println(messageBuilderFactory.builder().error(s).build());
89              } else if (entry.getKey() == SecDispatcher.ValidationResponse.Level.WARNING) {
90                  consumer = s -> context.terminal
91                          .writer()
92                          .println(messageBuilderFactory.builder().warning(s).build());
93              }
94              for (String line : entry.getValue()) {
95                  consumer.accept(indent + "  " + line);
96              }
97          }
98          for (SecDispatcher.ValidationResponse subsystem : response.getSubsystems()) {
99              dumpResponse(context, indent + "  ", subsystem);
100         }
101     }
102 
103     protected abstract int doExecute(DefaultEncryptInvoker.LocalContext context) throws Exception;
104 }