From 05e1c462afef54144647a4f492da06b1f7cb5eb4 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Wed, 1 May 2019 18:32:59 +0200 Subject: [PATCH] Merging upload artifact test to this archiva version --- .../web/api/DefaultFileUploadService.java | 25 +- .../archiva/upload/UploadArtifactsTest.java | 318 ++++++++---------- .../resources/spring-context-test-upload.xml | 2 +- 3 files changed, 164 insertions(+), 181 deletions(-) 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 b4d60cc15..aa0e59287 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 @@ -68,10 +68,7 @@ import javax.ws.rs.core.Response; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; +import java.nio.file.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -194,6 +191,7 @@ public class DefaultFileUploadService public Boolean deleteFile( String fileName ) throws ArchivaRestServiceException { + log.debug("Deleting file {}", fileName); // we make sure, that there are no other path components in the filename: String checkedFileName = Paths.get(fileName).getFileName().toString(); Path file = SystemUtils.getJavaIoTmpDir().toPath().resolve( checkedFileName ); @@ -354,6 +352,7 @@ public class DefaultFileUploadService throws ArchivaRestServiceException { + log.debug("Saving POM"); try { boolean fixChecksums = @@ -387,19 +386,23 @@ public class DefaultFileUploadService copyFile( Paths.get( fileMetadata.getServerFileName() ), targetPath, pomFilename, fixChecksums ); triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE ); queueRepositoryTask( repoConfig.getId(), targetPath.resolve(pomFilename ) ); + log.debug("Finished Saving POM"); } catch ( IOException ie ) { + log.error("IOException for POM {}", ie.getMessage()); throw new ArchivaRestServiceException( "Error encountered while uploading pom file: " + ie.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie ); } catch ( RepositoryException rep ) { + log.error("RepositoryException for POM {}", rep.getMessage()); throw new ArchivaRestServiceException( "Repository exception: " + rep.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep ); } catch ( RepositoryAdminException e ) { + log.error("RepositoryAdminException for POM {}", e.getMessage()); throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); } @@ -409,10 +412,11 @@ public class DefaultFileUploadService String artifactId, String version, String packaging ) throws ArchivaRestServiceException { + log.debug("Saving file"); try { - ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId ); + org.apache.archiva.repository.ManagedRepository repoConfig = repositoryRegistry.getManagedRepository(repositoryId); ArtifactReference artifactReference = new ArtifactReference(); artifactReference.setArtifactId( artifactId ); @@ -422,14 +426,14 @@ public class DefaultFileUploadService artifactReference.setType( StringUtils.isEmpty( fileMetadata.getPackaging() ) ? packaging : fileMetadata.getPackaging() ); - ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId ); + ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repoConfig ); String artifactPath = repository.toPath( artifactReference ); int lastIndex = artifactPath.lastIndexOf( '/' ); String path = artifactPath.substring( 0, lastIndex ); - Path targetPath = Paths.get( repoConfig.getLocation(), path ); + Path targetPath = Paths.get(repoConfig.getLocation()).resolve(path); log.debug( "artifactPath: {} found targetPath: {}", artifactPath, targetPath ); @@ -473,7 +477,7 @@ public class DefaultFileUploadService try { Path targetFile = targetPath.resolve( filename ); - if ( Files.exists(targetFile) && !VersionUtil.isSnapshot( version ) && repoConfig.isBlockRedeployments() ) + if ( Files.exists(targetFile) && !VersionUtil.isSnapshot( version ) && repoConfig.blocksRedeployments()) { throw new ArchivaRestServiceException( "Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed.", @@ -538,21 +542,26 @@ public class DefaultFileUploadService } catch ( RepositoryNotFoundException re ) { + log.error("RepositoryNotFoundException during save {}", re.getMessage()); + re.printStackTrace(); throw new ArchivaRestServiceException( "Target repository cannot be found: " + re.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), re ); } catch ( RepositoryException rep ) { + log.error("RepositoryException during save {}", rep.getMessage()); throw new ArchivaRestServiceException( "Repository exception: " + rep.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep ); } catch ( RepositoryAdminException e ) { + log.error("RepositoryAdminException during save {}", e.getMessage()); throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); } catch ( IOException e ) { + log.error("IOException during save {}", e.getMessage()); throw new ArchivaRestServiceException("Repository exception "+ e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); } diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/upload/UploadArtifactsTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/upload/UploadArtifactsTest.java index 254d9bb88..78debb1e0 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/upload/UploadArtifactsTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/upload/UploadArtifactsTest.java @@ -19,6 +19,7 @@ package org.apache.archiva.upload; */ import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; +import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.redback.rest.services.AbstractRestServicesTest; import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; @@ -33,8 +34,11 @@ import org.apache.cxf.jaxrs.ext.multipart.AttachmentBuilder; import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition; import org.apache.cxf.jaxrs.ext.multipart.MultipartBody; import org.apache.cxf.message.Message; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; + import javax.ws.rs.ClientErrorException; import java.io.IOException; import java.net.URLEncoder; @@ -46,232 +50,202 @@ import java.util.Collections; /** * @author Olivier Lamy */ -@RunWith( ArchivaBlockJUnit4ClassRunner.class ) +@RunWith(ArchivaBlockJUnit4ClassRunner.class) public class UploadArtifactsTest - extends AbstractRestServicesTest -{ + extends AbstractRestServicesTest { + private static String PREVIOUS_ARCHIVA_PATH; - @Override - protected String getSpringConfigLocation( ) + @BeforeClass + public static void initConfigurationPath() + throws Exception + { + PREVIOUS_ARCHIVA_PATH = System.getProperty(ArchivaConfiguration.USER_CONFIG_PROPERTY); + System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY, + System.getProperty( "test.resources.path/" ) + "archiva.xml" ); + } + + + @AfterClass + public static void restoreConfigurationPath() + throws Exception { + System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY, PREVIOUS_ARCHIVA_PATH ); + } + @Override + protected String getSpringConfigLocation() { return "classpath*:META-INF/spring-context.xml,classpath:/spring-context-test-upload.xml"; } @Override - protected String getRestServicesPath( ) - { + protected String getRestServicesPath() { return "restServices"; } - protected String getBaseUrl( ) - { - String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" ); - return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + getServerPort() : baseUrlSysProps; + protected String getBaseUrl() { + String baseUrlSysProps = System.getProperty("archiva.baseRestUrl"); + return StringUtils.isBlank(baseUrlSysProps) ? "http://localhost:" + getServerPort() : baseUrlSysProps; } - private FileUploadService getUploadService( ) - { + private FileUploadService getUploadService() { FileUploadService service = - JAXRSClientFactory.create( getBaseUrl( ) + "/" + getRestServicesPath( ) + "/archivaUiServices/", - FileUploadService.class, - Collections.singletonList( new JacksonJaxbJsonProvider( ) ) ); - log.debug( "Service class {}", service.getClass( ).getName( ) ); - WebClient.client( service ).header( "Authorization", authorizationHeader ); - WebClient.client( service ).header( "Referer", "http://localhost:" + getServerPort() ); - - WebClient.client( service ).header( "Referer", "http://localhost" ); - WebClient.getConfig( service ).getRequestContext( ).put( Message.MAINTAIN_SESSION, true ); - WebClient.getConfig( service).getRequestContext().put(Message.EXCEPTION_MESSAGE_CAUSE_ENABLED, true); - WebClient.getConfig( service).getRequestContext().put(Message.FAULT_STACKTRACE_ENABLED, true); - WebClient.getConfig( service).getRequestContext().put(Message.PROPOGATE_EXCEPTION, true); - WebClient.getConfig( service).getRequestContext().put("org.apache.cxf.transport.no_io_exceptions", true); + JAXRSClientFactory.create(getBaseUrl() + "/" + getRestServicesPath() + "/archivaUiServices/", + FileUploadService.class, + Collections.singletonList(new JacksonJaxbJsonProvider())); + log.debug("Service class {}", service.getClass().getName()); + WebClient.client(service).header("Authorization", authorizationHeader); + WebClient.client(service).header("Referer", "http://localhost:" + getServerPort()); + + WebClient.client(service).header("Referer", "http://localhost"); + WebClient.getConfig(service).getRequestContext().put(Message.MAINTAIN_SESSION, true); + WebClient.getConfig(service).getRequestContext().put(Message.EXCEPTION_MESSAGE_CAUSE_ENABLED, true); + WebClient.getConfig(service).getRequestContext().put(Message.FAULT_STACKTRACE_ENABLED, true); + WebClient.getConfig(service).getRequestContext().put(Message.PROPOGATE_EXCEPTION, true); + WebClient.getConfig(service).getRequestContext().put("org.apache.cxf.transport.no_io_exceptions", true); // WebClient.client( service ). return service; } @Test - public void clearUploadedFiles( ) - throws Exception - { - FileUploadService service = getUploadService( ); - service.clearUploadedFiles( ); + public void clearUploadedFiles() + throws Exception { + FileUploadService service = getUploadService(); + service.clearUploadedFiles(); } @Test - public void uploadFile( ) throws IOException, ArchivaRestServiceException - { - FileUploadService service = getUploadService( ); - try - { - Path file = Paths.get( "src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar" ); - final Attachment fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"" + file.getFileName( ).toString( ) + "\"; name=\"files[]\"" ) ).build( ); - MultipartBody body = new MultipartBody( fileAttachment ); - service.post( body ); - } - finally - { - service.clearUploadedFiles( ); + public void uploadFile() throws IOException, ArchivaRestServiceException { + FileUploadService service = getUploadService(); + try { + Path file = Paths.get("src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar"); + final Attachment fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"" + file.getFileName().toString() + "\"; name=\"files[]\"")).build(); + MultipartBody body = new MultipartBody(fileAttachment); + service.post(body); + } finally { + service.clearUploadedFiles(); } } @Test - public void failUploadFileWithBadFileName( ) throws IOException, ArchivaRestServiceException - { - FileUploadService service = getUploadService( ); - try - { - Path file = Paths.get( "src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar" ); - final Attachment fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"/../TestFile.testext\"; name=\"files[]\"" ) ).build( ); - MultipartBody body = new MultipartBody( fileAttachment ); - try - { - service.post( body ); - fail( "FileNames with path contents should not be allowed." ); - } - catch ( ClientErrorException e ) - { + public void failUploadFileWithBadFileName() throws IOException, ArchivaRestServiceException { + FileUploadService service = getUploadService(); + try { + Path file = Paths.get("src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar"); + final Attachment fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"/../TestFile.testext\"; name=\"files[]\"")).build(); + MultipartBody body = new MultipartBody(fileAttachment); + try { + service.post(body); + fail("FileNames with path contents should not be allowed."); + } catch (ClientErrorException e) { assertEquals(422, e.getResponse().getStatus()); } - } - finally - { - service.clearUploadedFiles( ); + } finally { + service.clearUploadedFiles(); } } @Test - public void uploadAndDeleteFile( ) throws IOException, ArchivaRestServiceException - { - FileUploadService service = getUploadService( ); - try - { - Path file = Paths.get( "src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar" ); - final Attachment fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"" + file.getFileName( ).toString( ) + "\"; name=\"files[]\"" ) ).build( ); - MultipartBody body = new MultipartBody( fileAttachment ); - service.post( body ); - service.deleteFile( file.getFileName( ).toString( ) ); - } - finally - { - service.clearUploadedFiles( ); + public void uploadAndDeleteFile() throws IOException, ArchivaRestServiceException { + FileUploadService service = getUploadService(); + try { + Path file = Paths.get("src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar"); + final Attachment fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"" + file.getFileName().toString() + "\"; name=\"files[]\"")).build(); + MultipartBody body = new MultipartBody(fileAttachment); + service.post(body); + service.deleteFile(file.getFileName().toString()); + } finally { + service.clearUploadedFiles(); } } @Test - public void failUploadAndDeleteWrongFile( ) throws IOException, ArchivaRestServiceException - { - FileUploadService service = getUploadService( ); - try - { - Path file = Paths.get( "src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar" ); - final Attachment fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"" + file.getFileName( ).toString( ) + "\"; name=\"files[]\"" ) ).build( ); - MultipartBody body = new MultipartBody( fileAttachment ); - service.post( body ); - assertFalse( service.deleteFile( "file123" + file.getFileName( ).toString( ) ) ); - } - finally - { - service.clearUploadedFiles( ); + public void failUploadAndDeleteWrongFile() throws IOException, ArchivaRestServiceException { + FileUploadService service = getUploadService(); + try { + Path file = Paths.get("src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar"); + final Attachment fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"" + file.getFileName().toString() + "\"; name=\"files[]\"")).build(); + MultipartBody body = new MultipartBody(fileAttachment); + service.post(body); + assertFalse(service.deleteFile("file123" + file.getFileName().toString())); + } finally { + service.clearUploadedFiles(); } } @Test - public void failUploadAndDeleteFileInOtherDir( ) throws IOException, ArchivaRestServiceException - { + public void failUploadAndDeleteFileInOtherDir() throws IOException, ArchivaRestServiceException { Path testFile = null; - try - { - FileUploadService service = getUploadService( ); - Path file = Paths.get( "src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar" ); - Path targetDir = Paths.get( "target/testDelete" ).toAbsolutePath( ); - if ( !Files.exists( targetDir ) ) Files.createDirectories( targetDir ); - Path tempDir = SystemUtils.getJavaIoTmpDir( ).toPath( ); - testFile = Files.createTempFile( targetDir, "TestFile", ".txt" ); - log.debug( "Test file {}", testFile.toAbsolutePath( ) ); - log.debug( "Tmp dir {}", tempDir.toAbsolutePath( ) ); - assertTrue( Files.exists( testFile ) ); - Path relativePath = tempDir.relativize( testFile.toAbsolutePath( ) ); - final Attachment fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"" + file.getFileName( ).toString( ) + "\"; name=\"files[]\"" ) ).build( ); - MultipartBody body = new MultipartBody( fileAttachment ); - service.post( body ); - String relativePathEncoded = URLEncoder.encode( "../target/" + relativePath.toString( ), "UTF-8" ); - log.debug( "Trying to delete with path traversal: {}, {}", relativePath, relativePathEncoded ); - try - { - service.deleteFile( relativePathEncoded ); - } - catch ( ArchivaRestServiceException ex ) - { + try { + FileUploadService service = getUploadService(); + Path file = Paths.get("src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar"); + Path targetDir = Paths.get("target/testDelete").toAbsolutePath(); + if (!Files.exists(targetDir)) Files.createDirectories(targetDir); + Path tempDir = SystemUtils.getJavaIoTmpDir().toPath(); + testFile = Files.createTempFile(targetDir, "TestFile", ".txt"); + log.debug("Test file {}", testFile.toAbsolutePath()); + log.debug("Tmp dir {}", tempDir.toAbsolutePath()); + assertTrue(Files.exists(testFile)); + Path relativePath = tempDir.relativize(testFile.toAbsolutePath()); + final Attachment fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"" + file.getFileName().toString() + "\"; name=\"files[]\"")).build(); + MultipartBody body = new MultipartBody(fileAttachment); + service.post(body); + String relativePathEncoded = URLEncoder.encode("../target/" + relativePath.toString(), "UTF-8"); + log.debug("Trying to delete with path traversal: {}, {}", relativePath, relativePathEncoded); + try { + service.deleteFile(relativePathEncoded); + } catch (ArchivaRestServiceException ex) { // Expected exception } - assertTrue( "File in another directory may not be deleted", Files.exists( testFile ) ); - } - finally - { - if ( testFile != null ) - { - Files.deleteIfExists( testFile ); + assertTrue("File in another directory may not be deleted", Files.exists(testFile)); + } finally { + if (testFile != null) { + Files.deleteIfExists(testFile); } } } @Test - public void failSaveFileWithBadParams( ) throws IOException, ArchivaRestServiceException - { - FileUploadService service = getUploadService( ); - Path targetFile = Paths.get( "target/test/test-testSave.4" ); - Path targetPom = Paths.get( "target/test/test-testSave.pom" ); - try - { - Path file = Paths.get( "src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar" ); - - Path targetDir = Paths.get( "target/appserver-base/test/testSave" ).toAbsolutePath( ); - Path repoDir = Paths.get("target/appserver-base/repositories/internal/org"); - log.info("Repo dir {}", repoDir.toAbsolutePath()); - if (!Files.exists(repoDir)) Files.createDirectories(repoDir); - assertTrue(Files.exists(repoDir)); - if ( !Files.exists( targetDir ) ) Files.createDirectories( targetDir ); - Files.deleteIfExists( targetFile ); - Files.deleteIfExists( targetPom ); - Attachment fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"archiva-model-1.2.jar\"; name=\"files[]\"" ) ).build( ); - MultipartBody body = new MultipartBody( fileAttachment ); - FileMetadata meta = service.post( body ); - log.debug( "Metadata {}", meta.toString( ) ); - assertTrue( service.save( "internal", "org.archiva", "archiva-model", "1.2", "jar", true ) ); - - fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"TestFile.FileExt\"; name=\"files[]\"" ) ).build( ); - body = new MultipartBody( fileAttachment ); - meta = service.post( body ); - log.debug( "Metadata {}", meta.toString( ) ); - try { - service.save("internal", "org", URLEncoder.encode("../../../test", "UTF-8"), URLEncoder.encode("testSave", "UTF-8"), "4", true); - fail("Error expected, if the content contains bad characters."); - } catch (ClientErrorException e) { - assertEquals(422, e.getResponse().getStatus()); - } - assertFalse( Files.exists( Paths.get( "target/test-testSave.4" ) ) ); - } - finally - { - // service.clearUploadedFiles( ); - Files.deleteIfExists( targetFile ); - Files.deleteIfExists( targetPom ); + public void failSaveFileWithBadParams() throws IOException, ArchivaRestServiceException { + Path path = Paths.get("target/appserver-base/repositories/internal/data/repositories/internal/org/apache/archiva/archiva-model/1.2/archiva-model-1.2.jar"); + Files.deleteIfExists(path); + FileUploadService service = getUploadService(); + Path file = Paths.get("src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar"); + + Attachment fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"archiva-model.jar\"; name=\"files[]\"")).build(); + MultipartBody body = new MultipartBody(fileAttachment); + service.post(body); + assertTrue(service.save("internal", "org.apache.archiva", "archiva-model", "1.2", "jar", true)); + + fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"TestFile.FileExt\"; name=\"files[]\"")).build(); + body = new MultipartBody(fileAttachment); + FileMetadata meta = service.post(body); + log.debug("Metadata {}", meta.toString()); + try { + service.save("internal", "org", URLEncoder.encode("../../../test", "UTF-8"), URLEncoder.encode("testSave", "UTF-8"), "4", true); + fail("Error expected, if the content contains bad characters."); + } catch (ClientErrorException e) { + assertEquals(422, e.getResponse().getStatus()); } + assertFalse(Files.exists(Paths.get("target/test-testSave.4"))); } @Test - public void saveFile( ) throws IOException, ArchivaRestServiceException - { + public void saveFile() throws IOException, ArchivaRestServiceException { + log.debug("Starting saveFile()"); Path path = Paths.get("target/appserver-base/repositories/internal/data/repositories/internal/org/apache/archiva/archiva-model/1.2/archiva-model-1.2.jar"); - Files.deleteIfExists( path ); - FileUploadService service = getUploadService( ); - Path file = Paths.get( "src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar" ); - final Attachment fileAttachment = new AttachmentBuilder( ).object( Files.newInputStream( file ) ).contentDisposition( new ContentDisposition( "form-data; filename=\"archiva-model.jar\"; name=\"files[]\"" ) ).build( ); - MultipartBody body = new MultipartBody( fileAttachment ); - service.post( body ); - service.save( "internal", "org.apache.archiva", "archiva-model", "1.2", "jar", true ); + log.debug("Jar exists: {}",Files.exists(path)); + Files.deleteIfExists(path); + path = Paths.get("target/appserver-base/repositories/internal/data/repositories/internal/org/apache/archiva/archiva-model/1.2/archiva-model-1.2.pom"); + Files.deleteIfExists(path); + FileUploadService service = getUploadService(); + service.clearUploadedFiles(); + Path file = Paths.get("src/test/repositories/snapshot-repo/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-20130425.081822-1.jar"); + log.debug("Upload file exists: {}", Files.exists(file)); + final Attachment fileAttachment = new AttachmentBuilder().object(Files.newInputStream(file)).contentDisposition(new ContentDisposition("form-data; filename=\"archiva-model.jar\"; name=\"files[]\"")).build(); + MultipartBody body = new MultipartBody(fileAttachment); + service.post(body); + service.save("internal", "org.apache.archiva", "archiva-model", "1.2", "jar", true); } } diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/resources/spring-context-test-upload.xml b/archiva-modules/archiva-web/archiva-web-common/src/test/resources/spring-context-test-upload.xml index 5109a9be2..d1adbce1b 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/resources/spring-context-test-upload.xml +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/resources/spring-context-test-upload.xml @@ -31,7 +31,7 @@ + base-package="org.apache.archiva.repository.content.maven2,org.apache.archiva.redback.keys,org.apache.archiva.rest.services.utils"/> -- 2.39.5