/*
 * Copyright 2015 Ryan Gilera.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.daytron.revworks.service;

import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinSession;
import java.security.Principal;

/**
 *
 * @author Ryan Gilera
 */
public class CurrentUserSession {
    /**
     * The attribute key used to store the username in the session.
     */
    public static final String CURRENT_USER_SESSION_ATTRIBUTE_KEY = 
            CurrentUserSession.class.getCanonicalName();
    
    private CurrentUserSession(){}
    
    /**
     * Sets the name of the current user and stores it in the current session.
     * Using a {@code null} username will remove the username from the session.
     * 
     * @param currentUser The current user
     * @throws IllegalStateException
     *             if the current session cannot be accessed.
     */
    public static void set(Principal currentUser) {
        if (currentUser == null) {
            getCurrentRequest().getWrappedSession().removeAttribute(
                    CURRENT_USER_SESSION_ATTRIBUTE_KEY);
        } else {
            getCurrentRequest().getWrappedSession().setAttribute(
                    CURRENT_USER_SESSION_ATTRIBUTE_KEY, currentUser);
        }
    }
    
    /**
     * Returns the name of the current user stored in the current session, or an
     * empty string if no user name is stored.
     * 
     * @return The user object
     * @throws IllegalStateException
     *             if the current session cannot be accessed.
     */
    public static Principal get() {
        Principal currentUser = (Principal) getCurrentRequest().getWrappedSession()
                .getAttribute(CURRENT_USER_SESSION_ATTRIBUTE_KEY);
        return (currentUser == null) ? null : currentUser;
    }
    
    private static VaadinRequest getCurrentRequest() {
        VaadinRequest request = VaadinService.getCurrentRequest();
        if (request == null) {
            throw new IllegalStateException(
                    "No request bound to current thread");
        }
        return request;
    }
    
    public static void signOut() {
        VaadinSession.getCurrent().getSession().invalidate();
    }
}

@@ -636,16 +636,59 @@
 rvice;%0A%0A
+import com.github.daytron.revworks.MainUI;%0A
 import c
@@ -799,16 +799,41 @@
 ession;%0A
+import com.vaadin.ui.UI;%0A
 import j
@@ -1569,227 +1569,32 @@
     
-if (currentUser == null) %7B%0A            getCurrentRequest().getWrappedSession().removeAttribute(%0A                    CURRENT_USER_SESSION_ATTRIBUTE_KEY);%0A        %7D else %7B%0A            getCurrentRequest().getWrappedSession
+VaadinSession.getCurrent
 ().s
@@ -1606,20 +1606,16 @@
 ribute(%0A
-    
         
@@ -1672,26 +1672,16 @@
 tUser);%0A
-        %7D%0A
     %7D%0A  
@@ -2049,45 +2049,32 @@
 al) 
-getCurrentRequest().getWrappedSession
+VaadinSession.getCurrent
 ()%0A 
@@ -2217,67 +2217,59 @@
     
-private static VaadinRequest getCurrentReques
+%0A    %0A    public static void signOu
 t() %7B%0A
+//
         
 Vaad
@@ -2268,199 +2268,86 @@
     
-VaadinRequest request = VaadinService.getCurrentRequest();%0A        if (request == null) %7B%0A            throw new IllegalStateException(%0A                    %22No request bound to current thread%22
+MainUI.MainUIServlet.removeSessonUponLogout(%0A//                get().getName()
 );%0A 
@@ -2349,25 +2349,24 @@
 ));%0A        
-%7D
 %0A        ret
@@ -2366,69 +2366,43 @@
     
-r
+VaadinSession.g
 et
+C
 ur
-n request;%0A    %7D%0A    %0A    public static void signOut() %7B
+rent().close();
 %0A   

Save attribute to global session instead of wrappedsession and modify signout procesure to trigger SessionDestroyListener attached in MainUIServlet class