Search in sources :

Example 1 with NoUserSessionException

use of com.haulmont.cuba.security.global.NoUserSessionException in project cuba by cuba-platform.

the class RestFileDownloadController method getSession.

protected UserSession getSession(HttpServletRequest request, HttpServletResponse response) {
    UUID sessionId;
    try {
        sessionId = UUID.fromString(request.getParameter("s"));
    } catch (Exception e) {
        return null;
    }
    AppContext.setSecurityContext(new SecurityContext(sessionId));
    try {
        UserSession userSession = userSessionService.getUserSession(sessionId);
        return userSession;
    } catch (NoUserSessionException e) {
        return null;
    } finally {
        AppContext.setSecurityContext(null);
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) UUID(java.util.UUID) IOException(java.io.IOException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 2 with NoUserSessionException

use of com.haulmont.cuba.security.global.NoUserSessionException in project cuba by cuba-platform.

the class PortalExceptionResolver method resolveException.

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    if (ex instanceof NoUserSessionException) {
        log.warn(ex.getMessage());
        HttpSession httpSession = request.getSession();
        httpSession.invalidate();
        if (StringUtils.isNotEmpty(noUserSessionUrl))
            return new ModelAndView("redirect:" + noUserSessionUrl);
        else
            return new ModelAndView("redirect:/");
    } else if (ex instanceof NoMiddlewareConnectionException) {
        log.error(ex.getMessage());
        HttpSession httpSession = request.getSession();
        httpSession.invalidate();
        if (StringUtils.isNotEmpty(noMiddlewareConnectionUrl))
            return new ModelAndView(noMiddlewareConnectionUrl);
        else
            return new ModelAndView("/");
    } else {
        log.error(ExceptionUtils.getStackTrace(ex));
    }
    return null;
}
Also used : HttpSession(javax.servlet.http.HttpSession) ModelAndView(org.springframework.web.servlet.ModelAndView) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 3 with NoUserSessionException

use of com.haulmont.cuba.security.global.NoUserSessionException in project cuba by cuba-platform.

the class DynamicAttributesCacheStrategy method init.

@Override
public void init() {
    clientCacheManager.getExecutorService().scheduleWithFixedDelay(() -> {
        if (needToValidateCache) {
            UserSession userSession = cacheUserSessionProvider.getUserSession();
            if (userSession == null) {
                // cache user session unavailable
                return;
            }
            try {
                AppContext.setSecurityContext(new SecurityContext(userSession));
                loadObject();
            } catch (NoUserSessionException e) {
                log.warn("Cache user session expired", e);
            } catch (Exception e) {
                log.error("Unable to update dynamic attributes cache", e);
            }
        }
    }, 0, 10, TimeUnit.SECONDS);
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 4 with NoUserSessionException

use of com.haulmont.cuba.security.global.NoUserSessionException in project cuba by cuba-platform.

the class DesktopTimer method handleTimerException.

protected void handleTimerException(RuntimeException ex) {
    if (ExceptionUtils.indexOfType(ex, java.net.ConnectException.class) > -1) {
        // If a ConnectException occurred, just log it and ignore
        log.warn("onTimer error: " + ex.getMessage());
    } else {
        // Otherwise throw the exception, but first search for NoUserSessionException in chain,
        // if found - stop the timer
        int reIdx = ExceptionUtils.indexOfType(ex, RemoteException.class);
        if (reIdx > -1) {
            RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(ex).get(reIdx);
            for (RemoteException.Cause cause : re.getCauses()) {
                // noinspection ThrowableResultOfMethodCallIgnored
                if (cause.getThrowable() instanceof NoUserSessionException) {
                    log.warn("NoUserSessionException in timer, timer will be stopped");
                    disposeTimer();
                    break;
                }
            }
        } else if (ExceptionUtils.indexOfThrowable(ex, NoUserSessionException.class) > -1) {
            log.warn("NoUserSessionException in timer, timer will be stopped");
            disposeTimer();
        }
        throw ex;
    }
}
Also used : RemoteException(com.haulmont.cuba.core.global.RemoteException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 5 with NoUserSessionException

use of com.haulmont.cuba.security.global.NoUserSessionException in project cuba by cuba-platform.

the class ServiceInterceptor method aroundInvoke.

private Object aroundInvoke(ProceedingJoinPoint ctx) throws Throwable {
    SecurityContext securityContext = AppContext.getSecurityContextNN();
    boolean internalInvocation = securityContext.incServiceInvocation() > 0;
    try {
        if (internalInvocation) {
            if (logInternalServiceInvocation) {
                log.warn("Invoking '{}' from another service", ctx.getSignature());
            }
            ValidateServiceMethodContext validatedContext = getValidateServiceMethodContext(ctx);
            validateMethodParameters(ctx, validatedContext);
            Object res = ctx.proceed();
            validateMethodResult(ctx, validatedContext, res);
            return res;
        } else {
            statisticsAccumulator.incMiddlewareRequestsCount();
            try {
                // Using UserSessionsAPI directly to make sure the session's "last used" timestamp is propagated to the cluster
                UserSession userSession = userSessions.getAndRefresh(securityContext.getSessionId(), true);
                if (userSession == null) {
                    throw new NoUserSessionException(securityContext.getSessionId());
                }
                ValidateServiceMethodContext validatedContext = getValidateServiceMethodContext(ctx);
                validateMethodParameters(ctx, validatedContext);
                boolean checkTransactionOnExit = Stores.getAdditional().isEmpty() && !persistence.isInTransaction();
                log.trace("Invoking: {}, session={}", ctx.getSignature(), userSession);
                Object res = ctx.proceed();
                validateMethodResult(ctx, validatedContext, res);
                if (checkTransactionOnExit && persistence.isInTransaction()) {
                    log.warn("Open transaction left in {}", ctx.getSignature().toShortString());
                }
                return res;
            } catch (Throwable e) {
                logException(e, ctx);
                // Propagate the special exception to avoid serialization errors on remote clients
                throw new RemoteException(e);
            }
        }
    } finally {
        securityContext.decServiceInvocation();
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Aggregations

NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)17 UserSession (com.haulmont.cuba.security.global.UserSession)9 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)6 IOException (java.io.IOException)3 UUID (java.util.UUID)3 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)2 RemoteException (com.haulmont.cuba.core.global.RemoteException)2 UserSessionService (com.haulmont.cuba.security.app.UserSessionService)2 HttpSession (javax.servlet.http.HttpSession)2 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 AppBeans (com.haulmont.cuba.core.global.AppBeans)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 Messages (com.haulmont.cuba.core.global.Messages)1 Connection (com.haulmont.cuba.desktop.Connection)1 Type (com.haulmont.cuba.gui.components.DialogAction.Type)1 IllegalConcurrentAccessException (com.haulmont.cuba.gui.executors.IllegalConcurrentAccessException)1 Icons (com.haulmont.cuba.gui.icons.Icons)1 App (com.haulmont.cuba.portal.App)1 Connection (com.haulmont.cuba.portal.Connection)1 PortalSession (com.haulmont.cuba.portal.security.PortalSession)1