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