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.internal.impl;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import org.eclipse.aether.artifact.Artifact;
25  import org.eclipse.aether.metadata.Metadata;
26  
27  import static java.util.Objects.requireNonNull;
28  import static org.eclipse.aether.util.PathUtils.validateArtifactComponents;
29  import static org.eclipse.aether.util.PathUtils.validateMetadataComponents;
30  
31  /**
32   * Default implementation of {@link LocalPathComposer}.
33   *
34   * @since 1.8.1
35   */
36  @Singleton
37  @Named
38  public final class DefaultLocalPathComposer implements LocalPathComposer {
39      @Override
40      public String getPathForArtifact(Artifact artifact, boolean local) {
41          requireNonNull(artifact);
42          validateArtifactComponents(artifact);
43  
44          StringBuilder path = new StringBuilder(128);
45  
46          path.append(artifact.getGroupId().replace('.', '/')).append('/');
47  
48          path.append(artifact.getArtifactId()).append('/');
49  
50          path.append(artifact.getBaseVersion()).append('/');
51  
52          path.append(artifact.getArtifactId()).append('-');
53          if (local) {
54              path.append(artifact.getBaseVersion());
55          } else {
56              path.append(artifact.getVersion());
57          }
58  
59          if (!artifact.getClassifier().isEmpty()) {
60              path.append('-').append(artifact.getClassifier());
61          }
62  
63          if (!artifact.getExtension().isEmpty()) {
64              path.append('.').append(artifact.getExtension());
65          }
66  
67          return requireContainedPath(path.toString());
68      }
69  
70      @Override
71      public String getPathForMetadata(Metadata metadata, String repositoryKey) {
72          requireNonNull(metadata);
73          requireNonNull(repositoryKey);
74          validateMetadataComponents(metadata);
75  
76          StringBuilder path = new StringBuilder(128);
77  
78          if (!metadata.getGroupId().isEmpty()) {
79              path.append(metadata.getGroupId().replace('.', '/')).append('/');
80  
81              if (!metadata.getArtifactId().isEmpty()) {
82                  path.append(metadata.getArtifactId()).append('/');
83  
84                  if (!metadata.getVersion().isEmpty()) {
85                      path.append(metadata.getVersion()).append('/');
86                  }
87              }
88          }
89  
90          path.append(insertRepositoryKey(metadata.getType(), repositoryKey));
91  
92          return requireContainedPath(path.toString());
93      }
94  
95      /**
96       * Defense in depth: the composed path must be relative and must not contain parent-reference or empty
97       * segments, so that resolving it against the local repository base directory always yields a path within
98       * it. The coordinate validation performed on entry should already guarantee this; this is a last-resort
99       * check on the composed result.
100      */
101     private static String requireContainedPath(String path) {
102         if (path.startsWith("/")
103                 || path.contains("//")
104                 || path.equals("..")
105                 || path.startsWith("../")
106                 || path.endsWith("/..")
107                 || path.contains("/../")) {
108             throw new IllegalArgumentException(
109                     "Invalid coordinates: composed local repository path is not a valid relative path: " + path);
110         }
111         return path;
112     }
113 
114     private String insertRepositoryKey(String metadataType, String repositoryKey) {
115         if (metadataType.contains("/") && !metadataType.endsWith("/")) {
116             int lastSlash = metadataType.lastIndexOf('/');
117             return metadataType.substring(0, lastSlash + 1)
118                     + insertRepositoryKey(metadataType.substring(lastSlash + 1), repositoryKey);
119         } else {
120             String result;
121             int idx = metadataType.indexOf('.');
122             if (idx < 0) {
123                 result = metadataType + '-' + repositoryKey;
124             } else {
125                 result = metadataType.substring(0, idx) + '-' + repositoryKey + metadataType.substring(idx);
126             }
127             return result;
128         }
129     }
130 }