Search in sources :

Example 1 with ArtifactContentEntry

use of org.apache.archiva.rest.api.model.ArtifactContentEntry in project archiva by apache.

the class DefaultBrowseService method readFileEntries.

protected List<ArtifactContentEntry> readFileEntries(final Path file, final String filterPath, final String repoId) throws IOException {
    String cleanedfilterPath = filterPath == null ? "" : (StringUtils.startsWith(filterPath, "/") ? StringUtils.substringAfter(filterPath, "/") : filterPath);
    Map<String, ArtifactContentEntry> artifactContentEntryMap = new HashMap<>();
    int filterDepth = StringUtils.countMatches(cleanedfilterPath, "/");
    if (!StringUtils.endsWith(cleanedfilterPath, "/") && !StringUtils.isEmpty(cleanedfilterPath)) {
        filterDepth++;
    }
    JarFile jarFile = new JarFile(file.toFile());
    try {
        Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries();
        while (jarEntryEnumeration.hasMoreElements()) {
            JarEntry currentEntry = jarEntryEnumeration.nextElement();
            String cleanedEntryName = // 
            StringUtils.endsWith(currentEntry.getName(), "/") ? StringUtils.substringBeforeLast(currentEntry.getName(), "/") : currentEntry.getName();
            String entryRootPath = getRootPath(cleanedEntryName);
            int depth = StringUtils.countMatches(cleanedEntryName, "/");
            if (// 
            StringUtils.isEmpty(cleanedfilterPath) && // 
            !artifactContentEntryMap.containsKey(entryRootPath) && depth == filterDepth) {
                artifactContentEntryMap.put(entryRootPath, new ArtifactContentEntry(entryRootPath, !currentEntry.isDirectory(), depth, repoId));
            } else {
                if (// 
                StringUtils.startsWith(cleanedEntryName, cleanedfilterPath) && (depth == filterDepth || (!currentEntry.isDirectory() && depth == filterDepth))) {
                    artifactContentEntryMap.put(cleanedEntryName, new ArtifactContentEntry(cleanedEntryName, !currentEntry.isDirectory(), depth, repoId));
                }
            }
        }
        if (StringUtils.isNotEmpty(cleanedfilterPath)) {
            Map<String, ArtifactContentEntry> filteredArtifactContentEntryMap = new HashMap<>();
            for (Map.Entry<String, ArtifactContentEntry> entry : artifactContentEntryMap.entrySet()) {
                filteredArtifactContentEntryMap.put(entry.getKey(), entry.getValue());
            }
            List<ArtifactContentEntry> sorted = getSmallerDepthEntries(filteredArtifactContentEntryMap);
            if (sorted == null) {
                return Collections.emptyList();
            }
            Collections.sort(sorted, ArtifactContentEntryComparator.INSTANCE);
            return sorted;
        }
    } finally {
        if (jarFile != null) {
            jarFile.close();
        }
    }
    List<ArtifactContentEntry> sorted = new ArrayList<>(artifactContentEntryMap.values());
    Collections.sort(sorted, ArtifactContentEntryComparator.INSTANCE);
    return sorted;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ArtifactContentEntry

use of org.apache.archiva.rest.api.model.ArtifactContentEntry in project archiva by apache.

the class BrowseServiceTest method readArtifactContentEntries.

@Test
public void readArtifactContentEntries() throws Exception {
    BrowseService browseService = getBrowseService(authorizationHeader, true);
    List<ArtifactContentEntry> artifactContentEntries = browseService.getArtifactContentEntries("commons-logging", "commons-logging", "1.1", null, null, null, TEST_REPO_ID);
    log.info("artifactContentEntries: {}", artifactContentEntries);
    // 
    assertThat(artifactContentEntries).isNotNull().isNotEmpty().hasSize(// 
    2).contains(// 
    new ArtifactContentEntry("org", false, 0, TEST_REPO_ID), new ArtifactContentEntry("META-INF", false, 0, TEST_REPO_ID));
}
Also used : ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) BrowseService(org.apache.archiva.rest.api.services.BrowseService) Test(org.junit.Test)

Example 3 with ArtifactContentEntry

use of org.apache.archiva.rest.api.model.ArtifactContentEntry in project archiva by apache.

the class ArtifactContentEntriesTests method readArtifactContentEntriesRootSlash.

@Test
public void readArtifactContentEntriesRootSlash() throws Exception {
    Path file = Paths.get(getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar");
    List<ArtifactContentEntry> artifactContentEntries = browseService.readFileEntries(file, "/", "foo");
    log.info("artifactContentEntries: {}", artifactContentEntries);
    assertThat(artifactContentEntries).isNotNull().isNotEmpty().hasSize(2).contains(new ArtifactContentEntry("org", false, 0, "foo"), new ArtifactContentEntry("META-INF", false, 0, "foo"));
}
Also used : Path(java.nio.file.Path) ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) Test(org.junit.Test)

Example 4 with ArtifactContentEntry

use of org.apache.archiva.rest.api.model.ArtifactContentEntry in project archiva by apache.

the class BrowseServiceTest method readArtifactContentEntriesFilesAndDirectories.

@Test
public void readArtifactContentEntriesFilesAndDirectories() throws Exception {
    BrowseService browseService = getBrowseService(authorizationHeader, true);
    List<ArtifactContentEntry> artifactContentEntries = browseService.getArtifactContentEntries("commons-logging", "commons-logging", "1.1", null, null, "org/apache/commons/logging/", TEST_REPO_ID);
    log.info("artifactContentEntries: {}", artifactContentEntries);
    assertThat(artifactContentEntries).isNotNull().isNotEmpty().hasSize(10).contains(new ArtifactContentEntry("org/apache/commons/logging/impl", false, 4, TEST_REPO_ID), new ArtifactContentEntry("org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID));
}
Also used : ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) BrowseService(org.apache.archiva.rest.api.services.BrowseService) Test(org.junit.Test)

Example 5 with ArtifactContentEntry

use of org.apache.archiva.rest.api.model.ArtifactContentEntry in project archiva by apache.

the class BrowseServiceTest method readArtifactContentEntriesRootPath.

@Test
public void readArtifactContentEntriesRootPath() throws Exception {
    BrowseService browseService = getBrowseService(authorizationHeader, true);
    List<ArtifactContentEntry> artifactContentEntries = browseService.getArtifactContentEntries("commons-logging", "commons-logging", "1.1", null, null, "org/", TEST_REPO_ID);
    log.info("artifactContentEntries: {}", artifactContentEntries);
    // 
    assertThat(artifactContentEntries).isNotNull().isNotEmpty().hasSize(// 
    1).contains(new ArtifactContentEntry("org/apache", false, 1, TEST_REPO_ID));
}
Also used : ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) BrowseService(org.apache.archiva.rest.api.services.BrowseService) Test(org.junit.Test)

Aggregations

ArtifactContentEntry (org.apache.archiva.rest.api.model.ArtifactContentEntry)11 Test (org.junit.Test)9 Path (java.nio.file.Path)6 BrowseService (org.apache.archiva.rest.api.services.BrowseService)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 List (java.util.List)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 VersionsList (org.apache.archiva.rest.api.model.VersionsList)1