use of org.apache.knox.gateway.config.ConfigurationException in project knox by apache.
the class RESTInvoker method invoke.
JSONObject invoke(String url, String username, String passwordAlias) {
JSONObject result = null;
CloseableHttpResponse response = null;
try {
HttpGet request = new HttpGet(url);
// If no configured username, then use default username alias
String password = null;
if (username == null) {
if (aliasService != null) {
try {
char[] defaultUser = aliasService.getPasswordFromAliasForGateway(DEFAULT_USER_ALIAS);
if (defaultUser != null) {
username = new String(defaultUser);
}
} catch (AliasServiceException e) {
log.aliasServiceUserError(DEFAULT_USER_ALIAS, e.getLocalizedMessage());
}
}
// If username is still null
if (username == null) {
log.aliasServiceUserNotFound();
throw new ConfigurationException("No username is configured for Ambari service discovery.");
}
}
if (aliasService != null) {
// If no password alias is configured, then try the default alias
if (passwordAlias == null) {
passwordAlias = DEFAULT_PWD_ALIAS;
}
try {
char[] pwd = aliasService.getPasswordFromAliasForGateway(passwordAlias);
if (pwd != null) {
password = new String(pwd);
}
} catch (AliasServiceException e) {
log.aliasServicePasswordError(passwordAlias, e.getLocalizedMessage());
}
}
// If the password could not be determined
if (password == null) {
log.aliasServicePasswordNotFound();
throw new ConfigurationException("No password is configured for Ambari service discovery.");
}
// Add an auth header if credentials are available
String encodedCreds = org.apache.commons.codec.binary.Base64.encodeBase64String((username + ":" + password).getBytes());
request.addHeader(new BasicHeader("Authorization", "Basic " + encodedCreds));
// Ambari CSRF protection
request.addHeader("X-Requested-By", "Knox");
response = httpClient.execute(request);
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = (JSONObject) JSONValue.parse((EntityUtils.toString(entity)));
log.debugJSON(result.toJSONString());
} else {
log.noJSON(url);
}
} else {
log.unexpectedRestResponseStatusCode(url, response.getStatusLine().getStatusCode());
}
} catch (ConnectTimeoutException e) {
log.restInvocationTimedOut(url, e);
} catch (IOException e) {
log.restInvocationError(url, e);
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
// Ignore
}
}
}
return result;
}
use of org.apache.knox.gateway.config.ConfigurationException in project knox by apache.
the class DefaultConfigurationInjector method injectFieldValue.
private void injectFieldValue(Field field, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding) throws ConfigurationException {
Configure annotation = field.getAnnotation(Configure.class);
if (annotation != null) {
Alias alias = field.getAnnotation(Alias.class);
String name = getConfigName(field, alias);
String bind = getBindName(target, name, binding);
Object value = retrieveValue(target, bind, name, field.getType(), adapter, binding);
if (value == null) {
Optional optional = field.getAnnotation(Optional.class);
if (optional == null) {
throw new ConfigurationException(String.format("Failed to find configuration for %s bound to %s of %s via %s", bind, name, target.getClass().getName(), adapter.getClass().getName()));
}
} else {
try {
if (!field.isAccessible()) {
field.setAccessible(true);
}
field.set(target, value);
} catch (Exception e) {
throw new ConfigurationException(String.format("Failed to inject field configuration property %s of %s", name, target.getClass().getName()), e);
}
}
}
}
use of org.apache.knox.gateway.config.ConfigurationException in project knox by apache.
the class ConfigurationAdapterFactory method get.
public static ConfigurationAdapter get(Object config) throws ConfigurationException {
if (config == null) {
throw new NullPointerException("Configuration adapter instantiation impossible for null config object.");
}
try {
Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters = getAdapters();
Class configType = config.getClass();
Class adapterType = findAdapterTypeForConfigTypeOrParent(adapters, configType);
if (adapterType == null) {
throw new ConfigurationException("No configuration adapter found for config type " + configType.getName());
}
Constructor c = findConstructorForConfigType(adapterType, configType);
if (!c.isAccessible()) {
c.setAccessible(true);
}
Object adapter = c.newInstance(config);
return ConfigurationAdapter.class.cast(adapter);
} catch (ConfigurationException e) {
throw e;
} catch (Exception e) {
throw new ConfigurationException("Configuration adapter instantiation failed.", e);
}
}
use of org.apache.knox.gateway.config.ConfigurationException in project knox by apache.
the class DefaultConfigurationInjector method injectMethodValue.
private void injectMethodValue(Method method, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding) throws ConfigurationException {
Configure methodTag = method.getAnnotation(Configure.class);
if (methodTag != null) {
Alias aliasTag = method.getAnnotation(Alias.class);
String methodName = getConfigName(method, aliasTag);
Class[] argTypes = method.getParameterTypes();
Object[] args = new Object[argTypes.length];
Annotation[][] argTags = method.getParameterAnnotations();
for (int i = 0; i < argTypes.length; i++) {
String argName = getConfigName(methodName, argTags[i]);
String bndName = getBindName(target, argName, binding);
Object argValue = retrieveValue(target, bndName, argName, argTypes[i], adapter, binding);
if (argValue == null) {
Default defTag = findAnnotation(argTags[i], Default.class);
if (defTag != null) {
String strValue = defTag.value();
argValue = convertValue(target, argName, strValue, argTypes[i]);
} else {
throw new ConfigurationException(String.format("Failed to find configuration for %s as %s of %s via %s", bndName, argName, target.getClass().getName(), adapter.getClass().getName()));
}
}
args[i] = argValue;
}
if (!method.isAccessible()) {
method.setAccessible(true);
}
try {
method.invoke(target, args);
} catch (Exception e) {
throw new ConfigurationException(String.format("Failed to inject method configuration via %s of %s", methodName, target.getClass().getName()), e);
}
}
}
Aggregations