Search in sources :

Example 1 with Artifact

use of se.light.assembly64.model.Artifact in project archiva by apache.

the class DependencyTreeBuilderTestMaven3 method testBuilderDependencies.

@Test
public void testBuilderDependencies() throws Exception {
    List<TreeEntry> treeEntries = builder.buildDependencyTree(Collections.singletonList(TEST_REPO_ID), TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION);
    Artifact artifact = new Artifact(TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, "", "");
    artifact.setFileExtension("jar");
    assertThat(treeEntries).isNotNull().isNotEmpty().contains(new TreeEntry(artifact));
    artifact = new Artifact("commons-lang", "commons-lang", "2.2", "compile", "");
    artifact.setFileExtension("jar");
    assertThat(treeEntries.get(0).getChilds()).isNotNull().isNotEmpty().contains(new TreeEntry(artifact));
}
Also used : TreeEntry(org.apache.archiva.maven2.model.TreeEntry) Artifact(org.apache.archiva.maven2.model.Artifact) Test(org.junit.Test)

Example 2 with Artifact

use of se.light.assembly64.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.maven2.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 3 with Artifact

use of se.light.assembly64.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 = repositorySessionFactory.createSession();
    MetadataRepository metadataRepository = repositorySession.getRepository();
    Set<Artifact> artifactSet = new HashSet<>();
    try {
        for (String repoId : checksumSearch.getRepositories()) {
            Collection<ArtifactMetadata> artifactMetadatas = metadataRepository.getArtifactsByChecksum(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.maven2.model.Artifact) HashSet(java.util.HashSet)

Example 4 with Artifact

use of se.light.assembly64.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.maven2.model.Artifact)

Example 5 with Artifact

use of se.light.assembly64.model.Artifact in project archiva by apache.

the class ArtifactBuilder method build.

public Artifact build() {
    ArtifactReference ref = new ArtifactReference();
    ref.setArtifactId(artifactMetadata.getProject());
    ref.setGroupId(artifactMetadata.getNamespace());
    ref.setVersion(artifactMetadata.getVersion());
    String type = null, classifier = null;
    MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    if (facet != null) {
        type = facet.getType();
        classifier = facet.getClassifier();
    }
    ref.setClassifier(classifier);
    ref.setType(type);
    Path file = managedRepositoryContent.toFile(ref);
    String extension = getExtensionFromFile(file);
    Artifact artifact = new Artifact(ref.getGroupId(), ref.getArtifactId(), ref.getVersion());
    artifact.setRepositoryId(artifactMetadata.getRepositoryId());
    artifact.setClassifier(classifier);
    artifact.setPackaging(type);
    artifact.setType(type);
    artifact.setFileExtension(extension);
    artifact.setPath(managedRepositoryContent.toPath(ref));
    // 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(ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType());
    return artifact;
}
Also used : Path(java.nio.file.Path) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) ArtifactReference(org.apache.archiva.model.ArtifactReference) Artifact(org.apache.archiva.maven2.model.Artifact)

Aggregations

Artifact (org.apache.archiva.maven2.model.Artifact)37 Test (org.junit.Test)29 SearchService (org.apache.archiva.rest.api.services.SearchService)14 ArrayList (java.util.ArrayList)13 BrowseService (org.apache.archiva.rest.api.services.BrowseService)11 File (java.io.File)10 SearchRequest (org.apache.archiva.rest.api.model.SearchRequest)9 Artifact (se.light.assembly64.model.Artifact)9 ManagedRepositoriesService (org.apache.archiva.rest.api.services.ManagedRepositoriesService)8 RepositoriesService (org.apache.archiva.rest.api.services.RepositoriesService)8 Path (java.nio.file.Path)7 List (java.util.List)6 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)6 CancelableTask (se.light.assembly64.model.CancelableTask)6 IOException (java.io.IOException)5 Optional (java.util.Optional)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 ArtifactDelta (se.light.assembly64.model.ArtifactDelta)5 ArtifactType (se.light.assembly64.model.ArtifactType)5