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.io.IOException;
22  import java.io.UncheckedIOException;
23  import java.net.URI;
24  import java.nio.charset.Charset;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.util.Optional;
28  
29  import org.apache.maven.api.services.Transport;
30  import org.eclipse.aether.spi.connector.transport.GetTask;
31  import org.eclipse.aether.spi.connector.transport.PutTask;
32  import org.eclipse.aether.spi.connector.transport.Transporter;
33  
34  import static java.util.Objects.requireNonNull;
35  
36  public class DefaultTransport implements Transport {
37      private final URI baseURI;
38      private final Transporter transporter;
39  
40      public DefaultTransport(URI baseURI, Transporter transporter) {
41          this.baseURI = requireNonNull(baseURI);
42          this.transporter = requireNonNull(transporter);
43      }
44  
45      @Override
46      public boolean get(URI relativeSource, Path target) {
47          requireNonNull(relativeSource, "relativeSource is null");
48          requireNonNull(target, "target is null");
49          if (relativeSource.isAbsolute()) {
50              throw new IllegalArgumentException("Supplied URI is not relative");
51          }
52          URI source = baseURI.resolve(relativeSource);
53          if (!source.toASCIIString().startsWith(baseURI.toASCIIString())) {
54              throw new IllegalArgumentException("Supplied relative URI escapes baseUrl");
55          }
56          GetTask getTask = new GetTask(source);
57          getTask.setDataPath(target);
58          try {
59              transporter.get(getTask);
60              return true;
61          } catch (Exception e) {
62              if (Transporter.ERROR_NOT_FOUND != transporter.classify(e)) {
63                  throw new RuntimeException(e);
64              }
65              return false;
66          }
67      }
68  
69      @Override
70      public Optional<byte[]> getBytes(URI relativeSource) {
71          try {
72              Path tempPath = null;
73              try {
74                  tempPath = Files.createTempFile("transport-get", "tmp");
75                  if (get(relativeSource, tempPath)) {
76                      // TODO: check file size and prevent OOM?
77                      return Optional.of(Files.readAllBytes(tempPath));
78                  }
79                  return Optional.empty();
80              } finally {
81                  if (tempPath != null) {
82                      Files.deleteIfExists(tempPath);
83                  }
84              }
85          } catch (IOException e) {
86              throw new UncheckedIOException(e);
87          }
88      }
89  
90      @Override
91      public Optional<String> getString(URI relativeSource, Charset charset) {
92          requireNonNull(charset, "charset is null");
93          Optional<byte[]> data = getBytes(relativeSource);
94          return data.map(bytes -> new String(bytes, charset));
95      }
96  
97      @Override
98      public void put(Path source, URI relativeTarget) {
99          requireNonNull(source, "source is null");
100         requireNonNull(relativeTarget, "relativeTarget is null");
101         if (Files.isRegularFile(source)) {
102             throw new IllegalArgumentException("source file does not exist or is not a file");
103         }
104         if (relativeTarget.isAbsolute()) {
105             throw new IllegalArgumentException("Supplied URI is not relative");
106         }
107         URI target = baseURI.resolve(relativeTarget);
108         if (!target.toASCIIString().startsWith(baseURI.toASCIIString())) {
109             throw new IllegalArgumentException("Supplied relative URI escapes baseUrl");
110         }
111 
112         PutTask putTask = new PutTask(target);
113         putTask.setDataPath(source);
114         try {
115             transporter.put(putTask);
116         } catch (Exception e) {
117             throw new RuntimeException(e);
118         }
119     }
120 
121     @Override
122     public void putBytes(byte[] source, URI relativeTarget) {
123         requireNonNull(source, "source is null");
124         try {
125             Path tempPath = null;
126             try {
127                 tempPath = Files.createTempFile("transport-get", "tmp");
128                 Files.write(tempPath, source);
129                 put(tempPath, relativeTarget);
130             } finally {
131                 if (tempPath != null) {
132                     Files.deleteIfExists(tempPath);
133                 }
134             }
135         } catch (IOException e) {
136             throw new UncheckedIOException(e);
137         }
138     }
139 
140     @Override
141     public void putString(String source, Charset charset, URI relativeTarget) {
142         requireNonNull(source, "source string is null");
143         requireNonNull(charset, "charset is null");
144         putBytes(source.getBytes(charset), relativeTarget);
145     }
146 
147     @Override
148     public void close() {
149         transporter.close();
150     }
151 }