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.HashMap;
025import java.util.Map;
026import java.util.function.Function;
027
028import org.eclipse.aether.internal.impl.checksum.Md5ChecksumAlgorithmFactory;
029import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
030import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractorStrategy;
031
032/**
033 * Generic checksum extractor that goes for "X-" headers.
034 */
035@Singleton
036@Named(XChecksumExtractor.NAME)
037public final class XChecksumExtractor extends ChecksumExtractorStrategy {
038    public static final String NAME = "xChecksum";
039
040    @Override
041    public Map<String, String> extractChecksums(Function<String, String> headerGetter) {
042        String value;
043        HashMap<String, String> result = new HashMap<>();
044        // Central style: x-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
045        value = headerGetter.apply("x-checksum-sha1");
046        if (value != null) {
047            result.put(Sha1ChecksumAlgorithmFactory.NAME, value);
048        }
049        // Central style: x-checksum-md5: 9ad0d8e3482767c122e85f83567b8ce6
050        value = headerGetter.apply("x-checksum-md5");
051        if (value != null) {
052            result.put(Md5ChecksumAlgorithmFactory.NAME, value);
053        }
054        if (!result.isEmpty()) {
055            return result;
056        }
057        // Google style: x-goog-meta-checksum-sha1: c74edb60ca2a0b57ef88d9a7da28f591e3d4ce7b
058        value = headerGetter.apply("x-goog-meta-checksum-sha1");
059        if (value != null) {
060            result.put(Sha1ChecksumAlgorithmFactory.NAME, value);
061        }
062        // Central style: x-goog-meta-checksum-sha1: 9ad0d8e3482767c122e85f83567b8ce6
063        value = headerGetter.apply("x-goog-meta-checksum-md5");
064        if (value != null) {
065            result.put(Md5ChecksumAlgorithmFactory.NAME, value);
066        }
067        return result.isEmpty() ? null : result;
068    }
069}