1 package org.apache.archiva.repository.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
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.commons.lang.StringUtils;
23 import org.apache.archiva.model.ArtifactReference;
24 import org.apache.archiva.repository.ManagedRepositoryContent;
25 import org.apache.archiva.repository.layout.LayoutException;
26 import org.apache.archiva.repository.metadata.MetadataTools;
29 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
35 public class RepositoryRequest
37 private PathParser defaultPathParser = new DefaultPathParser();
39 private PathParser legacyPathParser;
41 public RepositoryRequest (LegacyPathParser legacyPathParser)
43 this.legacyPathParser = legacyPathParser;
47 * Takes an incoming requested path (in "/" format) and gleans the layout
48 * and ArtifactReference appropriate for that content.
50 * @param requestedPath the relative path to the content.
51 * @return the ArtifactReference for the requestedPath.
52 * @throws LayoutException if the request path is not layout valid.
54 public ArtifactReference toArtifactReference( String requestedPath )
55 throws LayoutException
57 if ( StringUtils.isBlank( requestedPath ) )
59 throw new LayoutException( "Blank request path is not a valid." );
62 String path = requestedPath;
63 while ( path.startsWith( "/" ) )
65 path = path.substring( 1 );
67 // Only slash? that's bad, mmm-kay?
68 if ( "/".equals( path ) )
70 throw new LayoutException( "Invalid request path: Slash only." );
74 if ( isDefault( path ) )
76 return defaultPathParser.toArtifactReference( path );
78 else if ( isLegacy( path ) )
80 return legacyPathParser.toArtifactReference( path );
84 throw new LayoutException( "Not a valid request path layout, too short." );
90 * Tests the path to see if it conforms to the expectations of a metadata request.
93 * NOTE: This does a cursory check on the path's last element. A result of true
94 * from this method is not a guarantee that the metadata is in a valid format, or
95 * that it even contains data.
98 * @param requestedPath the path to test.
99 * @return true if the requestedPath is likely a metadata request.
101 public boolean isMetadata( String requestedPath )
103 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
108 * Tests the path to see if it conforms to the expectations of a support file request.
111 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
114 * NOTE: This does a cursory check on the path's extension only. A result of true
115 * from this method is not a guarantee that the support resource is in a valid format, or
116 * that it even contains data.
119 * @param requestedPath the path to test.
120 * @return true if the requestedPath is likely that of a support file request.
122 public boolean isSupportFile( String requestedPath )
124 int idx = requestedPath.lastIndexOf( '.' );
130 String ext = requestedPath.substring( idx );
131 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
134 public boolean isMetadataSupportFile( String requestedPath )
136 if ( isSupportFile( requestedPath ) )
138 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
139 if ( isMetadata( basefilePath ) )
150 * Tests the path to see if it conforms to the expectations of a default layout request.
153 * NOTE: This does a cursory check on the count of path elements only. A result of
154 * true from this method is not a guarantee that the path sections are valid and
155 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
156 * if you want a more complete analysis of the validity of the path.
159 * @param requestedPath the path to test.
160 * @return true if the requestedPath is likely that of a default layout request.
162 public boolean isDefault( String requestedPath )
164 if ( StringUtils.isBlank( requestedPath ) )
169 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
170 if ( pathParts.length > 3 )
174 else if ( pathParts.length == 3 )
176 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
177 if ( isMetadata( requestedPath ) )
183 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
184 int idx = requestedPath.lastIndexOf( '.' );
187 String base = requestedPath.substring( 0, idx );
188 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
205 * Tests the path to see if it conforms to the expectations of a legacy layout request.
208 * NOTE: This does a cursory check on the count of path elements only. A result of
209 * true from this method is not a guarantee that the path sections are valid and
210 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
211 * if you want a more complete analysis of the validity of the path.
214 * @param requestedPath the path to test.
215 * @return true if the requestedPath is likely that of a legacy layout request.
217 public boolean isLegacy( String requestedPath )
219 if ( StringUtils.isBlank( requestedPath ) )
224 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
225 return pathParts.length == 3;
229 * Adjust the requestedPath to conform to the native layout of the provided {@link ManagedRepositoryContent}.
231 * @param requestedPath the incoming requested path.
232 * @param repository the repository to adjust to.
233 * @return the adjusted (to native) path.
234 * @throws LayoutException if the path cannot be parsed.
236 public String toNativePath( String requestedPath, ManagedRepositoryContent repository )
237 throws LayoutException
239 if ( StringUtils.isBlank( requestedPath ) )
241 throw new LayoutException( "Request Path is blank." );
244 String referencedResource = requestedPath;
245 // No checksum by default.
246 String supportfile = "";
248 // Figure out support file, and actual referencedResource.
249 if ( isSupportFile( requestedPath ) )
251 int idx = requestedPath.lastIndexOf( '.' );
252 referencedResource = requestedPath.substring( 0, idx );
253 supportfile = requestedPath.substring( idx );
256 if ( isMetadata( referencedResource ) )
258 if ( repository instanceof ManagedLegacyRepositoryContent )
260 throw new LayoutException( "Cannot translate metadata request to legacy layout." );
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;