1 package org.apache.maven.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.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.FileTypes;
25 import org.apache.maven.archiva.model.ArtifactReference;
26 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
27 import org.apache.maven.archiva.repository.layout.LayoutException;
28 import org.apache.maven.archiva.repository.metadata.MetadataTools;
31 * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
34 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
38 * role="org.apache.maven.archiva.repository.content.RepositoryRequest"
40 public class RepositoryRequest
45 private FileTypes filetypes;
50 private ArchivaConfiguration archivaConfiguration;
53 * @plexus.requirement role-hint="default"
55 private PathParser defaultPathParser;
58 * @plexus.requirement role-hint="legacy"
60 private PathParser legacyPathParser;
63 * Takes an incoming requested path (in "/" format) and gleans the layout
64 * and ArtifactReference appropriate for that content.
66 * @param requestedPath the relative path to the content.
67 * @return the ArtifactReference for the requestedPath.
68 * @throws LayoutException if the request path is not layout valid.
70 public ArtifactReference toArtifactReference( String requestedPath )
71 throws LayoutException
73 if ( StringUtils.isBlank( requestedPath ) )
75 throw new LayoutException( "Blank request path is not a valid." );
78 String path = requestedPath;
79 while ( path.startsWith( "/" ) )
81 path = path.substring( 1 );
83 // Only slash? that's bad, mmm-kay?
84 if ( "/".equals( path ) )
86 throw new LayoutException( "Invalid request path: Slash only." );
90 if ( isDefault( path ) )
92 return defaultPathParser.toArtifactReference( path );
94 else if ( isLegacy( path ) )
96 return legacyPathParser.toArtifactReference( path );
100 throw new LayoutException( "Not a valid request path layout, too short." );
106 * Tests the path to see if it conforms to the expectations of a metadata request.
109 * NOTE: This does a cursory check on the path's last element. A result of true
110 * from this method is not a guarantee that the metadata is in a valid format, or
111 * that it even contains data.
114 * @param requestedPath the path to test.
115 * @return true if the requestedPath is likely a metadata request.
117 public boolean isMetadata( String requestedPath )
119 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
124 * Tests the path to see if it conforms to the expectations of a support file request.
127 * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
130 * NOTE: This does a cursory check on the path's extension only. A result of true
131 * from this method is not a guarantee that the support resource is in a valid format, or
132 * that it even contains data.
135 * @param requestedPath the path to test.
136 * @return true if the requestedPath is likely that of a support file request.
138 public boolean isSupportFile( String requestedPath )
140 int idx = requestedPath.lastIndexOf( '.' );
146 String ext = requestedPath.substring( idx );
147 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
152 * Tests the path to see if it conforms to the expectations of a default layout request.
155 * NOTE: This does a cursory check on the count of path elements only. A result of
156 * true from this method is not a guarantee that the path sections are valid and
157 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
158 * if you want a more complete analysis of the validity of the path.
161 * @param requestedPath the path to test.
162 * @return true if the requestedPath is likely that of a default layout request.
164 public boolean isDefault( String requestedPath )
166 if ( StringUtils.isBlank( requestedPath ) )
171 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
172 return pathParts.length > 3;
177 * Tests the path to see if it conforms to the expectations of a legacy layout request.
180 * NOTE: This does a cursory check on the count of path elements only. A result of
181 * true from this method is not a guarantee that the path sections are valid and
182 * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
183 * if you want a more complete analysis of the validity of the path.
186 * @param requestedPath the path to test.
187 * @return true if the requestedPath is likely that of a legacy layout request.
189 public boolean isLegacy( String requestedPath )
191 if ( StringUtils.isBlank( requestedPath ) )
196 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
197 return pathParts.length == 3;
201 * Adjust the requestedPath to conform to the native layout of the provided {@link ManagedRepositoryContent}.
203 * @param requestedPath the incoming requested path.
204 * @param repository the repository to adjust to.
205 * @return the adjusted (to native) path.
206 * @throws LayoutException if the path cannot be parsed.
208 public String toNativePath( String requestedPath, ManagedRepositoryContent repository ) throws LayoutException
210 if ( StringUtils.isBlank( requestedPath ) )
212 throw new LayoutException( "Request Path is blank." );
215 String referencedResource = requestedPath;
216 // No checksum by default.
217 String supportfile = "";
219 // Figure out support file, and actual referencedResource.
220 if( isSupportFile( requestedPath ) )
222 int idx = requestedPath.lastIndexOf( '.' );
223 referencedResource = requestedPath.substring( 0, idx );
224 supportfile = requestedPath.substring( idx );
227 if ( isMetadata( referencedResource ) )
229 if ( repository instanceof ManagedLegacyRepositoryContent )
231 throw new LayoutException( "Cannot translate metadata request to legacy layout." );
234 /* Nothing to translate.
235 * Default layout is the only layout that can contain maven-metadata.xml files, and
236 * if the managedRepository is layout legacy, this request would never occur.
238 return requestedPath;
241 // Treat as an artifact reference.
242 ArtifactReference ref = toArtifactReference( referencedResource );
243 String adjustedPath = repository.toPath( ref );
244 return adjustedPath + supportfile;