123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- package org.apache.archiva.repository.content.maven2;
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
- import org.apache.archiva.model.ArtifactReference;
- import org.apache.archiva.repository.*;
- import org.apache.archiva.repository.content.PathParser;
- import org.apache.archiva.repository.features.RepositoryFeature;
- import org.apache.archiva.repository.metadata.base.MetadataTools;
- import org.apache.commons.lang3.StringUtils;
-
- /**
- * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
- * ArtifactReference.
- */
- public class MavenRepositoryRequestInfo implements RepositoryRequestInfo
- {
- private PathParser defaultPathParser = new DefaultPathParser();
-
- ManagedRepository repository;
-
- public MavenRepositoryRequestInfo(ManagedRepository repository)
- {
- this.repository = repository;
- }
-
- /**
- * Takes an incoming requested path (in "/" format) and gleans the layout
- * and ArtifactReference appropriate for that content.
- *
- * @param requestedPath the relative path to the content.
- * @return the ArtifactReference for the requestedPath.
- * @throws LayoutException if the request path is not layout valid.
- */
- public ArtifactReference toArtifactReference( String requestedPath )
- throws LayoutException
- {
- if ( StringUtils.isBlank( requestedPath ) )
- {
- throw new LayoutException( "Blank request path is not a valid." );
- }
-
- String path = requestedPath;
- while ( path.startsWith( "/" ) )
- {
- path = path.substring( 1 );
-
- // Only slash? that's bad, mmm-kay?
- if ( "/".equals( path ) )
- {
- throw new LayoutException( "Invalid request path: Slash only." );
- }
- }
-
- if ( isDefault( path ) )
- {
- return defaultPathParser.toArtifactReference( path );
- }
- else if ( isLegacy( path ) )
- {
- throw new LayoutException( "Legacy Maven1 repository not supported anymore." );
- }
- else
- {
- throw new LayoutException( "Not a valid request path layout, too short." );
- }
- }
-
- /**
- * <p>
- * Tests the path to see if it conforms to the expectations of a metadata request.
- * </p>
- * <p>
- * NOTE: This does a cursory check on the path's last element. A result of true
- * from this method is not a guarantee that the metadata is in a valid format, or
- * that it even contains data.
- * </p>
- *
- * @param requestedPath the path to test.
- * @return true if the requestedPath is likely a metadata request.
- */
- public boolean isMetadata( String requestedPath )
- {
- return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
- }
-
- /**
- * @param requestedPath
- * @return true if the requestedPath is likely an archetype catalog request.
- */
- public boolean isArchetypeCatalog( String requestedPath )
- {
- return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
- }
-
- /**
- * <p>
- * Tests the path to see if it conforms to the expectations of a support file request.
- * </p>
- * <p>
- * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
- * </p>
- * <p>
- * NOTE: This does a cursory check on the path's extension only. A result of true
- * from this method is not a guarantee that the support resource is in a valid format, or
- * that it even contains data.
- * </p>
- *
- * @param requestedPath the path to test.
- * @return true if the requestedPath is likely that of a support file request.
- */
- public boolean isSupportFile( String requestedPath )
- {
- int idx = requestedPath.lastIndexOf( '.' );
- if ( idx <= 0 )
- {
- return false;
- }
-
- String ext = requestedPath.substring( idx );
- return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
- }
-
- public boolean isMetadataSupportFile( String requestedPath )
- {
- if ( isSupportFile( requestedPath ) )
- {
- String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
- if ( isMetadata( basefilePath ) )
- {
- return true;
- }
- }
-
- return false;
- }
-
- @Override
- public String getLayout(String requestPath) {
- if (isDefault(requestPath)) {
- return "default";
- } else if (isLegacy(requestPath)) {
- return "legacy";
- } else {
- return "unknown";
- }
- }
-
- /**
- * <p>
- * Tests the path to see if it conforms to the expectations of a default layout request.
- * </p>
- * <p>
- * NOTE: This does a cursory check on the count of path elements only. A result of
- * true from this method is not a guarantee that the path sections are valid and
- * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
- * if you want a more complete analysis of the validity of the path.
- * </p>
- *
- * @param requestedPath the path to test.
- * @return true if the requestedPath is likely that of a default layout request.
- */
- private boolean isDefault( String requestedPath )
- {
- if ( StringUtils.isBlank( requestedPath ) )
- {
- return false;
- }
-
- String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
- if ( pathParts.length > 3 )
- {
- return true;
- }
- else if ( pathParts.length == 3 )
- {
- // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
- if ( isMetadata( requestedPath ) )
- {
- return true;
- }
- else
- {
- // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
- int idx = requestedPath.lastIndexOf( '.' );
- if ( idx > 0 )
- {
- String base = requestedPath.substring( 0, idx );
- if ( isMetadata( base ) && isSupportFile( requestedPath ) )
- {
- return true;
- }
- }
-
- return false;
- }
- }
- else
- {
- return false;
- }
- }
-
- /**
- * <p>
- * Tests the path to see if it conforms to the expectations of a legacy layout request.
- * </p>
- * <p>
- * NOTE: This does a cursory check on the count of path elements only. A result of
- * true from this method is not a guarantee that the path sections are valid and
- * can be resolved to an artifact reference. use {@link #toArtifactReference(String)}
- * if you want a more complete analysis of the validity of the path.
- * </p>
- *
- * @param requestedPath the path to test.
- * @return true if the requestedPath is likely that of a legacy layout request.
- */
- private boolean isLegacy( String requestedPath )
- {
- if ( StringUtils.isBlank( requestedPath ) )
- {
- return false;
- }
-
- String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
- return pathParts.length == 3;
- }
-
- /**
- * Adjust the requestedPath to conform to the native layout of the provided {@link org.apache.archiva.repository.ManagedRepositoryContent}.
- *
- * @param requestedPath the incoming requested path.
- * @return the adjusted (to native) path.
- * @throws LayoutException if the path cannot be parsed.
- */
- public String toNativePath( String requestedPath)
- throws LayoutException
- {
- if ( StringUtils.isBlank( requestedPath ) )
- {
- throw new LayoutException( "Request Path is blank." );
- }
-
- String referencedResource = requestedPath;
- // No checksum by default.
- String supportfile = "";
-
- // Figure out support file, and actual referencedResource.
- if ( isSupportFile( requestedPath ) )
- {
- int idx = requestedPath.lastIndexOf( '.' );
- referencedResource = requestedPath.substring( 0, idx );
- supportfile = requestedPath.substring( idx );
- }
-
- if ( isMetadata( referencedResource ) )
- {
- /* Nothing to translate.
- * Default layout is the only layout that can contain maven-metadata.xml files, and
- * if the managedRepository is layout legacy, this request would never occur.
- */
- return requestedPath;
- }
-
- // Treat as an artifact reference.
- ArtifactReference ref = toArtifactReference( referencedResource );
- String adjustedPath = repository.getContent().toPath( ref );
- return adjustedPath + supportfile;
- }
-
- @Override
- public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature(Class<T> clazz) throws UnsupportedFeatureException {
- return null;
- }
-
- @Override
- public <T extends RepositoryFeature<T>> boolean supportsFeature(Class<T> clazz) {
- return false;
- }
- }
|