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.minio;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.util.Map;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.repository.RemoteRepository;
028import org.eclipse.aether.spi.connector.transport.Transporter;
029import org.eclipse.aether.spi.connector.transport.TransporterFactory;
030import org.eclipse.aether.spi.io.PathProcessor;
031import org.eclipse.aether.transfer.NoTransporterException;
032import org.eclipse.aether.util.ConfigUtils;
033
034import static java.util.Objects.requireNonNull;
035
036/**
037 * A transporter factory for repositories using the S3 API object storage using Minio.
038 *
039 * @since 2.0.2
040 */
041@Named(MinioTransporterFactory.NAME)
042public final class MinioTransporterFactory implements TransporterFactory {
043    public static final String NAME = "minio";
044
045    private static final float DEFAULT_PRIORITY = 0.0f;
046
047    private float priority = DEFAULT_PRIORITY;
048
049    private final Map<String, ObjectNameMapperFactory> objectNameMapperFactories;
050
051    private final PathProcessor pathProcessor;
052
053    @Inject
054    public MinioTransporterFactory(
055            Map<String, ObjectNameMapperFactory> objectNameMapperFactories, PathProcessor pathProcessor) {
056        this.objectNameMapperFactories = requireNonNull(objectNameMapperFactories, "objectNameMapperFactories");
057        this.pathProcessor = requireNonNull(pathProcessor, "pathProcessor");
058    }
059
060    @Override
061    public float getPriority() {
062        return priority;
063    }
064
065    public MinioTransporterFactory setPriority(float priority) {
066        this.priority = priority;
067        return this;
068    }
069
070    @Override
071    public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
072            throws NoTransporterException {
073        requireNonNull(session, "session cannot be null");
074        requireNonNull(repository, "repository cannot be null");
075
076        // this check is here only to support "minio+http" and "s3+http" protocols by default. But also when
077        // raised priorities by user, allow to "overtake" plain HTTP repositories, if needed.
078        RemoteRepository adjusted = repository;
079        if ("minio+http".equalsIgnoreCase(repository.getProtocol())
080                || "minio+https".equalsIgnoreCase(repository.getProtocol())) {
081            adjusted = new RemoteRepository.Builder(repository)
082                    .setUrl(repository.getUrl().substring("minio+".length()))
083                    .build();
084        } else if ("s3+http".equalsIgnoreCase(repository.getProtocol())
085                || "s3+https".equalsIgnoreCase(repository.getProtocol())) {
086            adjusted = new RemoteRepository.Builder(repository)
087                    .setUrl(repository.getUrl().substring("s3+".length()))
088                    .build();
089        } else if (priority == DEFAULT_PRIORITY) {
090            throw new NoTransporterException(
091                    repository,
092                    "To use Minio transport with plain HTTP/HTTPS repositories, increase the Minio transport priority");
093        }
094        String objectNameMapperConf = ConfigUtils.getString(
095                session,
096                MinioTransporterConfigurationKeys.DEFAULT_OBJECT_NAME_MAPPER,
097                MinioTransporterConfigurationKeys.CONFIG_PROP_OBJECT_NAME_MAPPER + "." + repository.getId(),
098                MinioTransporterConfigurationKeys.CONFIG_PROP_OBJECT_NAME_MAPPER);
099        ObjectNameMapperFactory objectNameMapperFactory = objectNameMapperFactories.get(objectNameMapperConf);
100        if (objectNameMapperFactory == null) {
101            throw new IllegalArgumentException("Unknown object name mapper configured '" + objectNameMapperConf
102                    + "' for repository " + repository.getId());
103        }
104        return new MinioTransporter(session, adjusted, objectNameMapperFactory, pathProcessor);
105    }
106}