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.logger.error(messageBuilderFactory
43                      .builder()
44                      .error("Maven Encryption is not configured, run `mvnenc init` first.")
45                      .build());
46              return ERROR;
47          }
48          return doExecute(context);
49      }
50  
51      protected boolean validateConfiguration(EncryptContext context) {
52          SecDispatcher.ValidationResponse response = secDispatcher.validateConfiguration();
53          if (!response.isValid() || context.invokerRequest.options().verbose().orElse(false)) {
54              dumpResponse(context, "", response);
55          }
56          return response.isValid();
57      }
58  
59      protected void dumpResponse(EncryptContext context, String indent, SecDispatcher.ValidationResponse response) {
60          context.logger.info(messageBuilderFactory
61                  .builder()
62                  .format(
63                          response.isValid()
64                                  ? messageBuilderFactory
65                                          .builder()
66                                          .success("%sConfiguration validation of %s: %s")
67                                          .build()
68                                  : messageBuilderFactory
69                                          .builder()
70                                          .failure("%sConfiguration validation of %s: %s")
71                                          .build(),
72                          indent,
73                          response.getSource(),
74                          response.isValid() ? "VALID" : "INVALID")
75                  .build());
76          for (Map.Entry<SecDispatcher.ValidationResponse.Level, List<String>> entry :
77                  response.getReport().entrySet()) {
78              Consumer<String> consumer = s ->
79                      context.logger.info(messageBuilderFactory.builder().info(s).build());
80              if (entry.getKey() == SecDispatcher.ValidationResponse.Level.ERROR) {
81                  consumer = s -> context.logger.error(
82                          messageBuilderFactory.builder().error(s).build());
83              } else if (entry.getKey() == SecDispatcher.ValidationResponse.Level.WARNING) {
84                  consumer = s -> context.logger.warn(
85                          messageBuilderFactory.builder().warning(s).build());
86              }
87              for (String line : entry.getValue()) {
88                  consumer.accept(indent + "  " + line);
89              }
90          }
91          for (SecDispatcher.ValidationResponse subsystem : response.getSubsystems()) {
92              dumpResponse(context, indent + "  ", subsystem);
93          }
94      }
95  
96      protected abstract int doExecute(EncryptContext context) throws Exception;
97  }