Java Code Examples for org.elasticsearch.common.Strings#hasText()

The following examples show how to use org.elasticsearch.common.Strings#hasText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentParser.java    From crate with Apache License 2.0 6 votes vote down vote up
private static String[] splitAndValidatePath(String fullFieldPath) {
    if (fullFieldPath.contains(".")) {
        String[] parts = fullFieldPath.split("\\.");
        for (String part : parts) {
            if (Strings.hasText(part) == false) {
                // check if the field name contains only whitespace
                if (Strings.isEmpty(part) == false) {
                    throw new IllegalArgumentException(
                            "object field cannot contain only whitespace: ['" + fullFieldPath + "']");
                }
                throw new IllegalArgumentException(
                        "object field starting or ending with a [.] makes object resolution ambiguous: [" + fullFieldPath + "]");
            }
        }
        return parts;
    } else {
        if (Strings.isEmpty(fullFieldPath)) {
            throw new IllegalArgumentException("field name cannot be an empty string");
        }
        return new String[] {fullFieldPath};
    }
}
 
Example 2
Source File: S3Service.java    From crate with Apache License 2.0 6 votes vote down vote up
static ClientConfiguration buildConfiguration(S3ClientSettings clientSettings) {
    final ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(clientSettings.protocol);

    if (Strings.hasText(clientSettings.proxyHost)) {
        // TODO: remove this leniency, these settings should exist together and be validated
        clientConfiguration.setProxyHost(clientSettings.proxyHost);
        clientConfiguration.setProxyPort(clientSettings.proxyPort);
        clientConfiguration.setProxyUsername(clientSettings.proxyUsername);
        clientConfiguration.setProxyPassword(clientSettings.proxyPassword);
    }

    clientConfiguration.setMaxErrorRetry(clientSettings.maxRetries);
    clientConfiguration.setUseThrottleRetries(clientSettings.throttleRetries);
    clientConfiguration.setSocketTimeout(clientSettings.readTimeoutMillis);

    return clientConfiguration;
}
 
Example 3
Source File: AwsEc2ServiceImpl.java    From crate with Apache License 2.0 6 votes vote down vote up
private AmazonEC2 buildClient(Ec2ClientSettings clientSettings) {
    final AWSCredentialsProvider credentials = buildCredentials(LOGGER, clientSettings);
    final ClientConfiguration configuration = buildConfiguration(LOGGER, clientSettings);
    final AmazonEC2 client = buildClient(credentials, configuration);
    if (Strings.hasText(clientSettings.endpoint)) {
        LOGGER.debug("using explicit ec2 endpoint [{}]", clientSettings.endpoint);
        client.setEndpoint(clientSettings.endpoint);
    } else {
        Region currentRegion = Regions.getCurrentRegion();
        if (currentRegion != null) {
            LOGGER.debug("using ec2 region [{}]", currentRegion);
            client.setRegion(currentRegion);
        }
    }
    return client;
}
 
Example 4
Source File: AliasValidator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void validateAliasStandalone(String alias, String indexRouting) {
    if (!Strings.hasText(alias)) {
        throw new IllegalArgumentException("alias name is required");
    }
    if (indexRouting != null && indexRouting.indexOf(',') != -1) {
        throw new IllegalArgumentException("alias [" + alias + "] has several index routing values associated with it");
    }
}
 
Example 5
Source File: IndicesAliasesRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public ActionRequestValidationException validate() {
    ActionRequestValidationException validationException = null;
    if (allAliasActions.isEmpty()) {
        return addValidationError("Must specify at least one alias action", validationException);
    }
    for (AliasActions aliasAction : allAliasActions) {
        if (CollectionUtils.isEmpty(aliasAction.aliases)) {
            validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
                    + "]: Property [alias/aliases] is either missing or null", validationException);
        } else {
            for (String alias : aliasAction.aliases) {
                if (!Strings.hasText(alias)) {
                    validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
                        + "]: [alias/aliases] may not be empty string", validationException);
                }
            }
        }
        if (CollectionUtils.isEmpty(aliasAction.indices)) {
            validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
                    + "]: Property [index/indices] is either missing or null", validationException);
        } else {
            for (String index : aliasAction.indices) {
                if (!Strings.hasText(index)) {
                    validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
                            + "]: [index/indices] may not be empty string", validationException);
                }
            }
        }
    }
    return validationException;
}
 
Example 6
Source File: DeleteWarmerRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private ActionRequestValidationException checkForEmptyString(ActionRequestValidationException validationException, String[] strings) {
    boolean containsEmptyString = false;
    for (String string : strings) {
        if (!Strings.hasText(string)) {
            containsEmptyString = true;
        }
    }
    if (containsEmptyString) {
        validationException = addValidationError("types must not contain empty strings", validationException);
    }
    return validationException;
}
 
Example 7
Source File: GetIndexTemplatesRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public ActionRequestValidationException validate() {
    ActionRequestValidationException validationException = null;
    if (names == null) {
        validationException = addValidationError("names is null or empty", validationException);
    } else {
        for (String name : names) {
            if (name == null || !Strings.hasText(name)) {
                validationException = addValidationError("name is missing", validationException);
            }
        }
    }
    return validationException;
}
 
Example 8
Source File: AliasValidator.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Validate a proposed alias.
 */
public void validateAlias(String alias, String index, @Nullable String indexRouting, Function<String, IndexMetaData> indexLookup) {
    validateAliasStandalone(alias, indexRouting);

    if (!Strings.hasText(index)) {
        throw new IllegalArgumentException("index name is required");
    }

    IndexMetaData indexNamedSameAsAlias = indexLookup.apply(alias);
    if (indexNamedSameAsAlias != null) {
        throw new InvalidAliasNameException(indexNamedSameAsAlias.getIndex(), alias, "an index exists with the same name as the alias");
    }
}
 
Example 9
Source File: AliasValidator.java    From crate with Apache License 2.0 5 votes vote down vote up
void validateAliasStandalone(String alias, String indexRouting) {
    if (!Strings.hasText(alias)) {
        throw new IllegalArgumentException("alias name is required");
    }
    MetaDataCreateIndexService.validateIndexOrAliasName(alias, InvalidAliasNameException::new);
    if (indexRouting != null && indexRouting.indexOf(',') != -1) {
        throw new IllegalArgumentException("alias [" + alias + "] has several index routing values associated with it");
    }
}
 
Example 10
Source File: AwsEc2ServiceImpl.java    From crate with Apache License 2.0 5 votes vote down vote up
static ClientConfiguration buildConfiguration(Logger logger, Ec2ClientSettings clientSettings) {
    final ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(clientSettings.protocol);
    if (Strings.hasText(clientSettings.proxyHost)) {
        // TODO: remove this leniency, these settings should exist together and be validated
        clientConfiguration.setProxyHost(clientSettings.proxyHost);
        clientConfiguration.setProxyPort(clientSettings.proxyPort);
        clientConfiguration.setProxyUsername(clientSettings.proxyUsername);
        clientConfiguration.setProxyPassword(clientSettings.proxyPassword);
    }
    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    final RetryPolicy retryPolicy = new RetryPolicy(
        RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
        (originalRequest, exception, retriesAttempted) -> {
            // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
            logger.warn("EC2 API request failed, retry again. Reason was:", exception);
            return 1000L * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
        },
        10,
        false);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout(clientSettings.readTimeoutMillis);
    return clientConfiguration;
}
 
Example 11
Source File: HdfsRepository.java    From crate with Apache License 2.0 5 votes vote down vote up
public HdfsRepository(RepositoryMetaData metadata, Environment environment,
                      NamedXContentRegistry namedXContentRegistry, ThreadPool threadPool) {
    super(metadata, environment.settings(), namedXContentRegistry, threadPool, BlobPath.cleanPath());

    this.environment = environment;
    this.chunkSize = metadata.settings().getAsBytesSize("chunk_size", null);

    String uriSetting = getMetadata().settings().get("uri");
    if (Strings.hasText(uriSetting) == false) {
        throw new IllegalArgumentException("No 'uri' defined for hdfs snapshot/restore");
    }
    uri = URI.create(uriSetting);
    if ("hdfs".equalsIgnoreCase(uri.getScheme()) == false) {
        throw new IllegalArgumentException(String.format(Locale.ROOT,
            "Invalid scheme [%s] specified in uri [%s]; only 'hdfs' uri allowed for hdfs snapshot/restore",
            uri.getScheme(), uriSetting));
    }
    if (Strings.hasLength(uri.getPath()) && uri.getPath().equals("/") == false) {
        throw new IllegalArgumentException(String.format(Locale.ROOT,
            "Use 'path' option to specify a path [%s], not the uri [%s] for hdfs snapshot/restore", uri.getPath(), uriSetting));
    }

    pathSetting = getMetadata().settings().get("path");
    // get configuration
    if (pathSetting == null) {
        throw new IllegalArgumentException("No 'path' defined for hdfs snapshot/restore");
    }
}
 
Example 12
Source File: AzureStorageSettings.java    From crate with Apache License 2.0 5 votes vote down vote up
public String buildConnectionString() {
    final StringBuilder connectionStringBuilder = new StringBuilder();
    connectionStringBuilder.append("DefaultEndpointsProtocol=https")
            .append(";AccountName=")
            .append(account)
            .append(";AccountKey=")
            .append(key);
    if (Strings.hasText(endpointSuffix)) {
        connectionStringBuilder.append(";EndpointSuffix=").append(endpointSuffix);
    }
    return connectionStringBuilder.toString();
}
 
Example 13
Source File: AzureConfiguration.java    From crate with Apache License 2.0 4 votes vote down vote up
private static boolean isPropertyMissing(Settings settings, String name) throws ElasticsearchException {
    return !Strings.hasText(settings.get(name));
}