use of org.apache.archiva.model.ArtifactReference in project archiva by apache.
the class MetadataTools method getFirstArtifact.
/**
* Get the first Artifact found in the provided VersionedReference location.
*
* @param managedRepository the repository to search within.
* @param reference the reference to the versioned reference to search within
* @return the ArtifactReference to the first artifact located within the versioned reference. or null if
* no artifact was found within the versioned reference.
* @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
* @throws LayoutException
*/
public ArtifactReference getFirstArtifact(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, IOException {
String path = toPath(reference);
int idx = path.lastIndexOf('/');
if (idx > 0) {
path = path.substring(0, idx);
}
Path repoDir = Paths.get(managedRepository.getRepoRoot(), path);
if (!Files.exists(repoDir)) {
throw new IOException("Unable to gather the list of snapshot versions on a non-existant directory: " + repoDir.toAbsolutePath());
}
if (!Files.isDirectory(repoDir)) {
throw new IOException("Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath());
}
try (Stream<Path> stream = Files.list(repoDir)) {
String result = stream.filter(Files::isRegularFile).map(path1 -> PathUtil.getRelative(managedRepository.getRepoRoot(), path1)).filter(filetypes::matchesArtifactPattern).findFirst().orElse(null);
if (result != null) {
return managedRepository.toArtifactReference(result);
}
}
// No artifact was found.
return null;
}
use of org.apache.archiva.model.ArtifactReference in project archiva by apache.
the class ArchivaRepositoryScanningTaskExecutorPhase2Test method testExecutorScanOnlyNewArtifacts.
@Test
public void testExecutorScanOnlyNewArtifacts() throws Exception {
RepositoryTask repoTask = new RepositoryTask();
repoTask.setRepositoryId(TEST_REPO_ID);
repoTask.setScanAll(false);
createAndSaveTestStats();
taskExecutor.executeTask(repoTask);
// check no artifacts processed
Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
assertNotNull(unprocessedResultList);
assertEquals("Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0, unprocessedResultList.size());
// check correctness of new stats
RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics(metadataRepository, TEST_REPO_ID);
assertEquals(0, newStats.getNewFileCount());
assertEquals(31, newStats.getTotalFileCount());
// FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
// assertEquals( 8, newStats.getTotalArtifactCount() );
// assertEquals( 3, newStats.getTotalGroupCount() );
// assertEquals( 5, newStats.getTotalProjectCount() );
// assertEquals( 14159, newStats.getTotalArtifactFileSize() );
Path newArtifactGroup = repoDir.resolve("org/apache/archiva");
assertFalse("newArtifactGroup should not exist.", Files.exists(newArtifactGroup));
FileUtils.copyDirectoryStructure(Paths.get("target/test-classes/test-repo/org/apache/archiva").toFile(), newArtifactGroup.toFile());
// update last modified date
Files.setLastModifiedTime(newArtifactGroup.resolve("archiva-index-methods-jar-test/1.0/pom.xml"), FileTime.fromMillis(Calendar.getInstance().getTimeInMillis() + 1000));
Files.setLastModifiedTime(newArtifactGroup.resolve("archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar"), FileTime.fromMillis(Calendar.getInstance().getTimeInMillis() + 1000));
assertTrue(Files.exists(newArtifactGroup));
taskExecutor.executeTask(repoTask);
unprocessedResultList = testConsumer.getConsumed();
assertNotNull(unprocessedResultList);
assertEquals("Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1, unprocessedResultList.size());
// check correctness of new stats
RepositoryStatistics updatedStats = repositoryStatisticsManager.getLastStatistics(metadataRepository, TEST_REPO_ID);
assertEquals(2, updatedStats.getNewFileCount());
assertEquals(33, updatedStats.getTotalFileCount());
// FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
// assertEquals( 8, newStats.getTotalArtifactCount() );
// assertEquals( 3, newStats.getTotalGroupCount() );
// assertEquals( 5, newStats.getTotalProjectCount() );
// assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
}
use of org.apache.archiva.model.ArtifactReference in project archiva by apache.
the class ArchivaRepositoryScanningTaskExecutorPhase2Test method testExecutorScanOnlyNewArtifactsChangeTimes.
@Test
public void testExecutorScanOnlyNewArtifactsChangeTimes() throws Exception {
RepositoryTask repoTask = new RepositoryTask();
repoTask.setRepositoryId(TEST_REPO_ID);
repoTask.setScanAll(false);
createAndSaveTestStats();
Path newArtifactGroup = repoDir.resolve("org/apache/archiva");
assertFalse("newArtifactGroup should not exist.", Files.exists(newArtifactGroup));
FileUtils.copyDirectoryStructure(Paths.get("target/test-classes/test-repo/org/apache/archiva").toFile(), newArtifactGroup.toFile());
// update last modified date, placing shortly after last scan
Files.setLastModifiedTime(newArtifactGroup.resolve("archiva-index-methods-jar-test/1.0/pom.xml"), FileTime.fromMillis(Calendar.getInstance().getTimeInMillis() + 1000));
Files.setLastModifiedTime(newArtifactGroup.resolve("archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar"), FileTime.fromMillis(Calendar.getInstance().getTimeInMillis() + 1000));
assertTrue(Files.exists(newArtifactGroup));
// scan using the really long previous duration
taskExecutor.executeTask(repoTask);
// check no artifacts processed
Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
assertNotNull(unprocessedResultList);
assertEquals("Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1, unprocessedResultList.size());
// check correctness of new stats
RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics(metadataRepository, TEST_REPO_ID);
assertEquals(2, newStats.getNewFileCount());
assertEquals(33, newStats.getTotalFileCount());
// FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
// assertEquals( 8, newStats.getTotalArtifactCount() );
// assertEquals( 3, newStats.getTotalGroupCount() );
// assertEquals( 5, newStats.getTotalProjectCount() );
// assertEquals( 19301, newStats.getTotalArtifactFileSize() );
}
use of org.apache.archiva.model.ArtifactReference in project archiva by apache.
the class ArchivaDavResourceFactory method fetchContentFromProxies.
private boolean fetchContentFromProxies(ManagedRepositoryContent managedRepository, DavServletRequest request, LogicalResource resource) throws DavException {
String path = resource.getPath();
if (repositoryRequest.isSupportFile(path)) {
Path proxiedFile = connectors.fetchFromProxies(managedRepository, path);
return (proxiedFile != null);
}
// Is it a Metadata resource?
if (repositoryRequest.isDefault(path) && repositoryRequest.isMetadata(path)) {
return connectors.fetchMetadataFromProxies(managedRepository, path).isModified();
}
// Is it an Archetype Catalog?
if (repositoryRequest.isArchetypeCatalog(path)) {
// FIXME we must implement a merge of remote archetype catalog from remote servers.
Path proxiedFile = connectors.fetchFromProxies(managedRepository, path);
return (proxiedFile != null);
}
// Not any of the above? Then it's gotta be an artifact reference.
try {
// Get the artifact reference in a layout neutral way.
ArtifactReference artifact = repositoryRequest.toArtifactReference(path);
if (artifact != null) {
String repositoryLayout = managedRepository.getRepository().getLayout();
RepositoryStorage repositoryStorage = this.applicationContext.getBean("repositoryStorage#" + repositoryLayout, RepositoryStorage.class);
repositoryStorage.applyServerSideRelocation(managedRepository, artifact);
Path proxiedFile = connectors.fetchFromProxies(managedRepository, artifact);
resource.setPath(managedRepository.toPath(artifact));
log.debug("Proxied artifact '{}:{}:{}'", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
return (proxiedFile != null);
}
} catch (LayoutException e) {
/* eat it */
} catch (ProxyDownloadException e) {
log.error(e.getMessage(), e);
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to fetch artifact resource.");
}
return false;
}
use of org.apache.archiva.model.ArtifactReference in project archiva by apache.
the class ArchivaDavResourceFactory method processRepository.
private DavResource processRepository(final DavServletRequest request, ArchivaDavResourceLocator archivaLocator, String activePrincipal, ManagedRepositoryContent managedRepositoryContent, org.apache.archiva.repository.ManagedRepository managedRepository) throws DavException {
DavResource resource = null;
if (isAuthorized(request, managedRepositoryContent.getId())) {
boolean readMethod = WebdavMethodUtil.isReadMethod(request.getMethod());
// Maven Centric part ask evaluation if -SNAPSHOT
// MRM-1846 test if read method to prevent issue with maven 2.2.1 and uniqueVersion false
String path = readMethod ? evaluatePathWithVersion(archivaLocator, managedRepositoryContent, request.getContextPath()) : getLogicalResource(archivaLocator, managedRepository, false);
if (path.startsWith("/")) {
path = path.substring(1);
}
LogicalResource logicalResource = new LogicalResource(path);
Path resourceFile = Paths.get(managedRepositoryContent.getRepoRoot(), path);
resource = new ArchivaDavResource(resourceFile.toAbsolutePath().toString(), path, managedRepositoryContent.getRepository(), request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
if (WebdavMethodUtil.isReadMethod(request.getMethod())) {
if (archivaLocator.getHref(false).endsWith("/") && !Files.isDirectory(resourceFile)) {
// force a resource not found
throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
} else {
if (!resource.isCollection()) {
boolean previouslyExisted = Files.exists(resourceFile);
boolean fromProxy = fetchContentFromProxies(managedRepositoryContent, request, logicalResource);
// legacy layout format.
try {
// Perform an adjustment of the resource to the managed
// repository expected path.
String localResourcePath = repositoryRequest.toNativePath(logicalResource.getPath(), managedRepositoryContent);
resourceFile = Paths.get(managedRepositoryContent.getRepoRoot(), localResourcePath);
resource = new ArchivaDavResource(resourceFile.toAbsolutePath().toString(), logicalResource.getPath(), managedRepositoryContent.getRepository(), request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
} catch (LayoutException e) {
if (!Files.exists(resourceFile)) {
throw new DavException(HttpServletResponse.SC_NOT_FOUND, e);
}
}
if (fromProxy) {
String action = (previouslyExisted ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE) + PROXIED_SUFFIX;
log.debug("Proxied artifact '{}' in repository '{}' (current user '{}')", resourceFile.getFileName(), managedRepositoryContent.getId(), activePrincipal);
triggerAuditEvent(request.getRemoteAddr(), archivaLocator.getRepositoryId(), logicalResource.getPath(), action, activePrincipal);
}
if (!Files.exists(resourceFile)) {
throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
}
}
}
}
if (request.getMethod().equals(HTTP_PUT_METHOD)) {
String resourcePath = logicalResource.getPath();
// we suppose that release-artifacts can be deployed only to repos enabled for releases
if (managedRepositoryContent.getRepository().getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !repositoryRequest.isMetadata(resourcePath) && !repositoryRequest.isSupportFile(resourcePath)) {
ArtifactReference artifact = null;
try {
artifact = managedRepositoryContent.toArtifactReference(resourcePath);
if (!VersionUtil.isSnapshot(artifact.getVersion())) {
// check if artifact already exists and if artifact re-deployment to the repository is allowed
if (managedRepositoryContent.hasContent(artifact) && managedRepositoryContent.getRepository().blocksRedeployments()) {
log.warn("Overwriting released artifacts in repository '{}' is not allowed.", managedRepositoryContent.getId());
throw new DavException(HttpServletResponse.SC_CONFLICT, "Overwriting released artifacts is not allowed.");
}
}
} catch (LayoutException e) {
log.warn("Artifact path '{}' is invalid.", resourcePath);
}
}
/*
* Create parent directories that don't exist when writing a file This actually makes this
* implementation not compliant to the WebDAV RFC - but we have enough knowledge about how the
* collection is being used to do this reasonably and some versions of Maven's WebDAV don't correctly
* create the collections themselves.
*/
Path rootDirectory = Paths.get(managedRepositoryContent.getRepoRoot());
Path destDir = rootDirectory.resolve(logicalResource.getPath()).getParent();
if (!Files.exists(destDir)) {
try {
Files.createDirectories(destDir);
} catch (IOException e) {
log.error("Could not create directory {}: {}", destDir, e.getMessage(), e);
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create directory " + destDir);
}
String relPath = PathUtil.getRelative(rootDirectory.toAbsolutePath().toString(), destDir);
log.debug("Creating destination directory '{}' (current user '{}')", destDir.getFileName(), activePrincipal);
triggerAuditEvent(request.getRemoteAddr(), managedRepositoryContent.getId(), relPath, AuditEvent.CREATE_DIR, activePrincipal);
}
}
}
return resource;
}
Aggregations