Search in sources :

Example 1 with ExceptionHandlers

use of com.haulmont.cuba.web.exception.ExceptionHandlers in project cuba by cuba-platform.

the class App method init.

/**
 * Called when <em>the first</em> UI of the session is initialized.
 */
protected void init(Locale requestLocale) {
    VaadinSession vSession = VaadinSession.getCurrent();
    vSession.setAttribute(App.class, this);
    vSession.setLocale(messageTools.getDefaultLocale());
    // set root error handler for all session
    vSession.setErrorHandler(event -> {
        try {
            getExceptionHandlers().handle(event);
            getAppLog().log(event);
        } catch (Throwable e) {
            log.error("Error handling exception\nOriginal exception:\n{}\nException in handlers:\n{}", ExceptionUtils.getStackTrace(event.getThrowable()), ExceptionUtils.getStackTrace(e));
        }
    });
    log.debug("Initializing application");
    appLog = new AppLog(webConfig.getAppLogMaxItemsCount(), beanLocator.get(TimeSource.NAME));
    connection = createConnection();
    exceptionHandlers = new ExceptionHandlers(this, beanLocator);
    cookies = new AppCookies();
    themeConstants = loadTheme();
    // get default locale from config
    Locale targetLocale = resolveLocale(requestLocale);
    setLocale(targetLocale);
}
Also used : VaadinSession(com.vaadin.server.VaadinSession) ExceptionHandlers(com.haulmont.cuba.web.exception.ExceptionHandlers) AppLog(com.haulmont.cuba.web.log.AppLog)

Example 2 with ExceptionHandlers

use of com.haulmont.cuba.web.exception.ExceptionHandlers in project cuba by cuba-platform.

the class LoginScreen method doLogin.

protected void doLogin() {
    String login = loginField.getValue();
    String password = passwordField.getValue() != null ? passwordField.getValue() : "";
    Map<String, Object> params = new HashMap<>(urlRouting.getState().getParams());
    if (StringUtils.isEmpty(login) || StringUtils.isEmpty(password)) {
        notifications.create(Notifications.NotificationType.WARNING).withCaption(messages.getMainMessage("loginWindow.emptyLoginOrPassword")).show();
        return;
    }
    try {
        Locale selectedLocale = localesSelect.getValue();
        app.setLocale(selectedLocale);
        doLogin(new LoginPasswordCredentials(login, password, selectedLocale, params));
        // locale could be set on the server
        if (connection.getSession() != null) {
            Locale loggedInLocale = connection.getSession().getLocale();
            if (globalConfig.getLocaleSelectVisible()) {
                app.addCookie(App.COOKIE_LOCALE, loggedInLocale.toLanguageTag());
            }
        }
    } catch (InternalAuthenticationException e) {
        log.error("Internal error during login", e);
        showUnhandledExceptionOnLogin(e);
    } catch (LoginException e) {
        log.info("Login failed: {}", e.toString());
        String message = StringUtils.abbreviate(e.getMessage(), 1000);
        showLoginException(message);
    } catch (Exception e) {
        if (connection.isAuthenticated()) {
            ExceptionHandlers handlers = app.getExceptionHandlers();
            handlers.handle(new ErrorEvent(e));
        } else {
            log.warn("Unable to login", e);
            showUnhandledExceptionOnLogin(e);
        }
    }
}
Also used : Locale(java.util.Locale) ExceptionHandlers(com.haulmont.cuba.web.exception.ExceptionHandlers) HashMap(java.util.HashMap) LoginPasswordCredentials(com.haulmont.cuba.security.auth.LoginPasswordCredentials) ErrorEvent(com.vaadin.server.ErrorEvent) LoginException(com.haulmont.cuba.security.global.LoginException) InternalAuthenticationException(com.haulmont.cuba.security.global.InternalAuthenticationException) LoginException(com.haulmont.cuba.security.global.LoginException) InternalAuthenticationException(com.haulmont.cuba.security.global.InternalAuthenticationException)

Example 3 with ExceptionHandlers

use of com.haulmont.cuba.web.exception.ExceptionHandlers in project cuba by cuba-platform.

the class AppLoginWindow method doLogin.

protected void doLogin() {
    String login = loginField.getValue();
    String password = passwordField.getValue() != null ? passwordField.getValue() : "";
    Map<String, Object> params = new HashMap<>(urlRouting.getState().getParams());
    if (StringUtils.isEmpty(login) || StringUtils.isEmpty(password)) {
        showNotification(messages.getMainMessage("loginWindow.emptyLoginOrPassword"), NotificationType.WARNING);
        return;
    }
    try {
        Locale selectedLocale = localesSelect.getValue();
        app.setLocale(selectedLocale);
        doLogin(new LoginPasswordCredentials(login, password, selectedLocale, params));
        // locale could be set on the server
        if (connection.getSession() != null) {
            Locale loggedInLocale = connection.getSession().getLocale();
            if (globalConfig.getLocaleSelectVisible()) {
                app.addCookie(App.COOKIE_LOCALE, loggedInLocale.toLanguageTag());
            }
        }
    } catch (InternalAuthenticationException e) {
        log.error("Internal error during login", e);
        showUnhandledExceptionOnLogin(e);
    } catch (LoginException e) {
        log.info("Login failed: {}", e.toString());
        String message = StringUtils.abbreviate(e.getMessage(), 1000);
        showLoginException(message);
    } catch (Exception e) {
        if (connection.isAuthenticated()) {
            ExceptionHandlers handlers = app.getExceptionHandlers();
            handlers.handle(new ErrorEvent(e));
        } else {
            log.warn("Unable to login", e);
            showUnhandledExceptionOnLogin(e);
        }
    }
}
Also used : Locale(java.util.Locale) ExceptionHandlers(com.haulmont.cuba.web.exception.ExceptionHandlers) HashMap(java.util.HashMap) LoginPasswordCredentials(com.haulmont.cuba.security.auth.LoginPasswordCredentials) ErrorEvent(com.vaadin.server.ErrorEvent) LoginException(com.haulmont.cuba.security.global.LoginException) InternalAuthenticationException(com.haulmont.cuba.security.global.InternalAuthenticationException) InternalAuthenticationException(com.haulmont.cuba.security.global.InternalAuthenticationException) LoginException(com.haulmont.cuba.security.global.LoginException)

Aggregations

ExceptionHandlers (com.haulmont.cuba.web.exception.ExceptionHandlers)3 LoginPasswordCredentials (com.haulmont.cuba.security.auth.LoginPasswordCredentials)2 InternalAuthenticationException (com.haulmont.cuba.security.global.InternalAuthenticationException)2 LoginException (com.haulmont.cuba.security.global.LoginException)2 ErrorEvent (com.vaadin.server.ErrorEvent)2 HashMap (java.util.HashMap)2 Locale (java.util.Locale)2 AppLog (com.haulmont.cuba.web.log.AppLog)1 VaadinSession (com.vaadin.server.VaadinSession)1