Search in sources :

Example 1 with ArchivaArtifact

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

the class DefaultBrowseService method artifactAvailable.

@Override
public Boolean 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) {
            ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repoId);
            if ((snapshot && !managedRepository.isSnapshots()) || (!snapshot && managedRepository.isSnapshots())) {
                continue;
            }
            ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
            // FIXME default to jar which can be wrong for war zip etc....
            ArchivaArtifact archivaArtifact = new ArchivaArtifact(groupId, artifactId, version, StringUtils.isEmpty(classifier) ? "" : classifier, "jar", repoId);
            Path file = managedRepositoryContent.toFile(archivaArtifact);
            if (file != null && Files.exists(file)) {
                return true;
            }
            // in case of SNAPSHOT we can have timestamped version locally !
            if (StringUtils.endsWith(version, VersionUtil.SNAPSHOT)) {
                Path metadataFile = file.getParent().resolve(MetadataTools.MAVEN_METADATA);
                if (Files.exists(metadataFile)) {
                    try {
                        ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.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();
                        Path timeStampFile = file.getParent().resolve(timeStampFileName);
                        log.debug("try to find timestamped snapshot version file: {}", timeStampFile.toAbsolutePath());
                        if (Files.exists(timeStampFile)) {
                            return true;
                        }
                    } catch (XMLException e) {
                        log.warn("skip fail to find timestamped snapshot file: {}", e.getMessage());
                    }
                }
            }
            String path = managedRepositoryContent.toPath(archivaArtifact);
            file = connectors.fetchFromProxies(managedRepositoryContent, path);
            if (file != null && Files.exists(file)) {
                // download pom now
                String pomPath = StringUtils.substringBeforeLast(path, ".jar") + ".pom";
                connectors.fetchFromProxies(managedRepositoryContent, pomPath);
                return true;
            }
        }
    } catch (RepositoryAdminException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
    return false;
}
Also used : Path(java.nio.file.Path) ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) XMLException(org.apache.archiva.xml.XMLException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 2 with ArchivaArtifact

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

the class DefaultBrowseService method getArtifactContentEntries.

@Override
public List<ArtifactContentEntry> getArtifactContentEntries(String groupId, String artifactId, String version, String classifier, String type, String path, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    try {
        for (String repoId : selectedRepos) {
            ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
            ArchivaArtifact archivaArtifact = new ArchivaArtifact(groupId, artifactId, version, classifier, StringUtils.isEmpty(type) ? "jar" : type, repoId);
            Path file = managedRepositoryContent.toFile(archivaArtifact);
            if (Files.exists(file)) {
                return readFileEntries(file, path, repoId);
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } catch (RepositoryNotFoundException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
    return Collections.emptyList();
}
Also used : Path(java.nio.file.Path) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException)

Example 3 with ArchivaArtifact

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

the class DefaultBrowseService method getArtifactContentText.

@Override
public ArtifactContent getArtifactContentText(String groupId, String artifactId, String version, String classifier, String type, String path, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    try {
        for (String repoId : selectedRepos) {
            ManagedRepositoryContent managedRepositoryContent = null;
            try {
                managedRepositoryContent = getManagedRepositoryContent(repoId);
            } catch (RepositoryException e) {
                log.error("No repository content found for " + repoId);
                continue;
            }
            ArchivaArtifact archivaArtifact = new ArchivaArtifact(groupId, artifactId, version, classifier, StringUtils.isEmpty(type) ? "jar" : type, repoId);
            Path file = managedRepositoryContent.toFile(archivaArtifact);
            if (!Files.exists(file)) {
                log.debug("file: {} not exists for repository: {} try next repository", file, repoId);
                continue;
            }
            if (StringUtils.isNotBlank(path)) {
                // zip entry of the path -> path must a real file entry of the archive
                JarFile jarFile = new JarFile(file.toFile());
                ZipEntry zipEntry = jarFile.getEntry(path);
                try (InputStream inputStream = jarFile.getInputStream(zipEntry)) {
                    return new ArtifactContent(IOUtils.toString(inputStream, ARTIFACT_CONTENT_ENCODING), repoId);
                } finally {
                    closeQuietly(jarFile);
                }
            }
            return new ArtifactContent(new String(Files.readAllBytes(file), ARTIFACT_CONTENT_ENCODING), repoId);
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
    log.debug("artifact: {}:{}:{}:{}:{} not found", groupId, artifactId, version, classifier, type);
    // 404 ?
    return new ArtifactContent();
}
Also used : Path(java.nio.file.Path) ArtifactContent(org.apache.archiva.rest.api.model.ArtifactContent) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile)

Aggregations

Path (java.nio.file.Path)3 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)3 ArchivaArtifact (org.apache.archiva.model.ArchivaArtifact)3 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)3 RepositoryException (org.apache.archiva.repository.RepositoryException)3 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)1 JarFile (java.util.jar.JarFile)1 ZipEntry (java.util.zip.ZipEntry)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)1 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)1 RepositoryNotFoundException (org.apache.archiva.repository.RepositoryNotFoundException)1 ArtifactContent (org.apache.archiva.rest.api.model.ArtifactContent)1 XMLException (org.apache.archiva.xml.XMLException)1