Example usage for com.vaadin.server VaadinSession setAttribute

List of usage examples for com.vaadin.server VaadinSession setAttribute

Introduction

In this page you can find the example usage for com.vaadin.server VaadinSession setAttribute.

Prototype

public <T> void setAttribute(Class<T> type, T value) 

Source Link

Document

Stores a value in this service session.

Usage

From source file:io.github.jikuja.vaadin_yamapa.ui.views.PoiMap.java

License:Creative Commons License

private void setupLocationButton() {
    locate.addStyleName("locatebutton");
    locate.setIcon(FontAwesome.CROSSHAIRS);
    locate.addStyleName(ValoTheme.BUTTON_LARGE);
    locate.addStyleName(ValoTheme.BUTTON_ICON_ONLY);
    locate.setDescription("Locate me");
    locate.addClickListener(event -> {
        Notification.show("Attention", "No real geolocation support. Map center is used as coordinates",
                Notification.Type.HUMANIZED_MESSAGE);

        VaadinSession session = VaadinSession.getCurrent();
        session.setAttribute("lat", map.getCenter().getLat());
        session.setAttribute("lon", map.getCenter().getLon());
        MyUI.getInstance().getMenu().updateButtons();
    });/*  w  ww . j  a va2 s  .  c  o m*/
}

From source file:org.lucidj.navigatormanager.DefaultNavigatorManager.java

License:Apache License

private ViewProvider get_or_create_proxy_view_provider(Navigator navigator) {
    UI current_ui = UI.getCurrent();//from ww  w . j av a2s  . com
    VaadinSession current_session = (current_ui != null) ? current_ui.getSession() : null;
    ViewProvider proxy_view_provider = null;

    if (current_session != null) {
        // Get or create the ProxyViewProvider, bound into VaadinSession
        Object view_provider_obj = current_session.getAttribute(ATTR_VIEW_PROVIDER);

        if (view_provider_obj instanceof ViewProvider) {
            // TODO: WHAT IF NAVIGATOR CHANGES INSIDE THE SAME UI? CREATE SOME WAY TO UPDATE IT.
            proxy_view_provider = (ViewProvider) view_provider_obj;
        } else {
            proxy_view_provider = serviceContext.wrapObject(ProxyViewProvider.class,
                    new ProxyViewProvider(this, navigator));
            current_session.setAttribute(ATTR_VIEW_PROVIDER, proxy_view_provider);
        }
    }
    return (proxy_view_provider);
}

From source file:org.ripla.web.internal.services.ControllerManager.java

License:Open Source License

@SuppressWarnings("unchecked")
private void setActiveMenuItem(final String inBundleName) {
    final VaadinSession lCurrentSession = VaadinSession.getCurrent();
    MenuItem lOldItem = null;/*  w  w w  .j ava2 s . c o m*/
    try {
        lCurrentSession.getLockInstance().lock();
        lOldItem = (MenuItem) lCurrentSession.getAttribute(Constants.SA_ACTIVE_MENU);
    } finally {
        lCurrentSession.getLockInstance().unlock();
    }

    if (lOldItem != null) {
        lOldItem.setStyleName("");
    }
    Map<String, MenuItem> lMenuMap = null;
    try {
        lCurrentSession.getLockInstance().lock();
        lMenuMap = (Map<String, MenuItem>) lCurrentSession.getAttribute(Constants.SA_MENU_MAP);
    } finally {
        lCurrentSession.getLockInstance().unlock();
    }
    final MenuItem lNewItem = lMenuMap == null ? null : lMenuMap.get(inBundleName);
    if (lNewItem != null) {
        lNewItem.setStyleName(Constants.CSS_ACTIVE_MENU);
    }
    try {
        lCurrentSession.getLockInstance().lock();
        lCurrentSession.setAttribute(Constants.SA_ACTIVE_MENU, lNewItem);
    } finally {
        lCurrentSession.getLockInstance().unlock();
    }
}

From source file:org.vaadin.spring.security.internal.SecurityContextVaadinRequestListener.java

License:Apache License

@Override
public void onRequestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
    try {//from ww  w.ja v a  2s . c om
        if (session != null) {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            logger.debug("Storing security context {} in VaadinSession {}", securityContext, session);
            session.lock();
            try {
                session.setAttribute(SECURITY_CONTEXT_SESSION_ATTRIBUTE, securityContext);
            } finally {
                session.unlock();
            }
        } else {
            logger.debug("No VaadinSession available for storing the security context");
        }
    } finally {
        logger.debug("Clearing security context");
        SecurityContextHolder.clearContext();
    }
}

From source file:org.vaadin.spring.security.managed.SecurityContextVaadinRequestListener.java

License:Apache License

@Override
public void onRequestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
    try {/*  www  .  ja v  a  2  s  . c  o  m*/
        if (session != null) {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            logger.trace("Storing security context {} in VaadinSession {}", securityContext, session);
            session.lock();
            try {
                session.setAttribute(SECURITY_CONTEXT_SESSION_ATTRIBUTE, securityContext);
            } finally {
                session.unlock();
            }
        } else {
            logger.trace("No VaadinSession available for storing the security context");
        }
    } finally {
        logger.trace("Clearing security context");
        SecurityContextHolder.clearContext();
    }
}

From source file:org.vaadin.tori.util.InputCacheUtil.java

License:Apache License

@SuppressWarnings("unchecked")
private Map<Object, String> getSessionCache() {
    VaadinSession session = UI.getCurrent().getSession();
    if (session.getAttribute(INPUT_CACHE) == null) {
        session.setAttribute(INPUT_CACHE, new HashMap<Object, String>());
    }/* w w  w .  j av  a2 s  . c o m*/
    return (Map<Object, String>) session.getAttribute(INPUT_CACHE);
}

From source file:process.control.LoginControl.java

public static void controllogin(String userlogin, String password) {
    User user = null;//from w w w  .j a v  a 2 s.  c o  m
    if (userlogin.equals("lars") & password.equals("lars")) {

        user = new User();
        user.setname(userlogin);

        VaadinSession session = UI.getCurrent().getSession();
        session.setAttribute("Lars", user);

        UI.getCurrent().getNavigator().navigateTo("MainView");

    }

    else {
        Notification.show("Nutzer nicht gefunden oder Eingaben falsch!", Notification.Type.ERROR_MESSAGE);
    }

}

From source file:uk.q3c.krail.core.shiro.DefaultSubjectProvider.java

License:Apache License

/**
 * Returns the subject for the application and thread which represents the
 * current user. The subject is always available; however it may represent an
 * anonymous user.//from   w w w.j  a v a  2 s.  c o m
 *
 * @return the subject for the current application and thread
 *
 * @see SecurityUtils#getSubject()
 */
public Subject getSubject() {
    VaadinSession session = VaadinSession.getCurrent();

    // This should never happen, but just in case we'll check.
    if (session == null) {
        throw new IllegalStateException("Unable to locate VaadinSession " + "to store Shiro Subject.");
    }

    Subject subject = (Subject) session.getAttribute(SUBJECT_ATTRIBUTE);

    if (subject == null) {

        // Create a new subject using the configured security manager.
        subject = (new Subject.Builder(securityManager)).buildSubject();
        session.setAttribute(SUBJECT_ATTRIBUTE, subject);
    }
    return subject;
}

From source file:uk.q3c.krail.core.shiro.VaadinSessionManager.java

License:Apache License

@Override
public Session start(SessionContext context) {
    log.debug("starting VaadinSessionManager");
    // Retrieve the VaadinSession for the current user.
    VaadinSession vaadinSession = sessionProvider.get();

    // Create a new security session using the session factory.
    SimpleSession shiroSession = (SimpleSession) sessionFactory.createSession(context);

    // Assign a unique ID to the session now because this session manager
    // doesn't use a SessionDAO for persistence as it delegates to any
    // VaadinSession configured persistence.
    shiroSession.setId(UUID.randomUUID().toString());

    // Put the security session in the VaadinSession. We use the session's ID as
    // part of the key just to be safe so we can double check that the security
    // session matches when it is requested in getSession.
    vaadinSession.setAttribute(SESSION_ATTRIBUTE_PREFIX + shiroSession.getId(), shiroSession);

    return shiroSession;
}

From source file:uk.q3c.krail.core.shiro.VaadinSessionManager.java

License:Apache License

@Override
public Session getSession(SessionKey key) throws SessionException {

    // Retrieve the VaadinSession for the current user.
    VaadinSession vaadinSession = sessionProvider.get();

    String attributeName = SESSION_ATTRIBUTE_PREFIX + key.getSessionId();

    if (vaadinSession != null) {
        // If we have a valid VaadinSession, try to get the Shiro Session.
        SimpleSession shiroSession = (SimpleSession) vaadinSession.getAttribute(attributeName);

        if (shiroSession != null) {

            // Make sure the Shiro Session hasn't been stopped or expired (i.e. the
            // user logged out).
            if (shiroSession.isValid()) {
                return shiroSession;
            } else {
                // This is an invalid or expired session so we'll clean it up.
                vaadinSession.setAttribute(attributeName, null);
            }//w  w w.j  a v  a  2  s.  c o m
        }
    }

    return null;
}