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.io.File;
22  import java.nio.file.Files;
23  
24  import org.eclipse.aether.repository.RemoteRepository;
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  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * A transporter using {@link java.io.File}.
36   */
37  final class FileTransporter extends AbstractTransporter {
38  
39      private static final Logger LOGGER = LoggerFactory.getLogger(FileTransporter.class);
40  
41      private final File basedir;
42  
43      FileTransporter(RemoteRepository repository) throws NoTransporterException {
44          if (!"file".equalsIgnoreCase(repository.getProtocol())) {
45              throw new NoTransporterException(repository);
46          }
47          basedir = new File(PathUtils.basedir(repository.getUrl())).getAbsoluteFile();
48      }
49  
50      File getBasedir() {
51          return basedir;
52      }
53  
54      public int classify(Throwable error) {
55          if (error instanceof ResourceNotFoundException) {
56              return ERROR_NOT_FOUND;
57          }
58          return ERROR_OTHER;
59      }
60  
61      @Override
62      protected void implPeek(PeekTask task) throws Exception {
63          getFile(task, true);
64      }
65  
66      @Override
67      protected void implGet(GetTask task) throws Exception {
68          File file = getFile(task, true);
69          utilGet(task, Files.newInputStream(file.toPath()), true, file.length(), false);
70      }
71  
72      @Override
73      protected void implPut(PutTask task) throws Exception {
74          File file = getFile(task, false);
75          file.getParentFile().mkdirs();
76          try {
77              utilPut(task, Files.newOutputStream(file.toPath()), true);
78          } catch (Exception e) {
79              if (!file.delete() && file.exists()) {
80                  LOGGER.debug("Could not delete partial file {}", file);
81              }
82              throw e;
83          }
84      }
85  
86      private File getFile(TransportTask task, boolean required) throws Exception {
87          String path = task.getLocation().getPath();
88          if (path.contains("../")) {
89              throw new IllegalArgumentException("illegal resource path: " + path);
90          }
91          File file = new File(basedir, path);
92          if (required && !file.exists()) {
93              throw new ResourceNotFoundException("Could not locate " + file);
94          }
95          return file;
96      }
97  
98      @Override
99      protected void implClose() {}
100 }