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();
}
}
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;
}
}
}
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);
}
}
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;
}
Aggregations