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.apache.maven.internal.impl;
20  
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  
24  import org.apache.maven.api.RemoteRepository;
25  import org.apache.maven.api.Session;
26  import org.apache.maven.api.di.Inject;
27  import org.apache.maven.api.di.Named;
28  import org.apache.maven.api.di.Singleton;
29  import org.apache.maven.api.services.Transport;
30  import org.apache.maven.api.services.TransportProvider;
31  import org.apache.maven.api.services.TransportProviderException;
32  import org.eclipse.aether.spi.connector.transport.TransporterProvider;
33  import org.eclipse.aether.transfer.NoTransporterException;
34  
35  import static java.util.Objects.requireNonNull;
36  
37  @Named
38  @Singleton
39  public class DefaultTransportProvider implements TransportProvider {
40      private final org.eclipse.aether.spi.connector.transport.TransporterProvider transporterProvider;
41  
42      @Inject
43      public DefaultTransportProvider(TransporterProvider transporterProvider) {
44          this.transporterProvider = requireNonNull(transporterProvider);
45      }
46  
47      @Override
48      public Transport transport(Session session, RemoteRepository repository) {
49          try {
50              URI baseURI = new URI(repository.getUrl());
51              return new DefaultTransport(
52                      baseURI,
53                      transporterProvider.newTransporter(
54                              InternalSession.from(session).getSession(),
55                              ((DefaultRemoteRepository) repository).getRepository()));
56          } catch (URISyntaxException e) {
57              throw new TransportProviderException("Remote repository URL invalid", e);
58          } catch (NoTransporterException e) {
59              throw new TransportProviderException("Unsupported remote repository", e);
60          }
61      }
62  }