1package org.apache.archiva.repository.content.maven2;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.model.ArtifactReference;
23import org.apache.archiva.repository.*;
24import org.apache.archiva.repository.content.PathParser;
25import org.apache.archiva.repository.features.RepositoryFeature;
26import org.apache.archiva.repository.metadata.base.MetadataTools;
27import org.apache.commons.lang3.StringUtils;
2829/**30 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate31 * ArtifactReference.32 */33publicclassMavenRepositoryRequestInfoimplements RepositoryRequestInfo
34 {
35private PathParser defaultPathParser = newDefaultPathParser();
3637 ManagedRepository repository;
3839publicMavenRepositoryRequestInfo(ManagedRepository repository)
40 {
41this.repository = repository;
42 }
4344/**45 * Takes an incoming requested path (in "/" format) and gleans the layout46 * and ArtifactReference appropriate for that content.47 *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.51 */52public ArtifactReference toArtifactReference( String requestedPath )
53throws LayoutException
54 {
55if ( StringUtils.isBlank( requestedPath ) )
56 {
57thrownew LayoutException( "Blank request path is not a valid." );
58 }
5960 String path = requestedPath;
61while ( path.startsWith( "/" ) )
62 {
63 path = path.substring( 1 );
6465// Only slash? that's bad, mmm-kay?66if ( "/".equals( path ) )
67 {
68thrownew LayoutException( "Invalid request path: Slash only." );
69 }
70 }
7172if ( isDefault( path ) )
73 {
74return defaultPathParser.toArtifactReference( path );
75 }
76elseif ( isLegacy( path ) )
77 {
78thrownew LayoutException( "Legacy Maven1 repository not supported anymore." );
79 }
80else81 {
82thrownew LayoutException( "Not a valid request path layout, too short." );
83 }
84 }
8586/**87 * <p>88 * Tests the path to see if it conforms to the expectations of a metadata request.89 * </p>90 * <p>91 * NOTE: This does a cursory check on the path's last element. A result of true92 * from this method is not a guarantee that the metadata is in a valid format, or93 * that it even contains data.94 * </p>95 *96 * @param requestedPath the path to test.97 * @return true if the requestedPath is likely a metadata request.98 */99publicboolean isMetadata( String requestedPath )
100 {
101return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
102 }
103104/**105 * @param requestedPath106 * @return true if the requestedPath is likely an archetype catalog request.107 */108publicboolean isArchetypeCatalog( String requestedPath )
109 {
110return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
111 }
112113/**114 * <p>115 * Tests the path to see if it conforms to the expectations of a support file request.116 * </p>117 * <p>118 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.119 * </p>120 * <p>121 * NOTE: This does a cursory check on the path's extension only. A result of true122 * from this method is not a guarantee that the support resource is in a valid format, or123 * that it even contains data.124 * </p>125 *126 * @param requestedPath the path to test.127 * @return true if the requestedPath is likely that of a support file request.128 */129publicboolean isSupportFile( String requestedPath )
130 {
131int idx = requestedPath.lastIndexOf( '.' );
132if ( idx <= 0 )
133 {
134return false;
135 }
136137 String ext = requestedPath.substring( idx );
138return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
139 }
140141publicboolean isMetadataSupportFile( String requestedPath )
142 {
143if ( isSupportFile( requestedPath ) )
144 {
145 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
146if ( isMetadata( basefilePath ) )
147 {
148returntrue;
149 }
150 }
151152return false;
153 }
154155 @Override
156public String getLayout(String requestPath) {
157if (isDefault(requestPath)) {
158return"default";
159 } elseif (isLegacy(requestPath)) {
160return"legacy";
161 } else {
162return"unknown";
163 }
164 }
165166/**167 * <p>168 * Tests the path to see if it conforms to the expectations of a default layout request.169 * </p>170 * <p>171 * NOTE: This does a cursory check on the count of path elements only. A result of172 * true from this method is not a guarantee that the path sections are valid and173 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}174 * if you want a more complete analysis of the validity of the path.175 * </p>176 *177 * @param requestedPath the path to test.178 * @return true if the requestedPath is likely that of a default layout request.179 */180privateboolean isDefault( String requestedPath )
181 {
182if ( StringUtils.isBlank( requestedPath ) )
183 {
184return false;
185 }
186187 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
188if ( pathParts.length > 3 )
189 {
190returntrue;
191 }
192elseif ( pathParts.length == 3 )
193 {
194// check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)195if ( isMetadata( requestedPath ) )
196 {
197returntrue;
198 }
199else200 {
201// check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)202int idx = requestedPath.lastIndexOf( '.' );
203if ( idx > 0 )
204 {
205 String base = requestedPath.substring( 0, idx );
206if ( isMetadata( base ) && isSupportFile( requestedPath ) )
207 {
208returntrue;
209 }
210 }
211212return false;
213 }
214 }
215else216 {
217return false;
218 }
219 }
220221/**222 * <p>223 * Tests the path to see if it conforms to the expectations of a legacy layout request.224 * </p>225 * <p>226 * NOTE: This does a cursory check on the count of path elements only. A result of227 * true from this method is not a guarantee that the path sections are valid and228 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}229 * if you want a more complete analysis of the validity of the path.230 * </p>231 *232 * @param requestedPath the path to test.233 * @return true if the requestedPath is likely that of a legacy layout request.234 */235privateboolean isLegacy( String requestedPath )
236 {
237if ( StringUtils.isBlank( requestedPath ) )
238 {
239return false;
240 }
241242 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
243return pathParts.length == 3;
244 }
245246/**247 * Adjust the requestedPath to conform to the native layout of the provided {@link org.apache.archiva.repository.ManagedRepositoryContent}.248 *249 * @param requestedPath the incoming requested path.250 * @return the adjusted (to native) path.251 * @throws LayoutException if the path cannot be parsed.252 */253public String toNativePath( String requestedPath)
254throws LayoutException
255 {
256if ( StringUtils.isBlank( requestedPath ) )
257 {
258thrownew LayoutException( "Request Path is blank." );
259 }
260261 String referencedResource = requestedPath;
262// No checksum by default.263 String supportfile = "";
264265// Figure out support file, and actual referencedResource.266if ( isSupportFile( requestedPath ) )
267 {
268int idx = requestedPath.lastIndexOf( '.' );
269 referencedResource = requestedPath.substring( 0, idx );
270 supportfile = requestedPath.substring( idx );
271 }
272273if ( isMetadata( referencedResource ) )
274 {
275/* Nothing to translate.276 * Default layout is the only layout that can contain maven-metadata.xml files, and277 * if the managedRepository is layout legacy, this request would never occur.278 */279return requestedPath;
280 }
281282// Treat as an artifact reference.283 ArtifactReference ref = toArtifactReference( referencedResource );
284 String adjustedPath = repository.getContent().toPath( ref );
285return adjustedPath + supportfile;
286 }
287288 @Override
289public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature(Class<T> clazz) throws UnsupportedFeatureException {
290returnnull;
291 }
292293 @Override
294public <T extends RepositoryFeature<T>> boolean supportsFeature(Class<T> clazz) {
295return false;
296 }
297 }