Search in sources :

Example 81 with StringUtils.isEmpty

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

the class SchemaAnalysis method analyze.

@Override
public void analyze(String dataSetId) {
    if (StringUtils.isEmpty(dataSetId)) {
        throw new IllegalArgumentException("Data set id cannot be null or empty.");
    }
    DistributedLock datasetLock = repository.createDatasetMetadataLock(dataSetId);
    datasetLock.lock();
    try {
        DataSetMetadata metadata = repository.get(dataSetId);
        if (metadata == null) {
            LOGGER.info("Unable to analyze schema of data set #{}: seems to be removed.", dataSetId);
            return;
        }
        // Schema analysis
        try (Stream<DataSetRow> stream = store.stream(metadata)) {
            LOGGER.info("Analyzing schema in dataset #{}...", dataSetId);
            // Configure analyzers
            final List<ColumnMetadata> columns = metadata.getRowMetadata().getColumns();
            try (Analyzer<Analyzers.Result> analyzer = analyzerService.schemaAnalysis(columns)) {
                // Determine schema for the content.
                stream.limit(100).map(row -> row.toArray(DataSetRow.SKIP_TDP_ID)).forEach(analyzer::analyze);
                // Find the best suitable type
                adapter.adapt(columns, analyzer.getResult());
                LOGGER.info("Analyzed schema in dataset #{}.", dataSetId);
                metadata.getLifecycle().schemaAnalyzed(true);
                DataSetMetadata savedDataSetMetadata = repository.get(dataSetId);
                // in order to check that the dataset was not deleted during analysis
                if (savedDataSetMetadata != null) {
                    repository.save(metadata);
                } else {
                    // $NON-NLS-1$
                    LOGGER.info("Data set #{} no longer exists.", dataSetId);
                }
            }
        } catch (Exception e) {
            LOGGER.error("Unable to analyse schema for dataset " + dataSetId + ".", e);
            TDPException.rethrowOrWrap(e, UNABLE_TO_ANALYZE_COLUMN_TYPES);
        }
    } finally {
        datasetLock.unlock();
    }
}
Also used : Analyzers(org.talend.dataquality.common.inference.Analyzers) TDPException(org.talend.dataprep.exception.TDPException) Logger(org.slf4j.Logger) DataSetMetadataRepository(org.talend.dataprep.dataset.store.metadata.DataSetMetadataRepository) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) AnalyzerService(org.talend.dataprep.quality.AnalyzerService) List(java.util.List) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) UNABLE_TO_ANALYZE_COLUMN_TYPES(org.talend.dataprep.exception.error.DataSetErrorCodes.UNABLE_TO_ANALYZE_COLUMN_TYPES) DistributedLock(org.talend.dataprep.lock.DistributedLock) StatisticsAdapter(org.talend.dataprep.dataset.StatisticsAdapter) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Analyzer(org.talend.dataquality.common.inference.Analyzer) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) ContentStoreRouter(org.talend.dataprep.dataset.store.content.ContentStoreRouter) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) DistributedLock(org.talend.dataprep.lock.DistributedLock) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) TDPException(org.talend.dataprep.exception.TDPException)

Example 82 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project accumulo by apache.

the class PrintInfo method execute.

@SuppressFBWarnings(value = "DM_EXIT", justification = "System.exit is fine here because it's a utility class executed by a main()")
@Override
public void execute(final String[] args) throws Exception {
    Opts opts = new Opts();
    opts.parseArgs("accumulo rfile-info", args);
    if (opts.files.isEmpty()) {
        System.err.println("No files were given");
        System.exit(1);
    }
    if ((opts.fullKeys || opts.dump) && opts.formatterClazz != null) {
        System.err.println("--formatter argument is incompatible with --dump or --fullKeys, specify either, not both.");
        System.exit(1);
    }
    var siteConfig = opts.getSiteConfiguration();
    Configuration conf = new Configuration();
    for (String confFile : opts.configFiles) {
        log.debug("Adding Hadoop configuration file {}", confFile);
        conf.addResource(new Path(confFile));
    }
    LogHistogram kvHistogram = new LogHistogram();
    KeyStats dataKeyStats = new KeyStats();
    KeyStats indexKeyStats = new KeyStats();
    for (String arg : opts.files) {
        Path path = new Path(arg);
        FileSystem fs = resolveFS(log, conf, path);
        System.out.println("Reading file: " + path.makeQualified(fs.getUri(), fs.getWorkingDirectory()));
        printCryptoParams(path, fs);
        CachableBuilder cb = new CachableBuilder().fsPath(fs, path).conf(conf).cryptoService(CryptoServiceFactory.newInstance(siteConfig, ClassloaderType.JAVA));
        Reader iter = new RFile.Reader(cb);
        MetricsGatherer<Map<String, ArrayList<VisibilityMetric>>> vmg = new VisMetricsGatherer();
        if (opts.vis || opts.hash) {
            iter.registerMetrics(vmg);
        }
        iter.printInfo(opts.printIndex);
        System.out.println();
        String propsPath = opts.getPropertiesPath();
        String[] mainArgs = propsPath == null ? new String[] { arg } : new String[] { "-props", propsPath, arg };
        org.apache.accumulo.core.file.rfile.bcfile.PrintInfo.main(mainArgs);
        Map<String, ArrayList<ByteSequence>> localityGroupCF = null;
        if (opts.histogram || opts.dump || opts.vis || opts.hash || opts.keyStats || opts.fullKeys || !StringUtils.isEmpty(opts.formatterClazz)) {
            localityGroupCF = iter.getLocalityGroupCF();
            FileSKVIterator dataIter;
            if (opts.useSample) {
                dataIter = iter.getSample();
                if (dataIter == null) {
                    System.out.println("ERROR : This rfile has no sample data");
                    return;
                }
            } else {
                dataIter = iter;
            }
            if (opts.keyStats) {
                FileSKVIterator indexIter = iter.getIndex();
                while (indexIter.hasTop()) {
                    indexKeyStats.add(indexIter.getTopKey());
                    indexIter.next();
                }
            }
            BiFunction<Key, Value, String> formatter = null;
            if (opts.formatterClazz != null) {
                final Class<? extends BiFunction<Key, Value, String>> formatterClass = getFormatter(opts.formatterClazz);
                formatter = formatterClass.getConstructor().newInstance();
            } else if (opts.fullKeys) {
                formatter = (key, value) -> key.toStringNoTruncate() + " -> " + value;
            } else if (opts.dump) {
                formatter = (key, value) -> key + " -> " + value;
            }
            for (String lgName : localityGroupCF.keySet()) {
                LocalityGroupUtil.seek(dataIter, new Range(), lgName, localityGroupCF);
                while (dataIter.hasTop()) {
                    Key key = dataIter.getTopKey();
                    Value value = dataIter.getTopValue();
                    if (formatter != null) {
                        System.out.println(formatter.apply(key, value));
                        if (System.out.checkError())
                            return;
                    }
                    if (opts.histogram) {
                        kvHistogram.add(key.getSize() + value.getSize());
                    }
                    if (opts.keyStats) {
                        dataKeyStats.add(key);
                    }
                    dataIter.next();
                }
            }
        }
        if (opts.printSummary) {
            SummaryReader.print(iter, System.out);
        }
        iter.close();
        if (opts.vis || opts.hash) {
            System.out.println();
            vmg.printMetrics(opts.hash, "Visibility", System.out);
        }
        if (opts.histogram) {
            System.out.println();
            kvHistogram.print("");
        }
        if (opts.keyStats) {
            System.out.println();
            System.out.println("Statistics for keys in data :");
            dataKeyStats.print("\t");
            System.out.println();
            System.out.println("Statistics for keys in index :");
            indexKeyStats.print("\t");
        }
        // If the output stream has closed, there is no reason to keep going.
        if (System.out.checkError()) {
            return;
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ConfigOpts(org.apache.accumulo.core.cli.ConfigOpts) ByteSequence(org.apache.accumulo.core.data.ByteSequence) Arrays(java.util.Arrays) CryptoUtils(org.apache.accumulo.core.crypto.CryptoUtils) NoFileEncrypter(org.apache.accumulo.core.spi.crypto.NoFileEncrypter) Parameter(com.beust.jcommander.Parameter) FileSystem(org.apache.hadoop.fs.FileSystem) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) LocalityGroupUtil(org.apache.accumulo.core.util.LocalityGroupUtil) Reader(org.apache.accumulo.core.file.rfile.RFile.Reader) Map(java.util.Map) Key(org.apache.accumulo.core.data.Key) Configuration(org.apache.hadoop.conf.Configuration) Path(org.apache.hadoop.fs.Path) Value(org.apache.accumulo.core.data.Value) KeywordExecutable(org.apache.accumulo.start.spi.KeywordExecutable) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) Utils(org.apache.accumulo.core.file.rfile.bcfile.Utils) Logger(org.slf4j.Logger) NumUtil(org.apache.accumulo.core.util.NumUtil) FileSKVIterator(org.apache.accumulo.core.file.FileSKVIterator) IOException(java.io.IOException) ClassloaderType(org.apache.accumulo.core.crypto.CryptoServiceFactory.ClassloaderType) SummaryReader(org.apache.accumulo.core.summary.SummaryReader) CryptoServiceFactory(org.apache.accumulo.core.crypto.CryptoServiceFactory) CachableBuilder(org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile.CachableBuilder) Range(org.apache.accumulo.core.data.Range) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) List(java.util.List) AutoService(com.google.auto.service.AutoService) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) FileSKVIterator(org.apache.accumulo.core.file.FileSKVIterator) Configuration(org.apache.hadoop.conf.Configuration) ConfigOpts(org.apache.accumulo.core.cli.ConfigOpts) ArrayList(java.util.ArrayList) Reader(org.apache.accumulo.core.file.rfile.RFile.Reader) SummaryReader(org.apache.accumulo.core.summary.SummaryReader) Range(org.apache.accumulo.core.data.Range) FileSystem(org.apache.hadoop.fs.FileSystem) Value(org.apache.accumulo.core.data.Value) CachableBuilder(org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile.CachableBuilder) Map(java.util.Map) Key(org.apache.accumulo.core.data.Key) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 83 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceImpl method create.

@Override
public SubscriptionEntity create(NewSubscriptionEntity newSubscriptionEntity, String customApiKey) {
    String plan = newSubscriptionEntity.getPlan();
    String application = newSubscriptionEntity.getApplication();
    try {
        logger.debug("Create a new subscription for plan {} and application {}", plan, application);
        PlanEntity planEntity = planService.findById(plan);
        if (planEntity.getStatus() == PlanStatus.DEPRECATED) {
            throw new PlanNotSubscribableException(plan);
        }
        if (planEntity.getStatus() == PlanStatus.CLOSED) {
            throw new PlanAlreadyClosedException(plan);
        }
        if (planEntity.getStatus() == PlanStatus.STAGING) {
            throw new PlanNotYetPublishedException(plan);
        }
        if (planEntity.getSecurity() == PlanSecurityType.KEY_LESS) {
            throw new PlanNotSubscribableException("A key_less plan is not subscribable !");
        }
        if (planEntity.getExcludedGroups() != null && !planEntity.getExcludedGroups().isEmpty()) {
            final boolean userAuthorizedToAccessApiData = groupService.isUserAuthorizedToAccessApiData(apiService.findById(planEntity.getApi()), planEntity.getExcludedGroups(), getAuthenticatedUsername());
            if (!userAuthorizedToAccessApiData && !isAdmin()) {
                throw new PlanRestrictedException(plan);
            }
        }
        if (planEntity.getGeneralConditions() != null && !planEntity.getGeneralConditions().isEmpty()) {
            if ((Boolean.FALSE.equals(newSubscriptionEntity.getGeneralConditionsAccepted()) || (newSubscriptionEntity.getGeneralConditionsContentRevision() == null))) {
                throw new PlanGeneralConditionAcceptedException(planEntity.getName());
            }
            PageEntity generalConditions = pageService.findById(planEntity.getGeneralConditions());
            if (!generalConditions.getContentRevisionId().equals(newSubscriptionEntity.getGeneralConditionsContentRevision())) {
                throw new PlanGeneralConditionRevisionException(planEntity.getName());
            }
        }
        ApplicationEntity applicationEntity = applicationService.findById(application);
        if (ApplicationStatus.ARCHIVED.name().equals(applicationEntity.getStatus())) {
            throw new ApplicationArchivedException(applicationEntity.getName());
        }
        // Check existing subscriptions
        List<Subscription> subscriptions = subscriptionRepository.search(new SubscriptionCriteria.Builder().applications(Collections.singleton(application)).apis(Collections.singleton(planEntity.getApi())).build());
        if (!subscriptions.isEmpty()) {
            Predicate<Subscription> onlyValidSubs = subscription -> subscription.getStatus() != Subscription.Status.REJECTED && subscription.getStatus() != Subscription.Status.CLOSED;
            // First, check that there is no subscription to the same plan
            long subscriptionCount = subscriptions.stream().filter(onlyValidSubs).filter(subscription -> subscription.getPlan().equals(plan)).count();
            if (subscriptionCount > 0) {
                throw new PlanAlreadySubscribedException(plan);
            }
            // Check that there is no existing subscription based on an OAuth2 or JWT plan
            if (planEntity.getSecurity() == PlanSecurityType.OAUTH2 || planEntity.getSecurity() == PlanSecurityType.JWT) {
                long count = subscriptions.stream().filter(onlyValidSubs).map(Subscription::getPlan).distinct().map(plan1 -> planService.findById(plan1)).filter(subPlan -> subPlan.getSecurity() == PlanSecurityType.OAUTH2 || subPlan.getSecurity() == PlanSecurityType.JWT).count();
                if (count > 0) {
                    throw new PlanOAuth2OrJWTAlreadySubscribedException("An other OAuth2 or JWT plan is already subscribed by the same application.");
                }
            }
        }
        // Extract the client_id according to the application type
        String clientId;
        if (ApplicationType.SIMPLE.name().equals(applicationEntity.getType())) {
            clientId = (applicationEntity.getSettings() != null && applicationEntity.getSettings().getApp() != null) ? applicationEntity.getSettings().getApp().getClientId() : null;
        } else {
            clientId = (applicationEntity.getSettings() != null && applicationEntity.getSettings().getoAuthClient() != null) ? applicationEntity.getSettings().getoAuthClient().getClientId() : null;
        }
        if (planEntity.getSecurity() == PlanSecurityType.OAUTH2 || planEntity.getSecurity() == PlanSecurityType.JWT) {
            // Check that the application contains a client_id
            if (clientId == null || clientId.trim().isEmpty()) {
                throw new PlanNotSubscribableException("A client_id is required to subscribe to an OAuth2 or JWT plan.");
            }
        }
        Subscription subscription = new Subscription();
        subscription.setPlan(plan);
        subscription.setId(UuidString.generateRandom());
        subscription.setApplication(application);
        subscription.setCreatedAt(new Date());
        subscription.setUpdatedAt(subscription.getCreatedAt());
        subscription.setStatus(Subscription.Status.PENDING);
        subscription.setRequest(newSubscriptionEntity.getRequest());
        subscription.setSubscribedBy(getAuthenticatedUser().getUsername());
        subscription.setClientId(clientId);
        String apiId = planEntity.getApi();
        subscription.setApi(apiId);
        subscription.setGeneralConditionsAccepted(newSubscriptionEntity.getGeneralConditionsAccepted());
        if (newSubscriptionEntity.getGeneralConditionsContentRevision() != null) {
            subscription.setGeneralConditionsContentRevision(newSubscriptionEntity.getGeneralConditionsContentRevision().getRevision());
            subscription.setGeneralConditionsContentPageId(newSubscriptionEntity.getGeneralConditionsContentRevision().getPageId());
        }
        subscription = subscriptionRepository.create(subscription);
        createAudit(apiId, application, SUBSCRIPTION_CREATED, subscription.getCreatedAt(), null, subscription);
        final ApiModelEntity api = apiService.findByIdForTemplates(apiId);
        final PrimaryOwnerEntity apiOwner = api.getPrimaryOwner();
        // final PrimaryOwnerEntity appOwner = applicationEntity.getPrimaryOwner();
        String managementURL = parameterService.find(Key.MANAGEMENT_URL, ParameterReferenceType.ORGANIZATION);
        String subscriptionsUrl = "";
        if (!StringUtils.isEmpty(managementURL)) {
            if (managementURL.endsWith("/")) {
                managementURL = managementURL.substring(0, managementURL.length() - 1);
            }
            subscriptionsUrl = managementURL + "/#!/environments/" + GraviteeContext.getCurrentEnvironmentOrDefault() + "/apis/" + api.getId() + "/subscriptions/" + subscription.getId();
        }
        final Map<String, Object> params = new NotificationParamsBuilder().api(api).plan(planEntity).application(applicationEntity).owner(apiOwner).subscription(convert(subscription)).subscriptionsUrl(subscriptionsUrl).build();
        if (PlanValidationType.AUTO == planEntity.getValidation()) {
            ProcessSubscriptionEntity process = new ProcessSubscriptionEntity();
            process.setId(subscription.getId());
            process.setAccepted(true);
            process.setStartingAt(new Date());
            process.setCustomApiKey(customApiKey);
            // Do process
            return process(process, SUBSCRIPTION_SYSTEM_VALIDATOR);
        } else {
            notifierService.trigger(ApiHook.SUBSCRIPTION_NEW, apiId, params);
            notifierService.trigger(ApplicationHook.SUBSCRIPTION_NEW, application, params);
            return convert(subscription);
        }
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to subscribe to the plan {}", plan, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to subscribe to the plan %s", plan), ex);
    }
}
Also used : io.gravitee.rest.api.service(io.gravitee.rest.api.service) java.util(java.util) System.lineSeparator(java.lang.System.lineSeparator) Pageable(io.gravitee.rest.api.model.common.Pageable) ApiHook(io.gravitee.rest.api.service.notification.ApiHook) Page(io.gravitee.common.data.domain.Page) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Autowired(org.springframework.beans.factory.annotation.Autowired) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) StringUtils(org.apache.commons.lang3.StringUtils) Subscription(io.gravitee.repository.management.model.Subscription) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) AuditEvent(io.gravitee.repository.management.model.Subscription.AuditEvent) io.gravitee.rest.api.model(io.gravitee.rest.api.model) API(io.gravitee.repository.management.model.Audit.AuditProperties.API) APPLICATION(io.gravitee.repository.management.model.Audit.AuditProperties.APPLICATION) ApplicationHook(io.gravitee.rest.api.service.notification.ApplicationHook) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) UuidString(io.gravitee.rest.api.service.common.UuidString) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Logger(org.slf4j.Logger) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) Collections.emptyList(java.util.Collections.emptyList) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Predicate(java.util.function.Predicate) ApplicationType(io.gravitee.repository.management.model.ApplicationType) Audit(io.gravitee.repository.management.model.Audit) Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) Collectors(java.util.stream.Collectors) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) Key(io.gravitee.rest.api.model.parameters.Key) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder) io.gravitee.rest.api.service.exceptions(io.gravitee.rest.api.service.exceptions) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationStatus(io.gravitee.repository.management.model.ApplicationStatus) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder) UuidString(io.gravitee.rest.api.service.common.UuidString) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder) Subscription(io.gravitee.repository.management.model.Subscription)

Example 84 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project gravitee-management-rest-api by gravitee-io.

the class OAIToAPIConverter method visit.

@Override
public SwaggerApiEntity visit(OpenAPI oai) {
    final SwaggerApiEntity apiEntity = new SwaggerApiEntity();
    // Name
    apiEntity.setName(oai.getInfo().getTitle());
    // Description
    apiEntity.setDescription(oai.getInfo().getDescription() == null ? "Description of " + apiEntity.getName() : oai.getInfo().getDescription());
    // Version
    apiEntity.setVersion(oai.getInfo().getVersion());
    fill(apiEntity, oai);
    // Use X-Gravitee to add information in API
    XGraviteeIODefinition xGraviteeIODefinition = null;
    if (oai.getExtensions() != null && oai.getExtensions().get(X_GRAVITEEIO_DEFINITION_VENDOR_EXTENSION) != null) {
        xGraviteeIODefinition = new ObjectMapper().convertValue(oai.getExtensions().get(X_GRAVITEEIO_DEFINITION_VENDOR_EXTENSION), XGraviteeIODefinition.class);
    }
    // Proxy
    Proxy proxy = new Proxy();
    String defaultEndpoint = null;
    // Proxy - Endpoints
    if (!oai.getServers().isEmpty()) {
        List<String> evaluatedServerUrl = mapServersToEndpoint(oai.getServers());
        EndpointGroup defaultGroup = new EndpointGroup();
        defaultGroup.setName("default-group");
        if (evaluatedServerUrl == null) {
            defaultGroup.setEndpoints(singleton(new HttpEndpoint("default", defaultEndpoint)));
        } else if (evaluatedServerUrl.size() == 1) {
            defaultEndpoint = evaluatedServerUrl.get(0);
            defaultGroup.setEndpoints(singleton(new HttpEndpoint("default", defaultEndpoint)));
        } else {
            defaultEndpoint = evaluatedServerUrl.get(0);
            defaultGroup.setEndpoints(new HashSet<>());
            for (int i = 0; i < evaluatedServerUrl.size(); i++) {
                defaultGroup.getEndpoints().add(new HttpEndpoint("server" + (i + 1), evaluatedServerUrl.get(i)));
            }
        }
        proxy.setGroups(singleton(defaultGroup));
        apiEntity.setProxy(proxy);
    }
    // Proxy - Context Path / Virtual Host
    if (xGraviteeIODefinition != null && xGraviteeIODefinition.getVirtualHosts() != null) {
        proxy.setVirtualHosts(xGraviteeIODefinition.getVirtualHosts().stream().map(vHost -> new VirtualHost(vHost.getHost(), vHost.getPath(), (vHost.getOverrideEntrypoint() != null ? vHost.getOverrideEntrypoint() : false))).collect(Collectors.toList()));
    } else {
        String contextPath = null;
        if (defaultEndpoint != null) {
            contextPath = URI.create(defaultEndpoint).getPath();
        }
        if (contextPath == null || contextPath.equals("/")) {
            contextPath = apiEntity.getName().replaceAll("\\s+", "").toLowerCase();
        }
        proxy.setVirtualHosts(singletonList(new VirtualHost(contextPath)));
    }
    apiEntity.setProxy(proxy);
    // Add xGraviteeIODefinition config
    if (xGraviteeIODefinition != null) {
        // Categories
        if (xGraviteeIODefinition.getCategories() != null && !xGraviteeIODefinition.getCategories().isEmpty()) {
            apiEntity.setCategories(new HashSet<>(xGraviteeIODefinition.getCategories()));
        }
        // Groups
        if (xGraviteeIODefinition.getGroups() != null && !xGraviteeIODefinition.getGroups().isEmpty()) {
            // Groups in schema are group name. Replace them by id
            Set<String> groupIdsToImport = xGraviteeIODefinition.getGroups().stream().flatMap(group -> groupService.findByName(group).stream()).map(GroupEntity::getId).collect(Collectors.toSet());
            apiEntity.setGroups(groupIdsToImport);
        }
        // Labels
        if (xGraviteeIODefinition.getLabels() != null && !xGraviteeIODefinition.getLabels().isEmpty()) {
            apiEntity.setLabels(xGraviteeIODefinition.getLabels());
        }
        // Metadata
        if (xGraviteeIODefinition.getMetadata() != null && !xGraviteeIODefinition.getMetadata().isEmpty()) {
            final List<ApiMetadataEntity> apiMetadataEntities = xGraviteeIODefinition.getMetadata().stream().map(metadata -> {
                ApiMetadataEntity apiMetadata = new ApiMetadataEntity();
                apiMetadata.setKey(IdGenerator.generate(metadata.getName()));
                apiMetadata.setName(metadata.getName());
                apiMetadata.setValue(metadata.getValue());
                apiMetadata.setFormat(metadata.getFormat() != null ? MetadataFormat.valueOf(metadata.getFormat().name()) : MetadataFormat.STRING);
                return apiMetadata;
            }).collect(Collectors.toList());
            apiEntity.setMetadata(apiMetadataEntities);
        }
        // Picture
        if (xGraviteeIODefinition.getPicture() != null && !StringUtils.isEmpty(xGraviteeIODefinition.getPicture())) {
            if (xGraviteeIODefinition.getPicture().matches(PICTURE_REGEX)) {
                apiEntity.setPicture(xGraviteeIODefinition.getPicture());
            }
        }
        // Properties
        if (xGraviteeIODefinition.getProperties() != null && !xGraviteeIODefinition.getProperties().isEmpty()) {
            Properties properties = new Properties();
            properties.setProperties(xGraviteeIODefinition.getProperties().stream().map(prop -> new Property(prop.getKey(), prop.getValue())).collect(Collectors.toList()));
            apiEntity.setProperties(properties);
        }
        // Tags
        if (xGraviteeIODefinition.getTags() != null && !xGraviteeIODefinition.getTags().isEmpty()) {
            final Map<String, String> tagMap = tagService.findByReference(GraviteeContext.getCurrentOrganization(), TagReferenceType.ORGANIZATION).stream().collect(toMap(TagEntity::getId, TagEntity::getName));
            final Set<String> tagIdToAdd = xGraviteeIODefinition.getTags().stream().map(tag -> findTagIdByName(tagMap, tag)).filter(Objects::nonNull).collect(Collectors.toSet());
            if (tagIdToAdd != null && !tagIdToAdd.isEmpty()) {
                apiEntity.setTags(tagIdToAdd);
            }
        }
        // Visibility
        if (xGraviteeIODefinition.getVisibility() != null) {
            apiEntity.setVisibility(Visibility.valueOf(xGraviteeIODefinition.getVisibility().name()));
        }
    }
    return apiEntity;
}
Also used : java.util(java.util) Properties(io.gravitee.definition.model.Properties) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) Operation(io.swagger.v3.oas.models.Operation) PolicyOperationVisitorManager(io.gravitee.rest.api.service.impl.swagger.policy.PolicyOperationVisitorManager) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) OAIOperationVisitor(io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor) PolicyHelper.clearNullValues(io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues) IdGenerator(io.gravitee.common.utils.IdGenerator) Matcher(java.util.regex.Matcher) Collections.singleton(java.util.Collections.singleton) Collectors.toMap(java.util.stream.Collectors.toMap) OpenAPI(io.swagger.v3.oas.models.OpenAPI) io.gravitee.rest.api.model(io.gravitee.rest.api.model) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) ServerVariables(io.swagger.v3.oas.models.servers.ServerVariables) URI(java.net.URI) GroupService(io.gravitee.rest.api.service.GroupService) XGraviteeIODefinition(io.gravitee.rest.api.service.swagger.converter.extension.XGraviteeIODefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PathItem(io.swagger.v3.oas.models.PathItem) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) io.gravitee.definition.model(io.gravitee.definition.model) Collectors(java.util.stream.Collectors) OAIDescriptorVisitor(io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIDescriptorVisitor) Consumer(java.util.function.Consumer) Server(io.swagger.v3.oas.models.servers.Server) HttpMethod(io.gravitee.common.http.HttpMethod) CollectionUtils(org.springframework.util.CollectionUtils) SwaggerApiEntity(io.gravitee.rest.api.model.api.SwaggerApiEntity) ServerVariable(io.swagger.v3.oas.models.servers.ServerVariable) Pattern(java.util.regex.Pattern) Policy(io.gravitee.policy.api.swagger.Policy) TagService(io.gravitee.rest.api.service.TagService) Properties(io.gravitee.definition.model.Properties) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) XGraviteeIODefinition(io.gravitee.rest.api.service.swagger.converter.extension.XGraviteeIODefinition) SwaggerApiEntity(io.gravitee.rest.api.model.api.SwaggerApiEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)76 List (java.util.List)53 Collectors (java.util.stream.Collectors)40 ArrayList (java.util.ArrayList)39 Map (java.util.Map)39 HashMap (java.util.HashMap)28 IOException (java.io.IOException)26 Collections (java.util.Collections)25 Set (java.util.Set)25 Logger (org.slf4j.Logger)25 Arrays (java.util.Arrays)24 LoggerFactory (org.slf4j.LoggerFactory)24 Optional (java.util.Optional)22 HashSet (java.util.HashSet)18 Collection (java.util.Collection)14 Autowired (org.springframework.beans.factory.annotation.Autowired)13 Objects (java.util.Objects)12 UUID (java.util.UUID)11 java.util (java.util)10 Component (org.springframework.stereotype.Component)10