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.file;
20  
21  import java.nio.file.*;
22  
23  import org.eclipse.aether.repository.RemoteRepository;
24  import org.eclipse.aether.repository.RepositoryUriUtils;
25  import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
26  import org.eclipse.aether.spi.connector.transport.GetTask;
27  import org.eclipse.aether.spi.connector.transport.PeekTask;
28  import org.eclipse.aether.spi.connector.transport.PutTask;
29  import org.eclipse.aether.spi.connector.transport.TransportTask;
30  import org.eclipse.aether.transfer.NoTransporterException;
31  
32  /**
33   * A transporter using {@link java.io.File}.
34   */
35  final class FileTransporter extends AbstractTransporter {
36  
37      private final Path basePath;
38  
39      FileTransporter(RemoteRepository repository) throws NoTransporterException {
40          try {
41              basePath = Paths.get(RepositoryUriUtils.toUri(repository.getUrl())).toAbsolutePath();
42          } catch (FileSystemNotFoundException | IllegalArgumentException e) {
43              throw new NoTransporterException(repository, e);
44          }
45      }
46  
47      Path getBasePath() {
48          return basePath;
49      }
50  
51      @Override
52      public int classify(Throwable error) {
53          if (error instanceof ResourceNotFoundException) {
54              return ERROR_NOT_FOUND;
55          }
56          return ERROR_OTHER;
57      }
58  
59      @Override
60      protected void implPeek(PeekTask task) throws Exception {
61          getPath(task, true);
62      }
63  
64      @Override
65      protected void implGet(GetTask task) throws Exception {
66          Path path = getPath(task, true);
67          utilGet(task, Files.newInputStream(path), true, Files.size(path), false);
68      }
69  
70      @Override
71      protected void implPut(PutTask task) throws Exception {
72          Path path = getPath(task, false);
73          Files.createDirectories(path.getParent());
74          try {
75              utilPut(task, Files.newOutputStream(path), true);
76          } catch (Exception e) {
77              Files.deleteIfExists(path);
78              throw e;
79          }
80      }
81  
82      private Path getPath(TransportTask task, boolean required) throws Exception {
83          String path = task.getLocation().getPath();
84          if (path.contains("../")) {
85              throw new IllegalArgumentException("illegal resource path: " + path);
86          }
87          Path file = basePath.resolve(path);
88          if (required && !Files.isRegularFile(file)) {
89              throw new ResourceNotFoundException("Could not locate " + file);
90          }
91          return file;
92      }
93  
94      @Override
95      protected void implClose() {}
96  }