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