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.Inject;
022import javax.inject.Named;
023
024import java.util.Collections;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.repository.RemoteRepository;
030import org.eclipse.aether.spi.connector.transport.Transporter;
031import org.eclipse.aether.spi.connector.transport.TransporterFactory;
032import org.eclipse.aether.transfer.NoTransporterException;
033
034import static java.util.Objects.requireNonNull;
035
036/**
037 * A transporter factory for repositories using the {@code http:} or {@code https:} protocol. The provided transporters
038 * support uploads to WebDAV servers and resumable downloads.
039 */
040@Named("http")
041public final class HttpTransporterFactory implements TransporterFactory {
042    private static Map<String, ChecksumExtractor> getManuallyCreatedExtractors() {
043        HashMap<String, ChecksumExtractor> map = new HashMap<>();
044        map.put(Nexus2ChecksumExtractor.NAME, new Nexus2ChecksumExtractor());
045        map.put(XChecksumChecksumExtractor.NAME, new XChecksumChecksumExtractor());
046        return Collections.unmodifiableMap(map);
047    }
048
049    private float priority = 5.0f;
050
051    private final Map<String, ChecksumExtractor> extractors;
052
053    /**
054     * Ctor for ServiceLocator.
055     */
056    @Deprecated
057    public HttpTransporterFactory() {
058        this(getManuallyCreatedExtractors());
059    }
060
061    /**
062     * Creates an (uninitialized) instance of this transporter factory. <em>Note:</em> In case of manual instantiation
063     * by clients, the new factory needs to be configured via its various mutators before first use or runtime errors
064     * will occur.
065     */
066    @Inject
067    public HttpTransporterFactory(Map<String, ChecksumExtractor> extractors) {
068        this.extractors = requireNonNull(extractors);
069    }
070
071    @Override
072    public float getPriority() {
073        return priority;
074    }
075
076    /**
077     * Sets the priority of this component.
078     *
079     * @param priority The priority.
080     * @return This component for chaining, never {@code null}.
081     */
082    public HttpTransporterFactory setPriority(float priority) {
083        this.priority = priority;
084        return this;
085    }
086
087    @Override
088    public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
089            throws NoTransporterException {
090        requireNonNull(session, "session cannot be null");
091        requireNonNull(repository, "repository cannot be null");
092
093        return new HttpTransporter(extractors, repository, session);
094    }
095}