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.Collections;
025import java.util.Map;
026
027import org.apache.http.Header;
028import org.apache.http.HttpHeaders;
029import org.apache.http.HttpResponse;
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 extends ChecksumExtractor {
039    public static final String NAME = "nexus2";
040
041    @Override
042    public Map<String, String> extractChecksums(HttpResponse response) {
043        // Nexus-style, ETag: "{SHA1{d40d68ba1f88d8e9b0040f175a6ff41928abd5e7}}"
044        Header header = response.getFirstHeader(HttpHeaders.ETAG);
045        String etag = header != null ? header.getValue() : null;
046        if (etag != null) {
047            int start = etag.indexOf("SHA1{"), end = etag.indexOf("}", start + 5);
048            if (start >= 0 && end > start) {
049                return Collections.singletonMap("SHA-1", etag.substring(start + 5, end));
050            }
051        }
052        return null;
053    }
054}