diff options
author | Olivier Lamy <olamy@apache.org> | 2014-04-16 18:11:38 +1000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2014-04-16 18:11:38 +1000 |
commit | ec1e655c9d996f20bd231b21505a6df865d8a1f7 (patch) | |
tree | 83a6e56ba6b0e2a0f9e0bd97c2ddd1b05b5d90aa /archiva-modules/archiva-web | |
parent | 767e00c182085b193c9f2481a815e90c5c054095 (diff) | |
download | archiva-ec1e655c9d996f20bd231b21505a6df865d8a1f7.tar.gz archiva-ec1e655c9d996f20bd231b21505a6df865d8a1f7.zip |
use Files.copy from java.nio.file
Diffstat (limited to 'archiva-modules/archiva-web')
7 files changed, 32 insertions, 126 deletions
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<String, Collection<String>> 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<AuditListener> auditListeners, RepositoryArchivaTaskScheduler scheduler , FileLockManager fileLockManager ) + List<AuditListener> 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 ) |