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.http;
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   * Helps to deal with URIs.
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.length() > 0) {
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  }