From ad9fee4399fef8fb45fa018f16ba8164b4481ae5 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Sat, 2 Sep 2017 13:14:33 +0200 Subject: [PATCH] First part in moving to java.nio --- .../archiva/common/utils/FileUtils.java | 58 ++++++++ .../repository/AbstractRepositoryPurge.java | 2 +- .../repository/DaysOldRepositoryPurge.java | 9 +- .../DefaultRepositoryProxyConnectors.java | 2 +- .../repository/ManagedRepositoryContent.java | 6 +- .../repository/metadata/MetadataTools.java | 125 +++++++++--------- .../metadata/RepositoryMetadataWriter.java | 10 +- .../rest/services/DefaultBrowseService.java | 43 +++--- .../services/DefaultRepositoriesService.java | 4 +- .../rest/services/utils/ArtifactBuilder.java | 9 +- .../services/ArtifactContentEntriesTests.java | 14 +- .../services/utils/ArtifactBuilderTest.java | 13 +- .../web/api/DefaultFileUploadService.java | 4 +- .../webdav/ArchivaDavResourceFactory.java | 2 +- .../maven2/Maven2RepositoryStorage.java | 7 +- .../ManagedDefaultRepositoryContent.java | 29 ++-- .../merge/Maven2RepositoryMerger.java | 4 +- 17 files changed, 208 insertions(+), 133 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java new file mode 100644 index 000000000..45a68ec7f --- /dev/null +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java @@ -0,0 +1,58 @@ +package org.apache.archiva.common.utils; +/* + * 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 java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Comparator; + +/** + * + * Utility class for file manipulation + * + * @author Martin Stockhammer + */ +public class FileUtils +{ + public static void deleteQuietly(Path dir) { + try + { + Files.walk(dir) + .sorted( Comparator.reverseOrder()) + .forEach( file -> { + try + { + Files.delete( file ); + } + catch ( IOException e ) + { + // Ignore this + } + + }); + } + catch ( IOException e ) + { + // Ignore this + } + + + } +} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java index 9b1615f76..44b7daa19 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java @@ -211,7 +211,7 @@ public abstract class AbstractRepositoryPurge log.error( "Error during metadata retrieval {}: {}", metaBaseId, e.getMessage( ) ); } } - Path artifactFile = repository.toFile( reference ).toPath( ); + Path artifactFile = repository.toFile( reference ); for ( RepositoryListener listener : listeners ) { diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java index 2231ffc19..f893ca511 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java @@ -31,6 +31,9 @@ import org.apache.archiva.repository.layout.LayoutException; import org.apache.commons.lang.time.DateUtils; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -112,12 +115,12 @@ public class DaysOldRepositoryPurge artifactFile.getAbsolutePath( ) ); newArtifactReference.setVersion( version ); - File newArtifactFile = repository.toFile( newArtifactReference ); + Path newArtifactFile = repository.toFile( newArtifactReference ); // Is this a generic snapshot "1.0-SNAPSHOT" ? if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion( ) ) ) { - if ( newArtifactFile.lastModified( ) < olderThanThisDate.getTimeInMillis( ) ) + if ( Files.getLastModifiedTime( newArtifactFile ).toMillis() < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( repository.getRelatedArtifacts( newArtifactReference ) ); } @@ -135,7 +138,7 @@ public class DaysOldRepositoryPurge } purge( artifactsToDelete ); } - catch ( ContentNotFoundException e ) + catch ( ContentNotFoundException | IOException e ) { throw new RepositoryPurgeException( e.getMessage( ), e ); } diff --git a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java index 1397c7eb5..3e8aab8c5 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java @@ -672,7 +672,7 @@ public class DefaultRepositoryProxyConnectors private File toLocalFile( ManagedRepositoryContent repository, ArtifactReference artifact ) { - return repository.toFile( artifact ); + return repository.toFile( artifact ).toFile(); } /** diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java index 13d75edc8..643f3e98b 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java @@ -26,7 +26,7 @@ import org.apache.archiva.model.ProjectReference; import org.apache.archiva.model.VersionedReference; import org.apache.archiva.repository.layout.LayoutException; -import java.io.File; +import java.nio.file.Path; import java.util.Set; /** @@ -197,7 +197,7 @@ public interface ManagedRepositoryContent * @param reference the artifact reference to use. * @return the relative path to the artifact. */ - File toFile( ArtifactReference reference ); + Path toFile( ArtifactReference reference ); /** * Given an {@link ArchivaArtifact}, return the file reference to the artifact. @@ -205,7 +205,7 @@ public interface ManagedRepositoryContent * @param reference the archiva artifact to use. * @return the relative path to the artifact. */ - File toFile( ArchivaArtifact reference ); + Path toFile( ArchivaArtifact reference ); /** * Given a {@link ProjectReference}, return the path to the metadata for diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java index 06038e960..71258fc08 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java @@ -21,6 +21,7 @@ package org.apache.archiva.repository.metadata; import org.apache.archiva.checksum.ChecksumAlgorithm; import org.apache.archiva.checksum.ChecksummedFile; +import org.apache.archiva.common.utils.FileUtils; import org.apache.archiva.common.utils.PathUtil; import org.apache.archiva.common.utils.VersionComparator; import org.apache.archiva.common.utils.VersionUtil; @@ -43,7 +44,6 @@ import org.apache.archiva.repository.RemoteRepositoryContent; import org.apache.archiva.repository.layout.LayoutException; import org.apache.archiva.xml.XMLException; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.time.DateUtils; @@ -54,8 +54,10 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.inject.Named; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -69,8 +71,10 @@ 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.regex.Matcher; +import java.util.stream.Stream; /** * MetadataTools @@ -362,9 +366,9 @@ public class MetadataTools ProjectReference reference, String proxyId ) { String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) ); - File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath ); + Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath ); - if ( !metadataFile.exists() || !metadataFile.isFile() ) + if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile )) { // Nothing to do. return null. return null; @@ -372,13 +376,13 @@ public class MetadataTools try { - return MavenMetadataReader.read( metadataFile ); + return MavenMetadataReader.read( metadataFile.toFile() ); } catch ( XMLException e ) { // TODO: [monitor] consider a monitor for this event. // TODO: consider a read-redo on monitor return code? - log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e ); + log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e ); return null; } } @@ -387,9 +391,9 @@ public class MetadataTools String logicalResource, String proxyId ) { String metadataPath = getRepositorySpecificName( proxyId, logicalResource ); - File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath ); + Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath ); - if ( !metadataFile.exists() || !metadataFile.isFile() ) + if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile)) { // Nothing to do. return null. return null; @@ -397,13 +401,13 @@ public class MetadataTools try { - return MavenMetadataReader.read( metadataFile ); + return MavenMetadataReader.read( metadataFile.toFile() ); } catch ( XMLException e ) { // TODO: [monitor] consider a monitor for this event. // TODO: consider a read-redo on monitor return code? - log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e ); + log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e ); return null; } } @@ -412,9 +416,9 @@ public class MetadataTools VersionedReference reference, String proxyId ) { String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) ); - File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath ); + Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath ); - if ( !metadataFile.exists() || !metadataFile.isFile() ) + if ( !Files.exists(metadataFile) || !Files.isRegularFile(metadataFile)) { // Nothing to do. return null. return null; @@ -422,13 +426,13 @@ public class MetadataTools try { - return MavenMetadataReader.read( metadataFile ); + return MavenMetadataReader.read( metadataFile.toFile() ); } catch ( XMLException e ) { // TODO: [monitor] consider a monitor for this event. // TODO: consider a read-redo on monitor return code? - log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e ); + log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e ); return null; } } @@ -436,7 +440,7 @@ public class MetadataTools public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource ) throws RepositoryMetadataException { - final File metadataFile = new File( managedRepository.getRepoRoot(), logicalResource ); + final Path metadataFile = Paths.get( managedRepository.getRepoRoot(), logicalResource ); ArchivaRepositoryMetadata metadata = null; //Gather and merge all metadata available @@ -464,7 +468,7 @@ public class MetadataTools { availableVersions.addAll( metadataAvailableVersions ); } - availableVersions = findPossibleVersions( availableVersions, metadataFile.getParentFile() ); + availableVersions = findPossibleVersions( availableVersions, metadataFile.getParent() ); if ( availableVersions.size() > 0 ) { @@ -473,7 +477,7 @@ public class MetadataTools RepositoryMetadataWriter.write( metadata, metadataFile ); - ChecksummedFile checksum = new ChecksummedFile( metadataFile ); + ChecksummedFile checksum = new ChecksummedFile( metadataFile.toFile() ); checksum.fixChecksums( algorithms ); } @@ -482,23 +486,31 @@ public class MetadataTools * subdirectories that contain poms. * * @param metadataParentDirectory - * @return origional set plus newley found versions + * @return origional set plus newly found versions */ - private Set findPossibleVersions( Set versions, File metadataParentDirectory ) + private Set findPossibleVersions( Set versions, Path metadataParentDirectory ) { + Set result = new HashSet( versions ); - for ( File directory : metadataParentDirectory.listFiles() ) - { - if ( directory.isDirectory() ) - { - for ( File possiblePom : directory.listFiles() ) + + try (Stream stream = Files.list( metadataParentDirectory )) { + stream.filter( Files::isDirectory ).filter( + p -> { - if ( possiblePom.getName().endsWith( ".pom" ) ) + try(Stream substream = Files.list(p)) { - result.add( directory.getName() ); + return substream.anyMatch( f -> Files.isRegularFile( f ) && f.endsWith( ".pom" )); + } + catch ( IOException e ) + { + return false; } } - } + ).forEach( + p -> result.add(p.getFileName().toString()) + ); + } catch (IOException e) { + // } return result; } @@ -507,12 +519,12 @@ public class MetadataTools ManagedRepositoryContent managedRepository, String logicalResource ) { List metadatas = new ArrayList<>(); - File file = new File( managedRepository.getRepoRoot(), logicalResource ); - if ( file.exists() ) + Path file = Paths.get( managedRepository.getRepoRoot(), logicalResource ); + if ( Files.exists(file) ) { try { - ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file ); + ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file.toFile() ); if ( existingMetadata != null ) { metadatas.add( existingMetadata ); @@ -520,7 +532,7 @@ public class MetadataTools } catch ( XMLException e ) { - log.debug( "Could not read metadata at {}. Metadata will be removed.", file.getAbsolutePath() ); + log.debug( "Could not read metadata at {}. Metadata will be removed.", file.toAbsolutePath() ); FileUtils.deleteQuietly( file ); } } @@ -563,7 +575,7 @@ public class MetadataTools public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference ) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException { - File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) ); + Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) ); long lastUpdated = getExistingLastUpdated( metadataFile ); @@ -578,11 +590,11 @@ public class MetadataTools // TODO: do we know this information instead? // Set allPlugins = managedRepository.getPlugins( reference ); Set allPlugins; - if ( metadataFile.exists() ) + if ( Files.exists(metadataFile)) { try { - allPlugins = new LinkedHashSet( MavenMetadataReader.read( metadataFile ).getPlugins() ); + allPlugins = new LinkedHashSet( MavenMetadataReader.read( metadataFile.toFile() ).getPlugins() ); } catch ( XMLException e ) { @@ -638,7 +650,7 @@ public class MetadataTools // Save the metadata model to disk. RepositoryMetadataWriter.write( metadata, metadataFile ); - ChecksummedFile checksum = new ChecksummedFile( metadataFile ); + ChecksummedFile checksum = new ChecksummedFile( metadataFile.toFile() ); checksum.fixChecksums( algorithms ); } @@ -733,9 +745,9 @@ public class MetadataTools } } - private long getExistingLastUpdated( File metadataFile ) + private long getExistingLastUpdated( Path metadataFile ) { - if ( !metadataFile.exists() ) + if ( !Files.exists(metadataFile) ) { // Doesn't exist. return 0; @@ -743,7 +755,7 @@ public class MetadataTools try { - ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile ); + ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile.toFile() ); return getLastUpdated( metadata ); } @@ -773,7 +785,7 @@ public class MetadataTools public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference ) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException { - File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) ); + Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) ); long lastUpdated = getExistingLastUpdated( metadataFile ); @@ -878,7 +890,7 @@ public class MetadataTools // Save the metadata model to disk. RepositoryMetadataWriter.write( metadata, metadataFile ); - ChecksummedFile checksum = new ChecksummedFile( metadataFile ); + ChecksummedFile checksum = new ChecksummedFile( metadataFile.toFile() ); checksum.fixChecksums( algorithms ); } @@ -936,39 +948,28 @@ public class MetadataTools path = path.substring( 0, idx ); } - File repoDir = new File( managedRepository.getRepoRoot(), path ); + Path repoDir = Paths.get( managedRepository.getRepoRoot(), path ); - if ( !repoDir.exists() ) + if ( !Files.exists(repoDir)) { throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: " - + repoDir.getAbsolutePath() ); + + repoDir.toAbsolutePath() ); } - if ( !repoDir.isDirectory() ) + if ( !Files.isDirectory( repoDir )) { throw new IOException( - "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() ); + "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath() ); } - File repoFiles[] = repoDir.listFiles(); - for ( int i = 0; i < repoFiles.length; i++ ) - { - if ( repoFiles[i].isDirectory() ) - { - // Skip it. it's a directory. - continue; - } - - String relativePath = PathUtil.getRelative( managedRepository.getRepoRoot(), repoFiles[i] ); - - if ( filetypes.matchesArtifactPattern( relativePath ) ) - { - ArtifactReference artifact = managedRepository.toArtifactReference( relativePath ); - - return artifact; + try(Stream stream = Files.list(repoDir)) { + String result = stream.filter( Files::isRegularFile ).map( path1 -> + PathUtil.getRelative( managedRepository.getRepoRoot(), path1.toFile() ) + ).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null ); + if (result!=null) { + return managedRepository.toArtifactReference( result ); } } - // No artifact was found. return null; } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java index 850210eae..0e553f634 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java @@ -19,21 +19,21 @@ package org.apache.archiva.repository.metadata; * under the License. */ +import org.apache.archiva.common.utils.FileUtils; import org.apache.archiva.model.ArchivaRepositoryMetadata; import org.apache.archiva.model.Plugin; import org.apache.archiva.xml.XMLException; import org.apache.archiva.xml.XMLWriter; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; -import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; +import java.nio.file.Path; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; @@ -44,11 +44,11 @@ import java.util.List; */ public class RepositoryMetadataWriter { - public static void write( ArchivaRepositoryMetadata metadata, File outputFile ) + public static void write( ArchivaRepositoryMetadata metadata, Path outputFile ) throws RepositoryMetadataException { boolean thrown = false; - try (FileWriter writer = new FileWriter( outputFile )) + try (FileWriter writer = new FileWriter( outputFile.toFile() )) { write( metadata, writer ); writer.flush(); @@ -57,7 +57,7 @@ public class RepositoryMetadataWriter { thrown = true; throw new RepositoryMetadataException( - "Unable to write metadata file: " + outputFile.getAbsolutePath() + " - " + e.getMessage(), e ); + "Unable to write metadata file: " + outputFile.toAbsolutePath() + " - " + e.getMessage(), e ); } finally { diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java index e2efea419..4fc786fbb 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java @@ -71,6 +71,9 @@ import javax.ws.rs.core.Response; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -94,6 +97,8 @@ public class DefaultBrowseService implements BrowseService { + private Charset ARTIFACT_CONTENT_ENCODING=Charset.forName( "UTF-8" ); + @Inject private DependencyTreeBuilder dependencyTreeBuilder; @@ -706,8 +711,8 @@ public class DefaultBrowseService ArchivaArtifact archivaArtifact = new ArchivaArtifact( groupId, artifactId, version, classifier, StringUtils.isEmpty( type ) ? "jar" : type, repoId ); - File file = managedRepositoryContent.toFile( archivaArtifact ); - if ( file.exists() ) + Path file = managedRepositoryContent.toFile( archivaArtifact ); + if ( Files.exists(file) ) { return readFileEntries( file, path, repoId ); } @@ -783,8 +788,8 @@ public class DefaultBrowseService ArchivaArtifact archivaArtifact = new ArchivaArtifact( groupId, artifactId, version, classifier, StringUtils.isEmpty( type ) ? "jar" : type, repoId ); - File file = managedRepositoryContent.toFile( archivaArtifact ); - if ( !file.exists() ) + Path file = managedRepositoryContent.toFile( archivaArtifact ); + if ( !Files.exists(file) ) { log.debug( "file: {} not exists for repository: {} try next repository", file, repoId ); continue; @@ -792,18 +797,18 @@ public class DefaultBrowseService if ( StringUtils.isNotBlank( path ) ) { // zip entry of the path -> path must a real file entry of the archive - JarFile jarFile = new JarFile( file ); + JarFile jarFile = new JarFile( file.toFile() ); ZipEntry zipEntry = jarFile.getEntry( path ); try (InputStream inputStream = jarFile.getInputStream( zipEntry )) { - return new ArtifactContent( IOUtils.toString( inputStream ), repoId ); + return new ArtifactContent( IOUtils.toString( inputStream, ARTIFACT_CONTENT_ENCODING ), repoId ); } finally { closeQuietly( jarFile ); } } - return new ArtifactContent( FileUtils.readFileToString( file ), repoId ); + return new ArtifactContent( new String(Files.readAllBytes( file ), ARTIFACT_CONTENT_ENCODING), repoId ); } } catch ( IOException e ) @@ -857,9 +862,9 @@ public class DefaultBrowseService StringUtils.isEmpty( classifier ) ? "" : classifier, "jar", repoId ); - File file = managedRepositoryContent.toFile( archivaArtifact ); + Path file = managedRepositoryContent.toFile( archivaArtifact ); - if ( file != null && file.exists() ) + if ( file != null && Files.exists(file) ) { return true; } @@ -867,13 +872,13 @@ public class DefaultBrowseService // in case of SNAPSHOT we can have timestamped version locally ! if ( StringUtils.endsWith( version, VersionUtil.SNAPSHOT ) ) { - File metadataFile = new File( file.getParent(), MetadataTools.MAVEN_METADATA ); - if ( metadataFile.exists() ) + Path metadataFile = file.getParent().resolve(MetadataTools.MAVEN_METADATA ); + if ( Files.exists(metadataFile) ) { try { ArchivaRepositoryMetadata archivaRepositoryMetadata = - MavenMetadataReader.read( metadataFile ); + MavenMetadataReader.read( metadataFile.toFile() ); int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber(); String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp(); // rebuild file name with timestamped version and build number @@ -884,9 +889,9 @@ public class DefaultBrowseService .append( ( StringUtils.isEmpty( classifier ) ? "" : "-" + classifier ) ) // .append( ".jar" ).toString(); - File timeStampFile = new File( file.getParent(), timeStampFileName ); - log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.getPath() ); - if ( timeStampFile.exists() ) + Path timeStampFile = file.getParent().resolve( timeStampFileName ); + log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.toAbsolutePath() ); + if ( Files.exists(timeStampFile) ) { return true; } @@ -900,9 +905,9 @@ public class DefaultBrowseService String path = managedRepositoryContent.toPath( archivaArtifact ); - file = connectors.fetchFromProxies( managedRepositoryContent, path ); + file = connectors.fetchFromProxies( managedRepositoryContent, path ).toPath(); - if ( file != null && file.exists() ) + if ( file != null && Files.exists(file) ) { // download pom now String pomPath = StringUtils.substringBeforeLast( path, ".jar" ) + ".pom"; @@ -1093,7 +1098,7 @@ public class DefaultBrowseService } } - protected List readFileEntries(final File file, final String filterPath, final String repoId ) + protected List readFileEntries(final Path file, final String filterPath, final String repoId ) throws IOException { String cleanedfilterPath = filterPath==null ? "" : (StringUtils.startsWith(filterPath, "/") ? @@ -1103,7 +1108,7 @@ public class DefaultBrowseService if (!StringUtils.endsWith(cleanedfilterPath,"/") && !StringUtils.isEmpty(cleanedfilterPath)) { filterDepth++; } - JarFile jarFile = new JarFile( file ); + JarFile jarFile = new JarFile( file.toFile() ); try { Enumeration jarEntryEnumeration = jarFile.entries(); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java index fc78a9a94..629a44d6c 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java @@ -613,7 +613,7 @@ public class DefaultRepositoriesService projectMetadata.setReleasedVersion( latestVersion ); } - RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile ); + RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile.toPath() ); if ( fixChecksums ) { @@ -1187,7 +1187,7 @@ public class DefaultRepositoriesService metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp ); metadata.setAvailableVersions( availableVersions ); - RepositoryMetadataWriter.write( metadata, metadataFile ); + RepositoryMetadataWriter.write( metadata, metadataFile.toPath() ); ChecksummedFile checksum = new ChecksummedFile( metadataFile ); checksum.fixChecksums( algorithms ); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java index b01bacf8d..8ad379b1b 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java @@ -26,6 +26,7 @@ import org.apache.archiva.maven2.model.Artifact; import org.apache.commons.io.FilenameUtils; import java.io.File; +import java.nio.file.Path; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; @@ -79,7 +80,7 @@ public class ArtifactBuilder ref.setClassifier( classifier ); ref.setType( type ); - File file = managedRepositoryContent.toFile( ref ); + Path file = managedRepositoryContent.toFile( ref ); String extension = getExtensionFromFile(file); @@ -124,10 +125,10 @@ public class ArtifactBuilder /** * Extract file extension */ - String getExtensionFromFile( File file ) + String getExtensionFromFile( Path file ) { // we are just interested in the section after the last - - String[] parts = file.getName().split( "-" ); + String[] parts = file.getFileName().toString().split( "-" ); if ( parts.length > 0 ) { // get anything after a dot followed by a letter a-z, including other dots @@ -139,7 +140,7 @@ public class ArtifactBuilder } } // just in case - return FilenameUtils.getExtension( file.getName() ); + return FilenameUtils.getExtension( file.toFile().getName() ); } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java index 461cb8018..8e692012b 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java @@ -28,6 +28,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -56,7 +58,7 @@ public class ArtifactContentEntriesTests throws Exception { - File file = new File( getBasedir(), + Path file = Paths.get( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = browseService.readFileEntries( file, null, "foo" ); @@ -74,7 +76,7 @@ public class ArtifactContentEntriesTests throws Exception { - File file = new File( getBasedir(), + Path file = Paths.get( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = browseService.readFileEntries( file, "", "foo" ); @@ -92,7 +94,7 @@ public class ArtifactContentEntriesTests throws Exception { - File file = new File( getBasedir(), + Path file = Paths.get( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = browseService.readFileEntries( file, "/", "foo" ); @@ -110,7 +112,7 @@ public class ArtifactContentEntriesTests throws Exception { - File file = new File( getBasedir(), + Path file = Paths.get( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = browseService.readFileEntries( file, "org", "foo" ); @@ -127,7 +129,7 @@ public class ArtifactContentEntriesTests throws Exception { - File file = new File( getBasedir(), + Path file = Paths.get( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = @@ -145,7 +147,7 @@ public class ArtifactContentEntriesTests throws Exception { - File file = new File( getBasedir(), + Path file = Paths.get( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java index 8b67c43c6..0fb6e917e 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java @@ -21,6 +21,7 @@ package org.apache.archiva.rest.services.utils; import static org.assertj.core.api.Assertions.*; import java.io.File; +import java.nio.file.Paths; import org.easymock.TestSubject; import org.junit.Test; @@ -33,36 +34,36 @@ public class ArtifactBuilderTest @Test public void testBuildSnapshot() { - assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-2.3-20141119.064321-40.jar" ) ) ).isEqualTo( "jar" ); + assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-2.3-20141119.064321-40.jar" ) ) ).isEqualTo( "jar" ); } @Test public void testBuildPom() { - assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.pom" ) ) ).isEqualTo( "pom" ); + assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.pom" ) ) ).isEqualTo( "pom" ); } @Test public void testBuildJar() { - assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0-sources.jar" ) ) ).isEqualTo( "jar" ); + assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0-sources.jar" ) ) ).isEqualTo( "jar" ); } @Test public void testBuildTarGz() { - assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.tar.gz" ) ) ).isEqualTo( "tar.gz" ); + assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.tar.gz" ) ) ).isEqualTo( "tar.gz" ); } @Test public void testBuildPomZip() { - assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.pom.zip" ) ) ).isEqualTo( "pom.zip" ); + assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.pom.zip" ) ) ).isEqualTo( "pom.zip" ); } @Test public void testBuildR00() { - assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.r00" ) ) ).isEqualTo( "r00" ); + assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.r00" ) ) ).isEqualTo( "r00" ); } } diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java index af0164d9e..b272489c1 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java @@ -643,7 +643,7 @@ public class DefaultFileUploadService projectMetadata.setReleasedVersion( latestVersion ); } - RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile ); + RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile.toPath() ); if ( fixChecksums ) { @@ -677,7 +677,7 @@ public class DefaultFileUploadService metadata.getSnapshotVersion().setTimestamp( timestamp ); metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp ); - RepositoryMetadataWriter.write( metadata, metadataFile ); + RepositoryMetadataWriter.write( metadata, metadataFile.toPath() ); if ( fixChecksums ) { diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java index d4958b113..3f4aeecfc 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java @@ -1269,7 +1269,7 @@ public class ArchivaDavResourceFactory } outputFile.getParentFile().mkdirs(); - RepositoryMetadataWriter.write( mergedMetadata, outputFile ); + RepositoryMetadataWriter.write( mergedMetadata, outputFile.toPath() ); createChecksumFile( outputFilename, digestSha1 ); createChecksumFile( outputFilename, digestMd5 ); diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java index efdb46087..37c1b9186 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java @@ -89,6 +89,7 @@ import java.io.IOException; import java.io.Reader; import java.nio.charset.Charset; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -687,9 +688,9 @@ public class Maven2RepositoryStorage connectors.fetchFromProxies( managedRepository, pomReference ); // Open and read the POM from the managed repo - File pom = managedRepository.toFile( pomReference ); + Path pom = managedRepository.toFile( pomReference ); - if ( !pom.exists() ) + if ( !Files.exists(pom) ) { return; } @@ -699,7 +700,7 @@ public class Maven2RepositoryStorage // MavenXpp3Reader leaves the file open, so we need to close it ourselves. Model model = null; - try (Reader reader = Files.newBufferedReader( pom.toPath(), Charset.defaultCharset() )) + try (Reader reader = Files.newBufferedReader( pom, Charset.defaultCharset() )) { model = MAVEN_XPP_3_READER.read( reader ); } diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java index aa4ca8a5a..03bdfe0d9 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java @@ -40,6 +40,9 @@ import javax.inject.Inject; import javax.inject.Named; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -167,25 +170,25 @@ public class ManagedDefaultRepositoryContent public Set getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException { - File artifactFile = toFile( reference ); - File repoDir = artifactFile.getParentFile(); + Path artifactFile = toFile( reference ); + Path repoDir = artifactFile.getParent(); - if ( !repoDir.exists() ) + if ( !Files.exists(repoDir)) { throw new ContentNotFoundException( - "Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() ); + "Unable to get related artifacts using a non-existant directory: " + repoDir.toAbsolutePath() ); } - if ( !repoDir.isDirectory() ) + if ( !Files.isDirectory( repoDir ) ) { throw new ContentNotFoundException( - "Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() ); + "Unable to get related artifacts using a non-directory: " + repoDir.toAbsolutePath() ); } Set foundArtifacts = new HashSet<>(); // First gather up the versions found as artifacts in the managed repository. - File repoFiles[] = repoDir.listFiles(); + File repoFiles[] = repoDir.toFile().listFiles(); for (File repoFile : repoFiles) { if (repoFile.isDirectory()) { @@ -350,8 +353,8 @@ public class ManagedDefaultRepositoryContent @Override public boolean hasContent( ArtifactReference reference ) { - File artifactFile = toFile( reference ); - return artifactFile.exists() && artifactFile.isFile(); + Path artifactFile = toFile( reference ); + return Files.exists(artifactFile) && Files.isRegularFile( artifactFile ); } @Override @@ -406,15 +409,15 @@ public class ManagedDefaultRepositoryContent } @Override - public File toFile( ArtifactReference reference ) + public Path toFile( ArtifactReference reference ) { - return new File( repository.getLocation(), toPath( reference ) ); + return Paths.get( repository.getLocation(), toPath( reference ) ); } @Override - public File toFile( ArchivaArtifact reference ) + public Path toFile( ArchivaArtifact reference ) { - return new File( repository.getLocation(), toPath( reference ) ); + return Paths.get( repository.getLocation(), toPath( reference ) ); } /** diff --git a/archiva-modules/plugins/stage-repository-merge/src/main/java/org/apache/archiva/stagerepository/merge/Maven2RepositoryMerger.java b/archiva-modules/plugins/stage-repository-merge/src/main/java/org/apache/archiva/stagerepository/merge/Maven2RepositoryMerger.java index 7a974db3c..d209098cf 100644 --- a/archiva-modules/plugins/stage-repository-merge/src/main/java/org/apache/archiva/stagerepository/merge/Maven2RepositoryMerger.java +++ b/archiva-modules/plugins/stage-repository-merge/src/main/java/org/apache/archiva/stagerepository/merge/Maven2RepositoryMerger.java @@ -329,7 +329,7 @@ public class Maven2RepositoryMerger projectMetadata.setReleasedVersion( latestVersion ); } - RepositoryMetadataWriter.write( projectMetadata, projectMetaDataFileIntargetRepo ); + RepositoryMetadataWriter.write( projectMetadata, projectMetaDataFileIntargetRepo.toPath() ); } @@ -346,7 +346,7 @@ public class Maven2RepositoryMerger } versionMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp ); - RepositoryMetadataWriter.write( versionMetadata, versionMetaDataFileInTargetRepo ); + RepositoryMetadataWriter.write( versionMetadata, versionMetaDataFileInTargetRepo.toPath() ); } private ArchivaRepositoryMetadata getMetadata( File metadataFile ) -- 2.39.5