Search in sources :

Example 41 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project kafka by apache.

the class FileConfigProvider method get.

/**
 * Retrieves the data with the given keys at the given Properties file.
 *
 * @param path the file where the data resides
 * @param keys the keys whose values will be retrieved
 * @return the configuration data
 */
public ConfigData get(String path, Set<String> keys) {
    Map<String, String> data = new HashMap<>();
    if (path == null || path.isEmpty()) {
        return new ConfigData(data);
    }
    try (Reader reader = reader(path)) {
        Properties properties = new Properties();
        properties.load(reader);
        for (String key : keys) {
            String value = properties.getProperty(key);
            if (value != null) {
                data.put(key, value);
            }
        }
        return new ConfigData(data);
    } catch (IOException e) {
        log.error("Could not read properties from file {}", path, e);
        throw new ConfigException("Could not read properties from file " + path);
    }
}
Also used : HashMap(java.util.HashMap) ConfigData(org.apache.kafka.common.config.ConfigData) Reader(java.io.Reader) ConfigException(org.apache.kafka.common.config.ConfigException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 42 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project kafka by apache.

the class FileConfigProvider method get.

/**
 * Retrieves the data at the given Properties file.
 *
 * @param path the file where the data resides
 * @return the configuration data
 */
public ConfigData get(String path) {
    Map<String, String> data = new HashMap<>();
    if (path == null || path.isEmpty()) {
        return new ConfigData(data);
    }
    try (Reader reader = reader(path)) {
        Properties properties = new Properties();
        properties.load(reader);
        Enumeration<Object> keys = properties.keys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement().toString();
            String value = properties.getProperty(key);
            if (value != null) {
                data.put(key, value);
            }
        }
        return new ConfigData(data);
    } catch (IOException e) {
        log.error("Could not read properties from file {}", path, e);
        throw new ConfigException("Could not read properties from file " + path);
    }
}
Also used : HashMap(java.util.HashMap) ConfigData(org.apache.kafka.common.config.ConfigData) Reader(java.io.Reader) ConfigException(org.apache.kafka.common.config.ConfigException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 43 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project kafka by apache.

the class ConfigurationUtils method validateFile.

/**
 * Validates that, if a value is supplied, is a file that:
 *
 * <li>
 *     <ul>exists</ul>
 *     <ul>has read permission</ul>
 *     <ul>points to a file</ul>
 * </li>
 *
 * If the value is null or an empty string, it is assumed to be an "empty" value and thus.
 * ignored. Any whitespace is trimmed off of the beginning and end.
 */
public Path validateFile(String name) {
    URL url = validateUrl(name);
    File file;
    try {
        file = new File(url.toURI().getRawPath()).getAbsoluteFile();
    } catch (URISyntaxException e) {
        throw new ConfigException(name, url.toString(), String.format("The OAuth configuration option %s contains a URL (%s) that is malformed: %s", name, url, e.getMessage()));
    }
    if (!file.exists())
        throw new ConfigException(name, file, String.format("The OAuth configuration option %s contains a file (%s) that doesn't exist", name, file));
    if (!file.canRead())
        throw new ConfigException(name, file, String.format("The OAuth configuration option %s contains a file (%s) that doesn't have read permission", name, file));
    if (file.isDirectory())
        throw new ConfigException(name, file, String.format("The OAuth configuration option %s references a directory (%s), not a file", name, file));
    return file.toPath();
}
Also used : ConfigException(org.apache.kafka.common.config.ConfigException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Example 44 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project kafka by apache.

the class ConfigurationUtils method validateUrl.

/**
 * Validates that the configured URL that:
 *
 * <li>
 *     <ul>is well-formed</ul>
 *     <ul>contains a scheme</ul>
 *     <ul>uses either HTTP, HTTPS, or file protocols</ul>
 * </li>
 *
 * No effort is made to connect to the URL in the validation step.
 */
public URL validateUrl(String name) {
    String value = validateString(name);
    URL url;
    try {
        url = new URL(value);
    } catch (MalformedURLException e) {
        throw new ConfigException(name, value, String.format("The OAuth configuration option %s contains a URL (%s) that is malformed: %s", name, value, e.getMessage()));
    }
    String protocol = url.getProtocol();
    if (protocol == null || protocol.trim().isEmpty())
        throw new ConfigException(name, value, String.format("The OAuth configuration option %s contains a URL (%s) that is missing the protocol", name, value));
    protocol = protocol.toLowerCase(Locale.ROOT);
    if (!(protocol.equals("http") || protocol.equals("https") || protocol.equals("file")))
        throw new ConfigException(name, value, String.format("The OAuth configuration option %s contains a URL (%s) that contains an invalid protocol (%s); only \"http\", \"https\", and \"file\" protocol are supported", name, value, protocol));
    return url;
}
Also used : MalformedURLException(java.net.MalformedURLException) ConfigException(org.apache.kafka.common.config.ConfigException) URL(java.net.URL)

Example 45 with ConfigException

use of org.apache.kafka.common.config.ConfigException in project kafka by apache.

the class ConfigurationUtils method validateString.

public String validateString(String name, boolean isRequired) throws ValidateException {
    String value = get(name);
    if (value == null) {
        if (isRequired)
            throw new ConfigException(String.format("The OAuth configuration option %s value must be non-null", name));
        else
            return null;
    }
    value = value.trim();
    if (value.isEmpty()) {
        if (isRequired)
            throw new ConfigException(String.format("The OAuth configuration option %s value must not contain only whitespace", name));
        else
            return null;
    }
    return value;
}
Also used : ConfigException(org.apache.kafka.common.config.ConfigException)

Aggregations

ConfigException (org.apache.kafka.common.config.ConfigException)136 HashMap (java.util.HashMap)29 Test (org.junit.jupiter.api.Test)28 Test (org.junit.Test)20 Properties (java.util.Properties)10 KafkaException (org.apache.kafka.common.KafkaException)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Pattern (java.util.regex.Pattern)9 Serde (org.apache.kafka.common.serialization.Serde)8 SimpleConfig (org.apache.kafka.connect.transforms.util.SimpleConfig)8 File (java.io.File)7 SSLContext (javax.net.ssl.SSLContext)7 Map (java.util.Map)6 ByteArraySerializer (org.apache.kafka.common.serialization.ByteArraySerializer)6 KeyStore (java.security.KeyStore)5 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)5 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)5 ConfigDef (org.apache.kafka.common.config.ConfigDef)5 IOException (java.io.IOException)4