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.ManagedRepository;
22 import org.apache.archiva.repository.RepositoryRequestInfo;
23 import org.apache.archiva.repository.UnsupportedFeatureException;
24 import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
25 import org.apache.archiva.repository.content.ItemSelector;
26 import org.apache.archiva.repository.content.LayoutException;
27 import org.apache.archiva.repository.features.RepositoryFeature;
28 import org.apache.archiva.repository.metadata.base.MetadataTools;
29 import org.apache.commons.lang3.StringUtils;
32 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
35 public class MavenRepositoryRequestInfo implements RepositoryRequestInfo
37 ManagedRepository repository;
39 public MavenRepositoryRequestInfo(ManagedRepository repository)
41 this.repository = repository;
45 public ItemSelector toItemSelector( String requestPath ) throws LayoutException
47 return repository.getContent( ).toItemSelector( requestPath );
52 * Tests the path to see if it conforms to the expectations of a metadata request.
55 * NOTE: This does a cursory check on the path's last element. A result of true
56 * from this method is not a guarantee that the metadata is in a valid format, or
57 * that it even contains data.
60 * @param requestedPath the path to test.
61 * @return true if the requestedPath is likely a metadata request.
63 public boolean isMetadata( String requestedPath )
65 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
69 * @param requestedPath
70 * @return true if the requestedPath is likely an archetype catalog request.
72 public boolean isArchetypeCatalog( String requestedPath )
74 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
79 * Tests the path to see if it conforms to the expectations of a support file request.
82 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
85 * NOTE: This does a cursory check on the path's extension only. A result of true
86 * from this method is not a guarantee that the support resource is in a valid format, or
87 * that it even contains data.
90 * @param requestedPath the path to test.
91 * @return true if the requestedPath is likely that of a support file request.
93 public boolean isSupportFile( String requestedPath )
95 int idx = requestedPath.lastIndexOf( '.' );
101 String ext = requestedPath.substring( idx );
102 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
105 public boolean isMetadataSupportFile( String requestedPath )
107 if ( isSupportFile( requestedPath ) )
109 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
110 if ( isMetadata( basefilePath ) )
120 public String getLayout(String requestPath) {
121 if (isDefault(requestPath)) {
123 } else if (isLegacy(requestPath)) {
132 * Tests the path to see if it conforms to the expectations of a default layout request.
135 * NOTE: This does a cursory check on the count of path elements only. A result of
136 * true from this method is not a guarantee that the path sections are valid and
137 * can be resolved to an artifact reference. use {@link #toItemSelector(String)}
138 * if you want a more complete analysis of the validity of the path.
141 * @param requestedPath the path to test.
142 * @return true if the requestedPath is likely that of a default layout request.
144 private boolean isDefault( String requestedPath )
146 if ( StringUtils.isBlank( requestedPath ) )
151 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
152 if ( pathParts.length > 3 )
156 else if ( pathParts.length == 3 )
158 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
159 if ( isMetadata( requestedPath ) )
165 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
166 int idx = requestedPath.lastIndexOf( '.' );
169 String base = requestedPath.substring( 0, idx );
170 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
187 * Tests the path to see if it conforms to the expectations of a legacy layout request.
190 * NOTE: This does a cursory check on the count of path elements only. A result of
191 * true from this method is not a guarantee that the path sections are valid and
192 * can be resolved to an artifact reference. Use {@link #toItemSelector(String)}
193 * if you want a more complete analysis of the validity of the path.
196 * @param requestedPath the path to test.
197 * @return true if the requestedPath is likely that of a legacy layout request.
199 private boolean isLegacy( String requestedPath )
201 if ( StringUtils.isBlank( requestedPath ) )
206 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
207 return pathParts.length == 3;
211 * Adjust the requestedPath to conform to the native layout of the provided {@link BaseRepositoryContentLayout}.
213 * @param requestedPath the incoming requested path.
214 * @return the adjusted (to native) path.
215 * @throws LayoutException if the path cannot be parsed.
217 public String toNativePath( String requestedPath)
218 throws LayoutException
220 if ( StringUtils.isBlank( requestedPath ) )
222 throw new LayoutException( "Request Path is blank." );
225 String referencedResource = requestedPath;
226 // No checksum by default.
227 String supportfile = "";
229 // Figure out support file, and actual referencedResource.
230 if ( isSupportFile( requestedPath ) )
232 int idx = requestedPath.lastIndexOf( '.' );
233 referencedResource = requestedPath.substring( 0, idx );
234 supportfile = requestedPath.substring( idx );
237 if ( isMetadata( referencedResource ) )
239 /* Nothing to translate.
240 * Default layout is the only layout that can contain maven-metadata.xml files, and
241 * if the managedRepository is layout legacy, this request would never occur.
243 if (requestedPath.startsWith( "/" )) {
244 return requestedPath;
247 return "/"+requestedPath;
253 // Treat as an artifact reference.
254 String adjustedPath = repository.getContent( ).toPath( repository.getContent( ).toItem( requestedPath ) );
255 return adjustedPath + supportfile;
259 public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature(Class<T> clazz) throws UnsupportedFeatureException {
264 public <T extends RepositoryFeature<T>> boolean supportsFeature(Class<T> clazz) {