Search in sources :

Example 1 with MetadataReader

use of org.apache.archiva.repository.metadata.MetadataReader in project archiva by apache.

the class DefaultBrowseService method artifactAvailable.

@Override
public AvailabilityStatus artifactAvailable(String groupId, String artifactId, String version, String classifier, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    boolean snapshot = VersionUtil.isSnapshot(version);
    try {
        for (String repoId : selectedRepos) {
            org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repoId);
            if (!proxyRegistry.hasHandler(managedRepo.getType())) {
                throw new RepositoryException("No proxy handler found for repository type " + managedRepo.getType());
            }
            RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(managedRepo.getType()).get(0);
            if ((snapshot && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT)) || (!snapshot && managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT))) {
                continue;
            }
            ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
            // FIXME default to jar which can be wrong for war zip etc....
            ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(StringUtils.isEmpty(classifier) ? "" : classifier).withType("jar").withArtifactId(artifactId).build();
            org.apache.archiva.repository.content.Artifact archivaArtifact = managedRepositoryContent.getItem(itemSelector).adapt(org.apache.archiva.repository.content.Artifact.class);
            StorageAsset file = archivaArtifact.getAsset();
            if (file != null && file.exists()) {
                return new AvailabilityStatus(true);
            }
            // in case of SNAPSHOT we can have timestamped version locally !
            if (StringUtils.endsWith(version, VersionUtil.SNAPSHOT)) {
                StorageAsset metadataFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + MetadataTools.MAVEN_METADATA);
                if (metadataFile.exists()) {
                    MetadataReader metadataReader = repositoryRegistry.getMetadataReader(managedRepositoryContent.getRepository().getType());
                    ArchivaRepositoryMetadata archivaRepositoryMetadata = metadataReader.read(metadataFile);
                    int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
                    String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
                    // rebuild file name with timestamped version and build number
                    String timeStampFileName = // 
                    new StringBuilder(artifactId).append('-').append(// 
                    StringUtils.remove(version, "-" + VersionUtil.SNAPSHOT)).append('-').append(// 
                    timeStamp).append('-').append(// 
                    Integer.toString(buildNumber)).append(// 
                    (StringUtils.isEmpty(classifier) ? "" : "-" + classifier)).append(".jar").toString();
                    StorageAsset timeStampFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + timeStampFileName);
                    log.debug("try to find timestamped snapshot version file: {}", timeStampFile.getPath());
                    if (timeStampFile.exists()) {
                        return new AvailabilityStatus(true);
                    }
                }
            }
            String path = managedRepositoryContent.toPath(archivaArtifact);
            file = proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), path);
            if (file != null && file.exists()) {
                // download pom now
                String pomPath = StringUtils.substringBeforeLast(path, ".jar") + ".pom";
                proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), pomPath);
                return new AvailabilityStatus(true);
            }
        }
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
    return new AvailabilityStatus(false);
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) MetadataReader(org.apache.archiva.repository.metadata.MetadataReader) RepositoryException(org.apache.archiva.repository.RepositoryException) org.apache.archiva.metadata.repository(org.apache.archiva.metadata.repository) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler)

Example 2 with MetadataReader

use of org.apache.archiva.repository.metadata.MetadataReader in project archiva by apache.

the class DefaultFileUploadService method getMetadata.

private ArchivaRepositoryMetadata getMetadata(StorageAsset metadataFile) throws RepositoryMetadataException {
    ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
    if (metadataFile.exists()) {
        Repository repo = repositoryRegistry.getRepositoryOfAsset(metadataFile);
        RepositoryType type = repo == null ? RepositoryType.MAVEN : repo.getType();
        MetadataReader metadataReader = repositoryRegistry.getMetadataReader(type);
        metadata = metadataReader.read(metadataFile);
    }
    return metadata;
}
Also used : Repository(org.apache.archiva.repository.Repository) RepositoryType(org.apache.archiva.repository.RepositoryType) MetadataReader(org.apache.archiva.repository.metadata.MetadataReader) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Aggregations

ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)2 MetadataReader (org.apache.archiva.repository.metadata.MetadataReader)2 org.apache.archiva.metadata.repository (org.apache.archiva.metadata.repository)1 RepositoryProxyHandler (org.apache.archiva.proxy.model.RepositoryProxyHandler)1 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)1 Repository (org.apache.archiva.repository.Repository)1 RepositoryException (org.apache.archiva.repository.RepositoryException)1 RepositoryType (org.apache.archiva.repository.RepositoryType)1 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)1 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)1 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)1