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.eclipse.aether.connector.basic;
20  
21  import java.nio.ByteBuffer;
22  import java.security.MessageDigest;
23  import java.security.NoSuchAlgorithmException;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Set;
29  
30  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm;
31  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
32  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
33  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport;
34  import org.eclipse.aether.util.StringDigestUtil;
35  
36  import static java.util.stream.Collectors.toList;
37  
38  /**
39   * Test implementation of {@link ChecksumAlgorithmFactorySelector}.
40   */
41  public class TestChecksumAlgorithmSelector implements ChecksumAlgorithmFactorySelector {
42      public static final String SHA512 = "SHA-512";
43  
44      public static final String SHA256 = "SHA-256";
45  
46      public static final String SHA1 = "SHA-1";
47  
48      public static final String MD5 = "MD5";
49  
50      public static final String TEST_CHECKSUM = "test";
51  
52      public static final String TEST_CHECKSUM_VALUE = "01020304";
53  
54      @Override
55      public Set<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() {
56          return Collections.emptySet(); // irrelevant
57      }
58  
59      @Override
60      public ChecksumAlgorithmFactory select(final String algorithm) {
61          if (TEST_CHECKSUM.equals(algorithm)) {
62              return new ChecksumAlgorithmFactorySupport(TEST_CHECKSUM, "test") {
63                  @Override
64                  public ChecksumAlgorithm getAlgorithm() {
65                      return new ChecksumAlgorithm() {
66                          @Override
67                          public void update(final ByteBuffer input) {}
68  
69                          @Override
70                          public String checksum() {
71                              return TEST_CHECKSUM_VALUE;
72                          }
73                      };
74                  }
75              };
76          }
77          return new MessageDigestChecksumAlgorithmFactory(algorithm);
78      }
79  
80      @Override
81      public List<ChecksumAlgorithmFactory> selectList(Collection<String> algorithmNames) {
82          return algorithmNames.stream().map(this::select).collect(toList());
83      }
84  
85      @Override
86      public boolean isChecksumExtension(String extension) {
87          throw new RuntimeException("not implemented");
88      }
89  
90      private static class MessageDigestChecksumAlgorithmFactory extends ChecksumAlgorithmFactorySupport {
91          public MessageDigestChecksumAlgorithmFactory(String name) {
92              super(name, name.replace("-", "").toLowerCase(Locale.ENGLISH));
93          }
94  
95          @Override
96          public ChecksumAlgorithm getAlgorithm() {
97              try {
98                  MessageDigest messageDigest = MessageDigest.getInstance(getName());
99                  return new ChecksumAlgorithm() {
100                     @Override
101                     public void update(final ByteBuffer input) {
102                         messageDigest.update(input);
103                     }
104 
105                     @Override
106                     public String checksum() {
107                         return StringDigestUtil.toHexString(messageDigest.digest());
108                     }
109                 };
110             } catch (NoSuchAlgorithmException e) {
111                 throw new IllegalArgumentException("Algorithm '" + getName() + "' not supported.");
112             }
113         }
114     }
115 }