1 package org.apache.archiva.repository.maven.content;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.model.ArtifactReference;
22 import org.apache.archiva.repository.*;
23 import org.apache.archiva.repository.content.ItemSelector;
24 import org.apache.archiva.repository.content.PathParser;
25 import org.apache.archiva.repository.features.RepositoryFeature;
26 import org.apache.archiva.repository.metadata.base.MetadataTools;
27 import org.apache.commons.lang3.StringUtils;
30 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
33 public class MavenRepositoryRequestInfo implements RepositoryRequestInfo
35 private PathParser defaultPathParser = new DefaultPathParser();
37 ManagedRepository repository;
39 public MavenRepositoryRequestInfo(ManagedRepository repository)
41 this.repository = repository;
45 * Takes an incoming requested path (in "/" format) and gleans the layout
46 * and ArtifactReference appropriate for that content.
48 * @param requestedPath the relative path to the content.
49 * @return the ArtifactReference for the requestedPath.
50 * @throws LayoutException if the request path is not layout valid.
52 public ArtifactReference toArtifactReference( String requestedPath )
53 throws LayoutException
55 if ( StringUtils.isBlank( requestedPath ) )
57 throw new LayoutException( "Blank request path is not a valid." );
60 String path = requestedPath;
61 while ( path.startsWith( "/" ) )
63 path = path.substring( 1 );
65 // Only slash? that's bad, mmm-kay?
66 if ( "/".equals( path ) )
68 throw new LayoutException( "Invalid request path: Slash only." );
72 if ( isDefault( path ) )
74 return defaultPathParser.toArtifactReference( path );
76 else if ( isLegacy( path ) )
78 throw new LayoutException( "Legacy Maven1 repository not supported anymore." );
82 throw new LayoutException( "Not a valid request path layout, too short." );
87 public ItemSelector toItemSelector( String requestPath ) throws LayoutException
89 return repository.getContent( ).toItemSelector( requestPath );
94 * Tests the path to see if it conforms to the expectations of a metadata request.
97 * NOTE: This does a cursory check on the path's last element. A result of true
98 * from this method is not a guarantee that the metadata is in a valid format, or
99 * that it even contains data.
102 * @param requestedPath the path to test.
103 * @return true if the requestedPath is likely a metadata request.
105 public boolean isMetadata( String requestedPath )
107 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
111 * @param requestedPath
112 * @return true if the requestedPath is likely an archetype catalog request.
114 public boolean isArchetypeCatalog( String requestedPath )
116 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
121 * Tests the path to see if it conforms to the expectations of a support file request.
124 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
127 * NOTE: This does a cursory check on the path's extension only. A result of true
128 * from this method is not a guarantee that the support resource is in a valid format, or
129 * that it even contains data.
132 * @param requestedPath the path to test.
133 * @return true if the requestedPath is likely that of a support file request.
135 public boolean isSupportFile( String requestedPath )
137 int idx = requestedPath.lastIndexOf( '.' );
143 String ext = requestedPath.substring( idx );
144 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
147 public boolean isMetadataSupportFile( String requestedPath )
149 if ( isSupportFile( requestedPath ) )
151 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
152 if ( isMetadata( basefilePath ) )
162 public String getLayout(String requestPath) {
163 if (isDefault(requestPath)) {
165 } else if (isLegacy(requestPath)) {
174 * Tests the path to see if it conforms to the expectations of a default layout request.
177 * NOTE: This does a cursory check on the count of path elements only. A result of
178 * true from this method is not a guarantee that the path sections are valid and
179 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
180 * if you want a more complete analysis of the validity of the path.
183 * @param requestedPath the path to test.
184 * @return true if the requestedPath is likely that of a default layout request.
186 private boolean isDefault( String requestedPath )
188 if ( StringUtils.isBlank( requestedPath ) )
193 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
194 if ( pathParts.length > 3 )
198 else if ( pathParts.length == 3 )
200 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
201 if ( isMetadata( requestedPath ) )
207 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
208 int idx = requestedPath.lastIndexOf( '.' );
211 String base = requestedPath.substring( 0, idx );
212 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
229 * Tests the path to see if it conforms to the expectations of a legacy layout request.
232 * NOTE: This does a cursory check on the count of path elements only. A result of
233 * true from this method is not a guarantee that the path sections are valid and
234 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
235 * if you want a more complete analysis of the validity of the path.
238 * @param requestedPath the path to test.
239 * @return true if the requestedPath is likely that of a legacy layout request.
241 private boolean isLegacy( String requestedPath )
243 if ( StringUtils.isBlank( requestedPath ) )
248 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
249 return pathParts.length == 3;
253 * Adjust the requestedPath to conform to the native layout of the provided {@link BaseRepositoryContentLayout}.
255 * @param requestedPath the incoming requested path.
256 * @return the adjusted (to native) path.
257 * @throws LayoutException if the path cannot be parsed.
259 public String toNativePath( String requestedPath)
260 throws LayoutException
262 if ( StringUtils.isBlank( requestedPath ) )
264 throw new LayoutException( "Request Path is blank." );
267 String referencedResource = requestedPath;
268 // No checksum by default.
269 String supportfile = "";
271 // Figure out support file, and actual referencedResource.
272 if ( isSupportFile( requestedPath ) )
274 int idx = requestedPath.lastIndexOf( '.' );
275 referencedResource = requestedPath.substring( 0, idx );
276 supportfile = requestedPath.substring( idx );
279 if ( isMetadata( referencedResource ) )
281 /* Nothing to translate.
282 * Default layout is the only layout that can contain maven-metadata.xml files, and
283 * if the managedRepository is layout legacy, this request would never occur.
285 if (requestedPath.startsWith( "/" )) {
286 return requestedPath;
289 return "/"+requestedPath;
295 // Treat as an artifact reference.
296 String adjustedPath = repository.getContent( ).toPath( repository.getContent( ).toItem( requestedPath ) );
297 return adjustedPath + supportfile;
301 public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature(Class<T> clazz) throws UnsupportedFeatureException {
306 public <T extends RepositoryFeature<T>> boolean supportsFeature(Class<T> clazz) {