Search in sources :

Example 6 with DolphinRuntimeException

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);
            }
        }
    });
}
Also used : PrivilegedAction(java.security.PrivilegedAction) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with DolphinRuntimeException

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);
    }
}
Also used : DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) URISyntaxException(java.net.URISyntaxException)

Example 8 with DolphinRuntimeException

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();
    }
}
Also used : DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) Method(java.lang.reflect.Method) Subscription(com.canoo.platform.core.functional.Subscription) InvocationTargetException(java.lang.reflect.InvocationTargetException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) ValueConverterException(com.canoo.platform.remoting.spi.converter.ValueConverterException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with DolphinRuntimeException

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!");
    }
}
Also used : HttpClientConnection(com.canoo.dp.impl.platform.core.http.HttpClientConnection) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) URI(java.net.URI) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException)

Example 10 with DolphinRuntimeException

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);
}
Also used : DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Aggregations

DolphinRuntimeException (com.canoo.platform.core.DolphinRuntimeException)19 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)4 HttpClientConnection (com.canoo.dp.impl.platform.core.http.HttpClientConnection)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 ServletException (javax.servlet.ServletException)2 SimpleDolphinPlatformThreadFactory (com.canoo.dp.impl.platform.core.SimpleDolphinPlatformThreadFactory)1 ACCEPT_CHARSET_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_CHARSET_HEADER)1 ACCEPT_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_HEADER)1 ClassInfo (com.canoo.dp.impl.remoting.info.ClassInfo)1 PropertyInfo (com.canoo.dp.impl.remoting.info.PropertyInfo)1 DefaultClasspathScanner (com.canoo.dp.impl.server.scanner.DefaultClasspathScanner)1 PlatformThreadFactory (com.canoo.platform.core.PlatformThreadFactory)1 Subscription (com.canoo.platform.core.functional.Subscription)1 ConnectionException (com.canoo.platform.core.http.ConnectionException)1 HttpException (com.canoo.platform.core.http.HttpException)1 HttpHeader (com.canoo.platform.core.http.HttpHeader)1