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.internal.impl.transport.http;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.function.Function;
29  
30  import org.eclipse.aether.internal.impl.checksum.Md5ChecksumAlgorithmFactory;
31  import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
32  import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractorStrategy;
33  
34  /**
35   * Generic checksum extractor that goes for "X-" headers.
36   */
37  @Singleton
38  @Named(XChecksumExtractor.NAME)
39  public final class XChecksumExtractor extends ChecksumExtractorStrategy {
40      public static final String NAME = "xChecksum";
41  
42      /**
43       * Header name prefixes, to be suffixed by lower case algorithm name. Tried in order, first prefix that yields
44       * any checksum wins.
45       */
46      private static final List<String> HEADER_PREFIXES = Arrays.asList(
47              // Central style: x-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
48              "x-checksum-",
49              // Google style: x-goog-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
50              "x-goog-meta-checksum-",
51              // AWS S3 style: x-amz-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
52              "x-amz-meta-checksum-");
53  
54      @Override
55      public Map<String, String> extractChecksums(Function<String, String> headerGetter) {
56          for (String headerPrefix : HEADER_PREFIXES) {
57              Map<String, String> result = extractChecksums(headerGetter, headerPrefix);
58              if (!result.isEmpty()) {
59                  return result;
60              }
61          }
62          return null;
63      }
64  
65      private static Map<String, String> extractChecksums(Function<String, String> headerGetter, String headerPrefix) {
66          HashMap<String, String> result = new HashMap<>();
67          String value = headerGetter.apply(headerPrefix + "sha1");
68          if (value != null) {
69              result.put(Sha1ChecksumAlgorithmFactory.NAME, value);
70          }
71          value = headerGetter.apply(headerPrefix + "md5");
72          if (value != null) {
73              result.put(Md5ChecksumAlgorithmFactory.NAME, value);
74          }
75          return result;
76      }
77  }