1 package org.apache.archiva.repository.content.maven2;
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
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.model.ArtifactReference;
23 import org.apache.archiva.repository.LayoutException;
24 import org.apache.archiva.repository.ManagedRepositoryContent;
25 import org.apache.archiva.repository.content.PathParser;
26 import org.apache.archiva.repository.metadata.MetadataTools;
27 import org.apache.commons.lang.StringUtils;
30 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
33 public class RepositoryRequest
35 private PathParser defaultPathParser = new DefaultPathParser();
37 public RepositoryRequest()
43 * Takes an incoming requested path (in "/" format) and gleans the layout
44 * and ArtifactReference appropriate for that content.
46 * @param requestedPath the relative path to the content.
47 * @return the ArtifactReference for the requestedPath.
48 * @throws LayoutException if the request path is not layout valid.
50 public ArtifactReference toArtifactReference( String requestedPath )
51 throws LayoutException
53 if ( StringUtils.isBlank( requestedPath ) )
55 throw new LayoutException( "Blank request path is not a valid." );
58 String path = requestedPath;
59 while ( path.startsWith( "/" ) )
61 path = path.substring( 1 );
63 // Only slash? that's bad, mmm-kay?
64 if ( "/".equals( path ) )
66 throw new LayoutException( "Invalid request path: Slash only." );
70 if ( isDefault( path ) )
72 return defaultPathParser.toArtifactReference( path );
74 else if ( isLegacy( path ) )
76 throw new LayoutException( "Legacy Maven1 repository not supported anymore." );
80 throw new LayoutException( "Not a valid request path layout, too short." );
86 * Tests the path to see if it conforms to the expectations of a metadata request.
89 * NOTE: This does a cursory check on the path's last element. A result of true
90 * from this method is not a guarantee that the metadata is in a valid format, or
91 * that it even contains data.
94 * @param requestedPath the path to test.
95 * @return true if the requestedPath is likely a metadata request.
97 public boolean isMetadata( String requestedPath )
99 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
103 * @param requestedPath
104 * @return true if the requestedPath is likely an archetype catalog request.
106 public boolean isArchetypeCatalog( String requestedPath )
108 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
113 * Tests the path to see if it conforms to the expectations of a support file request.
116 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
119 * NOTE: This does a cursory check on the path's extension only. A result of true
120 * from this method is not a guarantee that the support resource is in a valid format, or
121 * that it even contains data.
124 * @param requestedPath the path to test.
125 * @return true if the requestedPath is likely that of a support file request.
127 public boolean isSupportFile( String requestedPath )
129 int idx = requestedPath.lastIndexOf( '.' );
135 String ext = requestedPath.substring( idx );
136 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
139 public boolean isMetadataSupportFile( String requestedPath )
141 if ( isSupportFile( requestedPath ) )
143 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
144 if ( isMetadata( basefilePath ) )
155 * Tests the path to see if it conforms to the expectations of a default layout request.
158 * NOTE: This does a cursory check on the count of path elements only. A result of
159 * true from this method is not a guarantee that the path sections are valid and
160 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
161 * if you want a more complete analysis of the validity of the path.
164 * @param requestedPath the path to test.
165 * @return true if the requestedPath is likely that of a default layout request.
167 public boolean isDefault( String requestedPath )
169 if ( StringUtils.isBlank( requestedPath ) )
174 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
175 if ( pathParts.length > 3 )
179 else if ( pathParts.length == 3 )
181 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
182 if ( isMetadata( requestedPath ) )
188 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
189 int idx = requestedPath.lastIndexOf( '.' );
192 String base = requestedPath.substring( 0, idx );
193 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
210 * Tests the path to see if it conforms to the expectations of a legacy layout request.
213 * NOTE: This does a cursory check on the count of path elements only. A result of
214 * true from this method is not a guarantee that the path sections are valid and
215 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
216 * if you want a more complete analysis of the validity of the path.
219 * @param requestedPath the path to test.
220 * @return true if the requestedPath is likely that of a legacy layout request.
222 public boolean isLegacy( String requestedPath )
224 if ( StringUtils.isBlank( requestedPath ) )
229 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
230 return pathParts.length == 3;
234 * Adjust the requestedPath to conform to the native layout of the provided {@link org.apache.archiva.repository.ManagedRepositoryContent}.
236 * @param requestedPath the incoming requested path.
237 * @param repository the repository to adjust to.
238 * @return the adjusted (to native) path.
239 * @throws LayoutException if the path cannot be parsed.
241 public String toNativePath( String requestedPath, ManagedRepositoryContent repository )
242 throws LayoutException
244 if ( StringUtils.isBlank( requestedPath ) )
246 throw new LayoutException( "Request Path is blank." );
249 String referencedResource = requestedPath;
250 // No checksum by default.
251 String supportfile = "";
253 // Figure out support file, and actual referencedResource.
254 if ( isSupportFile( requestedPath ) )
256 int idx = requestedPath.lastIndexOf( '.' );
257 referencedResource = requestedPath.substring( 0, idx );
258 supportfile = requestedPath.substring( idx );
261 if ( isMetadata( referencedResource ) )
263 /* Nothing to translate.
264 * Default layout is the only layout that can contain maven-metadata.xml files, and
265 * if the managedRepository is layout legacy, this request would never occur.
267 return requestedPath;
270 // Treat as an artifact reference.
271 ArtifactReference ref = toArtifactReference( referencedResource );
272 String adjustedPath = repository.toPath( ref );
273 return adjustedPath + supportfile;