Search in sources :

Example 1 with ISessionService

use of com.willshex.blogwt.server.service.session.ISessionService in project blogwt by billy1380.

the class LoginActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(LoginRequest input, LoginResponse output) throws Exception {
    input = ApiValidator.request(input, LoginRequest.class);
    input.accessCode = ApiValidator.accessCode(input.accessCode, "input.accessCode");
    boolean foundToken = false;
    if (input.session != null && input.session.id != null) {
        foundToken = true;
    }
    if (!foundToken) {
        IUserService userService = UserServiceProvider.provide();
        User user = null;
        if (input.username != null) {
            user = userService.getLoginUser(input.username, input.password);
            if (user == null)
                throw new AuthenticationException(input.username);
        }
        if (user == null && input.email != null) {
            user = userService.getEmailLoginUser(input.email, input.password);
            if (user == null)
                throw new AuthenticationException(input.email);
        }
        if (user == null)
            throw new AuthenticationException("Either username or email addressed cannot be null");
        ISessionService sessionService = SessionServiceProvider.provide();
        if (LOG.isLoggable(Level.FINER)) {
            LOG.finer("Getting user session");
        }
        output.session = sessionService.getUserSession(user);
        if (output.session == null) {
            if (LOG.isLoggable(Level.FINER)) {
                LOG.finer("Existing session not found, creating new session");
            }
            output.session = sessionService.createUserSession(user, input.longTerm);
            UserServiceProvider.provide().updateUserIdLastLoggedIn(user.id);
        } else {
            if (input.longTerm != null) {
                output.session.longTerm(input.longTerm);
            }
            output.session = SessionServiceProvider.provide().extendSession(output.session);
            output.session.user = user;
        }
    } else {
        output.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
    }
    if (output.session.user.roleKeys != null) {
        output.session.user.roles = PersistenceHelper.batchLookup(RoleServiceProvider.provide(), output.session.user.roleKeys);
    }
    if (output.session.user.permissionKeys != null) {
        output.session.user.permissions = PersistenceHelper.batchLookup(PermissionServiceProvider.provide(), output.session.user.permissionKeys);
    }
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) AuthenticationException(com.willshex.blogwt.server.api.exception.AuthenticationException) ISessionService(com.willshex.blogwt.server.service.session.ISessionService) IUserService(com.willshex.blogwt.server.service.user.IUserService) LoginRequest(com.willshex.blogwt.shared.api.user.call.LoginRequest)

Example 2 with ISessionService

use of com.willshex.blogwt.server.service.session.ISessionService in project blogwt by billy1380.

the class ServletHelper method session.

public static Session session(HttpServletRequest request) {
    ISessionService sessionService = SessionServiceProvider.provide();
    Cookie[] cookies = request.getCookies();
    String sessionId = null;
    Session userSession = null;
    if (cookies != null) {
        for (Cookie currentCookie : cookies) {
            if ("session.id".equals(currentCookie.getName())) {
                sessionId = currentCookie.getValue();
                break;
            }
        }
        if (sessionId != null) {
            userSession = sessionService.getSession(Long.valueOf(sessionId));
            if (userSession != null) {
                try {
                    userSession = SessionValidator.lookupCheckAndExtend(userSession, "session");
                    UserHelper.stripPassword(userSession.user);
                    UserHelper.populateRolesAndPermissionsFromKeys(userSession.user);
                } catch (ServiceException e) {
                    userSession = null;
                }
            }
        }
    }
    return userSession;
}
Also used : Cookie(javax.servlet.http.Cookie) ServiceException(com.willshex.gson.web.service.server.ServiceException) ISessionService(com.willshex.blogwt.server.service.session.ISessionService) Session(com.willshex.blogwt.shared.api.datatype.Session)

Aggregations

ISessionService (com.willshex.blogwt.server.service.session.ISessionService)2 AuthenticationException (com.willshex.blogwt.server.api.exception.AuthenticationException)1 IUserService (com.willshex.blogwt.server.service.user.IUserService)1 Session (com.willshex.blogwt.shared.api.datatype.Session)1 User (com.willshex.blogwt.shared.api.datatype.User)1 LoginRequest (com.willshex.blogwt.shared.api.user.call.LoginRequest)1 ServiceException (com.willshex.gson.web.service.server.ServiceException)1 Cookie (javax.servlet.http.Cookie)1