Example usage for org.springframework.web.servlet.support RequestContextUtils getLocale

List of usage examples for org.springframework.web.servlet.support RequestContextUtils getLocale

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support RequestContextUtils getLocale.

Prototype

public static Locale getLocale(HttpServletRequest request) 

Source Link

Document

Retrieve the current locale from the given request, using the LocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the request's accept-header Locale.

Usage

From source file:org.shept.org.springframework.web.bind.support.FilterBindingInitializer.java

public void initBinder(WebRequest request, ComponentDataBinder binder, String componentPath) {
    if (logger.isInfoEnabled()) {
        logger.info("Register custom binding initializers");
    }/* www . j ava 2  s  .  co  m*/

    // Locale dependent registry - we should improve that - use java.text.spi ?
    Locale locale = RequestContextUtils.getLocale(((ServletWebRequest) request).getRequest());

    String format = formatResolver.resolveProperty(locale, DateTimeLocaleConstants.DATETIME_FORMAT_LONG);

    // register 'nullable' bean editors for all Strings within filter  objects
    // This way empty fields will not be part of the search criteria

    Map<String, CommandWrapper> pathMap = Collections.emptyMap();
    if (binder.getTarget() instanceof SubCommandProvider) {
        pathMap = ComponentUtils.getComponentPathMap((SubCommandProvider) binder.getTarget());
        CommandWrapper wrapper = pathMap.get(componentPath);
        if (wrapper.getCommand() instanceof FilteredListHolder) {
            FilteredListHolder flh = (FilteredListHolder) wrapper.getCommand();
            FilterDefinition filterDef = flh.getFilter();
            String path = ComponentUtils.getPropertyPathPrefix(componentPath)
                    + FilteredListHolder.FILTER_BINDING_NAME + PropertyAccessor.NESTED_PROPERTY_SEPARATOR;

            for (Field field : filterDef.getClass().getDeclaredFields()) {
                String propPath = path + field.getName();
                if (field.getType().equals(String.class)) {
                    binder.registerCustomEditor(String.class, propPath, new StringTrimmerEditor(true));
                    if (logger.isInfoEnabled()) {
                        logger.info("Registered nullable StringEditor for " + propPath);
                    }
                } else if (field.getType().equals(Calendar.class)) {
                    format = formatResolver.resolveProperty(locale, DateTimeLocaleConstants.DATE_FORMAT_SHORT);
                    binder.registerCustomEditor(Calendar.class, propPath,
                            new CustomCalendarEditor(new SimpleDateFormat(format), true));
                    if (logger.isInfoEnabled()) {
                        logger.info("Registered Calendar Editor for " + propPath);
                    }
                } else {
                    registerDependendEntities(binder, field.getType(), path + field.getName());
                }
            }
        }
    }
}

From source file:org.shept.org.springframework.web.servlet.mvc.delegation.ComponentUtils.java

/**
 * //from w w w  .  j  ava 2 s. c  o m
 * @param
 * @return
 *
 * @param wrapper
 * @param ctx
 * @param model
 * @param selector
 * @return
 */
public static String getComponentInfo(HttpServletRequest request, InfoItem item, Object model) {
    if (item == null)
        return null;
    if (item.getCode() == null)
        return null;
    WebApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
    Locale locale = RequestContextUtils.getLocale(request);
    String arg = null;
    Method mth;
    if (model != null) {
        if (StringUtils.hasText(item.getSelector())) {
            mth = ReflectionUtils.findMethod(model.getClass(),
                    StringUtilsExtended.getReadAccessor(item.getSelector()));
            if (mth != null) {
                arg = (String) ReflectionUtils.invokeMethod(mth, model);
            }
        }
    }
    if (StringUtils.hasText(arg)) {
        return ctx.getMessage(item.getCode(), new String[] { arg }, "???", locale);
    } else {
        return ctx.getMessage(item.getCode(), null, "???", locale);
    }
}

From source file:org.springframework.faces.mvc.annotation.support.AnnotatedMethodInvoker.java

/**
 * Resolve a standard argument type. This method can be overridden by subclasses to extend supported argument types.
 * @param parameterType The paramter type to resolve
 * @param webRequest The web request//  www.jav  a2s  .  c o m
 * @return The resolved argument (can be <tt>null</tt>) or {@link WebArgumentResolver#UNRESOLVED} if the argument
 * @throws Exception on error
 */
protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest) throws Exception {
    if ((webRequest.getNativeRequest() instanceof HttpServletRequest)
            && (webRequest.getNativeResponse() instanceof HttpServletResponse)) {

        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
        HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse();

        if (ServletRequest.class.isAssignableFrom(parameterType)) {
            return request;
        } else if (ServletResponse.class.isAssignableFrom(parameterType)) {
            return response;
        } else if (HttpSession.class.isAssignableFrom(parameterType)) {
            return request.getSession();
        } else if (Principal.class.isAssignableFrom(parameterType)) {
            return request.getUserPrincipal();
        } else if (Locale.class.equals(parameterType)) {
            return RequestContextUtils.getLocale(request);
        } else if (InputStream.class.isAssignableFrom(parameterType)) {
            return request.getInputStream();
        } else if (Reader.class.isAssignableFrom(parameterType)) {
            return request.getReader();
        } else if (OutputStream.class.isAssignableFrom(parameterType)) {
            return response.getOutputStream();
        } else if (Writer.class.isAssignableFrom(parameterType)) {
            return response.getWriter();
        } else if (WebRequest.class.isAssignableFrom(parameterType)) {
            return webRequest;
        }
    }
    return WebArgumentResolver.UNRESOLVED;
}

From source file:ro.cs.om.web.controller.root.ControllerUtils.java

/**
 * Returns a string (JSON pattern) of all departments, inclusive with fake dept, from an organization 
 * containing the name's of the departments
 *  /*from   ww w.j  a v a  2s . co  m*/
 * @author Adelina
 * 
 * @param organisationId
 * @param locale
 * @param errorMessages
 * @param messageSource
 * @return
 */
public String getDepartmentsWithFakeFromOrgAsJSON(HttpServletRequest request, Integer organisationId,
        Locale locale, ArrayList<String> errorMessages, MessageSource messageSource) {
    logger.debug("getDepartmentFromOrgAsJSON - START - ORGANISATION ".concat(String.valueOf(organisationId)));

    JSONArray jsonArray = new JSONArray();

    try {
        Set<Department> departments = BLOrganisation.getInstance().getDepartments(organisationId);
        JSONObject jsonObj = new JSONObject();
        for (Department department : departments) {
            if (department.getStatus() != IConstant.NOM_DEPARTMENT_FAKE) {
                jsonObj.accumulate("name", department.getName().concat(" "));
                jsonObj.accumulate("id", department.getDepartmentId());
                jsonArray.add(jsonObj);
                jsonObj.clear();
            } else {
                jsonObj.accumulate("name",
                        messageSource.getMessage(DEPARTMENT_FAKE,
                                new Object[] { ControllerUtils.getInstance().getFormattedCurrentTime() },
                                RequestContextUtils.getLocale(request)).concat(" "));
                jsonObj.accumulate("id", department.getDepartmentId());
                jsonArray.add(jsonObj);
                jsonObj.clear();
            }
        }
    } catch (BusinessException be) {
        logger.error("", be);
        errorMessages.add(messageSource.getMessage(GET_DEPARTMENTS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { be.getCode(), ControllerUtils.getInstance().getFormattedCurrentTime() },
                locale));
    } catch (Exception e) {
        logger.error("", e);
        errorMessages.add(messageSource.getMessage(GET_DEPARTMENTS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { null, ControllerUtils.getInstance().getFormattedCurrentTime() }, locale));
    }

    logger.debug("getDepartmentFromOrgAsJSON - END - json:".concat(jsonArray.toString()));
    return jsonArray.toString();
}