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.HashMap;
25  import java.util.Map;
26  import java.util.function.Function;
27  
28  import org.eclipse.aether.internal.impl.checksum.Md5ChecksumAlgorithmFactory;
29  import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
30  import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractorStrategy;
31  
32  /**
33   * Generic checksum extractor that goes for "X-" headers.
34   */
35  @Singleton
36  @Named(XChecksumExtractor.NAME)
37  public final class XChecksumExtractor extends ChecksumExtractorStrategy {
38      public static final String NAME = "xChecksum";
39  
40      @Override
41      public Map<String, String> extractChecksums(Function<String, String> headerGetter) {
42          String value;
43          HashMap<String, String> result = new HashMap<>();
44          // Central style: x-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
45          value = headerGetter.apply("x-checksum-sha1");
46          if (value != null) {
47              result.put(Sha1ChecksumAlgorithmFactory.NAME, value);
48          }
49          // Central style: x-checksum-md5: 9ad0d8e3482767c122e85f83567b8ce6
50          value = headerGetter.apply("x-checksum-md5");
51          if (value != null) {
52              result.put(Md5ChecksumAlgorithmFactory.NAME, value);
53          }
54          if (!result.isEmpty()) {
55              return result;
56          }
57          // Google style: x-goog-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
58          value = headerGetter.apply("x-goog-meta-checksum-sha1");
59          if (value != null) {
60              result.put(Sha1ChecksumAlgorithmFactory.NAME, value);
61          }
62          // Central style: x-goog-meta-checksum-sha1: 9ad0d8e3482767c122e85f83567b8ce6
63          value = headerGetter.apply("x-goog-meta-checksum-md5");
64          if (value != null) {
65              result.put(Md5ChecksumAlgorithmFactory.NAME, value);
66          }
67          return result.isEmpty() ? null : result;
68      }
69  }