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