Search in sources :

Example 11 with StoreResource

use of org.commonjava.indy.content.StoreResource in project indy by Commonjava.

the class MavenMetadataGeneratorTest method generateFileContent_VersionsMetadataWith2Versions.

@Test
public void generateFileContent_VersionsMetadataWith2Versions() throws Exception {
    final StoreResource resource = setupVersionsStructureWith2Versions();
    final ConcreteResource metadataFile = resource.getChild("maven-metadata.xml");
    final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), metadataFile.getPath(), new EventMetadata());
    assertThat(transfer, notNullValue());
    final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), new EventMetadata());
    assertThat(metadata, notNullValue());
    final VersioningView versioning = metadata.getVersioning();
    final List<SingleVersion> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.get(0).renderStandard(), equalTo("1.0"));
    assertThat(versions.get(1).renderStandard(), equalTo("1.1"));
    assertThat(versioning.getReleaseVersion().renderStandard(), equalTo("1.1"));
    assertThat(versioning.getLatestVersion().renderStandard(), equalTo("1.1"));
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) VersioningView(org.commonjava.maven.galley.maven.model.view.meta.VersioningView) StoreResource(org.commonjava.indy.content.StoreResource) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 12 with StoreResource

use of org.commonjava.indy.content.StoreResource in project indy by Commonjava.

the class MavenMetadataGeneratorTest method generateFileContent_SnapshotMetadataWith2Versions.

@Test
public void generateFileContent_SnapshotMetadataWith2Versions() throws Exception {
    final StoreResource resource = setupSnapshotDirWith2Snapshots();
    final EventMetadata emd = new EventMetadata();
    final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), resource.getChild("maven-metadata.xml").getPath(), emd);
    assertThat(transfer, notNullValue());
    final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), emd);
    assertThat(metadata, notNullValue());
    final VersioningView versioning = metadata.getVersioning();
    final LatestSnapshotView latestSnapshot = versioning.getLatestSnapshot();
    assertThat(latestSnapshot.isLocalCopy(), equalTo(false));
    assertThat(latestSnapshot.getTimestamp(), equalTo(SnapshotUtils.parseSnapshotTimestamp("20140828.225800")));
    assertThat(latestSnapshot.getBuildNumber(), equalTo(1));
    final List<SnapshotArtifactView> snapshots = versioning.getSnapshotArtifacts();
    assertThat(snapshots.size(), equalTo(4));
    for (final SnapshotArtifactView snap : snapshots) {
        final String extension = snap.getExtension();
        assertThat(extension.equals("jar") || extension.equals("pom"), equalTo(true));
        final String version = snap.getVersion();
        System.out.println(version);
        final SingleVersion parsed = VersionUtils.createSingleVersion(version);
        assertThat(parsed.isSnapshot(), equalTo(true));
        assertThat(parsed.isLocalSnapshot(), equalTo(false));
        final SnapshotPart part = parsed.getSnapshotPart();
        final String tstamp = SnapshotUtils.generateSnapshotTimestamp(part.getTimestamp());
        assertThat(tstamp.equals("20140828.225800") || tstamp.equals("20140828.221400"), equalTo(true));
    }
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) VersioningView(org.commonjava.maven.galley.maven.model.view.meta.VersioningView) StoreResource(org.commonjava.indy.content.StoreResource) SnapshotPart(org.commonjava.atlas.maven.ident.version.part.SnapshotPart) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) LatestSnapshotView(org.commonjava.maven.galley.maven.model.view.meta.LatestSnapshotView) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) SnapshotArtifactView(org.commonjava.maven.galley.maven.model.view.meta.SnapshotArtifactView) Test(org.junit.Test)

Example 13 with StoreResource

use of org.commonjava.indy.content.StoreResource in project indy by Commonjava.

the class ContentBrowseController method renderResult.

private ContentBrowseResult renderResult(final StoreKey key, final String requestPath, final String browseServiceUrl, final String contentServiceUrl, final UriFormatter uriFormatter, final EventMetadata eventMetadata) throws IndyWorkflowException {
    String path = requestPath.endsWith("/") ? requestPath : requestPath + "/";
    final ArtifactStore store = getStore(key);
    final List<StoreResource> listed = contentManager.list(store, path, eventMetadata);
    final Map<String, Set<String>> listingUrls = new TreeMap<>();
    final String storeBrowseUrl = uriFormatter.formatAbsolutePathTo(browseServiceUrl, key.getType().singularEndpointName(), key.getName());
    final String storeContentUrl = uriFormatter.formatAbsolutePathTo(contentServiceUrl, key.getType().singularEndpointName(), key.getName());
    if (listed != null) {
        // second pass, process the remainder.
        for (int pass = 0; pass < 2; pass++) {
            for (final ConcreteResource res : listed) {
                String p = res.getPath();
                if (pass == 0 && !p.endsWith("/")) {
                    continue;
                }
                if (p.endsWith("-") || p.endsWith("-/")) {
                    // skip npm adduser path to avoid the sensitive info showing.
                    continue;
                } else if (pass == 1) {
                    if (!p.endsWith("/")) {
                        final String dirpath = p + "/";
                        if (listingUrls.containsKey(normalize(storeBrowseUrl, dirpath))) {
                            p = dirpath;
                        }
                    } else {
                        continue;
                    }
                }
                String localUrl;
                if (p.endsWith("/")) {
                    localUrl = normalize(storeBrowseUrl, p);
                } else {
                    // So this means current path is a file not a directory, and needs to construct it to point to content api /api/content
                    localUrl = normalize(storeContentUrl, p);
                }
                Set<String> sources = listingUrls.computeIfAbsent(localUrl, k -> new HashSet<>());
                sources.add(normalize(res.getLocationUri(), res.getPath()));
            }
        }
    }
    final List<String> sources = new ArrayList<>();
    if (listed != null) {
        for (final ConcreteResource res : listed) {
            // KeyedLocation is all we use in Indy.
            logger.debug("Formatting sources URL for: {}", res);
            final KeyedLocation kl = (KeyedLocation) res.getLocation();
            final String uri = uriFormatter.formatAbsolutePathTo(browseServiceUrl, kl.getKey().getType().singularEndpointName(), kl.getKey().getName());
            if (!sources.contains(uri)) {
                logger.debug("adding source URI: '{}'", uri);
                sources.add(uri);
            }
        }
    }
    Collections.sort(sources);
    String parentPath = normalize(parentPath(path));
    if (!parentPath.endsWith("/")) {
        parentPath += "/";
    }
    final String parentUrl;
    if (parentPath.equals(path)) {
        parentPath = null;
        parentUrl = null;
    } else {
        parentUrl = uriFormatter.formatAbsolutePathTo(browseServiceUrl, key.getType().singularEndpointName(), key.getName(), parentPath);
    }
    final List<ContentBrowseResult.ListingURLResult> listingURLResults = new ArrayList<>(listingUrls.size());
    for (String localUrl : listingUrls.keySet()) {
        final String apiPath = localUrl.replace(storeBrowseUrl, "").replace(storeContentUrl, "");
        listingURLResults.add(new ContentBrowseResult.ListingURLResult(apiPath, localUrl, listingUrls.get(localUrl)));
    }
    final ContentBrowseResult result = new ContentBrowseResult();
    result.setListingUrls(listingURLResults);
    result.setParentUrl(parentUrl);
    result.setParentPath(parentPath);
    result.setPath(path);
    result.setStoreKey(key);
    result.setStoreBrowseUrl(storeBrowseUrl);
    result.setStoreContentUrl(storeContentUrl);
    result.setBaseBrowseUrl(browseServiceUrl);
    result.setBaseContentUrl(contentServiceUrl);
    result.setSources(sources);
    return result;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ContentBrowseResult(org.commonjava.indy.content.browse.model.ContentBrowseResult) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) StoreResource(org.commonjava.indy.content.StoreResource) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource)

Example 14 with StoreResource

use of org.commonjava.indy.content.StoreResource in project indy by Commonjava.

the class DefaultDownloadManager method list.

@Override
@Measure
public List<StoreResource> list(final List<? extends ArtifactStore> stores, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    final String dir = PathUtils.dirname(path);
    final List<StoreResource> result = new ArrayList<>();
    try {
        final List<ListingResult> results = transfers.listAll(locationExpander.expand(new VirtualResource(LocationUtils.toLocations(stores), path)), eventMetadata);
        for (final ListingResult lr : results) {
            if (lr != null && lr.getListing() != null) {
                for (final String file : lr.getListing()) {
                    result.add(new StoreResource((KeyedLocation) lr.getLocation(), dir, file));
                }
            }
        }
    } catch (final TransferLocationException e) {
        fireIndyStoreErrorEvent(e);
        logger.warn("Location Error: " + e.getMessage(), e);
        throw new IndyWorkflowException("Failed to list ALL paths: {} from: {}. Reason: {}", e, path, stores, e.getMessage());
    } catch (final TransferException e) {
        logger.error(e.getMessage(), e);
        throw new IndyWorkflowException("Failed to list ALL paths: {} from: {}. Reason: {}", e, path, stores, e.getMessage());
    }
    return dedupeListing(result);
}
Also used : StoreResource(org.commonjava.indy.content.StoreResource) TransferException(org.commonjava.maven.galley.TransferException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArrayList(java.util.ArrayList) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) ListingResult(org.commonjava.maven.galley.model.ListingResult) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 15 with StoreResource

use of org.commonjava.indy.content.StoreResource in project indy by Commonjava.

the class ContentController method renderListing.

/**
 * @deprecated directory listing has been moved to addons/content-browse
 */
@Deprecated
public String renderListing(final String acceptHeader, final StoreKey key, final String requestPath, final String serviceUrl, final UriFormatter uriFormatter, final EventMetadata eventMetadata) throws IndyWorkflowException {
    String path = requestPath;
    if (path.endsWith(LISTING_HTML_FILE)) {
        path = normalize(parentPath(path));
    }
    validatePath(key, path);
    final List<StoreResource> listed = getListing(key, path, eventMetadata);
    if (ApplicationContent.application_json.equals(acceptHeader)) {
        final DirectoryListingDTO dto = new DirectoryListingDTO(StoreResource.convertToEntries(listed));
        try {
            return mapper.writeValueAsString(dto);
        } catch (final JsonProcessingException e) {
            throw new IndyWorkflowException("Failed to render listing to JSON: %s. Reason: %s", e, dto, e.getMessage());
        }
    }
    final Map<String, Set<String>> listingUrls = new TreeMap<>();
    final String storeUrl = uriFormatter.formatAbsolutePathTo(serviceUrl, key.getType().singularEndpointName(), key.getName());
    if (listed != null) {
        // second pass, process the remainder.
        for (int pass = 0; pass < 2; pass++) {
            for (final ConcreteResource res : listed) {
                String p = res.getPath();
                if (pass == 0 && !p.endsWith("/")) {
                    continue;
                }
                if (p.endsWith("-") || p.endsWith("-/")) {
                    // skip npm adduser path to avoid the sensitive info showing.
                    continue;
                } else if (pass == 1) {
                    if (!p.endsWith("/")) {
                        final String dirpath = p + "/";
                        if (listingUrls.containsKey(normalize(storeUrl, dirpath))) {
                            p = dirpath;
                        }
                    } else {
                        continue;
                    }
                }
                final String localUrl = normalize(storeUrl, p);
                Set<String> sources = listingUrls.get(localUrl);
                if (sources == null) {
                    sources = new HashSet<>();
                    listingUrls.put(localUrl, sources);
                }
                sources.add(normalize(res.getLocationUri(), res.getPath()));
            }
        }
    }
    final List<String> sources = new ArrayList<>();
    if (listed != null) {
        for (final ConcreteResource res : listed) {
            // KeyedLocation is all we use in Indy.
            logger.debug("Formatting sources URL for: {}", res);
            final KeyedLocation kl = (KeyedLocation) res.getLocation();
            final String uri = uriFormatter.formatAbsolutePathTo(serviceUrl, kl.getKey().getType().singularEndpointName(), kl.getKey().getName());
            if (!sources.contains(uri)) {
                logger.debug("adding source URI: '{}'", uri);
                sources.add(uri);
            }
        }
    }
    Collections.sort(sources);
    String parentPath = normalize(parentPath(path));
    if (!parentPath.endsWith("/")) {
        parentPath += "/";
    }
    final String parentUrl;
    if (parentPath.equals(path)) {
        parentPath = null;
        parentUrl = null;
    } else {
        parentUrl = uriFormatter.formatAbsolutePathTo(serviceUrl, key.getType().singularEndpointName(), key.getName(), parentPath);
    }
    final Map<String, Object> params = new HashMap<>();
    params.put("items", listingUrls);
    params.put("parentUrl", parentUrl);
    params.put("parentPath", parentPath);
    params.put("path", path);
    params.put("storeKey", key);
    params.put("storeUrl", storeUrl);
    params.put("baseUrl", serviceUrl);
    params.put("sources", sources);
    // render...
    try {
        return templates.render(acceptHeader, "directory-listing", params);
    } catch (final IndyGroovyException e) {
        throw new IndyWorkflowException(e.getMessage(), e);
    }
}
Also used : DirectoryListingDTO(org.commonjava.indy.model.core.dto.DirectoryListingDTO) HashSet(java.util.HashSet) Set(java.util.Set) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException) StoreResource(org.commonjava.indy.content.StoreResource) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

StoreResource (org.commonjava.indy.content.StoreResource)24 ArrayList (java.util.ArrayList)16 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)13 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)10 HashMap (java.util.HashMap)9 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)8 List (java.util.List)7 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)7 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)7 ListingResult (org.commonjava.maven.galley.model.ListingResult)7 Set (java.util.Set)6 HashSet (java.util.HashSet)5 TransferException (org.commonjava.maven.galley.TransferException)5 ArtifactPathInfo (org.commonjava.atlas.maven.ident.util.ArtifactPathInfo)4 TransferLocationException (org.commonjava.maven.galley.TransferLocationException)4 Transfer (org.commonjava.maven.galley.model.Transfer)4 VirtualResource (org.commonjava.maven.galley.model.VirtualResource)4 Measure (org.commonjava.o11yphant.metrics.annotation.Measure)4 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3