001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.transport.http;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.http.Header;
028import org.apache.http.HttpResponse;
029
030/**
031 * A component extracting {@code x-} non-standard style checksums from response headers.
032 * Tried headers (in order):
033 * <ul>
034 *     <li>{@code x-checksum-sha1} - Maven Central and other CDNs</li>
035 *     <li>{@code x-checksum-md5} - Maven Central and other CDNs</li>
036 *     <li>{@code x-goog-meta-checksum-sha1} - GCS</li>
037 *     <li>{@code x-goog-meta-checksum-md5} - GCS</li>
038 * </ul>
039 *
040 * @since 1.8.0
041 */
042@Singleton
043@Named(XChecksumChecksumExtractor.NAME)
044public class XChecksumChecksumExtractor extends ChecksumExtractor {
045    public static final String NAME = "x-checksum";
046
047    @Override
048    public Map<String, String> extractChecksums(HttpResponse response) {
049        String value;
050        HashMap<String, String> result = new HashMap<>();
051        // Central style: x-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
052        value = extractChecksum(response, "x-checksum-sha1");
053        if (value != null) {
054            result.put("SHA-1", value);
055        }
056        // Central style: x-checksum-md5: 9ad0d8e3482767c122e85f83567b8ce6
057        value = extractChecksum(response, "x-checksum-md5");
058        if (value != null) {
059            result.put("MD5", value);
060        }
061        if (!result.isEmpty()) {
062            return result;
063        }
064        // Google style: x-goog-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
065        value = extractChecksum(response, "x-goog-meta-checksum-sha1");
066        if (value != null) {
067            result.put("SHA-1", value);
068        }
069        // Central style: x-goog-meta-checksum-sha1: 9ad0d8e3482767c122e85f83567b8ce6
070        value = extractChecksum(response, "x-goog-meta-checksum-md5");
071        if (value != null) {
072            result.put("MD5", value);
073        }
074
075        return result.isEmpty() ? null : result;
076    }
077
078    private String extractChecksum(HttpResponse response, String name) {
079        Header header = response.getFirstHeader(name);
080        return header != null ? header.getValue() : null;
081    }
082}