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

License:Apache License

@Override
protected void init(VaadinRequest request) {
    super.init(request);

    this.instanceConfig = getInstanceConfig(request);

    getPage().setTitle(instanceConfig.getInstanceDisplayName() + " (ANNIS Corpus Search)");

    queryController = new QueryController(this);

    refresh = new Refresher();
    // deactivate refresher by default
    refresh.setRefreshInterval(-1);/*from  w  ww .ja v  a  2s .c o  m*/
    refresh.addListener(queryController);
    addExtension(refresh);

    // always get the resize events directly
    setImmediate(true);

    VerticalLayout mainLayout = new VerticalLayout();
    setContent(mainLayout);

    mainLayout.setSizeFull();
    mainLayout.setMargin(false);

    final ScreenshotMaker screenshot = new ScreenshotMaker(this);
    addExtension(screenshot);

    css = new CSSInject(this);

    HorizontalLayout layoutToolbar = new HorizontalLayout();
    layoutToolbar.setWidth("100%");
    layoutToolbar.setHeight("-1px");

    mainLayout.addComponent(layoutToolbar);
    layoutToolbar.addStyleName("toolbar");
    layoutToolbar.addStyleName("border-layout");

    Button btAboutAnnis = new Button("About ANNIS");
    btAboutAnnis.addStyleName(ChameleonTheme.BUTTON_SMALL);
    btAboutAnnis.setIcon(new ThemeResource("info.gif"));

    btAboutAnnis.addClickListener(new AboutClickListener());

    btBugReport = new Button("Report Bug");
    btBugReport.addStyleName(ChameleonTheme.BUTTON_SMALL);
    btBugReport.setDisableOnClick(true);
    btBugReport.setIcon(new ThemeResource("../runo/icons/16/email.png"));
    btBugReport.addListener(new Button.ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
            screenshot.makeScreenshot();
            btBugReport.setCaption("bug report is initialized...");
        }
    });

    String bugmail = (String) VaadinSession.getCurrent().getAttribute("bug-e-mail");
    if (bugmail != null && !bugmail.isEmpty() && !bugmail.startsWith("${")
            && new EmailValidator("").isValid(bugmail)) {
        this.bugEMailAddress = bugmail;
    }
    btBugReport.setVisible(this.bugEMailAddress != null);

    lblUserName = new Label("not logged in");
    lblUserName.setWidth("-1px");
    lblUserName.setHeight("-1px");
    lblUserName.addStyleName("right-aligned-text");

    btLoginLogout = new Button("Login", new Button.ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
            if (isLoggedIn()) {
                // logout
                Helper.setUser(null);
                Notification.show("Logged out", Notification.Type.TRAY_NOTIFICATION);
                updateUserInformation();
            } else {
                showLoginWindow();
            }
        }
    });
    btLoginLogout.setSizeUndefined();
    btLoginLogout.setStyleName(ChameleonTheme.BUTTON_SMALL);
    btLoginLogout.setIcon(new ThemeResource("../runo/icons/16/user.png"));

    Button btOpenSource = new Button("Help us to make ANNIS better!");
    btOpenSource.setStyleName(BaseTheme.BUTTON_LINK);
    btOpenSource.addListener(new Button.ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
            Window w = new HelpUsWindow();
            w.setCaption("Help us to make ANNIS better!");
            w.setModal(true);
            w.setResizable(true);
            w.setWidth("600px");
            w.setHeight("500px");
            addWindow(w);
            w.center();
        }
    });

    layoutToolbar.addComponent(btAboutAnnis);
    layoutToolbar.addComponent(btBugReport);
    layoutToolbar.addComponent(btOpenSource);
    layoutToolbar.addComponent(lblUserName);
    layoutToolbar.addComponent(btLoginLogout);

    layoutToolbar.setSpacing(true);
    layoutToolbar.setComponentAlignment(btAboutAnnis, Alignment.MIDDLE_LEFT);
    layoutToolbar.setComponentAlignment(btBugReport, Alignment.MIDDLE_LEFT);
    layoutToolbar.setComponentAlignment(btOpenSource, Alignment.MIDDLE_CENTER);
    layoutToolbar.setComponentAlignment(lblUserName, Alignment.MIDDLE_RIGHT);
    layoutToolbar.setComponentAlignment(btLoginLogout, Alignment.MIDDLE_RIGHT);
    layoutToolbar.setExpandRatio(btOpenSource, 1.0f);

    //HorizontalLayout hLayout = new HorizontalLayout();
    final HorizontalSplitPanel hSplit = new HorizontalSplitPanel();
    hSplit.setSizeFull();

    mainLayout.addComponent(hSplit);
    mainLayout.setExpandRatio(hSplit, 1.0f);

    AutoGeneratedQueries autoGenQueries = new AutoGeneratedQueries("example queries", this);

    controlPanel = new ControlPanel(queryController, instanceConfig, autoGenQueries);
    controlPanel.setWidth(100f, Layout.Unit.PERCENTAGE);
    controlPanel.setHeight(100f, Layout.Unit.PERCENTAGE);
    hSplit.setFirstComponent(controlPanel);

    tutorial = new TutorialPanel();
    tutorial.setHeight("99%");

    mainTab = new TabSheet();
    mainTab.setSizeFull();
    mainTab.addTab(autoGenQueries, "example queries");
    mainTab.addTab(tutorial, "Tutorial");

    queryBuilder = new QueryBuilderChooser(queryController, this, instanceConfig);
    mainTab.addTab(queryBuilder, "Query Builder");

    hSplit.setSecondComponent(mainTab);
    hSplit.setSplitPosition(CONTROL_PANEL_WIDTH, Unit.PIXELS);
    hSplit.addSplitterClickListener(new AbstractSplitPanel.SplitterClickListener() {
        @Override
        public void splitterClick(AbstractSplitPanel.SplitterClickEvent event) {
            if (event.isDoubleClick()) {
                if (hSplit.getSplitPosition() == CONTROL_PANEL_WIDTH) {
                    // make small
                    hSplit.setSplitPosition(0.0f, Unit.PIXELS);
                } else {
                    // reset to default width
                    hSplit.setSplitPosition(CONTROL_PANEL_WIDTH, Unit.PIXELS);
                }
            }
        }
    });
    // hLayout.setExpandRatio(mainTab, 1.0f);

    addAction(new ShortcutListener("^Query builder") {
        @Override
        public void handleAction(Object sender, Object target) {
            mainTab.setSelectedTab(queryBuilder);
        }
    });

    addAction(new ShortcutListener("Tutor^eial") {
        @Override
        public void handleAction(Object sender, Object target) {
            mainTab.setSelectedTab(tutorial);
        }
    });

    getPage().addUriFragmentChangedListener(this);

    getSession().addRequestHandler(new RequestHandler() {
        @Override
        public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
                throws IOException {
            checkCitation(request);

            if (request.getPathInfo() != null && request.getPathInfo().startsWith("/vis-iframe-res/")) {
                String uuidString = StringUtils.removeStart(request.getPathInfo(), "/vis-iframe-res/");
                UUID uuid = UUID.fromString(uuidString);
                IFrameResourceMap map = VaadinSession.getCurrent().getAttribute(IFrameResourceMap.class);
                if (map == null) {
                    response.setStatus(404);
                } else {
                    IFrameResource res = map.get(uuid);
                    if (res != null) {
                        response.setStatus(200);
                        response.setContentType(res.getMimeType());
                        response.getOutputStream().write(res.getData());
                    }
                }
                return true;
            }

            return false;
        }
    });

    getSession().setAttribute(MediaController.class, new MediaControllerImpl());

    getSession().setAttribute(PDFController.class, new PDFControllerImpl());

    loadInstanceFonts();

    checkCitation(request);
    lastQueriedFragment = "";
    evaluateFragment(getPage().getUriFragment());

    updateUserInformation();
}

From source file:annis.gui.SearchUI.java

License:Apache License

public void checkCitation(VaadinRequest request) {
    Object origURLRaw = VaadinSession.getCurrent().getSession().getAttribute("citation");
    if (origURLRaw == null || !(origURLRaw instanceof String)) {
        return;//  ww w  . j  a v  a  2  s.co  m
    }
    String origURL = (String) origURLRaw;
    String parameters = origURL.replaceAll(".*?/Cite(/)?", "");
    if (!"".equals(parameters) && !origURL.equals(parameters)) {
        try {
            String decoded = URLDecoder.decode(parameters, "UTF-8");
            evaluateCitation(decoded);
        } catch (UnsupportedEncodingException ex) {
            log.error(null, ex);
        }
    }

}

From source file:annis.gui.SearchView.java

License:Apache License

public void checkCitation() {
    if (VaadinSession.getCurrent() == null || VaadinSession.getCurrent().getSession() == null) {
        return;//w w w.  j a v  a  2s.c o  m
    }
    Object origURLRaw = VaadinSession.getCurrent().getSession().getAttribute("citation");
    if (origURLRaw == null || !(origURLRaw instanceof String)) {
        return;
    }
    String origURL = (String) origURLRaw;
    String parameters = origURL.replaceAll(".*?/Cite(/)?", "");
    if (!"".equals(parameters) && !origURL.equals(parameters)) {
        try {
            String decoded = URLDecoder.decode(parameters, "UTF-8");
            evaluateCitation(decoded);
        } catch (UnsupportedEncodingException ex) {
            log.error(null, ex);
        }
    }

}

From source file:annis.libgui.Helper.java

License:Apache License

public static AnnisUser getUser() {

    VaadinSession vSession = VaadinSession.getCurrent();
    WrappedSession wrappedSession = null;

    if (vSession != null) {
        wrappedSession = vSession.getSession();
    }/*www .j av  a2 s  .  c  o  m*/

    if (wrappedSession != null) {

        Object o = VaadinSession.getCurrent().getSession().getAttribute(AnnisBaseUI.USER_KEY);
        if (o != null && o instanceof AnnisUser) {
            return (AnnisUser) o;
        }
    }

    return null;
}

From source file:annis.libgui.Helper.java

License:Apache License

public static void setUser(AnnisUser user) {
    if (user == null) {
        VaadinSession.getCurrent().getSession().removeAttribute(AnnisBaseUI.USER_KEY);
    } else {//  w w  w . ja va2s . c om
        VaadinSession.getCurrent().getSession().setAttribute(AnnisBaseUI.USER_KEY, user);
    }
}

From source file:annis.libgui.Helper.java

License:Apache License

/**
 * Gets or creates a web resource to the ANNIS service.
 *
 * This is a convenience wrapper to {@link #getAnnisWebResource(java.lang.String, annis.security.AnnisUser)
 * }//  ww  w  . j av  a  2  s  .co m
 * that does not need any arguments
 *
 * @return A reference to the ANNIS service root resource.
 */
public static WebResource getAnnisWebResource() {

    VaadinSession vSession = VaadinSession.getCurrent();

    // get URI used by the application
    String uri = null;

    if (vSession != null) {
        uri = (String) VaadinSession.getCurrent().getAttribute(KEY_WEB_SERVICE_URL);
    }

    // if already authentificated the REST client is set as the "user" property
    AnnisUser user = getUser();

    return getAnnisWebResource(uri, user);
}

From source file:annis.libgui.Helper.java

License:Apache License

/**
 * Gets or creates an asynchronous web resource to the ANNIS service.
 *
 * This is a convenience wrapper to {@link #getAnnisWebResource(java.lang.String, annis.security.AnnisUser)
 * }//from ww  w .  j  a va 2  s  .co  m
 * that does not need any arguments
 *
 * @return A reference to the ANNIS service root resource.
 */
public static AsyncWebResource getAnnisAsyncWebResource() {
    // get URI used by the application
    String uri = (String) VaadinSession.getCurrent().getAttribute(KEY_WEB_SERVICE_URL);

    // if already authentificated the REST client is set as the "user" property
    AnnisUser user = getUser();

    return getAnnisAsyncWebResource(uri, user);
}

From source file:annis.libgui.Helper.java

License:Apache License

public static String getContext() {
    if (VaadinService.getCurrentRequest() != null) {
        return VaadinService.getCurrentRequest().getContextPath();
    } else {/*from   w w w .j  av  a2s  . c  o  m*/
        return (String) VaadinSession.getCurrent().getAttribute(AnnisBaseUI.CONTEXT_PATH);
    }

}

From source file:annis.libgui.Helper.java

License:Apache License

/**
 * Returns true if the right-to-left heuristic should be disabled.
 *
 * @return//from w  w  w  .  j a  v  a  2  s  .  c o  m
 */
public static boolean isRTLDisabled() {
    String disableRtl = (String) VaadinSession.getCurrent().getAttribute("disable-rtl");
    return "true".equalsIgnoreCase(disableRtl);
}

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());
    }// w  ww  .j  ava 2s. 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;
}