use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.
the class ReflectionHelper method invokePrivileged.
public static <T> T invokePrivileged(final Method method, final Object instance, final Object... args) {
Assert.requireNonNull(method, "method");
Assert.requireNonNull(instance, "instance");
return AccessController.doPrivileged((PrivilegedAction<T>) new PrivilegedAction<Object>() {
@Override
public Object run() {
final boolean wasAccessible = method.isAccessible();
try {
method.setAccessible(true);
return method.invoke(instance, args);
} catch (final InvocationTargetException ex) {
throw new DolphinRuntimeException("Error while calling method '" + method.getName() + "' on instance of type '" + instance.getClass() + "'. Method details: " + method.toGenericString(), ex);
} catch (final IllegalAccessException ex) {
throw new DolphinRuntimeException("Cannot invoke method '" + method.getName() + "' on instance of type '" + instance.getClass() + "'. Method details: " + method.toGenericString(), ex);
} finally {
method.setAccessible(wasAccessible);
}
}
});
}
use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.
the class ClientSessionResponseHandler method handle.
@Override
public void handle(final HttpURLConnection response) {
Assert.requireNonNull(response, "response");
String clientIdInHeader = response.getHeaderField(PlatformConstants.CLIENT_ID_HTTP_HEADER_NAME);
try {
clientSessionStore.setClientIdentifierForUrl(response.getURL().toURI(), clientIdInHeader);
} catch (URISyntaxException e) {
LOG.error("Exception while converting to response URL {} to URI", response.getURL());
throw new DolphinRuntimeException("Exception while converting URL " + response.getURL() + "to URI", e);
}
}
use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.
the class ControllerHandler method invokeAction.
public void invokeAction(final String controllerId, final String actionName, final Map<String, Object> params) throws InvokeActionException {
Assert.requireNonBlank(controllerId, "controllerId");
Assert.requireNonBlank(actionName, "actionName");
Assert.requireNonNull(params, "params");
final Object controller = controllers.get(controllerId);
final Class controllerClass = controllerClassMapping.get(controllerId);
final Subscription controllerContextSubscription = ContextManagerImpl.getInstance().addThreadContext(CONTROLLER_CONTEXT, Optional.ofNullable(controllerClass).map(c -> c.getSimpleName()).orElse(UNKNOWN_CONTROLLER_CONTEXT));
final Subscription controllerActionContextSubscription = ContextManagerImpl.getInstance().addThreadContext(CONTROLLER_ACTION_CONTEXT, actionName);
try {
if (controller == null) {
throw new InvokeActionException("No controller for id " + controllerId + " found");
}
if (controllerClass == null) {
throw new InvokeActionException("No controllerClass for id " + controllerId + " found");
}
final Method actionMethod = getActionMethod(controllerClass, actionName);
if (actionMethod == null) {
throw new InvokeActionException("No actionMethod with name " + actionName + " in controller class " + ControllerUtils.getControllerName(controllerClass) + " found");
}
final List<Object> args = getArgs(actionMethod, params);
LOG.debug("Will call {} action for controller {} ({}.{}) with {} params.", actionName, controllerId, controllerClass, ControllerUtils.getActionMethodName(actionMethod), args.size());
if (LOG.isTraceEnabled()) {
int index = 1;
for (final Object param : args) {
if (param != null) {
LOG.trace("Action param {}: {} with type {} is called with value \"{}\" and type {}", index, actionMethod.getParameters()[index - 1].getName(), actionMethod.getParameters()[index - 1].getType().getSimpleName(), param, param.getClass());
} else {
LOG.trace("Action param {}: {} with type {} is called with value null", index, actionMethod.getParameters()[index - 1].getName(), actionMethod.getParameters()[index - 1].getType().getSimpleName());
}
index++;
}
}
try {
ReflectionHelper.invokePrivileged(actionMethod, controller, args.toArray());
} catch (final DolphinRuntimeException e) {
if (e.getCause() instanceof InvocationTargetException) {
final InvocationTargetException invocationTargetException = (InvocationTargetException) e.getCause();
final Throwable internalException = invocationTargetException.getCause();
actionErrorHandler.handle(internalException, controller, ControllerUtils.getControllerName(controllerClass), actionName);
}
throw e;
}
} catch (final InvokeActionException e) {
throw e;
} catch (final Exception e) {
throw new InvokeActionException("Can not call action '" + actionName + "'", e);
} finally {
controllerContextSubscription.unsubscribe();
controllerActionContextSubscription.unsubscribe();
}
}
use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.
the class KeycloakLogoutServlet method doPost.
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
try {
LOG.debug("Logout endpoint called");
final String realmName = Optional.ofNullable(req.getHeader(REALM_NAME_HEADER)).orElse(configuration.getRealmName());
final String authEndPoint = configuration.getAuthEndpoint();
final URI url = new URI(authEndPoint + "/realms/" + realmName + "/protocol/openid-connect/logout");
LOG.debug("Calling Keycloak");
final HttpClientConnection clientConnection = new HttpClientConnection(url, RequestMethod.GET);
clientConnection.addRequestHeader(AUTHORIZATION_HEADER, req.getHeader(AUTHORIZATION_HEADER));
final int responseCode = clientConnection.readResponseCode();
if (responseCode != HTTP_OK) {
LOG.debug("Error in logout!");
throw new DolphinRuntimeException("Error in logout!");
}
LOG.debug("Logout done");
} catch (final Exception e) {
resp.sendError(SC_INTERNAL_SERVER_ERROR, "Error in logout!");
}
}
use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.
the class KeycloakSecurity method login.
@Override
public Future<Void> login(final String user, final String password, final PlatformConfiguration securityConfig) {
Assert.requireNonNull(securityConfig, "securityConfig");
return executor.submit(() -> {
loginLogoutLock.lock();
try {
if (authorized.get()) {
throw new DolphinRuntimeException("Already logged in!");
}
final String realmName = securityConfig.getProperty(REALM_PROPERTY_NAME, defaultRealmName);
final String appName = securityConfig.getProperty(APPLICATION_PROPERTY_NAME, defaultAppName);
try {
final String encodedUser = encode(user);
final String encodedPassword = encode(password);
final String encodedAppName = encode(appName);
final KeycloakOpenidConnectResult connectResult = receiveTokenByLogin(encodedUser, encodedPassword, realmName, appName);
accessToken.set(connectResult.getAccess_token());
final KeycloakAuthentification auth = new KeycloakAuthentification(accessToken.get(), appName, realmName);
KeycloakAuthentificationManager.getInstance().setAuth(auth);
authorized.set(true);
userContextSubscription.set(ContextManagerImpl.getInstance().addGlobalContext(USER_CONTEXT, user));
startTokenRefreshRunner(connectResult, realmName, encodedAppName);
} catch (final IOException | URISyntaxException e) {
throw new DolphinRuntimeException("Can not receive security token!", e);
}
} finally {
loginLogoutLock.unlock();
}
}, null);
}
Aggregations