Search in sources :

Example 1 with StringUtils.substringAfterLast

use of org.apache.commons.lang3.StringUtils.substringAfterLast in project cas by apereo.

the class ConfigurationMetadataGenerator method processHints.

private static Set<ConfigurationMetadataHint> processHints(final Collection<ConfigurationMetadataProperty> props, final Collection<ConfigurationMetadataProperty> groups) {
    var hints = new LinkedHashSet<ConfigurationMetadataHint>(0);
    val allValidProps = props.stream().filter(p -> p.getDeprecation() == null || !Deprecation.Level.ERROR.equals(p.getDeprecation().getLevel())).collect(Collectors.toList());
    for (val entry : allValidProps) {
        try {
            val propName = StringUtils.substringAfterLast(entry.getName(), ".");
            val groupName = StringUtils.substringBeforeLast(entry.getName(), ".");
            val grp = groups.stream().filter(g -> g.getName().equalsIgnoreCase(groupName)).findFirst().orElseThrow(() -> new IllegalArgumentException("Cant locate group " + groupName));
            val matcher = PATTERN_GENERICS.matcher(grp.getType());
            val className = matcher.find() ? matcher.group(1) : grp.getType();
            val clazz = ClassUtils.getClass(className);
            val hint = new ConfigurationMetadataHint();
            hint.setName(entry.getName());
            val annotation = Arrays.stream(clazz.getAnnotations()).filter(a -> a.annotationType().equals(RequiresModule.class)).findFirst().map(RequiresModule.class::cast).orElseThrow(() -> new RuntimeException(clazz.getCanonicalName() + " is missing @RequiresModule"));
            val valueHint = new ValueHint();
            valueHint.setValue(toJson(Map.of("module", annotation.name(), "automated", annotation.automated())));
            valueHint.setDescription(RequiresModule.class.getName());
            hint.getValues().add(valueHint);
            val grpHint = new ValueHint();
            grpHint.setValue(toJson(Map.of("owner", clazz.getCanonicalName())));
            grpHint.setDescription(PropertyOwner.class.getName());
            hint.getValues().add(grpHint);
            val names = RelaxedPropertyNames.forCamelCase(propName);
            names.getValues().forEach(Unchecked.consumer(name -> {
                val f = ReflectionUtils.findField(clazz, name);
                if (f != null && f.isAnnotationPresent(RequiredProperty.class)) {
                    val propertyHint = new ValueHint();
                    propertyHint.setValue(toJson(Map.of("owner", clazz.getName())));
                    propertyHint.setDescription(RequiredProperty.class.getName());
                    hint.getValues().add(propertyHint);
                }
                if (f != null && f.isAnnotationPresent(DurationCapable.class)) {
                    val propertyHint = new ValueHint();
                    propertyHint.setDescription(DurationCapable.class.getName());
                    propertyHint.setValue(toJson(List.of(DurationCapable.class.getName())));
                    hint.getValues().add(propertyHint);
                }
                if (f != null && f.isAnnotationPresent(ExpressionLanguageCapable.class)) {
                    val propertyHint = new ValueHint();
                    propertyHint.setDescription(ExpressionLanguageCapable.class.getName());
                    propertyHint.setValue(toJson(List.of(ExpressionLanguageCapable.class.getName())));
                    hint.getValues().add(propertyHint);
                }
            }));
            if (!hint.getValues().isEmpty()) {
                hints.add(hint);
            }
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return hints;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) lombok.val(lombok.val) RequiresModule(org.apereo.cas.configuration.support.RequiresModule) NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DurationCapable(org.apereo.cas.configuration.support.DurationCapable) StringUtils(org.apache.commons.lang3.StringUtils) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) LiteralStringValueExpr(com.github.javaparser.ast.expr.LiteralStringValueExpr) HashSet(java.util.HashSet) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) ClassUtils(org.apache.commons.lang3.ClassUtils) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) LinkedHashSet(java.util.LinkedHashSet) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) Unchecked(org.jooq.lambda.Unchecked) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Deprecation(org.springframework.boot.configurationmetadata.Deprecation) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) Set(java.util.Set) RequiredProperty(org.apereo.cas.configuration.support.RequiredProperty) ConfigurationMetadataProperty(org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) ExpressionLanguageCapable(org.apereo.cas.configuration.support.ExpressionLanguageCapable) StaticJavaParser(com.github.javaparser.StaticJavaParser) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ReflectionUtils(org.springframework.util.ReflectionUtils) PropertyOwner(org.apereo.cas.configuration.support.PropertyOwner) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Pattern(java.util.regex.Pattern) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) RelaxedPropertyNames(org.apereo.cas.configuration.support.RelaxedPropertyNames) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) RequiresModule(org.apereo.cas.configuration.support.RequiresModule) PropertyOwner(org.apereo.cas.configuration.support.PropertyOwner)

Example 2 with StringUtils.substringAfterLast

use of org.apache.commons.lang3.StringUtils.substringAfterLast in project ddf by codice.

the class DocumentationTest method testBrokenAnchorsPresent.

@Test
public void testBrokenAnchorsPresent() throws IOException, URISyntaxException {
    List<Path> docs = Files.list(getPath()).filter(f -> f.toString().endsWith(HTML_DIRECTORY)).collect(Collectors.toList());
    Set<String> links = new HashSet<>();
    Set<String> anchors = new HashSet<>();
    for (Path path : docs) {
        Document doc = Jsoup.parse(path.toFile(), "UTF-8", EMPTY_STRING);
        String thisDoc = StringUtils.substringAfterLast(path.toString(), File.separator);
        Elements elements = doc.body().getAllElements();
        for (Element element : elements) {
            if (!element.toString().contains(":") && StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE) != null) {
                links.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE));
            }
            anchors.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), ID, CLOSE));
        }
    }
    links.removeAll(anchors);
    assertThat("Anchors missing section reference: " + links.toString(), links.isEmpty());
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) URISyntaxException(java.net.URISyntaxException) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) HashSet(java.util.HashSet) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) Path(java.nio.file.Path) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with StringUtils.substringAfterLast

use of org.apache.commons.lang3.StringUtils.substringAfterLast in project kylo by Teradata.

the class DefaultServiceLevelAgreementService method saveAndScheduleSla.

/**
 * In order to Save an SLA if it is related to a Feed(s) the user needs to have EDIT_DETAILS permission on the Feed(s)
 *
 * @param serviceLevelAgreement the sla to save
 * @param feed                  an option Feed to relate to this SLA.  If this is not present the related feeds are also embedded in the SLA policies.  The Feed is a pointer access to the current
 *                              feed the user is editing if they are creating an SLA from the Feed Details page. If creating an SLA from the main SLA page the feed property will not be populated.
 */
private ServiceLevelAgreement saveAndScheduleSla(ServiceLevelAgreementGroup serviceLevelAgreement, FeedMetadata feed) {
    // ensure user has permissions to edit the SLA
    if (serviceLevelAgreement != null) {
        ServiceLevelAgreementMetricTransformerHelper transformer = new ServiceLevelAgreementMetricTransformerHelper();
        // Read the feeds on the SLA as a Service. Then verify the current user has access to edit these feeds
        List<String> feedsOnSla = metadataAccess.read(() -> {
            List<String> feedIds = new ArrayList<>();
            // all referencing Feeds
            List<String> systemCategoryAndFeedNames = transformer.getCategoryFeedNames(serviceLevelAgreement);
            for (String categoryAndFeed : systemCategoryAndFeedNames) {
                // fetch and update the reference to the sla
                String categoryName = StringUtils.trim(StringUtils.substringBefore(categoryAndFeed, "."));
                String feedName = StringUtils.trim(StringUtils.substringAfterLast(categoryAndFeed, "."));
                Feed feedEntity = feedProvider.findBySystemName(categoryName, feedName);
                if (feedEntity != null) {
                    feedIds.add(feedEntity.getId().toString());
                }
            }
            return feedIds;
        }, MetadataAccess.SERVICE);
        boolean allowedToEdit = feedsOnSla.isEmpty() ? true : feedsOnSla.stream().allMatch(feedId -> feedManagerFeedService.checkFeedPermission(feedId, FeedAccessControl.EDIT_DETAILS));
        if (allowedToEdit) {
            return metadataAccess.commit(() -> {
                // Re read back in the Feeds for this session
                Set<Feed> slaFeeds = new HashSet<Feed>();
                Set<Feed.ID> slaFeedIds = new HashSet<Feed.ID>();
                feedsOnSla.stream().forEach(feedId -> {
                    Feed feedEntity = feedProvider.findById(feedProvider.resolveId(feedId));
                    if (feedEntity != null) {
                        slaFeeds.add(feedEntity);
                        slaFeedIds.add(feedEntity.getId());
                    }
                });
                if (feed != null) {
                    feedManagerFeedService.checkFeedPermission(feed.getId(), FeedAccessControl.EDIT_DETAILS);
                }
                if (feed != null) {
                    transformer.applyFeedNameToCurrentFeedProperties(serviceLevelAgreement, feed.getCategory().getSystemName(), feed.getSystemFeedName());
                }
                ServiceLevelAgreement sla = transformer.getServiceLevelAgreement(serviceLevelAgreement);
                ServiceLevelAgreementBuilder slaBuilder = null;
                com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreement.ID existingId = null;
                if (StringUtils.isNotBlank(sla.getId())) {
                    existingId = slaProvider.resolve(sla.getId());
                }
                if (existingId != null) {
                    slaBuilder = slaProvider.builder(existingId);
                } else {
                    slaBuilder = slaProvider.builder();
                }
                slaBuilder.name(sla.getName()).description(sla.getDescription());
                for (com.thinkbiganalytics.metadata.rest.model.sla.ObligationGroup group : sla.getGroups()) {
                    ObligationGroupBuilder groupBuilder = slaBuilder.obligationGroupBuilder(ObligationGroup.Condition.valueOf(group.getCondition()));
                    for (Obligation o : group.getObligations()) {
                        groupBuilder.obligationBuilder().metric(o.getMetrics()).description(o.getDescription()).build();
                    }
                    groupBuilder.build();
                }
                com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreement savedSla = slaBuilder.build();
                List<ServiceLevelAgreementActionConfiguration> actions = transformer.getActionConfigurations(serviceLevelAgreement);
                // now assign the sla checks
                slaProvider.slaCheckBuilder(savedSla.getId()).removeSlaChecks().actionConfigurations(actions).build();
                // relate them
                Set<Feed.ID> feedIds = new HashSet<>();
                FeedServiceLevelAgreementRelationship feedServiceLevelAgreementRelationship = feedSlaProvider.relateFeeds(savedSla, slaFeeds);
                if (feedServiceLevelAgreementRelationship != null && feedServiceLevelAgreementRelationship.getFeeds() != null) {
                    feedIds = feedServiceLevelAgreementRelationship.getFeeds().stream().map(f -> f.getId()).collect(Collectors.toSet());
                }
                Set<VelocityTemplate.ID> velocityTemplates = findVelocityTemplates(serviceLevelAgreement);
                // Update the JPA mapping in Ops Manager for this SLA and its related Feeds
                serviceLevelAgreementDescriptionProvider.updateServiceLevelAgreement(savedSla.getId(), savedSla.getName(), savedSla.getDescription(), feedIds, velocityTemplates);
                com.thinkbiganalytics.metadata.rest.model.sla.FeedServiceLevelAgreement restModel = serviceLevelAgreementTransform.toModel(savedSla, slaFeeds, true);
                // schedule it
                serviceLevelAgreementScheduler.scheduleServiceLevelAgreement(savedSla);
                return restModel;
            });
        }
    }
    return null;
}
Also used : ServiceLevelAgreementDescriptionProvider(com.thinkbiganalytics.metadata.api.sla.ServiceLevelAgreementDescriptionProvider) JpaVelocityTemplate(com.thinkbiganalytics.metadata.jpa.common.JpaVelocityTemplate) FeedManagerFeedService(com.thinkbiganalytics.feedmgr.service.feed.FeedManagerFeedService) Feed(com.thinkbiganalytics.metadata.api.feed.Feed) ServicesApplicationStartupListener(com.thinkbiganalytics.app.ServicesApplicationStartupListener) ServiceLevelAgreementActionConfiguration(com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreementActionConfiguration) LoggerFactory(org.slf4j.LoggerFactory) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ServiceLevelAgreementBuilder(com.thinkbiganalytics.metadata.sla.spi.ServiceLevelAgreementBuilder) ServiceLevelAgreementEmailTemplate(com.thinkbiganalytics.metadata.sla.spi.ServiceLevelAgreementEmailTemplate) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) FeedServiceLevelAgreementRelationship(com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreementRelationship) Lists(com.google.common.collect.Lists) FeedProvider(com.thinkbiganalytics.metadata.api.feed.FeedProvider) ObligationGroupBuilder(com.thinkbiganalytics.metadata.sla.spi.ObligationGroupBuilder) FeedAccessControl(com.thinkbiganalytics.metadata.api.feed.security.FeedAccessControl) FeedServiceLevelAgreement(com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreement) ServiceLevelAgreementActionValidation(com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreementActionValidation) Map(java.util.Map) AccessController(com.thinkbiganalytics.security.AccessController) FeedServicesAccessControl(com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) ObligationGroup(com.thinkbiganalytics.metadata.sla.api.ObligationGroup) FeedNotFoundException(com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException) ServiceLevelAgreement(com.thinkbiganalytics.metadata.rest.model.sla.ServiceLevelAgreement) PolicyPropertyTypes(com.thinkbiganalytics.policy.PolicyPropertyTypes) Logger(org.slf4j.Logger) Obligation(com.thinkbiganalytics.metadata.rest.model.sla.Obligation) LabelValue(com.thinkbiganalytics.rest.model.LabelValue) ServiceLevelAgreementDescription(com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreementDescription) DateTime(org.joda.time.DateTime) Set(java.util.Set) ServiceLevelAgreementProvider(com.thinkbiganalytics.metadata.sla.spi.ServiceLevelAgreementProvider) ServiceLevelAgreementActionTemplateProvider(com.thinkbiganalytics.metadata.api.sla.ServiceLevelAgreementActionTemplateProvider) FieldRuleProperty(com.thinkbiganalytics.policy.rest.model.FieldRuleProperty) Collectors(java.util.stream.Collectors) List(java.util.List) ServiceLevelAgreementActionTemplate(com.thinkbiganalytics.metadata.api.sla.ServiceLevelAgreementActionTemplate) FeedServiceLevelAgreementProvider(com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreementProvider) VelocityTemplateProvider(com.thinkbiganalytics.common.velocity.service.VelocityTemplateProvider) AccessControlException(java.security.AccessControlException) VelocityTemplate(com.thinkbiganalytics.common.velocity.model.VelocityTemplate) ServiceLevelAgreementScheduler(com.thinkbiganalytics.metadata.sla.spi.ServiceLevelAgreementScheduler) Collections(java.util.Collections) Obligation(com.thinkbiganalytics.metadata.rest.model.sla.Obligation) ServiceLevelAgreementBuilder(com.thinkbiganalytics.metadata.sla.spi.ServiceLevelAgreementBuilder) ArrayList(java.util.ArrayList) ObligationGroupBuilder(com.thinkbiganalytics.metadata.sla.spi.ObligationGroupBuilder) ServiceLevelAgreementActionConfiguration(com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreementActionConfiguration) Feed(com.thinkbiganalytics.metadata.api.feed.Feed) HashSet(java.util.HashSet) FeedServiceLevelAgreement(com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreement) ServiceLevelAgreement(com.thinkbiganalytics.metadata.rest.model.sla.ServiceLevelAgreement) FeedServiceLevelAgreementRelationship(com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreementRelationship)

Example 4 with StringUtils.substringAfterLast

use of org.apache.commons.lang3.StringUtils.substringAfterLast in project kylo by Teradata.

the class DerivedDatasourceFactory method ensureDataTransformationSourceDatasources.

/**
 * Builds the list of data sources for the specified data transformation feed.
 *
 * @param feed the feed
 * @return the list of data sources
 * @throws NullPointerException if the feed has no data transformation
 */
@Nonnull
private Set<Datasource.ID> ensureDataTransformationSourceDatasources(@Nonnull final FeedMetadata feed) {
    final Set<String> dataSetIds = new HashSet<>();
    final Set<Datasource.ID> datasources = new HashSet<>();
    if (feed.getSourceDataSets() != null) {
        List<String> datasetIds = feed.getSourceDataSets().stream().map(ds -> ds.getId()).collect(Collectors.toList());
        dataSetIds.addAll(datasetIds);
    }
    final List<String> catalogSources = feed.getDataTransformation().getCatalogDataSourceIds() != null ? feed.getDataTransformation().getCatalogDataSourceIds() : new ArrayList<>();
    // Extract nodes in chart view model
    @SuppressWarnings("unchecked") final Stream<Map<String, Object>> nodes = Optional.ofNullable(feed.getDataTransformation().getChartViewModel()).map(model -> (List<Map<String, Object>>) model.get("nodes")).map(Collection::stream).orElse(Stream.empty());
    // Create a data source for each node
    final DatasourceDefinition hiveDefinition = datasourceDefinitionProvider.findByProcessorType(DATA_TRANSFORMATION_HIVE_DEFINITION);
    final DatasourceDefinition jdbcDefinition = datasourceDefinitionProvider.findByProcessorType(DATA_TRANSFORMATION_JDBC_DEFINITION);
    nodes.forEach(node -> {
        // Filter data sets
        if (!StringUtils.equalsAnyIgnoreCase((String) node.get("datasourceId"), null, "HIVE") && node.get("dataset") != null && !Objects.equals(node.get("datasetMatchesUserDataSource"), Boolean.TRUE)) {
            dataSetIds.add((String) node.get("datasourceId"));
            return;
        }
        // Extract properties from node
        DatasourceDefinition datasourceDefinition = null;
        final Map<String, String> properties = new HashMap<>();
        String userDatasourceId = (String) node.get("datasourceId");
        if ((userDatasourceId == null && node.get("dataset") == null) || (userDatasourceId != null && userDatasourceId.equalsIgnoreCase("HIVE"))) {
            final String name = (String) node.get("name");
            datasourceDefinition = hiveDefinition;
            properties.put(HIVE_SCHEMA_KEY, StringUtils.trim(StringUtils.substringBefore(name, ".")));
            properties.put(HIVE_TABLE_KEY, StringUtils.trim(StringUtils.substringAfterLast(name, ".")));
        } else if (userDatasourceId != null) {
            final Datasource datasource = datasourceProvider.getDatasource(datasourceProvider.resolve(userDatasourceId));
            if (datasource != null) {
                datasourceDefinition = jdbcDefinition;
                properties.put(JDBC_CONNECTION_KEY, datasource.getName());
                properties.put(JDBC_TABLE_KEY, (String) node.get("name"));
                properties.putAll(parseDataTransformControllerServiceProperties(datasourceDefinition, datasource.getName()));
            }
        }
        if (datasourceDefinition != null) {
            // Create the derived data source
            final String identityString = propertyExpressionResolver.resolveVariables(datasourceDefinition.getIdentityString(), properties);
            final String title = datasourceDefinition.getTitle() != null ? propertyExpressionResolver.resolveVariables(datasourceDefinition.getTitle(), properties) : identityString;
            final String desc = propertyExpressionResolver.resolveVariables(datasourceDefinition.getDescription(), properties);
            final DerivedDatasource datasource = datasourceProvider.ensureDerivedDatasource(datasourceDefinition.getDatasourceType(), identityString, title, desc, new HashMap<>(properties));
            datasources.add(datasource.getId());
        }
    });
    // Build the data sources from the data source ids
    if (dataSetIds.isEmpty()) {
        final List<String> datasourceIds = Optional.ofNullable(feed.getDataTransformation()).map(FeedDataTransformation::getDatasourceIds).orElse(Collections.emptyList());
        datasourceIds.stream().filter(id -> !dataSetIds.contains(id) && !catalogSources.contains(id)).map(datasourceProvider::resolve).forEach(datasources::add);
    }
    return datasources;
}
Also used : FeedDataTransformation(com.thinkbiganalytics.feedmgr.rest.model.FeedDataTransformation) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) StringUtils(org.apache.commons.lang3.StringUtils) DatasourceDefinitionProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinitionProvider) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) DatasourceDefinition(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinition) TemplateProcessorDatasourceDefinition(com.thinkbiganalytics.feedmgr.rest.model.TemplateProcessorDatasourceDefinition) Map(java.util.Map) PropertyExpressionResolver(com.thinkbiganalytics.feedmgr.nifi.PropertyExpressionResolver) TableSetup(com.thinkbiganalytics.feedmgr.rest.model.schema.TableSetup) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) Nonnull(javax.annotation.Nonnull) FeedManagerTemplateService(com.thinkbiganalytics.feedmgr.service.template.FeedManagerTemplateService) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) Logger(org.slf4j.Logger) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) NifiControllerServiceProperties(com.thinkbiganalytics.feedmgr.nifi.NifiControllerServiceProperties) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Objects(java.util.Objects) RegisteredTemplateCache(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateCache) List(java.util.List) Stream(java.util.stream.Stream) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) Optional(java.util.Optional) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) Collections(java.util.Collections) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) HashMap(java.util.HashMap) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) DatasourceDefinition(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinition) TemplateProcessorDatasourceDefinition(com.thinkbiganalytics.feedmgr.rest.model.TemplateProcessorDatasourceDefinition) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 5 with StringUtils.substringAfterLast

use of org.apache.commons.lang3.StringUtils.substringAfterLast in project data-prep by Talend.

the class ResourceLoaderContentCache method getResource.

private DeletableResource getResource(ContentCacheKey key) {
    try {
        final DeletableResource[] patternMatches = resolver.getResources(CACHE_PREFIX + key.getKey() + "*");
        final DeletableResource[] directMatches = resolver.getResources(CACHE_PREFIX + key.getKey());
        final DeletableResource[] resources = new DeletableResource[patternMatches.length + directMatches.length];
        System.arraycopy(patternMatches, 0, resources, 0, patternMatches.length);
        System.arraycopy(directMatches, 0, resources, patternMatches.length, directMatches.length);
        if (resources.length <= 0) {
            return null;
        } else {
            // resources.length > 0
            final Optional<DeletableResource> reduce = stream(resources).reduce(maxBy((r1, r2) -> {
                final String suffix1 = substringAfterLast(r1.getFilename(), ".");
                final String suffix2 = substringAfterLast(r2.getFilename(), ".");
                if (StringUtils.isEmpty(suffix1) || StringUtils.isEmpty(suffix2)) {
                    return 0;
                }
                final long i1 = parseLong(suffix1);
                final long i2 = parseLong(suffix2);
                return Long.compare(i1, i2);
            }));
            return reduce.filter(r -> {
                if (!r.exists()) {
                    return false;
                }
                final String suffix = StringUtils.substringAfterLast(r.getFilename(), ".");
                if (NumberUtils.isCreatable(suffix)) {
                    final long time = parseLong(suffix);
                    return time > System.currentTimeMillis();
                } else {
                    return true;
                }
            }).orElse(null);
        }
    } catch (IOException e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : VolumeMetered(org.talend.dataprep.metrics.VolumeMetered) TDPException(org.talend.dataprep.exception.TDPException) ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) BinaryOperator.maxBy(java.util.function.BinaryOperator.maxBy) OutputStream(java.io.OutputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Logger(org.slf4j.Logger) StringUtils.substringAfterLast(org.apache.commons.lang3.StringUtils.substringAfterLast) DeletableResource(org.talend.daikon.content.DeletableResource) Optional.ofNullable(java.util.Optional.ofNullable) Predicate(java.util.function.Predicate) IOException(java.io.IOException) ContentCache(org.talend.dataprep.cache.ContentCache) Component(org.springframework.stereotype.Component) ContentServiceEnabled(org.talend.daikon.content.ContentServiceEnabled) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) CommonErrorCodes(org.talend.dataprep.exception.error.CommonErrorCodes) ResourceResolver(org.talend.daikon.content.ResourceResolver) Long.parseLong(java.lang.Long.parseLong) Arrays.stream(java.util.Arrays.stream) Timed(org.talend.dataprep.metrics.Timed) InputStream(java.io.InputStream) TDPException(org.talend.dataprep.exception.TDPException) DeletableResource(org.talend.daikon.content.DeletableResource) IOException(java.io.IOException)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)7 List (java.util.List)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 Map (java.util.Map)5 File (java.io.File)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)2 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 StaticJavaParser (com.github.javaparser.StaticJavaParser)2 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)2 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)2