use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class ArchivaIndexManagerMock method move.
@Override
public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
if (context == null) {
return null;
}
if (context.supports(IndexingContext.class)) {
try {
StorageAsset newPath = getIndexPath(repo);
IndexingContext ctx = context.getBaseContext(IndexingContext.class);
Path oldPath = ctx.getIndexDirectoryFile().toPath();
if (oldPath.equals(newPath)) {
// Nothing to do, if path does not change
return context;
}
if (!Files.exists(oldPath)) {
return createContext(repo);
} else if (context.isEmpty()) {
context.close();
return createContext(repo);
} else {
context.close(false);
Files.move(oldPath, newPath.getFilePath());
return createContext(repo);
}
} catch (IOException e) {
log.error("IOException while moving index directory {}", e.getMessage(), e);
throw new IndexCreationFailedException("Could not recreated the index.", e);
} catch (UnsupportedBaseContextException e) {
throw new IndexCreationFailedException("The given context, is not a maven context.");
}
} else {
throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
}
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class DefaultIndexMerger method cleanTemporaryGroupIndex.
@Async
@Override
public void cleanTemporaryGroupIndex(TemporaryGroupIndex temporaryGroupIndex) {
if (temporaryGroupIndex == null) {
return;
}
try {
Optional<ArchivaIndexingContext> ctxOpt = temporaryContextes.stream().filter(ctx -> ctx.getId().equals(temporaryGroupIndex.getIndexId())).findFirst();
if (ctxOpt.isPresent()) {
ArchivaIndexingContext ctx = ctxOpt.get();
ctx.close(true);
temporaryGroupIndexes.remove(temporaryGroupIndex);
temporaryContextes.remove(ctx);
StorageAsset directory = temporaryGroupIndex.getDirectory();
if (directory != null && directory.exists()) {
org.apache.archiva.repository.storage.util.StorageUtil.deleteRecursively(directory);
}
}
} catch (IOException e) {
log.warn("fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e);
}
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class MetadataTools method getMetadatasForManagedRepository.
private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository(ManagedRepositoryContent managedRepository, String logicalResource) {
List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
StorageAsset file = managedRepository.getRepository().getAsset(logicalResource);
if (file.exists()) {
ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, file);
if (existingMetadata != null) {
metadatas.add(existingMetadata);
}
}
Set<String> proxyIds = proxies.get(managedRepository.getId());
if (proxyIds != null) {
for (String proxyId : proxyIds) {
ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, logicalResource, proxyId);
if (proxyMetadata != null) {
metadatas.add(proxyMetadata);
}
}
}
return metadatas;
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class DefaultRepositoriesService method copyArtifact.
@Override
public ActionStatus copyArtifact(ArtifactTransferRequest artifactTransferRequest) throws ArchivaRestServiceException {
// check parameters
String userName = getAuditInformation().getUser().getUsername();
if (StringUtils.isBlank(userName)) {
throw new ArchivaRestServiceException("copyArtifact call: userName not found", null);
}
if (StringUtils.isBlank(artifactTransferRequest.getRepositoryId())) {
throw new ArchivaRestServiceException("copyArtifact call: sourceRepositoryId cannot be null", null);
}
if (StringUtils.isBlank(artifactTransferRequest.getTargetRepositoryId())) {
throw new ArchivaRestServiceException("copyArtifact call: targetRepositoryId cannot be null", null);
}
ManagedRepository source = null;
source = repositoryRegistry.getManagedRepository(artifactTransferRequest.getRepositoryId());
if (source == null) {
throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null);
}
ManagedRepository target = null;
target = repositoryRegistry.getManagedRepository(artifactTransferRequest.getTargetRepositoryId());
if (target == null) {
throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null);
}
if (StringUtils.isBlank(artifactTransferRequest.getGroupId())) {
throw new ArchivaRestServiceException("groupId is mandatory", null);
}
if (StringUtils.isBlank(artifactTransferRequest.getArtifactId())) {
throw new ArchivaRestServiceException("artifactId is mandatory", null);
}
if (StringUtils.isBlank(artifactTransferRequest.getVersion())) {
throw new ArchivaRestServiceException("version is mandatory", null);
}
if (VersionUtil.isSnapshot(artifactTransferRequest.getVersion())) {
throw new ArchivaRestServiceException("copy of SNAPSHOT not supported", null);
}
// end check parameters
User user = null;
try {
user = securitySystem.getUserManager().findUser(userName);
} catch (UserNotFoundException e) {
throw new ArchivaRestServiceException("user " + userName + " not found", e);
} catch (UserManagerException e) {
throw new ArchivaRestServiceException("ArchivaRestServiceException:" + e.getMessage(), e);
}
// check karma on source : read
AuthenticationResult authn = new AuthenticationResult(true, userName, null);
SecuritySession securitySession = new DefaultSecuritySession(authn, user);
try {
boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_READ_REPOSITORY, artifactTransferRequest.getRepositoryId());
if (!authz) {
throw new ArchivaRestServiceException("not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null);
}
} catch (AuthorizationException e) {
log.error("error reading permission: {}", e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
}
// check karma on target: write
try {
boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_ADD_ARTIFACT, artifactTransferRequest.getTargetRepositoryId());
if (!authz) {
throw new ArchivaRestServiceException("not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null);
}
} catch (AuthorizationException e) {
log.error("error reading permission: {}", e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
}
// sounds good we can continue !
String packaging = StringUtils.trim(artifactTransferRequest.getPackaging());
ItemSelector selector = ArchivaItemSelector.builder().withProjectId(artifactTransferRequest.getArtifactId()).withArtifactId(artifactTransferRequest.getArtifactId()).withNamespace(artifactTransferRequest.getGroupId()).withArtifactVersion(artifactTransferRequest.getVersion()).withClassifier(artifactTransferRequest.getClassifier()).withExtension(StringUtils.isEmpty(packaging) ? "jar" : packaging).build();
try {
ManagedRepositoryContent sourceRepository = getManagedRepositoryContent(artifactTransferRequest.getRepositoryId());
BaseRepositoryContentLayout layout = sourceRepository.getLayout(BaseRepositoryContentLayout.class);
// String artifactSourcePath = sourceRepository.toPath( selector );
org.apache.archiva.repository.content.Artifact sourceArtifact = layout.getArtifact(selector);
if (!sourceArtifact.exists()) {
log.error("cannot find artifact {}", artifactTransferRequest);
throw new ArchivaRestServiceException("cannot find artifact " + artifactTransferRequest.toString(), null);
}
StorageAsset artifactFile = sourceArtifact.getAsset();
ManagedRepositoryContent targetRepository = getManagedRepositoryContent(artifactTransferRequest.getTargetRepositoryId());
String artifactPath = artifactFile.getPath();
int lastIndex = artifactPath.lastIndexOf('/');
String path = artifactPath.substring(0, lastIndex);
StorageAsset targetDir = target.getAsset(path);
Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
int newBuildNumber = 1;
String timestamp = null;
StorageAsset versionMetadataFile = target.getAsset(path + "/" + MetadataTools.MAVEN_METADATA);
/* unused */
getMetadata(targetRepository.getRepository().getType(), versionMetadataFile);
if (!targetDir.exists()) {
targetDir = target.addAsset(targetDir.getPath(), true);
targetDir.create();
}
String filename = artifactPath.substring(lastIndex + 1);
boolean fixChecksums = !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
StorageAsset targetFile = target.getAsset(targetDir.getPath() + "/" + filename);
if (targetFile.exists() && target.blocksRedeployments()) {
throw new ArchivaRestServiceException("artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId() + " and redeployment blocked", null);
} else {
copyFile(artifactFile, targetFile, fixChecksums);
queueRepositoryTask(target.getId(), targetFile);
}
// copy source pom to target repo
String pomFilename = filename;
if (StringUtils.isNotBlank(artifactTransferRequest.getClassifier())) {
pomFilename = StringUtils.remove(pomFilename, "-" + artifactTransferRequest.getClassifier());
}
pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
StorageAsset pomFile = source.getAsset(artifactPath.substring(0, artifactPath.lastIndexOf('/')) + "/" + pomFilename);
if (pomFile != null && pomFile.exists()) {
StorageAsset targetPomFile = target.getAsset(targetDir.getPath() + "/" + pomFilename);
copyFile(pomFile, targetPomFile, fixChecksums);
queueRepositoryTask(target.getId(), targetPomFile);
}
// explicitly update only if metadata-updater consumer is not enabled!
if (!archivaAdministration.getKnownContentConsumers().contains("metadata-updater")) {
updateProjectMetadata(target.getType(), target, targetDir, lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, artifactTransferRequest);
}
String msg = "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId() + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'" + artifactTransferRequest.getTargetRepositoryId() + "\'";
log.debug("copyArtifact {}", msg);
} catch (RepositoryException | LayoutException e) {
log.error("RepositoryException: {}", e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
} catch (RepositoryAdminException e) {
log.error("RepositoryAdminException: {}", e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
} catch (IOException e) {
log.error("IOException: {}", e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class DefaultRepositoriesService method updateProjectMetadata.
private void updateProjectMetadata(RepositoryType repositoryType, RepositoryStorage storage, StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber, boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest) throws RepositoryMetadataException {
List<String> availableVersions = new ArrayList<>();
String latestVersion = artifactTransferRequest.getVersion();
StorageAsset projectDir = targetPath.getParent();
StorageAsset projectMetadataFile = storage.getAsset(projectDir.getPath() + "/" + MetadataTools.MAVEN_METADATA);
ArchivaRepositoryMetadata projectMetadata = getMetadata(repositoryType, projectMetadataFile);
if (projectMetadataFile.exists()) {
availableVersions = projectMetadata.getAvailableVersions();
Collections.sort(availableVersions, VersionComparator.getInstance());
if (!availableVersions.contains(artifactTransferRequest.getVersion())) {
availableVersions.add(artifactTransferRequest.getVersion());
}
latestVersion = availableVersions.get(availableVersions.size() - 1);
} else {
availableVersions.add(artifactTransferRequest.getVersion());
projectMetadata.setGroupId(artifactTransferRequest.getGroupId());
projectMetadata.setArtifactId(artifactTransferRequest.getArtifactId());
}
if (projectMetadata.getGroupId() == null) {
projectMetadata.setGroupId(artifactTransferRequest.getGroupId());
}
if (projectMetadata.getArtifactId() == null) {
projectMetadata.setArtifactId(artifactTransferRequest.getArtifactId());
}
projectMetadata.setLatestVersion(latestVersion);
projectMetadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
projectMetadata.setAvailableVersions(availableVersions);
if (!VersionUtil.isSnapshot(artifactTransferRequest.getVersion())) {
projectMetadata.setReleasedVersion(latestVersion);
}
try (OutputStreamWriter writer = new OutputStreamWriter(projectMetadataFile.getWriteStream(true))) {
RepositoryMetadataWriter.write(projectMetadata, writer);
} catch (IOException e) {
throw new RepositoryMetadataException(e);
}
if (fixChecksums) {
fixChecksums(projectMetadataFile);
}
}
Aggregations