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