Example usage for com.vaadin.server VaadinSession getCurrent

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

Introduction

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

Prototype

public static VaadinSession getCurrent() 

Source Link

Document

Gets the currently used session.

Usage

From source file:com.github.daytron.twaattin.ui.TimelineScreen.java

License:Open Source License

public TimelineScreen() {
    setMargin(true);//from w  ww.ja  va2  s .c o m

    Label label = new Label(VaadinSession.getCurrent().getAttribute(Principal.class).getName());

    Button logoutButton = new Button("Logout");
    logoutButton.addClickListener(new LogoutBehaviour());

    HorizontalLayout menuBar = new HorizontalLayout(label, logoutButton);
    menuBar.setWidth(100, Unit.PERCENTAGE);
    menuBar.setComponentAlignment(logoutButton, Alignment.MIDDLE_RIGHT);
    addComponent(menuBar);

    addComponentAttachListener(new TweetRefresherBehaviour());

    Table table = new Table();
    addComponent(table);

    table.addGeneratedColumn("source", new SourceColumnDecorator());
    table.addGeneratedColumn("screenName", new ScreenNameColumnGenerator());
    table.addGeneratedColumn("name", new NameColumnGenerator());
    table.addGeneratedColumn("profileImage", new ProfileImageColumnGenerator());
    table.addGeneratedColumn("text", new TweetColumnDecorator());

    table.setColumnHeader("source", "via");
    table.setColumnHeader("screenName", "Screen name");
    table.setColumnHeader("profileImage", "");
    table.setColumnHeader("text", "Tweet");

    table.setVisibleColumns(
            new Object[] { "text", "name", "screenName", "profileImage", "createdAt", "source" });

}

From source file:com.gmail.volodymyrdotsenko.cms.fe.vaadin.LoginUI.java

License:Apache License

private void login() {
    try {/*  w w  w  .  j  a v a2  s. c  o m*/
        vaadinSecurity.login(userName.getValue(), passwordField.getValue(), rememberMe.getValue());

        VaadinSession.getCurrent().setLocale(new Locale((String) lang.getValue()));
    } catch (AuthenticationException ex) {
        userName.focus();
        userName.selectAll();
        passwordField.setValue("");
        loginFailedLabel.setValue(String.format("Login failed: %s", ex.getMessage()));
        loginFailedLabel.setVisible(true);
        if (loggedOutLabel != null) {
            loggedOutLabel.setVisible(false);
        }
    } catch (Exception ex) {
        Notification.show("An unexpected error occurred", ex.getMessage(), Notification.Type.ERROR_MESSAGE);
        LoggerFactory.getLogger(getClass()).error("Unexpected error while logging in", ex);
    } finally {
        login.setEnabled(true);
    }
}

From source file:com.hack23.cia.web.impl.ui.application.CitizenIntelligenceAgencyUI.java

License:Apache License

@Override
protected void init(final VaadinRequest request) {
    VaadinSession.getCurrent().setErrorHandler(new UiInstanceErrorHandler(this));
    setSizeFull();/*from  w w w .  j a  v  a 2s. co m*/
    final DiscoveryNavigator navigator = new DiscoveryNavigator(this, this);
    navigator.addView("", mainView);
    setNavigator(navigator);

    final Page currentPage = Page.getCurrent();
    final String requestUrl = currentPage.getLocation().toString();
    final String language = request.getLocale().getLanguage();
    final UserConfiguration userConfiguration = configurationManager.getUserConfiguration(requestUrl, language);

    currentPage.setTitle(
            userConfiguration.getAgency().getAgencyName() + ":" + userConfiguration.getPortal().getPortalName()
                    + ":" + userConfiguration.getLanguage().getLanguageName());

    if (getSession().getUIs().isEmpty()) {
        final WebBrowser webBrowser = currentPage.getWebBrowser();

        final CreateApplicationSessionRequest serviceRequest = new CreateApplicationSessionRequest();
        serviceRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());

        final String ipInformation = WebBrowserUtil.getIpInformation(webBrowser);
        serviceRequest.setIpInformation(ipInformation);
        serviceRequest.setUserAgentInformation(webBrowser.getBrowserApplication());
        serviceRequest.setLocale(webBrowser.getLocale().toString());
        serviceRequest.setOperatingSystem(WebBrowserUtil.getOperatingSystem(webBrowser));
        serviceRequest.setSessionType(ApplicationSessionType.ANONYMOUS);

        final ServiceResponse serviceResponse = applicationManager.service(serviceRequest);
        LOGGER.info(LOG_INFO_BROWSER_ADDRESS_APPLICATION_SESSION_ID_RESULT, requestUrl, language, ipInformation,
                webBrowser.getBrowserApplication(), serviceRequest.getSessionId(),
                serviceResponse.getResult().toString());
    }
}

From source file:com.haulmont.cuba.web.AbstractConnection.java

License:Apache License

@Override
@Nullable
public UserSession getSession() {
    return VaadinSession.getCurrent().getAttribute(UserSession.class);
}

From source file:com.haulmont.cuba.web.AbstractConnection.java

License:Apache License

protected void setSession(ClientUserSession clientUserSession) {
    VaadinSession.getCurrent().setAttribute(UserSession.class, clientUserSession);
}

From source file:com.haulmont.cuba.web.App.java

License:Apache License

public List<AppUI> getAppUIs() {
    List<AppUI> list = new ArrayList<>();
    for (UI ui : VaadinSession.getCurrent().getUIs()) {
        if (ui instanceof AppUI)
            list.add((AppUI) ui);/*from  w w  w.j  ava 2s  .c o  m*/
        else
            log.warn("Invalid UI in the session: {}", ui);
    }
    return list;
}

From source file:com.haulmont.cuba.web.App.java

License:Apache License

/**
 * Called when <em>the first</em> UI of the session is initialized.
 *///ww w . java  2 s. c  o m
protected void init(Locale requestLocale) {
    VaadinSession vSession = VaadinSession.getCurrent();
    vSession.setAttribute(App.class, this);

    vSession.setLocale(messageTools.getDefaultLocale());

    // set root error handler for all session
    vSession.setErrorHandler(event -> {
        try {
            getExceptionHandlers().handle(event);
            getAppLog().log(event);
        } catch (Throwable e) {
            //noinspection ThrowableResultOfMethodCallIgnored
            log.error("Error handling exception\nOriginal exception:\n{}\nException in handlers:\n{}",
                    ExceptionUtils.getStackTrace(event.getThrowable()), ExceptionUtils.getStackTrace(e));
        }
    });

    appLog = new AppLog();

    connection = createConnection();
    exceptionHandlers = new ExceptionHandlers(this);
    cookies = new AppCookies();

    themeConstants = loadTheme();

    VaadinServlet vaadinServlet = VaadinServlet.getCurrent();
    ServletContext sc = vaadinServlet.getServletContext();
    String resourcesTimestamp = sc.getInitParameter("webResourcesTs");
    if (StringUtils.isNotEmpty(resourcesTimestamp)) {
        this.webResourceTimestamp = resourcesTimestamp;
    }

    log.debug("Initializing application");

    // get default locale from config
    Locale targetLocale = resolveLocale(requestLocale);
    setLocale(targetLocale);
}

From source file:com.haulmont.cuba.web.App.java

License:Apache License

/**
 * @return Current App instance. Can be invoked anywhere in application code.
 * @throws IllegalStateException if no application instance is bound to the current {@link VaadinSession}
 *//*w  w  w.ja  v a 2s.  c o m*/
public static App getInstance() {
    VaadinSession vSession = VaadinSession.getCurrent();
    if (vSession == null) {
        throw new IllegalStateException("No VaadinSession found");
    }
    if (!vSession.hasLock()) {
        throw new IllegalStateException("VaadinSession is not owned by the current thread");
    }
    App app = vSession.getAttribute(App.class);
    if (app == null) {
        throw new IllegalStateException("No App is bound to the current VaadinSession");
    }
    return app;
}

From source file:com.haulmont.cuba.web.App.java

License:Apache License

/**
 * @return true if an {@link App} instance is currently bound and can be safely obtained by {@link #getInstance()}
 *//*from ww w . j a  v a 2 s  .  c o m*/
public static boolean isBound() {
    VaadinSession vSession = VaadinSession.getCurrent();
    return vSession != null && vSession.hasLock() && vSession.getAttribute(App.class) != null;
}

From source file:com.haulmont.cuba.web.App.java

License:Apache License

public Locale getLocale() {
    return VaadinSession.getCurrent().getLocale();
}