Search in sources :

Example 6 with JoinString

use of org.commonjava.atlas.maven.ident.util.JoinString in project indy by Commonjava.

the class ImpliedRepositoryDetector method addImpliedRepositories.

private void addImpliedRepositories(final ImplicationsJob job) {
    job.implied = new ArrayList<>();
    logger.debug("Retrieving repository/pluginRepository declarations from:\n  {}", new JoinString("\n  ", job.pomView.getDocRefStack()));
    final List<List<RepositoryView>> repoLists = Arrays.asList(job.pomView.getNonProfileRepositories(), job.pomView.getAllPluginRepositories());
    final ImpliedRepositoryCreator creator = createRepoCreator();
    if (creator == null) {
        logger.error("Cannot proceed without a valid ImpliedRepositoryCreator instance. Aborting detection.");
        return;
    }
    for (final List<RepositoryView> repos : repoLists) {
        if (repos == null || repos.isEmpty()) {
            continue;
        }
        for (final RepositoryView repo : repos) {
            final ProjectVersionRef gav = job.pathInfo.getProjectId();
            try {
                if (config.isBlacklisted(repo.getUrl())) {
                    logger.debug("Discarding blacklisted repository: {}", repo);
                    continue;
                } else if (!config.isIncludeSnapshotRepos() && !repo.isReleasesEnabled()) {
                    logger.debug("Discarding snapshot repository: {}", repo);
                    continue;
                }
            } catch (final MalformedURLException e) {
                logger.error(String.format("Cannot add implied remote repo: %s from: %s (transfer: %s). Failed to check if repository is blacklisted.", repo.getUrl(), gav, job.transfer), e);
            }
            logger.debug("Detected POM-declared repository: {}", repo);
            List<RemoteRepository> rrs = null;
            try {
                rrs = storeManager.query().getRemoteRepositoryByUrl(MAVEN_PKG_KEY, repo.getUrl());
            } catch (IndyDataException e) {
                logger.error(String.format("Cannot lookup remote repositories by URL: %s. Reason: %s", e.getMessage()), e);
            }
            final RemoteRepository ref = creator.createFrom(gav, repo, LoggerFactory.getLogger(creator.getClass()));
            if (ref == null) {
                logger.warn("ImpliedRepositoryCreator didn't create anything for repo: {}, specified in: {}. Skipping.", repo.getId(), gav);
                continue;
            }
            if (rrs != null && !rrs.isEmpty()) {
                rrs = rrs.stream().filter(rr -> rr.isAllowReleases() == ref.isAllowReleases()).filter(rr -> rr.isAllowSnapshots() == ref.isAllowSnapshots()).filter(rr -> (isEmpty(rr.getPathMaskPatterns()) && isEmpty(ref.getPathMaskPatterns())) || rr.getPathMaskPatterns().equals(ref.getPathMaskPatterns())).collect(Collectors.toList());
            }
            if (rrs == null || rrs.isEmpty()) {
                logger.debug("Creating new RemoteRepository for: {}", repo);
                final RemoteRepository rr = ref.copyOf();
                rr.setMetadata(METADATA_ORIGIN, IMPLIED_REPO_ORIGIN);
                try {
                    rr.setMetadata(IMPLIED_BY_STORES, mapper.writeValueAsString(new ImpliedRepoMetadataManager.ImpliedRemotesWrapper(Collections.singletonList(job.store.getKey()))));
                } catch (JsonProcessingException e) {
                    logger.error("Failed to set {}", IMPLIED_BY_STORES);
                    continue;
                }
                if (!remoteValidator.isValid(rr)) {
                    logger.warn("Implied repository to: {} is invalid! Repository created was: {}", repo.getUrl(), rr);
                    continue;
                }
                final String changelog = String.format("Adding remote repository: %s (url: %s, name: %s), which is implied by the POM: %s (at: %s/%s)", repo.getId(), repo.getUrl(), repo.getName(), gav, job.transfer.getLocation().getUri(), job.transfer.getPath());
                final ChangeSummary summary = new ChangeSummary(ChangeSummary.SYSTEM_USER, changelog);
                try {
                    final boolean result = storeManager.storeArtifactStore(rr, summary, true, false, new EventMetadata().set(StoreDataManager.EVENT_ORIGIN, IMPLIED_REPOS_DETECTION).set(IMPLIED_BY_POM_TRANSFER, job.transfer));
                    logger.debug("Stored new RemoteRepository: {}. (successful? {})", rr, result);
                    job.implied.add(rr);
                } catch (final IndyDataException e) {
                    logger.error(String.format("Cannot add implied remote repo: %s from: %s (transfer: %s). Failed to store new remote repository.", repo.getUrl(), gav, job.transfer), e);
                }
            } else {
                logger.debug("Found existing RemoteRepositories: {}", rrs);
                for (final RemoteRepository rr : rrs) {
                    rr.setMetadata(METADATA_ORIGIN, IMPLIED_REPO_ORIGIN);
                    try {
                        metadataManager.updateImpliedBy(rr, job.store);
                    } catch (ImpliedReposException e) {
                        logger.error("Failed to set {}", IMPLIED_BY_STORES);
                        continue;
                    }
                    final String changelog = String.format("Updating the existing remote repository: %s (url: %s, name: %s), which is implied by the POM: %s (at: %s/%s)", repo.getId(), repo.getUrl(), repo.getName(), gav, job.transfer.getLocation().getUri(), job.transfer.getPath());
                    final ChangeSummary summary = new ChangeSummary(ChangeSummary.SYSTEM_USER, changelog);
                    try {
                        final boolean result = storeManager.storeArtifactStore(rr, summary, false, false, null);
                        logger.debug("Updated the RemoteRepository: {}. (successful? {})", rr, result);
                        job.implied.add(rr);
                    } catch (final IndyDataException e) {
                        logger.error(String.format("Cannot add implied remote repo: %s from: %s (transfer: %s). Failed to update the remote repository.", repo.getUrl(), gav, job.transfer), e);
                    }
                }
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) LoggerFactory(org.slf4j.LoggerFactory) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) StringUtils(org.apache.commons.lang3.StringUtils) WeftManaged(org.commonjava.cdi.util.weft.WeftManaged) FileStorageEvent(org.commonjava.maven.galley.event.FileStorageEvent) MavenPomReader(org.commonjava.maven.galley.maven.parse.MavenPomReader) Observes(javax.enterprise.event.Observes) ArtifactStoreValidator(org.commonjava.indy.data.ArtifactStoreValidator) CoreEventManagerConstants(org.commonjava.indy.change.event.CoreEventManagerConstants) ImpliedReposException(org.commonjava.indy.implrepo.ImpliedReposException) StoreKey(org.commonjava.indy.model.core.StoreKey) MAVEN_PKG_KEY(org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor.MAVEN_PKG_KEY) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) Collection(java.util.Collection) IMPLIED_REPO_ORIGIN(org.commonjava.indy.implrepo.data.ImpliedReposStoreDataManagerDecorator.IMPLIED_REPO_ORIGIN) METADATA_ORIGIN(org.commonjava.indy.model.core.ArtifactStore.METADATA_ORIGIN) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyDataException(org.commonjava.indy.data.IndyDataException) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) IMPLIED_BY_STORES(org.commonjava.indy.implrepo.data.ImpliedRepoMetadataManager.IMPLIED_BY_STORES) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) Group(org.commonjava.indy.model.core.Group) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Transfer(org.commonjava.maven.galley.model.Transfer) RepositoryView(org.commonjava.maven.galley.maven.model.view.RepositoryView) ImpliedRepoConfig(org.commonjava.indy.implrepo.conf.ImpliedRepoConfig) ExecutorConfig(org.commonjava.cdi.util.weft.ExecutorConfig) IMPLIED_STORES(org.commonjava.indy.implrepo.data.ImpliedRepoMetadataManager.IMPLIED_STORES) Location(org.commonjava.maven.galley.model.Location) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) MalformedURLException(java.net.MalformedURLException) Executor(java.util.concurrent.Executor) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ScriptEngine(org.commonjava.indy.subsys.template.ScriptEngine) ImpliedRepoMetadataManager(org.commonjava.indy.implrepo.data.ImpliedRepoMetadataManager) Collections(java.util.Collections) StoreDataManager(org.commonjava.indy.data.StoreDataManager) MalformedURLException(java.net.MalformedURLException) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) RepositoryView(org.commonjava.maven.galley.maven.model.view.RepositoryView) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyDataException(org.commonjava.indy.data.IndyDataException) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) List(java.util.List) ArrayList(java.util.ArrayList) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ImpliedReposException(org.commonjava.indy.implrepo.ImpliedReposException)

Example 7 with JoinString

use of org.commonjava.atlas.maven.ident.util.JoinString in project indy by Commonjava.

the class StoreAdminHandler method getAll.

@ApiOperation("Retrieve the definitions of all artifact stores of a given type on the system")
@ApiResponses({ @ApiResponse(code = 200, response = StoreListingDTO.class, message = "The store definitions") })
@GET
@Produces(ApplicationContent.application_json)
public Response getAll(@ApiParam("Filter only stores that support the package type (eg. maven, npm). NOTE: '_all' returns all.") @PathParam("packageType") final String packageType, @ApiParam(allowableValues = "hosted,group,remote", required = true) @PathParam("type") final String type) {
    final StoreType st = StoreType.get(type);
    Response response;
    try {
        final List<ArtifactStore> stores = adminController.getAllOfType(packageType, st);
        logger.info("Returning listing containing stores:\n\t{}", new JoinString("\n\t", stores));
        final StoreListingDTO<ArtifactStore> dto = new StoreListingDTO<>(stores);
        response = responseHelper.formatOkResponseWithJsonEntity(dto);
    } catch (final IndyWorkflowException e) {
        logger.error(e.getMessage(), e);
        response = responseHelper.formatResponse(e);
    }
    return response;
}
Also used : StoreType(org.commonjava.indy.model.core.StoreType) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) StoreListingDTO(org.commonjava.indy.model.core.dto.StoreListingDTO) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with JoinString

use of org.commonjava.atlas.maven.ident.util.JoinString in project indy by Commonjava.

the class IndyLocationExpander method expand.

/**
 * For group references, expand into a list of concrete repository locations (hosted or remote). For remote repository references that are
 * specified as cache-only locations (see: {@link CacheOnlyLocation}), lookup the corresponding {@link RemoteRepository} and use it to create a
 * {@link RepositoryLocation} that contains any relevant SSL, authentication, proxy, etc. attbributes.
 */
@Override
public <T extends Location> List<Location> expand(final Collection<T> locations) throws TransferException {
    final List<Location> result = new ArrayList<Location>();
    for (final Location location : locations) {
        if (location instanceof GroupLocation) {
            final GroupLocation gl = (GroupLocation) location;
            try {
                logger.debug("Expanding group: {}", gl.getKey());
                final List<ArtifactStore> members = data.query().getOrderedConcreteStoresInGroup(gl.getKey().getPackageType(), gl.getKey().getName());
                if (members != null) {
                    for (final ArtifactStore member : members) {
                        if (!result.contains(member)) {
                            logger.debug("expansion += {}", member.getKey());
                            result.add(LocationUtils.toLocation(member));
                        }
                    }
                    logger.debug("Expanded group: {} to:\n  {}", gl.getKey(), new JoinString("\n  ", result));
                }
            } catch (final IndyDataException e) {
                throw new TransferException("Failed to lookup ordered concrete artifact stores contained in group: {}. Reason: {}", e, gl, e.getMessage());
            }
        } else if (location instanceof CacheOnlyLocation && !((CacheOnlyLocation) location).isHostedRepository()) {
            final StoreKey key = ((KeyedLocation) location).getKey();
            try {
                final ArtifactStore store = data.getArtifactStore(key);
                if (store == null) {
                    throw new TransferException("Cannot find ArtifactStore to match key: %s.", key);
                }
                logger.debug("Adding single store: {} for location: {}", store, location);
                result.add(LocationUtils.toLocation(store));
            } catch (final IndyDataException e) {
                throw new TransferException("Failed to lookup store for key: {}. Reason: {}", e, key, e.getMessage());
            }
        } else {
            logger.debug("No expansion available for location: {}", location);
            result.add(location);
        }
    }
    return result;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) TransferException(org.commonjava.maven.galley.TransferException) GroupLocation(org.commonjava.indy.model.galley.GroupLocation) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) CacheOnlyLocation(org.commonjava.indy.model.galley.CacheOnlyLocation) ArrayList(java.util.ArrayList) StoreKey(org.commonjava.indy.model.core.StoreKey) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) CacheOnlyLocation(org.commonjava.indy.model.galley.CacheOnlyLocation) RepositoryLocation(org.commonjava.indy.model.galley.RepositoryLocation) GroupLocation(org.commonjava.indy.model.galley.GroupLocation) Location(org.commonjava.maven.galley.model.Location)

Example 9 with JoinString

use of org.commonjava.atlas.maven.ident.util.JoinString in project indy by Commonjava.

the class JaxRsUriFormatter method formatAbsolutePathTo.

@Override
public String formatAbsolutePathTo(final String base, final String... parts) {
    logger.debug("Formatting URL from base: '{}' and parts: {}", base, new JoinString(", ", parts));
    String url = null;
    try {
        url = UrlUtils.buildUrl(base, parts);
    } catch (final MalformedURLException e) {
        logger.warn("Failed to use UrlUtils to build URL from base: {} and parts: {}", base, join(parts, ", "));
        url = PathUtils.normalize(base, PathUtils.normalize(parts));
    }
    if (url.length() > 0 && !url.matches("[a-zA-Z0-9]+\\:\\/\\/.+") && url.charAt(0) != '/') {
        url = "/" + url;
    }
    logger.debug("Resulting URL: '{}'", url);
    return url;
}
Also used : JoinString(org.commonjava.atlas.maven.ident.util.JoinString) MalformedURLException(java.net.MalformedURLException) JoinString(org.commonjava.atlas.maven.ident.util.JoinString)

Aggregations

JoinString (org.commonjava.atlas.maven.ident.util.JoinString)9 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)6 StoreKey (org.commonjava.indy.model.core.StoreKey)5 IndyDataException (org.commonjava.indy.data.IndyDataException)4 ArrayList (java.util.ArrayList)3 ImpliedReposException (org.commonjava.indy.implrepo.ImpliedReposException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponse (io.swagger.annotations.ApiResponse)2 ApiResponses (io.swagger.annotations.ApiResponses)2 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 Inject (javax.inject.Inject)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 Response (javax.ws.rs.core.Response)2 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)2 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)2 Location (org.commonjava.maven.galley.model.Location)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Api (io.swagger.annotations.Api)1