Example usage for org.springframework.web.servlet.i18n CookieLocaleResolver CookieLocaleResolver

List of usage examples for org.springframework.web.servlet.i18n CookieLocaleResolver CookieLocaleResolver

Introduction

In this page you can find the example usage for org.springframework.web.servlet.i18n CookieLocaleResolver CookieLocaleResolver.

Prototype

public CookieLocaleResolver() 

Source Link

Document

Create a new instance of the CookieLocaleResolver class using the #DEFAULT_COOKIE_NAME default cookie name .

Usage

From source file:cz.muni.fi.editor.webapp.config.MvcConfiguration.java

@Bean(name = "localeResolver")
public CookieLocaleResolver cookieLocaleResolver() {
    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);

    return cookieLocaleResolver;
}

From source file:de.interseroh.report.webconfig.WebMvcConfig.java

@Bean
public LocaleResolver localeResolver() {
    Locale.setDefault(Locale.ENGLISH);
    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setCookieName("reportengine-language");
    return cookieLocaleResolver;
}

From source file:com.pavikumbhar.javaheart.springconfiguration.WebAppConfig.java

@Bean
public LocaleResolver localeResolver() {
    final CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
    return cookieLocaleResolver;
}

From source file:com.MockGatewayApplication.java

@Bean
public LocaleResolver localeResolver() {
    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(StringUtils.parseLocaleString("en"));
    cookieLocaleResolver.setCookieName("gatewayLanguage");
    cookieLocaleResolver.setCookieMaxAge(604800);//one month
    return cookieLocaleResolver;
}

From source file:com.kabiliravi.kaman.web.WebMvcContextConfiguration.java

@Bean
public LocaleResolver localeResolver() {
    return new CookieLocaleResolver();
}

From source file:com.devnexus.ting.web.config.WebConfig.java

@Bean
public CookieLocaleResolver localeResolver() {
    return new CookieLocaleResolver();
}

From source file:org.openmrs.module.referenceapplication.page.controller.LoginPageController.java

/**
 * Processes requests to authenticate a user
 *
 * @param username/*w w w .  ja  v a2s.  c  o m*/
 * @param password
 * @param sessionLocationId
 * @param locationService
 * @param ui {@link UiUtils} object
 * @param pageRequest {@link PageRequest} object
 * @param sessionContext
 * @return
 * @should redirect the user back to the redirectUrl if any
 * @should redirect the user to the home page if the redirectUrl is the login page
 * @should send the user back to the login page if an invalid location is selected
 * @should send the user back to the login page when authentication fails
 */
public String post(@RequestParam(value = "username", required = false) String username,
        @RequestParam(value = "password", required = false) String password,
        @RequestParam(value = "sessionLocation", required = false) Integer sessionLocationId,
        @SpringBean("locationService") LocationService locationService, UiUtils ui, PageRequest pageRequest,
        UiSessionContext sessionContext) {

    String redirectUrl = pageRequest.getRequest().getParameter(REQUEST_PARAMETER_NAME_REDIRECT_URL);
    redirectUrl = getRelativeUrl(redirectUrl, pageRequest);
    Location sessionLocation = null;
    if (sessionLocationId != null) {
        try {
            // TODO as above, grant this privilege to Anonymous instead of using a proxy privilege
            Context.addProxyPrivilege(VIEW_LOCATIONS);
            Context.addProxyPrivilege(GET_LOCATIONS);
            sessionLocation = locationService.getLocation(sessionLocationId);
        } finally {
            Context.removeProxyPrivilege(VIEW_LOCATIONS);
            Context.removeProxyPrivilege(GET_LOCATIONS);
        }
    }

    //TODO uncomment this to replace the if clause after it
    if (sessionLocation != null && sessionLocation.hasTag(EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN)) {
        // Set a cookie, so next time someone logs in on this machine, we can default to that same location
        pageRequest.setCookieValue(COOKIE_NAME_LAST_SESSION_LOCATION, sessionLocationId.toString());

        try {
            Context.authenticate(username, password);

            if (Context.isAuthenticated()) {
                if (log.isDebugEnabled())
                    log.debug("User has successfully authenticated");

                sessionContext.setSessionLocation(sessionLocation);

                // set the locale based on the user's default locale
                Locale userLocale = GeneralUtils
                        .getDefaultLocale(Context.getUserContext().getAuthenticatedUser());
                if (userLocale != null) {
                    Context.getUserContext().setLocale(userLocale);
                    pageRequest.getResponse().setLocale(userLocale);
                    new CookieLocaleResolver().setDefaultLocale(userLocale);
                }

                if (StringUtils.isNotBlank(redirectUrl)) {
                    //don't redirect back to the login page on success nor an external url
                    if (!redirectUrl.contains("login.")) {
                        if (log.isDebugEnabled())
                            log.debug("Redirecting user to " + redirectUrl);

                        return "redirect:" + redirectUrl;
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("Redirect contains 'login.', redirecting to home page");
                    }
                }

                return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
            }
        } catch (ContextAuthenticationException ex) {
            if (log.isDebugEnabled())
                log.debug("Failed to authenticate user");

            pageRequest.getSession().setAttribute(
                    ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
                    ui.message(ReferenceApplicationConstants.MODULE_ID + ".error.login.fail"));
        }

    } else if (sessionLocation == null) {
        pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
                ui.message("referenceapplication.login.error.locationRequired"));
    } else {
        // the UI shouldn't allow this, but protect against it just in case
        pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
                ui.message("referenceapplication.login.error.invalidLocation", sessionLocation.getName()));
    }

    if (log.isDebugEnabled())
        log.debug("Sending user back to login page");

    //TODO limit login attempts by IP Address

    pageRequest.getSession().setAttribute(SESSION_ATTRIBUTE_REDIRECT_URL, redirectUrl);

    return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "login");
}