1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }