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.jdk;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import org.eclipse.aether.RepositorySystemSession;
25  import org.eclipse.aether.repository.RemoteRepository;
26  import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractor;
27  import org.eclipse.aether.spi.connector.transport.http.HttpTransporter;
28  import org.eclipse.aether.spi.connector.transport.http.HttpTransporterFactory;
29  import org.eclipse.aether.spi.io.PathProcessor;
30  import org.eclipse.aether.transfer.NoTransporterException;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * JDK Transport factory: on Java11+ it works.
36   *
37   * @since 2.0.0
38   */
39  @Named(JdkTransporterFactory.NAME)
40  public final class JdkTransporterFactory implements HttpTransporterFactory {
41      public static final String NAME = "jdk";
42  
43      private float priority = 10.0f;
44  
45      private final ChecksumExtractor checksumExtractor;
46  
47      private final PathProcessor pathProcessor;
48  
49      @Inject
50      public JdkTransporterFactory(ChecksumExtractor checksumExtractor, PathProcessor pathProcessor) {
51          this.checksumExtractor = requireNonNull(checksumExtractor, "checksumExtractor");
52          this.pathProcessor = requireNonNull(pathProcessor, "pathProcessor");
53      }
54  
55      @Override
56      public float getPriority() {
57          return priority;
58      }
59  
60      public JdkTransporterFactory setPriority(float priority) {
61          this.priority = priority;
62          return this;
63      }
64  
65      @Override
66      public HttpTransporter newInstance(RepositorySystemSession session, RemoteRepository repository)
67              throws NoTransporterException {
68          requireNonNull(session, "session cannot be null");
69          requireNonNull(repository, "repository cannot be null");
70  
71          if (!"http".equalsIgnoreCase(repository.getProtocol()) && !"https".equalsIgnoreCase(repository.getProtocol())) {
72              throw new NoTransporterException(repository, "Only HTTP/HTTPS is supported");
73          }
74  
75          return new JdkTransporter(session, repository, javaVersion(), checksumExtractor, pathProcessor);
76      }
77  
78      private static int javaVersion() {
79          try {
80              final String version = System.getProperty("java.version", "11" /* default must pass */);
81              final int dot = version.indexOf('.');
82              final int hyphen = version.indexOf('-');
83              final int sep = (dot > 0 && dot < hyphen || hyphen < 0) ? dot : hyphen;
84              return Integer.parseInt(sep > 0 ? version.substring(0, sep) : version);
85          } catch (final NumberFormatException nfe) {
86              return 11; // cannot be a pre-java 11 version so let it pass
87          }
88      }
89  }