1 package org.eclipse.aether.transport.http;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.http.Header;
23 import org.apache.http.HttpResponse;
24
25 import javax.inject.Named;
26 import javax.inject.Singleton;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 /**
31 * A component extracting {@code x-} non-standard style checksums from response headers.
32 * Tried headers (in order):
33 * <ul>
34 * <li>{@code x-checksum-sha1} - Maven Central and other CDNs</li>
35 * <li>{@code x-checksum-md5} - Maven Central and other CDNs</li>
36 * <li>{@code x-goog-meta-checksum-sha1} - GCS</li>
37 * <li>{@code x-goog-meta-checksum-md5} - GCS</li>
38 * </ul>
39 *
40 * @since 1.8.0
41 */
42 @Singleton
43 @Named( XChecksumChecksumExtractor.NAME )
44 public class XChecksumChecksumExtractor
45 extends ChecksumExtractor
46 {
47 public static final String NAME = "x-checksum";
48
49 @Override
50 public Map<String, String> extractChecksums( HttpResponse response )
51 {
52 String value;
53 HashMap<String, String> result = new HashMap<>();
54 // Central style: x-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
55 value = extractChecksum( response, "x-checksum-sha1" );
56 if ( value != null )
57 {
58 result.put( "SHA-1", value );
59 }
60 // Central style: x-checksum-md5: 9ad0d8e3482767c122e85f83567b8ce6
61 value = extractChecksum( response, "x-checksum-md5" );
62 if ( value != null )
63 {
64 result.put( "MD5", value );
65 }
66 if ( !result.isEmpty() )
67 {
68 return result;
69 }
70 // Google style: x-goog-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
71 value = extractChecksum( response, "x-goog-meta-checksum-sha1" );
72 if ( value != null )
73 {
74 result.put( "SHA-1", value );
75 }
76 // Central style: x-goog-meta-checksum-sha1: 9ad0d8e3482767c122e85f83567b8ce6
77 value = extractChecksum( response, "x-goog-meta-checksum-md5" );
78 if ( value != null )
79 {
80 result.put( "MD5", value );
81 }
82
83 return result.isEmpty() ? null : result;
84 }
85
86 private String extractChecksum( HttpResponse response, String name )
87 {
88 Header header = response.getFirstHeader( name );
89 return header != null ? header.getValue() : null;
90 }
91 }