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.HttpHeaders;
024import org.apache.http.HttpResponse;
025
026import javax.inject.Named;
027import javax.inject.Singleton;
028import java.util.Collections;
029import java.util.Map;
030
031/**
032 * A component extracting Nexus2 ETag "shielded" style-checksums from response headers.
033 *
034 * @since 1.8.0
035 */
036@Singleton
037@Named( Nexus2ChecksumExtractor.NAME )
038public class Nexus2ChecksumExtractor
039        extends ChecksumExtractor
040{
041    public static final String NAME = "nexus2";
042
043    @Override
044    public Map<String, String> extractChecksums( HttpResponse response )
045    {
046        // Nexus-style, ETag: "{SHA1{d40d68ba1f88d8e9b0040f175a6ff41928abd5e7}}"
047        Header header = response.getFirstHeader( HttpHeaders.ETAG );
048        String etag = header != null ? header.getValue() : null;
049        if ( etag != null )
050        {
051            int start = etag.indexOf( "SHA1{" ), end = etag.indexOf( "}", start + 5 );
052            if ( start >= 0 && end > start )
053            {
054                return Collections.singletonMap( "SHA-1", etag.substring( start + 5, end ) );
055            }
056        }
057        return null;
058    }
059}