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.ManagedRepositoryContent;
24 import org.apache.archiva.repository.content.PathParser;
25 import org.apache.archiva.repository.content.legacy.LegacyPathParser;
26 import org.apache.archiva.repository.content.legacy.ManagedLegacyRepositoryContent;
27 import org.apache.archiva.repository.layout.LayoutException;
28 import org.apache.archiva.repository.metadata.MetadataTools;
29 import org.apache.commons.lang.StringUtils;
32 * 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 org.apache.archiva.repository.layout.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 );
107 * @param requestedPath
108 * @return true if the requestedPath is likely an archetype catalog request.
110 public boolean isArchetypeCatalog( String requestedPath )
112 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
117 * Tests the path to see if it conforms to the expectations of a support file request.
120 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
123 * NOTE: This does a cursory check on the path's extension only. A result of true
124 * from this method is not a guarantee that the support resource is in a valid format, or
125 * that it even contains data.
128 * @param requestedPath the path to test.
129 * @return true if the requestedPath is likely that of a support file request.
131 public boolean isSupportFile( String requestedPath )
133 int idx = requestedPath.lastIndexOf( '.' );
139 String ext = requestedPath.substring( idx );
140 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
143 public boolean isMetadataSupportFile( String requestedPath )
145 if ( isSupportFile( requestedPath ) )
147 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
148 if ( isMetadata( basefilePath ) )
159 * Tests the path to see if it conforms to the expectations of a default layout request.
162 * NOTE: This does a cursory check on the count of path elements only. A result of
163 * true from this method is not a guarantee that the path sections are valid and
164 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
165 * if you want a more complete analysis of the validity of the path.
168 * @param requestedPath the path to test.
169 * @return true if the requestedPath is likely that of a default layout request.
171 public boolean isDefault( String requestedPath )
173 if ( StringUtils.isBlank( requestedPath ) )
178 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
179 if ( pathParts.length > 3 )
183 else if ( pathParts.length == 3 )
185 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
186 if ( isMetadata( requestedPath ) )
192 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
193 int idx = requestedPath.lastIndexOf( '.' );
196 String base = requestedPath.substring( 0, idx );
197 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
214 * Tests the path to see if it conforms to the expectations of a legacy layout request.
217 * NOTE: This does a cursory check on the count of path elements only. A result of
218 * true from this method is not a guarantee that the path sections are valid and
219 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
220 * if you want a more complete analysis of the validity of the path.
223 * @param requestedPath the path to test.
224 * @return true if the requestedPath is likely that of a legacy layout request.
226 public boolean isLegacy( String requestedPath )
228 if ( StringUtils.isBlank( requestedPath ) )
233 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
234 return pathParts.length == 3;
238 * Adjust the requestedPath to conform to the native layout of the provided {@link org.apache.archiva.repository.ManagedRepositoryContent}.
240 * @param requestedPath the incoming requested path.
241 * @param repository the repository to adjust to.
242 * @return the adjusted (to native) path.
243 * @throws org.apache.archiva.repository.layout.LayoutException if the path cannot be parsed.
245 public String toNativePath( String requestedPath, ManagedRepositoryContent repository )
246 throws LayoutException
248 if ( StringUtils.isBlank( requestedPath ) )
250 throw new LayoutException( "Request Path is blank." );
253 String referencedResource = requestedPath;
254 // No checksum by default.
255 String supportfile = "";
257 // Figure out support file, and actual referencedResource.
258 if ( isSupportFile( requestedPath ) )
260 int idx = requestedPath.lastIndexOf( '.' );
261 referencedResource = requestedPath.substring( 0, idx );
262 supportfile = requestedPath.substring( idx );
265 if ( isMetadata( referencedResource ) )
267 if ( repository instanceof ManagedLegacyRepositoryContent )
269 throw new LayoutException( "Cannot translate metadata request to legacy layout." );
272 /* Nothing to translate.
273 * Default layout is the only layout that can contain maven-metadata.xml files, and
274 * if the managedRepository is layout legacy, this request would never occur.
276 return requestedPath;
279 // Treat as an artifact reference.
280 ArtifactReference ref = toArtifactReference( referencedResource );
281 String adjustedPath = repository.toPath( ref );
282 return adjustedPath + supportfile;