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.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 27 import org.apache.http.Header; 28 import org.apache.http.HttpResponse; 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 extends ChecksumExtractor { 45 public static final String NAME = "x-checksum"; 46 47 @Override 48 public Map<String, String> extractChecksums(HttpResponse response) { 49 String value; 50 HashMap<String, String> result = new HashMap<>(); 51 // Central style: x-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b 52 value = extractChecksum(response, "x-checksum-sha1"); 53 if (value != null) { 54 result.put("SHA-1", value); 55 } 56 // Central style: x-checksum-md5: 9ad0d8e3482767c122e85f83567b8ce6 57 value = extractChecksum(response, "x-checksum-md5"); 58 if (value != null) { 59 result.put("MD5", value); 60 } 61 if (!result.isEmpty()) { 62 return result; 63 } 64 // Google style: x-goog-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b 65 value = extractChecksum(response, "x-goog-meta-checksum-sha1"); 66 if (value != null) { 67 result.put("SHA-1", value); 68 } 69 // Central style: x-goog-meta-checksum-sha1: 9ad0d8e3482767c122e85f83567b8ce6 70 value = extractChecksum(response, "x-goog-meta-checksum-md5"); 71 if (value != null) { 72 result.put("MD5", value); 73 } 74 75 return result.isEmpty() ? null : result; 76 } 77 78 private String extractChecksum(HttpResponse response, String name) { 79 Header header = response.getFirstHeader(name); 80 return header != null ? header.getValue() : null; 81 } 82 }