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 * @param requestedPath
109 * @return true if the requestedPath is likely an archetype catalog request.
111 public boolean isArchetypeCatalog( String requestedPath )
113 //TODO: Make it static final String
114 return requestedPath.endsWith( "/archetype-catalog.xml");
119 * Tests the path to see if it conforms to the expectations of a support file request.
122 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
125 * NOTE: This does a cursory check on the path's extension only. A result of true
126 * from this method is not a guarantee that the support resource is in a valid format, or
127 * that it even contains data.
130 * @param requestedPath the path to test.
131 * @return true if the requestedPath is likely that of a support file request.
133 public boolean isSupportFile( String requestedPath )
135 int idx = requestedPath.lastIndexOf( '.' );
141 String ext = requestedPath.substring( idx );
142 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
145 public boolean isMetadataSupportFile( String requestedPath )
147 if ( isSupportFile( requestedPath ) )
149 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
150 if ( isMetadata( basefilePath ) )
161 * Tests the path to see if it conforms to the expectations of a default layout request.
164 * NOTE: This does a cursory check on the count of path elements only. A result of
165 * true from this method is not a guarantee that the path sections are valid and
166 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
167 * if you want a more complete analysis of the validity of the path.
170 * @param requestedPath the path to test.
171 * @return true if the requestedPath is likely that of a default layout request.
173 public boolean isDefault( String requestedPath )
175 if ( StringUtils.isBlank( requestedPath ) )
180 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
181 if ( pathParts.length > 3 )
185 else if ( pathParts.length == 3 )
187 // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
188 if ( isMetadata( requestedPath ) )
194 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
195 int idx = requestedPath.lastIndexOf( '.' );
198 String base = requestedPath.substring( 0, idx );
199 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
216 * Tests the path to see if it conforms to the expectations of a legacy layout request.
219 * NOTE: This does a cursory check on the count of path elements only. A result of
220 * true from this method is not a guarantee that the path sections are valid and
221 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
222 * if you want a more complete analysis of the validity of the path.
225 * @param requestedPath the path to test.
226 * @return true if the requestedPath is likely that of a legacy layout request.
228 public boolean isLegacy( String requestedPath )
230 if ( StringUtils.isBlank( requestedPath ) )
235 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
236 return pathParts.length == 3;
240 * Adjust the requestedPath to conform to the native layout of the provided {@link ManagedRepositoryContent}.
242 * @param requestedPath the incoming requested path.
243 * @param repository the repository to adjust to.
244 * @return the adjusted (to native) path.
245 * @throws LayoutException if the path cannot be parsed.
247 public String toNativePath( String requestedPath, ManagedRepositoryContent repository )
248 throws LayoutException
250 if ( StringUtils.isBlank( requestedPath ) )
252 throw new LayoutException( "Request Path is blank." );
255 String referencedResource = requestedPath;
256 // No checksum by default.
257 String supportfile = "";
259 // Figure out support file, and actual referencedResource.
260 if ( isSupportFile( requestedPath ) )
262 int idx = requestedPath.lastIndexOf( '.' );
263 referencedResource = requestedPath.substring( 0, idx );
264 supportfile = requestedPath.substring( idx );
267 if ( isMetadata( referencedResource ) )
269 if ( repository instanceof ManagedLegacyRepositoryContent )
271 throw new LayoutException( "Cannot translate metadata request to legacy layout." );
274 /* Nothing to translate.
275 * Default layout is the only layout that can contain maven-metadata.xml files, and
276 * if the managedRepository is layout legacy, this request would never occur.
278 return requestedPath;
281 // Treat as an artifact reference.
282 ArtifactReference ref = toArtifactReference( referencedResource );
283 String adjustedPath = repository.toPath( ref );
284 return adjustedPath + supportfile;