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.ManagedRepositoryContent;
24import org.apache.archiva.repository.content.PathParser;
25import org.apache.archiva.repository.layout.LayoutException;
26import org.apache.archiva.repository.metadata.MetadataTools;
27import org.apache.commons.lang.StringUtils;
2829/**30 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate31 * ArtifactReference.32 */33publicclassRepositoryRequest34 {
35private PathParser defaultPathParser = newDefaultPathParser();
3637publicRepositoryRequest()
38 {
39// no op40 }
4142/**43 * Takes an incoming requested path (in "/" format) and gleans the layout44 * and ArtifactReference appropriate for that content.45 *46 * @param requestedPath the relative path to the content.47 * @return the ArtifactReference for the requestedPath.48 * @throws org.apache.archiva.repository.layout.LayoutException if the request path is not layout valid.49 */50public ArtifactReference toArtifactReference( String requestedPath )
51throws LayoutException
52 {
53if ( StringUtils.isBlank( requestedPath ) )
54 {
55thrownew LayoutException( "Blank request path is not a valid." );
56 }
5758 String path = requestedPath;
59while ( path.startsWith( "/" ) )
60 {
61 path = path.substring( 1 );
6263// Only slash? that's bad, mmm-kay?64if ( "/".equals( path ) )
65 {
66thrownew LayoutException( "Invalid request path: Slash only." );
67 }
68 }
6970if ( isDefault( path ) )
71 {
72return defaultPathParser.toArtifactReference( path );
73 }
74elseif ( isLegacy( path ) )
75 {
76thrownew LayoutException( "Legacy Maven1 repository not supported anymore." );
77 }
78else79 {
80thrownew LayoutException( "Not a valid request path layout, too short." );
81 }
82 }
8384/**85 * <p>86 * Tests the path to see if it conforms to the expectations of a metadata request.87 * </p>88 * <p>89 * NOTE: This does a cursory check on the path's last element. A result of true90 * from this method is not a guarantee that the metadata is in a valid format, or91 * that it even contains data.92 * </p>93 *94 * @param requestedPath the path to test.95 * @return true if the requestedPath is likely a metadata request.96 */97publicboolean isMetadata( String requestedPath )
98 {
99return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
100 }
101102/**103 * @param requestedPath104 * @return true if the requestedPath is likely an archetype catalog request.105 */106publicboolean isArchetypeCatalog( String requestedPath )
107 {
108return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
109 }
110111/**112 * <p>113 * Tests the path to see if it conforms to the expectations of a support file request.114 * </p>115 * <p>116 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.117 * </p>118 * <p>119 * NOTE: This does a cursory check on the path's extension only. A result of true120 * from this method is not a guarantee that the support resource is in a valid format, or121 * that it even contains data.122 * </p>123 *124 * @param requestedPath the path to test.125 * @return true if the requestedPath is likely that of a support file request.126 */127publicboolean isSupportFile( String requestedPath )
128 {
129int idx = requestedPath.lastIndexOf( '.' );
130if ( idx <= 0 )
131 {
132return false;
133 }
134135 String ext = requestedPath.substring( idx );
136return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
137 }
138139publicboolean isMetadataSupportFile( String requestedPath )
140 {
141if ( isSupportFile( requestedPath ) )
142 {
143 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
144if ( isMetadata( basefilePath ) )
145 {
146returntrue;
147 }
148 }
149150return false;
151 }
152153/**154 * <p>155 * Tests the path to see if it conforms to the expectations of a default layout request.156 * </p>157 * <p>158 * NOTE: This does a cursory check on the count of path elements only. A result of159 * true from this method is not a guarantee that the path sections are valid and160 * 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.162 * </p>163 *164 * @param requestedPath the path to test.165 * @return true if the requestedPath is likely that of a default layout request.166 */167publicboolean isDefault( String requestedPath )
168 {
169if ( StringUtils.isBlank( requestedPath ) )
170 {
171return false;
172 }
173174 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
175if ( pathParts.length > 3 )
176 {
177returntrue;
178 }
179elseif ( pathParts.length == 3 )
180 {
181// check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)182if ( isMetadata( requestedPath ) )
183 {
184returntrue;
185 }
186else187 {
188// check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)189int idx = requestedPath.lastIndexOf( '.' );
190if ( idx > 0 )
191 {
192 String base = requestedPath.substring( 0, idx );
193if ( isMetadata( base ) && isSupportFile( requestedPath ) )
194 {
195returntrue;
196 }
197 }
198199return false;
200 }
201 }
202else203 {
204return false;
205 }
206 }
207208/**209 * <p>210 * Tests the path to see if it conforms to the expectations of a legacy layout request.211 * </p>212 * <p>213 * NOTE: This does a cursory check on the count of path elements only. A result of214 * true from this method is not a guarantee that the path sections are valid and215 * 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.217 * </p>218 *219 * @param requestedPath the path to test.220 * @return true if the requestedPath is likely that of a legacy layout request.221 */222publicboolean isLegacy( String requestedPath )
223 {
224if ( StringUtils.isBlank( requestedPath ) )
225 {
226return false;
227 }
228229 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
230return pathParts.length == 3;
231 }
232233/**234 * Adjust the requestedPath to conform to the native layout of the provided {@link org.apache.archiva.repository.ManagedRepositoryContent}.235 *236 * @param requestedPath the incoming requested path.237 * @param repository the repository to adjust to.238 * @return the adjusted (to native) path.239 * @throws org.apache.archiva.repository.layout.LayoutException if the path cannot be parsed.240 */241public String toNativePath( String requestedPath, ManagedRepositoryContent repository )
242throws LayoutException
243 {
244if ( StringUtils.isBlank( requestedPath ) )
245 {
246thrownew LayoutException( "Request Path is blank." );
247 }
248249 String referencedResource = requestedPath;
250// No checksum by default.251 String supportfile = "";
252253// Figure out support file, and actual referencedResource.254if ( isSupportFile( requestedPath ) )
255 {
256int idx = requestedPath.lastIndexOf( '.' );
257 referencedResource = requestedPath.substring( 0, idx );
258 supportfile = requestedPath.substring( idx );
259 }
260261if ( isMetadata( referencedResource ) )
262 {
263/* Nothing to translate.264 * Default layout is the only layout that can contain maven-metadata.xml files, and265 * if the managedRepository is layout legacy, this request would never occur.266 */267return requestedPath;
268 }
269270// Treat as an artifact reference.271 ArtifactReference ref = toArtifactReference( referencedResource );
272 String adjustedPath = repository.toPath( ref );
273return adjustedPath + supportfile;
274 }
275 }