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.codehaus.plexus.components.secdispatcher.internal.sources;
20  
21  import java.util.function.Function;
22  import java.util.function.Predicate;
23  
24  import org.codehaus.plexus.components.secdispatcher.MasterSource;
25  import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
26  import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Master password source support class.
32   */
33  public abstract class MasterSourceSupport implements MasterSource {
34      private final Predicate<String> matcher;
35      private final Function<String, String> transformer;
36  
37      public MasterSourceSupport(Predicate<String> matcher, Function<String, String> transformer) {
38          this.matcher = requireNonNull(matcher);
39          this.transformer = requireNonNull(transformer);
40      }
41  
42      @Override
43      public String handle(String masterSource) throws SecDispatcherException {
44          if (matcher.test(masterSource)) {
45              return doHandle(transformer.apply(masterSource));
46          }
47          return null;
48      }
49  
50      protected abstract String doHandle(String transformed) throws SecDispatcherException;
51  
52      public SecDispatcher.ValidationResponse validateConfiguration(String masterSource) {
53          if (matcher.test(masterSource)) {
54              return doValidateConfiguration(transformer.apply(masterSource));
55          }
56          return null;
57      }
58  
59      protected abstract SecDispatcher.ValidationResponse doValidateConfiguration(String transformed);
60  }