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:annis.gui.QueryController.java

License:Apache License

/**
 * Executes a query./*from ww  w.  j  a v  a2s .co m*/
 * @param replaceOldTab
 * @param freshQuery If true the offset and the selected matches are reset before executing the query. 
 */
public void executeSearch(boolean replaceOldTab, boolean freshQuery) {
    if (freshQuery) {
        getState().getOffset().setValue(0l);
        getState().getSelectedMatches().setValue(new TreeSet<Long>());
        // get the value for the visible segmentation from the configured context
        Set<String> selectedCorpora = getState().getSelectedCorpora().getValue();
        CorpusConfig config = new CorpusConfig();
        if (selectedCorpora != null && !selectedCorpora.isEmpty()) {
            config = ui.getCorpusConfigWithCache(selectedCorpora.iterator().next());
        }

        if (config.containsKey(SearchOptionsPanel.KEY_DEFAULT_BASE_TEXT_SEGMENTATION)) {
            String configVal = config.getConfig(SearchOptionsPanel.KEY_DEFAULT_BASE_TEXT_SEGMENTATION);
            if ("".equals(configVal) || "tok".equals(configVal)) {
                configVal = null;
            }
            getState().getVisibleBaseText().setValue(configVal);
        } else {
            getState().getVisibleBaseText().setValue(getState().getContextSegmentation().getValue());
        }
    }
    // construct a query from the current properties
    DisplayedResultQuery displayedQuery = getSearchQuery();

    searchView.getControlPanel().getQueryPanel().setStatus("Searching...");

    cancelSearch();

    // cleanup resources
    VaadinSession session = VaadinSession.getCurrent();
    session.setAttribute(IFrameResourceMap.class, new IFrameResourceMap());
    if (session.getAttribute(MediaController.class) != null) {
        session.getAttribute(MediaController.class).clearMediaPlayers();
    }

    searchView.updateFragment(displayedQuery);

    if (displayedQuery.getCorpora() == null || displayedQuery.getCorpora().isEmpty()) {
        Notification.show("Please select a corpus", Notification.Type.WARNING_MESSAGE);
        return;
    }
    if ("".equals(displayedQuery.getQuery())) {
        Notification.show("Empty query", Notification.Type.WARNING_MESSAGE);
        return;
    }

    addHistoryEntry(displayedQuery);

    AsyncWebResource res = Helper.getAnnisAsyncWebResource();

    //
    // begin execute match fetching
    //
    ResultViewPanel oldPanel = searchView.getLastSelectedResultView();
    if (replaceOldTab) {
        // remove old panel from view
        searchView.closeTab(oldPanel);
    }

    ResultViewPanel newResultView = new ResultViewPanel(ui, ui, ui.getInstanceConfig(), displayedQuery);
    newResultView.getPaging()
            .addCallback(new SpecificPagingCallback(ui, searchView, newResultView, displayedQuery));

    TabSheet.Tab newTab;

    List<ResultViewPanel> existingResultPanels = getResultPanels();
    String caption = existingResultPanels.isEmpty() ? "Query Result"
            : "Query Result #" + (existingResultPanels.size() + 1);

    newTab = searchView.getMainTab().addTab(newResultView, caption);
    newTab.setClosable(true);
    newTab.setIcon(FontAwesome.SEARCH);

    searchView.getMainTab().setSelectedTab(newResultView);
    searchView.notifiyQueryStarted();

    Background.run(new ResultFetchJob(displayedQuery, newResultView, ui));

    //
    // end execute match fetching
    //
    // 
    // begin execute count
    //
    // start count query
    searchView.getControlPanel().getQueryPanel().setCountIndicatorEnabled(true);

    AsyncWebResource countRes = res.path("query").path("search").path("count")
            .queryParam("q", Helper.encodeJersey(displayedQuery.getQuery()))
            .queryParam("corpora", Helper.encodeJersey(StringUtils.join(displayedQuery.getCorpora(), ",")));
    Future<MatchAndDocumentCount> futureCount = countRes.get(MatchAndDocumentCount.class);
    state.getExecutedTasks().put(QueryUIState.QueryType.COUNT, futureCount);

    Background.run(new CountCallback(newResultView, displayedQuery.getLimit(), ui));

    //
    // end execute count
    //
}

From source file:annis.gui.servlets.AnnisServlet.java

License:Apache License

private void loadPropertyFile(File f, VaadinSession session) {
    if (f.canRead() && f.isFile()) {
        try (FileInputStream fis = new FileInputStream(f)) {
            Properties p = new Properties();
            p.load(fis);//w w w .j av  a2  s. c  o  m

            // copy all properties to the session
            for (String name : p.stringPropertyNames()) {
                session.setAttribute(name, p.getProperty(name));
            }

        } catch (IOException ex) {

        }
    }
}

From source file:annis.gui.servlets.AnnisServlet.java

License:Apache License

@Override
protected void servletInitialized() throws ServletException {
    super.servletInitialized();

    getService().addSessionInitListener(new SessionInitListener() {

        @Override//from w w w .  jav a2s  .  c  o  m
        public void sessionInit(SessionInitEvent event) throws ServiceException {
            VaadinSession session = event.getSession();
            if (Helper.isKickstarter(session)) {
                // only load configurations from the WEB-INF/conf folder
                File config = new File(event.getService().getBaseDirectory(),
                        "/WEB-INF/conf/annis-gui.properties");
                loadPropertyFile(config, session);
                // manually override the service URL if given
                String serviceURL = session.getConfiguration().getInitParameters()
                        .getProperty(Helper.KEY_WEB_SERVICE_URL);
                if (serviceURL != null) {
                    session.setAttribute(Helper.KEY_WEB_SERVICE_URL, serviceURL);
                }
            } else {
                // load some additional properties from our ANNIS configuration
                loadApplicationProperties("annis-gui.properties", session);
            }

            event.getSession().addRequestHandler(new ShortenerRequestHandler());
        }
    });
}

From source file:annis.libgui.visualizers.AbstractIFrameVisualizer.java

License:Apache License

@Override
public Component createComponent(final VisualizerInput vis, VisualizationToggle visToggle) {

    VaadinSession session = VaadinSession.getCurrent();
    if (session.getAttribute(IFrameResourceMap.class) == null) {
        session.setAttribute(IFrameResourceMap.class, new IFrameResourceMap());
    }//from  ww w . j  a v a 2 s  .  com

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    writeOutput(vis, outStream);

    IFrameResource res = new IFrameResource();
    res.setData(outStream.toByteArray());
    res.setMimeType(getContentType());

    UUID uuid = UUID.randomUUID();
    session.getAttribute(IFrameResourceMap.class).put(uuid, res);

    URI base = UI.getCurrent().getPage().getLocation();
    AutoHeightIFrame iframe = new AutoHeightIFrame(base.resolve("vis-iframe-res/" + uuid.toString()));
    return iframe;
}

From source file:com.foc.vaadin.FocWebApplication.java

License:Apache License

public void setFocWebSession(FocWebSession focSession) {
    //MultiTab     
    VaadinSession vaadinSession = getSession();
    if (vaadinSession != null) {
        vaadinSession.setAttribute(ATT_WEB_SESSION, focSession);
        //        WrappedHttpSession wSession = (WrappedHttpSession) vaadinSession.getSession();
        //        setHttpSession(wSession.getHttpSession());
        //        setFocWebSession(getHttpSession(), focSession);
    } else if (getHttpSession() != null) {
        getHttpSession().setAttribute(ATT_WEB_SESSION, focSession);
    } else {/*from  ww w  .jav  a 2s. c om*/
        focSession_ForNonVaadinAndHTTP = focSession;
    }
}

From source file:com.freebox.engeneering.application.system.ApplicationScope.java

License:Apache License

@Override
public Object get(String name, ObjectFactory<?> objectFactory) {

    final VaadinSession current = VaadinSession.getCurrent();
    String uiId = getCurrentUiId();
    Object bean = current.getAttribute(APPLICATION_SCOPE + uiId + name);
    if (bean == null) {
        bean = objectFactory.getObject();
        current.setAttribute(APPLICATION_SCOPE + uiId + name, bean);
    }/* w w w  .j av a2 s. co  m*/
    return bean;
}

From source file:com.freebox.engeneering.application.system.ApplicationScope.java

License:Apache License

@Override
public Object remove(String name) {
    final VaadinSession current = VaadinSession.getCurrent();

    String uiId = getCurrentUiId();
    Object bean = current.getAttribute(APPLICATION_SCOPE + uiId + name);
    if (bean != null) {
        current.setAttribute(APPLICATION_SCOPE + uiId + name, null);
        this.destructionCallbacks.remove(name);
    }/*from   w w w  .  ja v  a 2s .co  m*/
    return bean;
}

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

License:Apache License

/**
 * Called when <em>the first</em> UI of the session is initialized.
 *//*from w w w . j  av a  2 s. co 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.settings.WebSettingsClient.java

License:Apache License

public void clearCache() {
    VaadinSession session = VaadinSession.getCurrent();
    session.setAttribute(SettingsClient.NAME, null);
}

From source file:com.haulmont.cuba.web.settings.WebSettingsClient.java

License:Apache License

protected Map<String, Optional<String>> getCache() {
    VaadinSession session = VaadinSession.getCurrent();
    @SuppressWarnings("unchecked")
    Map<String, Optional<String>> settings = (Map<String, Optional<String>>) session
            .getAttribute(SettingsClient.NAME);
    if (settings == null) {
        settings = new ConcurrentHashMap<>();
        session.setAttribute(SettingsClient.NAME, settings);
    }/* ww  w. j  a va  2s.c om*/
    return settings;
}