From ec1e655c9d996f20bd231b21505a6df865d8a1f7 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 16 Apr 2014 18:11:38 +1000 Subject: [PATCH] use Files.copy from java.nio.file --- .../filelock/DefaultFileLockManagerTest.java | 34 ++++++++-------- .../DefaultFileLockManagerTimeoutTest.java | 17 +++----- .../metadata/RepositoryMetadataWriter.java | 7 +--- .../org/apache/archiva/xml/XMLReader.java | 30 +++++--------- .../rest/services/DefaultBrowseService.java | 4 +- .../rest/services/DefaultCommonServices.java | 19 ++------- .../services/DefaultRepositoriesService.java | 30 +++++--------- .../web/api/DefaultFileUploadService.java | 25 ++---------- .../remotedownload/DownloadArtifactsTest.java | 12 +++--- .../archiva/webdav/ArchivaDavResource.java | 28 ++++--------- .../apache/archiva/webdav/util/MimeTypes.java | 40 +------------------ .../maven2/Maven2RepositoryStorage.java | 2 - .../file/FileMetadataRepository.java | 29 +++++--------- 13 files changed, 79 insertions(+), 198 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java index 40c47442f..e72704e35 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java @@ -21,8 +21,6 @@ package org.apache.archiva.common.filelock; import edu.umd.cs.mtc.MultithreadedTestCase; import edu.umd.cs.mtc.TestFramework; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,11 +33,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.inject.Inject; import javax.inject.Named; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.concurrent.atomic.AtomicInteger; +//import org.apache.commons.io.IOUtils; + /** * @author Olivier Lamy */ @@ -89,7 +90,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -107,7 +108,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -124,8 +125,8 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( Paths.get( lock.getFile().getPath() ), + new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { @@ -143,7 +144,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -161,7 +162,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -178,8 +179,8 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( lock.getFile().toPath(), + new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { @@ -197,7 +198,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -214,8 +215,7 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { @@ -233,7 +233,7 @@ public class DefaultFileLockManagerTest try { lock.getFile().delete(); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath() ); } finally { @@ -250,8 +250,8 @@ public class DefaultFileLockManagerTest Lock lock = fileLockManager.readFileLock( this.file ); try { - IOUtils.copy( new FileInputStream( lock.getFile() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); + Files.copy( lock.getFile().toPath(), + new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); } finally { diff --git a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java index acf7fe66e..281d13105 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTimeoutTest.java @@ -19,11 +19,6 @@ package org.apache.archiva.common.filelock; * under the License. */ -import edu.umd.cs.mtc.MultithreadedTestCase; -import edu.umd.cs.mtc.TestFramework; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,10 +30,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.inject.Inject; import javax.inject.Named; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicInteger; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; /** * @author Olivier Lamy @@ -64,18 +57,20 @@ public class DefaultFileLockManagerTimeoutTest fileLockManager.clearLockFiles(); } - @Test( expected = FileLockTimeoutException.class ) + @Test(expected = FileLockTimeoutException.class) public void testTimeout() throws Throwable { File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" ); + Files.deleteIfExists( file.toPath() ); + File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" ); Lock lock = fileLockManager.writeFileLock( file ); - FileUtils.copyFile( largeJar, lock.getFile() ); + Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING ); lock = fileLockManager.writeFileLock( file ); 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 ab47e78c1..0eb06b225 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 @@ -25,7 +25,6 @@ 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.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentHelper; @@ -35,6 +34,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; +import java.nio.file.Files; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; @@ -49,10 +49,8 @@ public class RepositoryMetadataWriter throws RepositoryMetadataException { boolean thrown = false; - FileWriter writer = null; - try + try (FileWriter writer = new FileWriter( outputFile )) { - writer = new FileWriter( outputFile ); write( metadata, writer ); writer.flush(); } @@ -64,7 +62,6 @@ public class RepositoryMetadataWriter } finally { - IOUtils.closeQuietly( writer ); if ( thrown ) { FileUtils.deleteQuietly( outputFile ); diff --git a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java index 43cb09c4b..603fc884e 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java +++ b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java @@ -19,7 +19,6 @@ package org.apache.archiva.xml; * under the License. */ -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Attribute; import org.dom4j.Document; @@ -44,9 +43,7 @@ import java.util.List; import java.util.Map; /** - * XMLReader - a set of common xml utility methods for reading content out of an xml file. - * - * + * XMLReader - a set of common xml utility methods for reading content out of an xml file. */ public class XMLReader { @@ -98,12 +95,10 @@ public class XMLReader this.documentType = type; this.xmlUrl = url; - InputStream in = null; SAXReader reader = new SAXReader(); - - try + + try (InputStream in = url.openStream()) { - in = url.openStream(); InputStreamReader inReader = new InputStreamReader( in, "UTF-8" ); LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader ); this.document = reader.read( latinReader ); @@ -116,10 +111,6 @@ public class XMLReader { throw new XMLException( "Unable to open stream to " + url + ": " + e.getMessage(), e ); } - finally - { - IOUtils.closeQuietly( in ); - } Element root = this.document.getRootElement(); if ( root == null ) @@ -129,8 +120,9 @@ public class XMLReader if ( !StringUtils.equals( root.getName(), documentType ) ) { - throw new XMLException( "Invalid " + documentType + " xml: Unexpected root element <" + root.getName() - + ">, expected <" + documentType + ">" ); + throw new XMLException( + "Invalid " + documentType + " xml: Unexpected root element <" + root.getName() + ">, expected <" + + documentType + ">" ); } } @@ -164,7 +156,7 @@ public class XMLReader { // Unknown evaluated type. throw new XMLException( ".getElement( Expr: " + xpathExpr + " ) resulted in non-Element type -> (" - + evaluated.getClass().getName() + ") " + evaluated ); + + evaluated.getClass().getName() + ") " + evaluated ); } } @@ -246,8 +238,8 @@ public class XMLReader else { // Unknown evaluated type. - throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr - + " ) resulted in non-Element type -> (" + evaluated.getClass().getName() + ") " + evaluated ); + throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr + " ) resulted in non-Element type -> (" + + evaluated.getClass().getName() + ") " + evaluated ); } } @@ -271,7 +263,7 @@ public class XMLReader { // Unknown evaluated type. throw new XMLException( ".getElementText( Expr: " + xpathExpr + " ) resulted in non-Element type -> (" - + evaluated.getClass().getName() + ") " + evaluated ); + + evaluated.getClass().getName() + ") " + evaluated ); } } @@ -306,7 +298,7 @@ public class XMLReader { // Unknown evaluated type. throw new XMLException( ".getElementList( Expr: " + xpathExpr + " ) resulted in non-List type -> (" - + evaluated.getClass().getName() + ") " + evaluated ); + + evaluated.getClass().getName() + ") " + evaluated ); } } 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 c98b5f9d2..c929ff106 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 @@ -786,15 +786,13 @@ public class DefaultBrowseService // zip entry of the path -> path must a real file entry of the archive JarFile jarFile = new JarFile( file ); ZipEntry zipEntry = jarFile.getEntry( path ); - InputStream inputStream = jarFile.getInputStream( zipEntry ); - try + try (InputStream inputStream = jarFile.getInputStream( zipEntry )) { return new ArtifactContent( IOUtils.toString( inputStream ), repoId ); } finally { closeQuietly( jarFile ); - IOUtils.closeQuietly( inputStream ); } } return new ArtifactContent( FileUtils.readFileToString( file ), repoId ); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java index 795aaf169..b7cb60d34 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java @@ -23,7 +23,6 @@ import org.apache.archiva.redback.rest.api.services.RedbackServiceException; import org.apache.archiva.redback.rest.api.services.UtilServices; import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.CommonServices; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * @author Olivier Lamy */ -@Service( "commonServices#rest" ) +@Service("commonServices#rest") public class DefaultCommonServices implements CommonServices { @@ -121,11 +120,9 @@ public class DefaultCommonServices private void loadResource( final Properties finalProperties, String resourceName, String locale ) throws IOException { - InputStream is = null; Properties properties = new Properties(); - try + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName )) { - is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ); if ( is != null ) { properties.load( is ); @@ -139,10 +136,6 @@ public class DefaultCommonServices } } } - finally - { - IOUtils.closeQuietly( is ); - } } @Override @@ -181,10 +174,8 @@ public class DefaultCommonServices private void loadFromString( String propsStr, Properties properties ) throws ArchivaRestServiceException { - InputStream inputStream = null; - try + try (InputStream inputStream = new ByteArrayInputStream( propsStr.getBytes() )) { - inputStream = new ByteArrayInputStream( propsStr.getBytes() ); properties.load( inputStream ); } catch ( IOException e ) @@ -192,10 +183,6 @@ public class DefaultCommonServices throw new ArchivaRestServiceException( e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); } - finally - { - IOUtils.closeQuietly( inputStream ); - } } 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 a623d82fe..4fdb5f646 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 @@ -78,7 +78,6 @@ import org.apache.archiva.security.ArchivaSecurityException; import org.apache.archiva.security.common.ArchivaRoleConstants; import org.apache.archiva.xml.XMLException; import org.apache.commons.io.FilenameUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.maven.index.context.IndexingContext; import org.slf4j.Logger; @@ -89,9 +88,8 @@ import javax.inject.Inject; import javax.inject.Named; import javax.ws.rs.core.Response; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -107,7 +105,7 @@ import java.util.TimeZone; * @author Olivier Lamy * @since 1.4-M1 */ -@Service( "repositoriesService#rest" ) +@Service("repositoriesService#rest") public class DefaultRepositoriesService extends AbstractRestService implements RepositoriesService @@ -115,7 +113,7 @@ public class DefaultRepositoriesService private Logger log = LoggerFactory.getLogger( getClass() ); @Inject - @Named( value = "taskExecutor#indexing" ) + @Named(value = "taskExecutor#indexing") private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor; @Inject @@ -134,14 +132,14 @@ public class DefaultRepositoriesService private RepositoryContentFactory repositoryFactory; @Inject - @Named( value = "archivaTaskScheduler#repository" ) + @Named(value = "archivaTaskScheduler#repository") private ArchivaTaskScheduler scheduler; @Inject private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler; @Inject - @Named( value = "repositorySessionFactory" ) + @Named(value = "repositorySessionFactory") protected RepositorySessionFactory repositorySessionFactory; @Inject @@ -154,7 +152,7 @@ public class DefaultRepositoriesService * Cache used for namespaces */ @Inject - @Named( value = "cache#namespaces" ) + @Named(value = "cache#namespaces") private Cache> namespacesCache; private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 }; @@ -423,7 +421,8 @@ public class DefaultRepositoriesService { throw new ArchivaRestServiceException( "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId() - + " and redeployment blocked", null ); + + " and redeployment blocked", null + ); } else { @@ -531,18 +530,7 @@ public class DefaultRepositoriesService private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums ) throws IOException { - FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) ); - FileInputStream input = new FileInputStream( sourceFile ); - - try - { - IOUtils.copy( input, out ); - } - finally - { - IOUtils.closeQuietly( out ); - IOUtils.closeQuietly( input ); - } + Files.copy( sourceFile.toPath(), new File( targetPath, targetFilename ).toPath() ); if ( fixChecksums ) { 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 2bbaf4b06..c232e03fa 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 @@ -60,17 +60,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; - import javax.inject.Inject; import javax.inject.Named; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -540,15 +539,11 @@ public class DefaultFileUploadService File pomFile = new File( targetPath, filename ); MavenXpp3Writer writer = new MavenXpp3Writer(); - FileWriter w = new FileWriter( pomFile ); - try + + try (FileWriter w = new FileWriter( pomFile )) { writer.write( w, projectModel ); } - finally - { - IOUtils.closeQuietly( w ); - } return pomFile; } @@ -581,20 +576,8 @@ public class DefaultFileUploadService private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums ) throws IOException { - FileOutputStream out = null; - FileInputStream input = null; - try - { - out = new FileOutputStream( new File( targetPath, targetFilename ) ); - input = new FileInputStream( sourceFile ); - IOUtils.copy( input, out ); - } - finally - { - IOUtils.closeQuietly( out ); - IOUtils.closeQuietly( input ); - } + Files.copy( sourceFile.toPath(), new File( targetPath, targetFilename ).toPath() ); if ( fixChecksums ) { diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java index 3192fce54..b009e8c71 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactsTest.java @@ -21,7 +21,7 @@ package org.apache.archiva.remotedownload; import org.apache.archiva.admin.model.beans.RemoteRepository; import org.apache.archiva.redback.rest.api.services.RoleManagementService; import org.apache.archiva.security.common.ArchivaRoleConstants; -import org.apache.commons.compress.utils.IOUtils; +import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; import org.apache.commons.io.FileUtils; import org.apache.maven.wagon.providers.http.HttpWagon; import org.apache.maven.wagon.repository.Repository; @@ -42,12 +42,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Files; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; /** * @author Olivier Lamy @@ -85,8 +84,9 @@ public class DownloadArtifactsTest protected String getSpringConfigLocation() { return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml"; - } @Override + } + @Override @Before public void startServer() @@ -182,8 +182,6 @@ public class DownloadArtifactsTest } - - public static class RedirectServlet extends HttpServlet { @@ -212,7 +210,7 @@ public class DownloadArtifactsTest throws ServletException, IOException { File jar = new File( System.getProperty( "basedir" ), "src/test/junit-4.9.jar" ); - IOUtils.copy( new FileInputStream( jar ), resp.getOutputStream() ); + Files.copy( jar.toPath(), resp.getOutputStream() ); } } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResource.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResource.java index 6961d2206..c1c73eee8 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResource.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResource.java @@ -65,9 +65,10 @@ import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletResponse; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -134,7 +135,8 @@ public class ArchivaDavResource public ArchivaDavResource( String localResource, String logicalResource, ManagedRepository repository, String remoteAddr, String principal, DavSession session, ArchivaDavResourceLocator locator, DavResourceFactory factory, MimeTypes mimeTypes, - List auditListeners, RepositoryArchivaTaskScheduler scheduler , FileLockManager fileLockManager ) + List auditListeners, RepositoryArchivaTaskScheduler scheduler, + FileLockManager fileLockManager ) { this( localResource, logicalResource, repository, session, locator, factory, mimeTypes, auditListeners, scheduler, fileLockManager ); @@ -218,18 +220,10 @@ public class ArchivaDavResource if ( !isCollection() && outputContext.hasStream() ) { Lock lock = fileLockManager.readFileLock( localResource ); - FileInputStream is = null; - try + try (InputStream is = Files.newInputStream( lock.getFile().toPath() )) { - // Write content to stream - is = new FileInputStream( lock.getFile() ); IOUtils.copy( is, outputContext.getOutputStream() ); } - finally - { - IOUtils.closeQuietly( is ); - fileLockManager.release( lock ); - } } else if ( outputContext.hasStream() ) { @@ -283,7 +277,7 @@ public class ArchivaDavResource return null; } - @SuppressWarnings ("unchecked") + @SuppressWarnings("unchecked") @Override public MultiStatusResponse alterProperties( List changeList ) throws DavException @@ -325,20 +319,14 @@ public class ArchivaDavResource if ( isCollection() && inputContext.hasStream() ) // New File { - FileOutputStream stream = null; - try + try (OutputStream stream = Files.newOutputStream( localFile.toPath() )) { - stream = new FileOutputStream( localFile ); IOUtils.copy( inputContext.getInputStream(), stream ); } catch ( IOException e ) { throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e ); } - finally - { - IOUtils.closeQuietly( stream ); - } // TODO: a bad deployment shouldn't delete an existing file - do we need to write to a temporary location first? long expectedContentLength = inputContext.getContentLength(); diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/util/MimeTypes.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/util/MimeTypes.java index f437efb03..482b79902 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/util/MimeTypes.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/util/MimeTypes.java @@ -27,9 +27,6 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -40,10 +37,8 @@ import java.util.StringTokenizer; /** * MimeTypes - * - * */ -@Service( "mimeTpes" ) +@Service("mimeTpes") public class MimeTypes { private static final String DEFAULT_MIME_TYPE = "application/octet-stream"; @@ -88,30 +83,6 @@ public class MimeTypes load( resource ); } - public void load( File file ) - { - if ( !file.exists() || !file.isFile() || !file.canRead() ) - { - log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : not a readable file." ); - return; - } - - FileInputStream fis = null; - - try - { - fis = new FileInputStream( file ); - } - catch ( FileNotFoundException e ) - { - log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : " + e.getMessage(), e ); - } - finally - { - IOUtils.closeQuietly( fis ); - } - } - public void load( String resourceName ) { ClassLoader cloader = this.getClass().getClassLoader(); @@ -124,21 +95,14 @@ public class MimeTypes throw new IllegalStateException( "Unable to find resource " + resourceName ); } - InputStream mimeStream = null; - - try + try (InputStream mimeStream = mimeURL.openStream()) { - mimeStream = mimeURL.openStream(); load( mimeStream ); } catch ( IOException e ) { log.error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e ); } - finally - { - IOUtils.closeQuietly( mimeStream ); - } } public void load( InputStream mimeStream ) 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 c85518a42..c7303cef5 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 @@ -573,8 +573,6 @@ public class Maven2RepositoryStorage // scan all directories in the namespace, and only include those that are known to be projects List projects = new ArrayList<>(); - - File[] files = dir.listFiles( new DirectoryFilter( filter ) ); if ( files != null ) { diff --git a/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java b/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java index d4b3c6fed..15129c9a4 100644 --- a/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java +++ b/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java @@ -37,15 +37,15 @@ import org.apache.archiva.metadata.model.Scm; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.MetadataRepositoryException; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -61,6 +61,8 @@ import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; +//import org.apache.commons.io.IOUtils; + public class FileMetadataRepository implements MetadataRepository { @@ -575,7 +577,7 @@ public class FileMetadataRepository } @Override - public T obtainAccess( Class aClass ) + public T obtainAccess( Class aClass ) { throw new IllegalArgumentException( "Access using " + aClass + " is not supported on the file metadata storage" ); @@ -647,7 +649,8 @@ public class FileMetadataRepository File directory = new File( getDirectory( artifactMetadata.getRepositoryId() ), artifactMetadata.getNamespace() + "/" + artifactMetadata.getProject() + "/" - + baseVersion ); + + baseVersion + ); Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); @@ -870,16 +873,11 @@ public class FileMetadataRepository throws IOException { Properties properties = new Properties(); - FileInputStream in = null; - try + try (InputStream in = Files.newInputStream( new File( directory, propertiesKey + ".properties" ).toPath() )) { - in = new FileInputStream( new File( directory, propertiesKey + ".properties" ) ); + properties.load( in ); } - finally - { - IOUtils.closeQuietly( in ); - } return properties; } @@ -1240,15 +1238,10 @@ public class FileMetadataRepository throws IOException { directory.mkdirs(); - FileOutputStream os = new FileOutputStream( new File( directory, propertiesKey + ".properties" ) ); - try + try (OutputStream os = Files.newOutputStream( new File( directory, propertiesKey + ".properties" ).toPath() )) { properties.store( os, null ); } - finally - { - IOUtils.closeQuietly( os ); - } } private static class ArtifactComparator -- 2.39.5