com.vaadin.server.VaadinSession Java Examples

The following examples show how to use com.vaadin.server.VaadinSession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringAuthManager.java    From jdal with Apache License 2.0 6 votes vote down vote up
@Override
public boolean validate(String username, String password) {
	UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
	try {
		Authentication auth = this.authenticationManager.authenticate(token);
		if (auth.isAuthenticated()) {
			// execute session authentication strategy
			if (this.sessionStrategy != null)
				this.sessionStrategy.onAuthentication(auth, VaadinServletService.getCurrentServletRequest(),
						VaadinServletService.getCurrentResponse());
			SecurityContextHolder.getContext().setAuthentication(auth);
			// save request in context session
			VaadinSession.getCurrent().getSession().setAttribute(
					HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
					SecurityContextHolder.getContext());
			
			return  true;
		}
		SecurityContextHolder.clearContext();
		return false;
	}
	catch(AuthenticationException ae) {
		SecurityContextHolder.clearContext();
		return false;
	}
}
 
Example #2
Source File: SockJSPushConnection.java    From vertx-vaadin with MIT License 6 votes vote down vote up
/**
 * Pushes pending state changes and client RPC calls to the client. If
 * {@code isConnected()} is false, defers the push until a connection is
 * established.
 *
 * @param async True if this push asynchronously originates from the server,
 *              false if it is a response to a client request.
 */
void push(boolean async) {
    if (!isConnected()) {
        if (async && state != State.RESPONSE_PENDING) {
            state = State.PUSH_PENDING;
        } else {
            state = State.RESPONSE_PENDING;
        }
    } else {
        try {
            UI ui = VaadinSession.getCurrent().getUIById(this.uiId);
            Writer writer = new StringWriter();
            new UidlWriter().write(ui, writer, async);
            sendMessage("for(;;);[{" + writer + "}]");
        } catch (Exception e) {
            throw new PushException("Push failed", e);
        }
    }
}
 
Example #3
Source File: VertxVaadinService.java    From vertx-vaadin with MIT License 6 votes vote down vote up
@Override
public String getMainDivId(VaadinSession session, VaadinRequest request, Class<? extends UI> uiClass) {
    String appId = request.getPathInfo();
    if (appId == null || "".equals(appId) || "/".equals(appId)) {
        appId = "ROOT";
    }
    appId = appId.replaceAll("[^a-zA-Z0-9]", "");
    // Add hashCode to the end, so that it is still (sort of)
    // predictable, but indicates that it should not be used in CSS
    // and
    // such:
    int hashCode = appId.hashCode();
    if (hashCode < 0) {
        hashCode = -hashCode;
    }
    appId = appId + "-" + hashCode;
    return appId;
}
 
Example #4
Source File: App.java    From cuba with Apache License 2.0 6 votes vote down vote up
public void setLocale(Locale locale) {
    UserSession session = getConnection().getSession();
    if (session != null) {
        session.setLocale(locale);
    }

    AppUI currentUi = AppUI.getCurrent();
    // it can be null if we handle request in a custom RequestHandler
    if (currentUi != null) {
        currentUi.setLocale(locale);
        currentUi.updateClientSystemMessages(locale);
    }

    VaadinSession.getCurrent().setLocale(locale);

    for (AppUI ui : getAppUIs()) {
        if (ui != currentUi) {
            ui.accessSynchronously(() -> {
                ui.setLocale(locale);
                ui.updateClientSystemMessages(locale);
            });
        }
    }
}
 
Example #5
Source File: WebLookupFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #6
Source File: WebFieldGroupTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };
}
 
Example #7
Source File: WebPickerFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #8
Source File: WebOptionsGroupTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #9
Source File: WebLookupPickerFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #10
Source File: WebDateFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #11
Source File: WebOptionsListTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #12
Source File: WebTextFieldTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #13
Source File: WebLookupFieldDsTest.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            vaadinSession.getLocale(); result = Locale.ENGLISH; minTimes = 0;
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;

            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;

            globalConfig.getAvailableLocales(); result = ImmutableMap.of("en", Locale.ENGLISH); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };
    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #14
Source File: LoginWindow.java    From jpa-invoicer with The Unlicense 6 votes vote down vote up
@Override
public void attach() {
    super.attach();

    service = createService();
    String url = service.getAuthorizationUrl(null);
    
    gplusLoginButton = new Link("Login with Google", new ExternalResource(url));
    gplusLoginButton.addStyleName(ValoTheme.LINK_LARGE);

    VaadinSession.getCurrent().addRequestHandler(this);

    setContent(new MVerticalLayout(gplusLoginButton).alignAll(
            Alignment.MIDDLE_CENTER).withFullHeight());
    setModal(true);
    setWidth("300px");
    setHeight("200px");

}
 
Example #15
Source File: SchemeRequestHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
  response.setContentType("text/css");

  byte[] text = generateCss();

  response.setCacheTime(-1);
  response.setStatus(HttpURLConnection.HTTP_OK);

  try (OutputStream stream = response.getOutputStream()) {
    response.setContentLength(text.length);
    stream.write(text);
  }

  return true;
}
 
Example #16
Source File: VaadinScope.java    From jdal with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String getConversationId() {
	Integer uiId = null;
	
	UI ui =  UI.getCurrent();
	if (ui == null) {
		UIid id = CurrentInstance.get(UIid.class);
		if (id != null) {
			uiId = id.getUiId();
		}
	}
	else if (ui != null) {
		if (!sessions.containsKey(ui)) {
			ui.addDetachListener(this);
			sessions.put(ui, VaadinSession.getCurrent().getSession().getId());
		}

		uiId = ui.getUIId();
	}
	
	return uiId != null ? getConversationId(uiId) : null;
}
 
Example #17
Source File: VaadinSessionScope.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public String getConversationId() {
    if (VaadinSession.getCurrent() == null || !VaadinSession.getCurrent().hasLock()) {
        throw new IllegalStateException("Unable to use VaadinSessionScope from non-Vaadin thread");
    }

    return VaadinSession.getCurrent().getSession().getId();
}
 
Example #18
Source File: App.java    From cuba with Apache License 2.0 5 votes vote down vote up
public List<AppUI> getAppUIs() {
    List<AppUI> list = new ArrayList<>();
    for (UI ui : VaadinSession.getCurrent().getUIs()) {
        if (ui instanceof AppUI)
            list.add((AppUI) ui);
        else
            log.warn("Invalid UI in the session: {}", ui);
    }
    return list;
}
 
Example #19
Source File: VaadinSessionScope.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public Object remove(String name) {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null || !session.hasLock()) {
        throw new IllegalStateException("Unable to use VaadinSessionScope from non-Vaadin thread");
    }

    Object bean = session.getAttribute(name);
    session.setAttribute(name, null);
    return bean;
}
 
Example #20
Source File: VaadinSessionScope.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null || !session.hasLock()) {
        throw new IllegalStateException("Unable to use VaadinSessionScope from non-Vaadin thread");
    }

    Object object = session.getAttribute(name);
    if (object == null) {
        object = objectFactory.getObject();
        session.setAttribute(name, object);
    }
    return object;
}
 
Example #21
Source File: ConnectionImpl.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected void setSessionInternal(@Nullable ClientUserSession userSession) {
    VaadinSession.getCurrent().setAttribute(UserSession.class, userSession);
    if (userSession != null) {
        AppContext.setSecurityContext(new SecurityContext(userSession));
    } else {
        AppContext.setSecurityContext(null);
    }
}
 
Example #22
Source File: WebLookupPickerFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;
            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
            configuration.getConfig(ClientConfig.class); result = clientConfig; minTimes = 0;
            clientConfig.getPickerShortcutModifiers(); result = "CTRL-ALT"; minTimes = 0;
        }
    };
    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #23
Source File: ConnectionImpl.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected WebBrowser getWebBrowserDetails() {
    // timezone info is passed only on VaadinSession creation
    WebBrowser webBrowser = VaadinSession.getCurrent().getBrowser();
    VaadinRequest currentRequest = VaadinService.getCurrentRequest();
    // update web browser instance if current request is not null
    // it can be null in case of background/async processing of login request
    if (currentRequest != null) {
        webBrowser.updateRequestDetails(currentRequest);
    }
    return webBrowser;
}
 
Example #24
Source File: WebLabelDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            VaadinSession.getCurrent(); result = vaadinSession; minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };
    uiComponents = new TestUiComponents(applicationContext);
}
 
Example #25
Source File: WebPickerFieldDsTest.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
protected void initExpectations() {
    super.initExpectations();

    new Expectations() {
        {
            VaadinSession.getCurrent(); result = vaadinSession;  minTimes = 0;
            vaadinSession.getConverterFactory(); result = new DefaultConverterFactory(); minTimes = 0;
            AppContext.getProperty("cuba.mainMessagePack"); result = "com.haulmont.cuba.web"; minTimes = 0;
        }
    };

    this.uiComponents = new TestUiComponents(applicationContext);
}
 
Example #26
Source File: SockJSPushHandler.java    From vertx-vaadin with MIT License 5 votes vote down vote up
/**
 * Call the session's {@link ErrorHandler}, if it has one, with the given
 * exception wrapped in an {@link ErrorEvent}.
 */
private void callErrorHandler(VaadinSession session, Exception e) {
    try {
        ErrorHandler errorHandler = ErrorEvent.findErrorHandler(session);
        if (errorHandler != null) {
            errorHandler.error(new ErrorEvent(e));
        }
    } catch (Exception ex) {
        // Let's not allow error handling to cause trouble; log fails
        logger.warn("ErrorHandler call failed", ex);
    }
}
 
Example #27
Source File: UserUIContext.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Keep user session in server sessions
 *
 * @param userSession current user
 * @param billingAc   account information of current user
 */
public void setSessionVariables(SimpleUser userSession, SimpleBillingAccount billingAc) {
    session = userSession;
    billingAccount = billingAc;

    String language = session.getLanguage();
    userLocale = language != null ? LocalizationHelper.getLocaleInstance(language) : billingAccount.getLocaleInstance();
    VaadinSession.getCurrent().setLocale(userLocale);
    messageHelper = LocalizationHelper.getMessageConveyor(userLocale);

    userTimeZone = TimezoneVal.valueOf(session.getTimezone());
    MyCollabSession.putSessionVariable(MyCollabSession.USER_VAL, userSession);
}
 
Example #28
Source File: CitizenIntelligenceAgencyUI.java    From cia with Apache License 2.0 5 votes vote down vote up
@Override
protected void init(final VaadinRequest request) {
	VaadinSession.getCurrent().setErrorHandler(new UiInstanceErrorHandler(this));
	setSizeFull();
	springNavigator.addView(CRLF_REPLACEMENT, mainView);
	setNavigator(springNavigator);


	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.setTimeZone(webBrowser.getTimeZoneId());
		serviceRequest.setScreenSize(webBrowser.getScreenWidth() + "x" + webBrowser.getScreenHeight());
		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.replaceAll(CRLF,CRLF_REPLACEMENT),language.replaceAll(CRLF,CRLF_REPLACEMENT),ipInformation.replaceAll(CRLF,CRLF_REPLACEMENT),webBrowser.getBrowserApplication().replaceAll(CRLF,CRLF_REPLACEMENT),serviceRequest.getSessionId().replaceAll(CRLF,CRLF_REPLACEMENT),serviceResponse.getResult().toString().replaceAll(CRLF,CRLF_REPLACEMENT));
	}
}
 
Example #29
Source File: LoginWindow.java    From jpa-invoicer with The Unlicense 5 votes vote down vote up
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request,
        VaadinResponse response) throws IOException {
    if (request.getParameter("code") != null) {
        String code = request.getParameter("code");
        Verifier v = new Verifier(code);
        Token t = service.getAccessToken(null, v);

        OAuthRequest r = new OAuthRequest(Verb.GET,
                "https://www.googleapis.com/plus/v1/people/me");
        service.signRequest(t, r);
        Response resp = r.send();

        GooglePlusAnswer answer = new Gson().fromJson(resp.getBody(),
                GooglePlusAnswer.class);

        userSession.login(answer.emails[0].value, answer.displayName);

        close();
        VaadinSession.getCurrent().removeRequestHandler(this);

        ((VaadinServletResponse) response).getHttpServletResponse().
                sendRedirect(redirectUrl);
        return true;
    }

    return false;
}
 
Example #30
Source File: WebVaadinCompatibleSecurityContextHolder.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public SecurityContext get() {
    VaadinSession vaadinSession = VaadinSession.getCurrent();
    if (vaadinSession != null && vaadinSession.hasLock()) {
        return vaadinSession.getAttribute(SecurityContext.class);
    }

    return super.get();
}