Search in sources :

Example 46 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class DefaultFileUploadService method saveFile.

protected void saveFile(String repositoryId, FileMetadata fileMetadata, boolean generatePom, String groupId, String artifactId, String version, String packaging) throws ArchivaRestServiceException {
    try {
        ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository(repositoryId);
        ArtifactReference artifactReference = new ArtifactReference();
        artifactReference.setArtifactId(artifactId);
        artifactReference.setGroupId(groupId);
        artifactReference.setVersion(version);
        artifactReference.setClassifier(fileMetadata.getClassifier());
        artifactReference.setType(StringUtils.isEmpty(fileMetadata.getPackaging()) ? packaging : fileMetadata.getPackaging());
        ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent(repositoryId);
        String artifactPath = repository.toPath(artifactReference);
        int lastIndex = artifactPath.lastIndexOf('/');
        String path = artifactPath.substring(0, lastIndex);
        Path targetPath = Paths.get(repoConfig.getLocation(), path);
        log.debug("artifactPath: {} found targetPath: {}", artifactPath, targetPath);
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        int newBuildNumber = -1;
        String timestamp = null;
        Path versionMetadataFile = targetPath.resolve(MetadataTools.MAVEN_METADATA);
        ArchivaRepositoryMetadata versionMetadata = getMetadata(versionMetadataFile);
        if (VersionUtil.isSnapshot(version)) {
            TimeZone timezone = TimeZone.getTimeZone("UTC");
            DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
            fmt.setTimeZone(timezone);
            timestamp = fmt.format(lastUpdatedTimestamp);
            if (versionMetadata.getSnapshotVersion() != null) {
                newBuildNumber = versionMetadata.getSnapshotVersion().getBuildNumber() + 1;
            } else {
                newBuildNumber = 1;
            }
        }
        if (!Files.exists(targetPath)) {
            Files.createDirectories(targetPath);
        }
        String filename = artifactPath.substring(lastIndex + 1);
        if (VersionUtil.isSnapshot(version)) {
            filename = filename.replaceAll(VersionUtil.SNAPSHOT, timestamp + "-" + newBuildNumber);
        }
        boolean fixChecksums = !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
        try {
            Path targetFile = targetPath.resolve(filename);
            if (Files.exists(targetFile) && !VersionUtil.isSnapshot(version) && repoConfig.isBlockRedeployments()) {
                throw new ArchivaRestServiceException("Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed.", Response.Status.BAD_REQUEST.getStatusCode(), null);
            } else {
                copyFile(Paths.get(fileMetadata.getServerFileName()), targetPath, filename, fixChecksums);
                triggerAuditEvent(repository.getId(), path + "/" + filename, AuditEvent.UPLOAD_FILE);
                queueRepositoryTask(repository.getId(), targetFile);
            }
        } catch (IOException ie) {
            log.error("IOException copying file: {}", ie.getMessage(), ie);
            throw new ArchivaRestServiceException("Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed.", Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
        }
        if (generatePom) {
            String pomFilename = filename;
            if (StringUtils.isNotEmpty(fileMetadata.getClassifier())) {
                pomFilename = StringUtils.remove(pomFilename, "-" + fileMetadata.getClassifier());
            }
            pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
            try {
                Path generatedPomFile = createPom(targetPath, pomFilename, fileMetadata, groupId, artifactId, version, packaging);
                triggerAuditEvent(repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE);
                if (fixChecksums) {
                    fixChecksums(generatedPomFile);
                }
                queueRepositoryTask(repoConfig.getId(), generatedPomFile);
            } catch (IOException ie) {
                throw new ArchivaRestServiceException("Error encountered while writing pom file: " + ie.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
            }
        }
        // explicitly update only if metadata-updater consumer is not enabled!
        if (!archivaAdministration.getKnownContentConsumers().contains("metadata-updater")) {
            updateProjectMetadata(targetPath.toAbsolutePath().toString(), lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, fileMetadata, groupId, artifactId, version, packaging);
            if (VersionUtil.isSnapshot(version)) {
                updateVersionMetadata(versionMetadata, versionMetadataFile, lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, fileMetadata, groupId, artifactId, version, packaging);
            }
        }
    } catch (RepositoryNotFoundException re) {
        throw new ArchivaRestServiceException("Target repository cannot be found: " + re.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), re);
    } catch (RepositoryException rep) {
        throw new ArchivaRestServiceException("Repository exception: " + rep.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep);
    } catch (RepositoryAdminException e) {
        throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } catch (IOException e) {
        throw new ArchivaRestServiceException("Repository exception " + e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
}
Also used : Path(java.nio.file.Path) ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Date(java.util.Date) TimeZone(java.util.TimeZone) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) ArtifactReference(org.apache.archiva.model.ArtifactReference) SimpleDateFormat(java.text.SimpleDateFormat)

Example 47 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class DefaultFileUploadService method savePomFile.

protected void savePomFile(String repositoryId, FileMetadata fileMetadata, String groupId, String artifactId, String version, String packaging) throws ArchivaRestServiceException {
    try {
        boolean fixChecksums = !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
        ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository(repositoryId);
        ArtifactReference artifactReference = new ArtifactReference();
        artifactReference.setArtifactId(artifactId);
        artifactReference.setGroupId(groupId);
        artifactReference.setVersion(version);
        artifactReference.setClassifier(fileMetadata.getClassifier());
        artifactReference.setType(packaging);
        ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent(repositoryId);
        String artifactPath = repository.toPath(artifactReference);
        int lastIndex = artifactPath.lastIndexOf('/');
        String path = artifactPath.substring(0, lastIndex);
        Path targetPath = Paths.get(repoConfig.getLocation(), path);
        String pomFilename = artifactPath.substring(lastIndex + 1);
        if (StringUtils.isNotEmpty(fileMetadata.getClassifier())) {
            pomFilename = StringUtils.remove(pomFilename, "-" + fileMetadata.getClassifier());
        }
        pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
        copyFile(Paths.get(fileMetadata.getServerFileName()), targetPath, pomFilename, fixChecksums);
        triggerAuditEvent(repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE);
        queueRepositoryTask(repoConfig.getId(), targetPath.resolve(pomFilename));
    } catch (IOException ie) {
        throw new ArchivaRestServiceException("Error encountered while uploading pom file: " + ie.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
    } catch (RepositoryException rep) {
        throw new ArchivaRestServiceException("Repository exception: " + rep.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep);
    } catch (RepositoryAdminException e) {
        throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
}
Also used : Path(java.nio.file.Path) ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 48 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class ChecksumTransferTest method testGetChecksumNoSha1CorrectMd5.

@Test
public void testGetChecksumNoSha1CorrectMd5() throws Exception {
    String path = "org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    FileUtils.deleteDirectory(expectedFile.getParent());
    assertFalse(Files.exists(expectedFile));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    Path proxied1File = Paths.get(REPOPATH_PROXIED1, path);
    assertFileEquals(expectedFile, downloadedFile, proxied1File);
    assertNoTempFiles(expectedFile);
    assertChecksums(expectedFile, null, "f3af5201bf8da801da37db8842846e1c *get-checksum-md5-only-1.0.jar");
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) Test(org.junit.Test)

Example 49 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class ChecksumTransferTest method testGetChecksumCorrectSha1BadMd5UsingFailSetting.

@Test
public void testGetChecksumCorrectSha1BadMd5UsingFailSetting() throws Exception {
    String path = "org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    FileUtils.deleteDirectory(expectedFile.getParent());
    assertFalse(Files.exists(expectedFile));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FAIL, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    assertNotDownloaded(downloadedFile);
    assertChecksums(expectedFile, null, null);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) Test(org.junit.Test)

Example 50 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class ChecksumTransferTest method testGetChecksumNotFoundOnRemote.

@Test
public void testGetChecksumNotFoundOnRemote() throws Exception {
    String path = "org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    FileUtils.deleteDirectory(expectedFile.getParent());
    assertFalse(Files.exists(expectedFile.getParent()));
    assertFalse(Files.exists(expectedFile));
    saveRemoteRepositoryConfig("badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default");
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, "badproxied", ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false);
    wagonMock.get(EasyMock.eq(path), EasyMock.anyObject(File.class));
    EasyMock.expectLastCall().once();
    wagonMock.get(EasyMock.eq(path + ".sha1"), EasyMock.anyObject(File.class));
    EasyMock.expectLastCall().once();
    wagonMock.get(EasyMock.eq(path + ".md5"), EasyMock.anyObject(File.class));
    EasyMock.expectLastCall().andThrow(new ResourceDoesNotExistException("Resource does not exist.")).once();
    wagonMockControl.replay();
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    wagonMockControl.verify();
    // Do what the mock doesn't do.
    Path proxyPath = Paths.get(REPOPATH_PROXIED1, path).toAbsolutePath();
    Path localPath = managedDefaultDir.resolve(path).toAbsolutePath();
    Files.copy(proxyPath, localPath, StandardCopyOption.REPLACE_EXISTING);
    Files.copy(proxyPath.resolveSibling(proxyPath.getFileName() + ".sha1"), localPath.resolveSibling(localPath.getFileName() + ".sha1"), StandardCopyOption.REPLACE_EXISTING);
    // Test results.
    Path proxied1File = Paths.get(REPOPATH_PROXIED1, path);
    assertFileEquals(expectedFile, downloadedFile, proxied1File);
    assertNoTempFiles(expectedFile);
    assertChecksums(expectedFile, "748a3a013bf5eacf2bbb40a2ac7d37889b728837 *get-checksum-sha1-only-1.0.jar", null);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) File(java.io.File) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) Test(org.junit.Test)

Aggregations

ArtifactReference (org.apache.archiva.model.ArtifactReference)77 Path (java.nio.file.Path)62 Test (org.junit.Test)50 LayoutException (org.apache.archiva.repository.LayoutException)13 IOException (java.io.IOException)10 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)9 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)8 RepositoryException (org.apache.archiva.repository.RepositoryException)8 VersionedReference (org.apache.archiva.model.VersionedReference)7 File (java.io.File)5 Date (java.util.Date)5 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)5 RepositoryTask (org.apache.archiva.scheduler.repository.model.RepositoryTask)5 ResourceDoesNotExistException (org.apache.maven.wagon.ResourceDoesNotExistException)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)4 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)4 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)4 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)4