import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
-import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
throws ConsumerException, MalformedURLException
{
ManagedRepository repo = new ManagedRepository();
- repo.setId( new File( path ).getName() );
+ repo.setId( Paths.get( path ).getFileName().toString());
repo.setName( "Archiva CLI Provided Repo" );
repo.setLocation( path );
import java.io.File;
import java.net.URI;
+import java.nio.file.Paths;
/**
* BaseFile - convenient File object that tracks the Base Directory and can provide relative path values
public BaseFile( File repoDir, File pathFile )
{
- this( repoDir, PathUtil.getRelative( repoDir.getAbsolutePath(), pathFile ) );
+ this( repoDir, PathUtil.getRelative(repoDir.getAbsolutePath(), pathFile.toPath() ) );
}
public BaseFile( File parent, String child )
public String getRelativePath()
{
- return PathUtil.getRelative( this.baseDir.getAbsolutePath(), this );
+ return PathUtil.getRelative( this.baseDir.getAbsolutePath(), this.toPath() );
}
public void setBaseDir( File baseDir )
import org.apache.commons.lang.StringUtils;
-import java.io.File;
import java.net.MalformedURLException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
/**
* PathUtil - simple utility methods for path manipulation.
return path;
}
- return toUrl( new File( path ) );
+ return toUrl( Paths.get( path ) );
}
- public static String toUrl( File file )
+ public static String toUrl( Path file )
{
try
{
- return file.toURI().toURL().toExternalForm();
+ return file.toUri().toURL().toExternalForm();
}
catch ( MalformedURLException e )
{
- String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' );
+ String pathCorrected = StringUtils.replaceChars( file.toAbsolutePath().toString(), '\\', '/' );
if ( pathCorrected.startsWith( "file:/" ) )
{
return pathCorrected;
*
* @param basedir the basedir.
* @param file the file to get the relative path for.
- * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character)
+ * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character)
*/
- public static String getRelative( String basedir, File file )
+ public static String getRelative( Path basedir, Path file )
{
- return getRelative( basedir, file.getAbsolutePath() );
+ if (basedir.isAbsolute() && !file.isAbsolute()) {
+ return basedir.normalize().relativize(file.toAbsolutePath()).toString();
+ } else if (!basedir.isAbsolute() && file.isAbsolute()) {
+ return basedir.toAbsolutePath().relativize(file.normalize()).toString();
+ } else {
+ return basedir.normalize().relativize(file.normalize()).toString();
+ }
+ }
+
+ public static String getRelative(String basedir, Path file) {
+ return getRelative(Paths.get(basedir), file);
}
/**
*
* @param basedir the basedir.
* @param child the child path (can be a full path)
- * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character)
+ * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character)
*/
public static String getRelative( String basedir, String child )
{
- if ( basedir.endsWith( "/" ) || basedir.endsWith( "\\" ) )
- {
- basedir = basedir.substring( 0, basedir.length() - 1 );
- }
-
- if ( child.startsWith( basedir ) )
- {
- // simple solution.
- return child.substring( basedir.length() + 1 );
- }
-
- String absoluteBasedir = new File( basedir ).getAbsolutePath();
- if ( child.startsWith( absoluteBasedir ) )
- {
- // resolved basedir solution.
- return child.substring( absoluteBasedir.length() + 1 );
- }
- // File is not within basedir.
- throw new IllegalStateException(
- "Unable to obtain relative path of file " + child + ", it is not within basedir " + basedir + "." );
+ return getRelative(basedir, Paths.get(child));
}
}
* under the License.
*/
+import junit.framework.TestCase;
import org.apache.commons.lang.StringUtils;
-import java.io.File;
-
-import junit.framework.TestCase;
+import java.nio.file.Path;
+import java.nio.file.Paths;
/**
* PathUtilTest
public void testToUrlRelativePath()
{
- File workingDir = new File( "." );
+ Path workingDir = Paths.get( "" );
- String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
+ String workingDirname = StringUtils.replaceChars( workingDir.toAbsolutePath().toString(), '\\', '/' );
// Some JVM's retain the "." at the end of the path. Drop it.
if ( workingDirname.endsWith( "/." ) )
public void testToUrlUsingFileUrl()
{
- File workingDir = new File( "." );
+ Path workingDir = Paths.get( "." );
- String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
+ String workingDirname = StringUtils.replaceChars( workingDir.toAbsolutePath().toString(), '\\', '/' );
// Some JVM's retain the "." at the end of the path. Drop it.
if ( workingDirname.endsWith( "/." ) )
import org.apache.commons.lang.StringUtils;
-import java.io.File;
import java.io.IOException;
import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
/**
* <code>ResourceUtils</code>
* @param resourcePath the path to the resource relative to the root of the classpath
* @return File a file object pointing to the resource on the classpath or null if the resource cannot be found
*/
- public static File getResource( String resourcePath )
+ public static Path getResource(String resourcePath )
throws IOException
{
return getResource( resourcePath, null );
* @param classloader the classloader who's classpath should be searched for the resource
* @return File a file object pointing to the resource on the classpath or null if the resource cannot be found
*/
- public static File getResource( String resourcePath, ClassLoader classloader )
+ public static Path getResource( String resourcePath, ClassLoader classloader )
throws IOException
{
- File testResource = null;
+ Path testResource = null;
if ( StringUtils.isNotBlank( resourcePath ) )
{
{
throw new IOException( "Could not find test resource at path '" + resourcePath + "'" );
}
- testResource = new File( resourceUrl.getFile() );
+ testResource = Paths.get( resourceUrl.getFile() );
}
return testResource;
{
try
{
- String defaultRepositoryUrl = PathUtil.toUrl( repositoryDirectory.toFile() );
+ String defaultRepositoryUrl = PathUtil.toUrl( repositoryDirectory );
ManagedRepository legacyRepository = new ManagedRepository();
legacyRepository.setId( "legacy" );
import org.apache.archiva.configuration.FileTypes;
import org.apache.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.archiva.maven2.metadata.MavenMetadataReader;
-import org.apache.archiva.model.ArchivaRepositoryMetadata;
-import org.apache.archiva.model.ArtifactReference;
-import org.apache.archiva.model.Plugin;
-import org.apache.archiva.model.ProjectReference;
-import org.apache.archiva.model.SnapshotVersion;
-import org.apache.archiva.model.VersionedReference;
+import org.apache.archiva.model.*;
import org.apache.archiva.redback.components.registry.Registry;
import org.apache.archiva.redback.components.registry.RegistryListener;
import org.apache.archiva.repository.ContentNotFoundException;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
+import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Stream;
try(Stream<Path> stream = Files.list(repoDir)) {
String result = stream.filter( Files::isRegularFile ).map( path1 ->
- PathUtil.getRelative( managedRepository.getRepoRoot(), path1.toFile() )
+ PathUtil.getRelative( managedRepository.getRepoRoot(), path1 )
).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null );
if (result!=null) {
return managedRepository.toArtifactReference( result );
log.error("Could not create directory {}: {}", destDir, e.getMessage(), e);
throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create directory "+destDir );
}
- String relPath = PathUtil.getRelative( rootDirectory.toAbsolutePath().toString(), destDir.toFile() );
+ String relPath = PathUtil.getRelative( rootDirectory.toAbsolutePath().toString(), destDir );
log.debug( "Creating destination directory '{}' (current user '{}')", destDir.getFileName(),
activePrincipal );
import javax.inject.Inject;
import javax.inject.Named;
-import java.io.File;
import java.nio.file.NoSuchFileException;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
config = new ManagedRepository();
config.setId( TEST_REPO );
- config.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
+ config.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toString() );
metadataRepository = mock( MetadataRepository.class );