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.repository.*;
22 import org.apache.archiva.repository.content.ItemSelector;
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 public ItemSelector toItemSelector( String requestPath ) throws LayoutException
46 return repository.getContent( ).toItemSelector( requestPath );
51 * Tests the path to see if it conforms to the expectations of a metadata request.
54 * NOTE: This does a cursory check on the path's last element. A result of true
55 * from this method is not a guarantee that the metadata is in a valid format, or
56 * that it even contains data.
59 * @param requestedPath the path to test.
60 * @return true if the requestedPath is likely a metadata request.
62 public boolean isMetadata( String requestedPath )
64 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
68 * @param requestedPath
69 * @return true if the requestedPath is likely an archetype catalog request.
71 public boolean isArchetypeCatalog( String requestedPath )
73 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
78 * Tests the path to see if it conforms to the expectations of a support file request.
81 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
84 * NOTE: This does a cursory check on the path's extension only. A result of true
85 * from this method is not a guarantee that the support resource is in a valid format, or
86 * that it even contains data.
89 * @param requestedPath the path to test.
90 * @return true if the requestedPath is likely that of a support file request.
92 public boolean isSupportFile( String requestedPath )
94 int idx = requestedPath.lastIndexOf( '.' );
100 String ext = requestedPath.substring( idx );
101 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
104 public boolean isMetadataSupportFile( String requestedPath )
106 if ( isSupportFile( requestedPath ) )
108 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
109 if ( isMetadata( basefilePath ) )
119 public String getLayout(String requestPath) {
120 if (isDefault(requestPath)) {
122 } else if (isLegacy(requestPath)) {
131 * Tests the path to see if it conforms to the expectations of a default layout request.
134 * NOTE: This does a cursory check on the count of path elements only. A result of
135 * true from this method is not a guarantee that the path sections are valid and
136 * can be resolved to an artifact reference. use {@link #toItemSelector(String)}
137 * if you want a more complete analysis of the validity of the path.
140 * @param requestedPath the path to test.
141 * @return true if the requestedPath is likely that of a default layout request.
143 private boolean isDefault( String requestedPath )
145 if ( StringUtils.isBlank( requestedPath ) )
150 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
151 if ( pathParts.length > 3 )
155 else if ( pathParts.length == 3 )
157 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
158 if ( isMetadata( requestedPath ) )
164 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
165 int idx = requestedPath.lastIndexOf( '.' );
168 String base = requestedPath.substring( 0, idx );
169 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
186 * Tests the path to see if it conforms to the expectations of a legacy layout request.
189 * NOTE: This does a cursory check on the count of path elements only. A result of
190 * true from this method is not a guarantee that the path sections are valid and
191 * can be resolved to an artifact reference. Use {@link #toItemSelector(String)}
192 * if you want a more complete analysis of the validity of the path.
195 * @param requestedPath the path to test.
196 * @return true if the requestedPath is likely that of a legacy layout request.
198 private boolean isLegacy( String requestedPath )
200 if ( StringUtils.isBlank( requestedPath ) )
205 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
206 return pathParts.length == 3;
210 * Adjust the requestedPath to conform to the native layout of the provided {@link BaseRepositoryContentLayout}.
212 * @param requestedPath the incoming requested path.
213 * @return the adjusted (to native) path.
214 * @throws LayoutException if the path cannot be parsed.
216 public String toNativePath( String requestedPath)
217 throws LayoutException
219 if ( StringUtils.isBlank( requestedPath ) )
221 throw new LayoutException( "Request Path is blank." );
224 String referencedResource = requestedPath;
225 // No checksum by default.
226 String supportfile = "";
228 // Figure out support file, and actual referencedResource.
229 if ( isSupportFile( requestedPath ) )
231 int idx = requestedPath.lastIndexOf( '.' );
232 referencedResource = requestedPath.substring( 0, idx );
233 supportfile = requestedPath.substring( idx );
236 if ( isMetadata( referencedResource ) )
238 /* Nothing to translate.
239 * Default layout is the only layout that can contain maven-metadata.xml files, and
240 * if the managedRepository is layout legacy, this request would never occur.
242 if (requestedPath.startsWith( "/" )) {
243 return requestedPath;
246 return "/"+requestedPath;
252 // Treat as an artifact reference.
253 String adjustedPath = repository.getContent( ).toPath( repository.getContent( ).toItem( requestedPath ) );
254 return adjustedPath + supportfile;
258 public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature(Class<T> clazz) throws UnsupportedFeatureException {
263 public <T extends RepositoryFeature<T>> boolean supportsFeature(Class<T> clazz) {