View Javadoc
1   package org.eclipse.aether.transport.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Helps to deal with URIs.
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  }