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.internal.impl.transport.http;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.util.Arrays;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.function.Function;
029
030import org.eclipse.aether.internal.impl.checksum.Md5ChecksumAlgorithmFactory;
031import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
032import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractorStrategy;
033
034/**
035 * Generic checksum extractor that goes for "X-" headers.
036 */
037@Singleton
038@Named(XChecksumExtractor.NAME)
039public final class XChecksumExtractor extends ChecksumExtractorStrategy {
040    public static final String NAME = "xChecksum";
041
042    /**
043     * Header name prefixes, to be suffixed by lower case algorithm name. Tried in order, first prefix that yields
044     * any checksum wins.
045     */
046    private static final List<String> HEADER_PREFIXES = Arrays.asList(
047            // Central style: x-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
048            "x-checksum-",
049            // Google style: x-goog-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
050            "x-goog-meta-checksum-",
051            // AWS S3 style: x-amz-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
052            "x-amz-meta-checksum-");
053
054    @Override
055    public Map<String, String> extractChecksums(Function<String, String> headerGetter) {
056        for (String headerPrefix : HEADER_PREFIXES) {
057            Map<String, String> result = extractChecksums(headerGetter, headerPrefix);
058            if (!result.isEmpty()) {
059                return result;
060            }
061        }
062        return null;
063    }
064
065    private static Map<String, String> extractChecksums(Function<String, String> headerGetter, String headerPrefix) {
066        HashMap<String, String> result = new HashMap<>();
067        String value = headerGetter.apply(headerPrefix + "sha1");
068        if (value != null) {
069            result.put(Sha1ChecksumAlgorithmFactory.NAME, value);
070        }
071        value = headerGetter.apply(headerPrefix + "md5");
072        if (value != null) {
073            result.put(Md5ChecksumAlgorithmFactory.NAME, value);
074        }
075        return result;
076    }
077}