View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.minio;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.Map;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.repository.RemoteRepository;
28  import org.eclipse.aether.spi.connector.transport.Transporter;
29  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
30  import org.eclipse.aether.transfer.NoTransporterException;
31  import org.eclipse.aether.util.ConfigUtils;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * A transporter factory for repositories using the S3 API object storage using Minio.
37   *
38   * @since 2.0.2
39   */
40  @Named(MinioTransporterFactory.NAME)
41  public final class MinioTransporterFactory implements TransporterFactory {
42      public static final String NAME = "minio";
43  
44      private static final float DEFAULT_PRIORITY = 0.0f;
45  
46      private float priority = DEFAULT_PRIORITY;
47  
48      private final Map<String, ObjectNameMapperFactory> objectNameMapperFactories;
49  
50      @Inject
51      public MinioTransporterFactory(Map<String, ObjectNameMapperFactory> objectNameMapperFactories) {
52          this.objectNameMapperFactories = requireNonNull(objectNameMapperFactories, "objectNameMapperFactories");
53      }
54  
55      @Override
56      public float getPriority() {
57          return priority;
58      }
59  
60      public MinioTransporterFactory setPriority(float priority) {
61          this.priority = priority;
62          return this;
63      }
64  
65      @Override
66      public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
67              throws NoTransporterException {
68          requireNonNull(session, "session cannot be null");
69          requireNonNull(repository, "repository cannot be null");
70  
71          // this check is here only to support "minio+http" and "s3+http" protocols by default. But also when
72          // raised priorities by user, allow to "overtake" plain HTTP repositories, if needed.
73          RemoteRepository adjusted = repository;
74          if ("minio+http".equalsIgnoreCase(repository.getProtocol())
75                  || "minio+https".equalsIgnoreCase(repository.getProtocol())) {
76              adjusted = new RemoteRepository.Builder(repository)
77                      .setUrl(repository.getUrl().substring("minio+".length()))
78                      .build();
79          } else if ("s3+http".equalsIgnoreCase(repository.getProtocol())
80                  || "s3+https".equalsIgnoreCase(repository.getProtocol())) {
81              adjusted = new RemoteRepository.Builder(repository)
82                      .setUrl(repository.getUrl().substring("s3+".length()))
83                      .build();
84          } else if (priority == DEFAULT_PRIORITY) {
85              throw new NoTransporterException(
86                      repository,
87                      "To use Minio transport with plain HTTP/HTTPS repositories, increase the Minio transport priority");
88          }
89          String objectNameMapperConf = ConfigUtils.getString(
90                  session,
91                  MinioTransporterConfigurationKeys.DEFAULT_OBJECT_NAME_MAPPER,
92                  MinioTransporterConfigurationKeys.CONFIG_PROP_OBJECT_NAME_MAPPER + "." + repository.getId(),
93                  MinioTransporterConfigurationKeys.CONFIG_PROP_OBJECT_NAME_MAPPER);
94          ObjectNameMapperFactory objectNameMapperFactory = objectNameMapperFactories.get(objectNameMapperConf);
95          if (objectNameMapperFactory == null) {
96              throw new IllegalArgumentException("Unknown object name mapper configured '" + objectNameMapperConf
97                      + "' for repository " + repository.getId());
98          }
99          return new MinioTransporter(session, adjusted, objectNameMapperFactory);
100     }
101 }