Search in sources :

Example 1 with Artifact

use of org.apache.archiva.maven.model.Artifact in project archiva by apache.

the class AbstractRestService method buildArtifacts.

protected List<Artifact> buildArtifacts(Collection<ArtifactMetadata> artifactMetadatas, String repositoryId) throws ArchivaRestServiceException {
    try {
        if (artifactMetadatas != null && !artifactMetadatas.isEmpty()) {
            List<Artifact> artifacts = new ArrayList<>(artifactMetadatas.size());
            for (ArtifactMetadata artifact : artifactMetadatas) {
                String repoId = repositoryId != null ? repositoryId : artifact.getRepositoryId();
                if (repoId == null) {
                    throw new IllegalStateException("Repository Id is null");
                }
                ManagedRepository repo = repositoryRegistry.getManagedRepository(repoId);
                if (repo == null) {
                    throw new RepositoryException("Repository not found " + repoId);
                }
                ManagedRepositoryContent content = repo.getContent();
                ArtifactBuilder builder = new ArtifactBuilder().forArtifactMetadata(artifact).withManagedRepositoryContent(content);
                Artifact art = builder.build();
                art.setUrl(getArtifactUrl(art, repositoryId));
                artifacts.add(art);
            }
            return artifacts;
        }
        return Collections.emptyList();
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
}
Also used : ManagedRepository(org.apache.archiva.repository.ManagedRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ArrayList(java.util.ArrayList) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ArtifactBuilder(org.apache.archiva.rest.services.utils.ArtifactBuilder) Artifact(org.apache.archiva.maven.model.Artifact)

Example 2 with Artifact

use of org.apache.archiva.maven.model.Artifact in project archiva by apache.

the class DefaultSearchService method getArtifactByChecksum.

public List<Artifact> getArtifactByChecksum(ChecksumSearch checksumSearch) throws ArchivaRestServiceException {
    // if no repos set we use ones available for the user
    if (checksumSearch.getRepositories() == null || checksumSearch.getRepositories().isEmpty()) {
        checksumSearch.setRepositories(getObservableRepos());
    }
    RepositorySession repositorySession = null;
    try {
        repositorySession = repositorySessionFactory.createSession();
    } catch (MetadataRepositoryException e) {
        e.printStackTrace();
    }
    MetadataRepository metadataRepository = repositorySession.getRepository();
    Set<Artifact> artifactSet = new HashSet<>();
    try {
        for (String repoId : checksumSearch.getRepositories()) {
            Collection<ArtifactMetadata> artifactMetadatas = metadataRepository.getArtifactsByChecksum(repositorySession, repoId, checksumSearch.getChecksum());
            artifactSet.addAll(buildArtifacts(artifactMetadatas, repoId));
        }
        return new ArrayList<>(artifactSet);
    } catch (MetadataRepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } finally {
        repositorySession.closeQuietly();
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ArrayList(java.util.ArrayList) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Artifact(org.apache.archiva.maven.model.Artifact) HashSet(java.util.HashSet)

Example 3 with Artifact

use of org.apache.archiva.maven.model.Artifact in project archiva by apache.

the class DefaultSearchService method redirectToArtifactFile.

@Override
public Response redirectToArtifactFile(String repositoryId, String groupId, String artifactId, String version, String packaging, String classifier) throws ArchivaRestServiceException {
    try {
        if (StringUtils.isEmpty(groupId)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "groupId mandatory";
                }
            }).build();
        }
        if (StringUtils.isEmpty(version)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "version mandatory";
                }
            }).build();
        }
        if (StringUtils.isEmpty(artifactId)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "artifactId mandatory";
                }
            }).build();
        }
        SearchFields searchField = new SearchFields();
        searchField.setGroupId(groupId);
        searchField.setArtifactId(artifactId);
        searchField.setPackaging(StringUtils.isBlank(packaging) ? "jar" : packaging);
        if (!StringUtils.equals(version, LATEST_KEYWORD)) {
            searchField.setVersion(version);
        }
        searchField.setClassifier(classifier);
        List<String> userRepos = getObservablesRepoIds().getStrings();
        searchField.setRepositories(StringUtils.isEmpty(repositoryId) ? userRepos : Arrays.asList(repositoryId));
        searchField.setExactSearch(true);
        SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, null);
        List<Artifact> artifacts = getArtifacts(searchResults);
        if (artifacts.isEmpty()) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.NO_CONTENT.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.NO_CONTENT.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "your query doesn't return any artifact";
                }
            }).build();
        }
        // so simple loop and retain only artifact with null classifier
        if (classifier == null) {
            List<Artifact> filteredArtifacts = new ArrayList<>(artifacts.size());
            for (Artifact artifact : artifacts) {
                if (artifact.getClassifier() == null) {
                    filteredArtifacts.add(artifact);
                }
            }
            artifacts = filteredArtifacts;
        }
        // TODO return json result of the query ?
        if (artifacts.size() > 1 && !StringUtils.equals(version, LATEST_KEYWORD)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "your query return more than one artifact";
                }
            }).build();
        }
        // version is LATEST so we have to find the latest one from the result
        if (artifacts.size() > 1 && StringUtils.equals(version, LATEST_KEYWORD)) {
            TreeMap<String, Artifact> artifactPerVersion = new TreeMap<>(VersionComparator.getInstance());
            for (Artifact artifact : artifacts) {
                artifactPerVersion.put(artifact.getVersion(), artifact);
            }
            return Response.temporaryRedirect(new URI(artifactPerVersion.lastEntry().getValue().getUrl())).build();
        }
        Artifact artifact = artifacts.get(0);
        return Response.temporaryRedirect(new URI(artifact.getUrl())).build();
    } catch (Exception e) {
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) ArrayList(java.util.ArrayList) SearchResults(org.apache.archiva.indexer.search.SearchResults) TreeMap(java.util.TreeMap) URI(java.net.URI) Artifact(org.apache.archiva.maven.model.Artifact) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException)

Example 4 with Artifact

use of org.apache.archiva.maven.model.Artifact in project archiva by apache.

the class DefaultSearchService method getArtifacts.

// -------------------------------------
// internal
// -------------------------------------
protected List<Artifact> getArtifacts(SearchResults searchResults) throws ArchivaRestServiceException {
    if (searchResults == null || searchResults.isEmpty()) {
        return Collections.emptyList();
    }
    List<Artifact> artifacts = new ArrayList<>(searchResults.getReturnedHitsCount());
    for (SearchResultHit hit : searchResults.getHits()) {
        // duplicate Artifact one per available version
        if (hit.getVersions().size() > 0) {
            for (String version : hit.getVersions()) {
                Artifact versionned = getModelMapper().map(hit, Artifact.class);
                if (StringUtils.isNotBlank(version)) {
                    versionned.setVersion(version);
                    versionned.setUrl(getArtifactUrl(versionned));
                    artifacts.add(versionned);
                }
            }
        }
    }
    return artifacts;
}
Also used : SearchResultHit(org.apache.archiva.indexer.search.SearchResultHit) ArrayList(java.util.ArrayList) Artifact(org.apache.archiva.maven.model.Artifact)

Example 5 with Artifact

use of org.apache.archiva.maven.model.Artifact in project archiva by apache.

the class ArtifactBuilder method build.

public Artifact build() {
    String type = null, classifier = null;
    MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    if (facet != null) {
        type = facet.getType();
        classifier = facet.getClassifier();
    }
    ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactMetadata.getNamespace()).withProjectId(artifactMetadata.getProject()).withVersion(artifactMetadata.getProjectVersion()).withArtifactId(artifactMetadata.getProject()).withArtifactVersion(artifactMetadata.getVersion());
    if (StringUtils.isNotEmpty(type)) {
        selectorBuilder.withType(type);
    }
    if (StringUtils.isNotEmpty(classifier)) {
        selectorBuilder.withClassifier(classifier);
    }
    BaseRepositoryContentLayout layout;
    try {
        layout = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class);
    } catch (LayoutException e) {
        throw new RuntimeException("Could not convert to layout " + e.getMessage());
    }
    org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact(selectorBuilder.build());
    String extension = repoArtifact.getExtension();
    Artifact artifact = new Artifact(repoArtifact.getVersion().getProject().getNamespace().getId(), repoArtifact.getId(), repoArtifact.getArtifactVersion());
    artifact.setRepositoryId(artifactMetadata.getRepositoryId());
    artifact.setClassifier(classifier);
    artifact.setPackaging(type);
    artifact.setType(type);
    artifact.setFileExtension(extension);
    artifact.setPath(managedRepositoryContent.toPath(repoArtifact));
    // TODO: find a reusable formatter for this
    double s = this.artifactMetadata.getSize();
    String symbol = "b";
    if (s > 1024) {
        symbol = "K";
        s /= 1024;
        if (s > 1024) {
            symbol = "M";
            s /= 1024;
            if (s > 1024) {
                symbol = "G";
                s /= 1024;
            }
        }
    }
    artifact.setContext(managedRepositoryContent.getId());
    DecimalFormat df = new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.US));
    artifact.setSize(df.format(s) + " " + symbol);
    artifact.setId(repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType());
    return artifact;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) Artifact(org.apache.archiva.maven.model.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout)

Aggregations

Artifact (org.apache.archiva.maven.model.Artifact)37 Test (org.junit.Test)29 SearchService (org.apache.archiva.rest.api.services.SearchService)14 BrowseService (org.apache.archiva.rest.api.services.BrowseService)11 SearchRequest (org.apache.archiva.rest.api.model.SearchRequest)9 ManagedRepositoriesService (org.apache.archiva.rest.api.services.ManagedRepositoriesService)8 RepositoriesService (org.apache.archiva.rest.api.services.RepositoriesService)8 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)7 Path (java.nio.file.Path)6 ArrayList (java.util.ArrayList)4 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)4 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)3 VersionsList (org.apache.archiva.rest.api.model.VersionsList)3 BadRequestException (javax.ws.rs.BadRequestException)2 ForbiddenException (javax.ws.rs.ForbiddenException)2 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)2 ProxyConnector (org.apache.archiva.admin.model.beans.ProxyConnector)2 RemoteRepository (org.apache.archiva.admin.model.beans.RemoteRepository)2 RepositoryGroup (org.apache.archiva.admin.model.beans.RepositoryGroup)2 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)2