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.PathParser;
24 import org.apache.archiva.repository.features.RepositoryFeature;
25 import org.apache.archiva.repository.metadata.base.MetadataTools;
26 import org.apache.commons.lang3.StringUtils;
29 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
32 public class MavenRepositoryRequestInfo implements RepositoryRequestInfo
34 private PathParser defaultPathParser = new DefaultPathParser();
36 ManagedRepository repository;
38 public MavenRepositoryRequestInfo(ManagedRepository repository)
40 this.repository = repository;
44 * Takes an incoming requested path (in "/" format) and gleans the layout
45 * and ArtifactReference appropriate for that content.
47 * @param requestedPath the relative path to the content.
48 * @return the ArtifactReference for the requestedPath.
49 * @throws LayoutException if the request path is not layout valid.
51 public ArtifactReference toArtifactReference( String requestedPath )
52 throws LayoutException
54 if ( StringUtils.isBlank( requestedPath ) )
56 throw new LayoutException( "Blank request path is not a valid." );
59 String path = requestedPath;
60 while ( path.startsWith( "/" ) )
62 path = path.substring( 1 );
64 // Only slash? that's bad, mmm-kay?
65 if ( "/".equals( path ) )
67 throw new LayoutException( "Invalid request path: Slash only." );
71 if ( isDefault( path ) )
73 return defaultPathParser.toArtifactReference( path );
75 else if ( isLegacy( path ) )
77 throw new LayoutException( "Legacy Maven1 repository not supported anymore." );
81 throw new LayoutException( "Not a valid request path layout, too short." );
87 * Tests the path to see if it conforms to the expectations of a metadata request.
90 * NOTE: This does a cursory check on the path's last element. A result of true
91 * from this method is not a guarantee that the metadata is in a valid format, or
92 * that it even contains data.
95 * @param requestedPath the path to test.
96 * @return true if the requestedPath is likely a metadata request.
98 public boolean isMetadata( String requestedPath )
100 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
104 * @param requestedPath
105 * @return true if the requestedPath is likely an archetype catalog request.
107 public boolean isArchetypeCatalog( String requestedPath )
109 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
114 * Tests the path to see if it conforms to the expectations of a support file request.
117 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
120 * NOTE: This does a cursory check on the path's extension only. A result of true
121 * from this method is not a guarantee that the support resource is in a valid format, or
122 * that it even contains data.
125 * @param requestedPath the path to test.
126 * @return true if the requestedPath is likely that of a support file request.
128 public boolean isSupportFile( String requestedPath )
130 int idx = requestedPath.lastIndexOf( '.' );
136 String ext = requestedPath.substring( idx );
137 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
140 public boolean isMetadataSupportFile( String requestedPath )
142 if ( isSupportFile( requestedPath ) )
144 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
145 if ( isMetadata( basefilePath ) )
155 public String getLayout(String requestPath) {
156 if (isDefault(requestPath)) {
158 } else if (isLegacy(requestPath)) {
167 * Tests the path to see if it conforms to the expectations of a default layout request.
170 * NOTE: This does a cursory check on the count of path elements only. A result of
171 * true from this method is not a guarantee that the path sections are valid and
172 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
173 * if you want a more complete analysis of the validity of the path.
176 * @param requestedPath the path to test.
177 * @return true if the requestedPath is likely that of a default layout request.
179 private boolean isDefault( String requestedPath )
181 if ( StringUtils.isBlank( requestedPath ) )
186 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
187 if ( pathParts.length > 3 )
191 else if ( pathParts.length == 3 )
193 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
194 if ( isMetadata( requestedPath ) )
200 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
201 int idx = requestedPath.lastIndexOf( '.' );
204 String base = requestedPath.substring( 0, idx );
205 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
222 * Tests the path to see if it conforms to the expectations of a legacy layout request.
225 * NOTE: This does a cursory check on the count of path elements only. A result of
226 * true from this method is not a guarantee that the path sections are valid and
227 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
228 * if you want a more complete analysis of the validity of the path.
231 * @param requestedPath the path to test.
232 * @return true if the requestedPath is likely that of a legacy layout request.
234 private boolean isLegacy( String requestedPath )
236 if ( StringUtils.isBlank( requestedPath ) )
241 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
242 return pathParts.length == 3;
246 * Adjust the requestedPath to conform to the native layout of the provided {@link BaseRepositoryContentLayout}.
248 * @param requestedPath the incoming requested path.
249 * @return the adjusted (to native) path.
250 * @throws LayoutException if the path cannot be parsed.
252 public String toNativePath( String requestedPath)
253 throws LayoutException
255 if ( StringUtils.isBlank( requestedPath ) )
257 throw new LayoutException( "Request Path is blank." );
260 String referencedResource = requestedPath;
261 // No checksum by default.
262 String supportfile = "";
264 // Figure out support file, and actual referencedResource.
265 if ( isSupportFile( requestedPath ) )
267 int idx = requestedPath.lastIndexOf( '.' );
268 referencedResource = requestedPath.substring( 0, idx );
269 supportfile = requestedPath.substring( idx );
272 if ( isMetadata( referencedResource ) )
274 /* Nothing to translate.
275 * Default layout is the only layout that can contain maven-metadata.xml files, and
276 * if the managedRepository is layout legacy, this request would never occur.
278 return requestedPath;
281 // Treat as an artifact reference.
282 ArtifactReference ref = toArtifactReference( referencedResource );
283 String adjustedPath = repository.getContent().getLayout( BaseRepositoryContentLayout.class ).toPath( ref );
284 return adjustedPath + supportfile;
288 public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature(Class<T> clazz) throws UnsupportedFeatureException {
293 public <T extends RepositoryFeature<T>> boolean supportsFeature(Class<T> clazz) {