1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.apache;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.http.client.utils.URIUtils;
27
28
29
30
31 final class UriUtils {
32
33 public static URI resolve(URI base, URI ref) {
34 String path = ref.getRawPath();
35 if (path != null && !path.isEmpty()) {
36 path = base.getRawPath();
37 if (path == null || !path.endsWith("/")) {
38 try {
39 base = new URI(base.getScheme(), base.getAuthority(), base.getPath() + '/', null, null);
40 } catch (URISyntaxException e) {
41 throw new IllegalStateException(e);
42 }
43 }
44 }
45 return URIUtils.resolve(base, ref);
46 }
47
48 public static List<URI> getDirectories(URI base, URI uri) {
49 List<URI> dirs = new ArrayList<>();
50 for (URI dir = uri.resolve("."); !isBase(base, dir); dir = dir.resolve("..")) {
51 dirs.add(dir);
52 }
53 return dirs;
54 }
55
56 private static boolean isBase(URI base, URI uri) {
57 String path = uri.getRawPath();
58 if (path == null || "/".equals(path)) {
59 return true;
60 }
61 if (base != null) {
62 URI rel = base.relativize(uri);
63 if (rel.getRawPath() == null || rel.getRawPath().isEmpty() || rel.equals(uri)) {
64 return true;
65 }
66 }
67 return false;
68 }
69 }