package org.vaadin.mockapp.samples.authentication;

import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;

/**
 * Class for retrieving and setting the name of the current user of the current
 * session (without using JAAS). All methods of this class require that a
 * {@link VaadinRequest} is bound to the current thread.
 * 
 * 
 * @see com.vaadin.server.VaadinService#getCurrentRequest()
 */
public final class CurrentUser {

    /**
     * The attribute key used to store the username in the session.
     */
    public static final String CURRENT_USER_SESSION_ATTRIBUTE_KEY = CurrentUser.class
            .getCanonicalName();

    private CurrentUser() {
    }

    /**
     * Returns the name of the current user stored in the current session, or an
     * empty string if no user name is stored.
     * 
     * @throws IllegalStateException
     *             if the current session cannot be accessed.
     */
    public static String get() {
        String currentUser = (String) getCurrentRequest().getWrappedSession()
                .getAttribute(CURRENT_USER_SESSION_ATTRIBUTE_KEY);
        if (currentUser == null) {
            return "";
        } else {
            return currentUser;
        }
    }

    /**
     * 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.
     * 
     * @throws IllegalStateException
     *             if the current session cannot be accessed.
     */
    public static void set(String currentUser) {
        if (currentUser == null) {
            getCurrentRequest().getWrappedSession().removeAttribute(
                    CURRENT_USER_SESSION_ATTRIBUTE_KEY);
        } else {
            getCurrentRequest().getWrappedSession().setAttribute(
                    CURRENT_USER_SESSION_ATTRIBUTE_KEY, currentUser);
        }
    }

    private static VaadinRequest getCurrentRequest() {
        VaadinRequest request = VaadinService.getCurrentRequest();
        if (request == null) {
            throw new IllegalStateException(
                    "No request bound to current thread");
        }
        return request;
    }
}

@@ -118,21 +118,62 @@
 VaadinSe
-rvice
+ssion;%0Aimport com.vaadin.server.WrappedSession
 ;%0A%0A/**%0A 
@@ -384,21 +384,19 @@
 read.%0A *
- 
 %0A *
- 
 %0A * @see
@@ -859,33 +859,32 @@
 s stored.%0A     *
- 
 %0A     * @throws 
@@ -1060,37 +1060,35 @@
 rent
-Request().getWrappedSession()
+HttpSession().getAttribute(
 %0A   
@@ -1100,30 +1100,16 @@
         
-.getAttribute(
 CURRENT_
@@ -1253,32 +1253,324 @@
        %7D%0A    %7D%0A%0A
+    private static WrappedSession getCurrentHttpSession() %7B%0A        VaadinSession s = VaadinSession.getCurrent();%0A        if (s == null) %7B%0A            throw new IllegalStateException(%0A                    %22No session found for current thread%22);%0A        %7D%0A        return s.getSession();%0A    %7D%0A%0A
     /**%0A     * S
@@ -1726,17 +1726,16 @@
 .%0A     *
- 
 %0A     * 
@@ -1940,36 +1940,20 @@
 tCurrent
-Request().getWrapped
+Http
 Session(
@@ -2071,28 +2071,12 @@
 rent
-Request().getWrapped
+Http
 Sess
@@ -2186,303 +2186,6 @@
  %7D%0A%0A
-    private static VaadinRequest getCurrentRequest() %7B%0A        VaadinRequest request = VaadinService.getCurrentRequest();%0A        if (request == null) %7B%0A            throw new IllegalStateException(%0A                    %22No request bound to current thread%22);%0A        %7D%0A        return request;%0A    %7D%0A
 %7D%0A

Use current session instead of current request