Example usage for javax.servlet.http HttpServletRequest getLocale

List of usage examples for javax.servlet.http HttpServletRequest getLocale

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getLocale.

Prototype

public Locale getLocale();

Source Link

Document

Returns the preferred Locale that the client will accept content in, based on the Accept-Language header.

Usage

From source file:org.wte4j.ui.server.filters.SessionContextFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {

    if (request instanceof HttpServletRequest) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        serviceContext.setLocale(httpRequest.getLocale());

        if (httpRequest.getUserPrincipal() != null) {
            String userName = httpRequest.getUserPrincipal().getName();
            serviceContext.setUser(new User(userName, userName));
        } else {/*w  ww. ja  va 2s.c om*/
            serviceContext.setUser(new User(ANONYMOUS_USER, ANONYMOUS_USER));
        }

    }

    filterChain.doFilter(request, response);
}

From source file:cz.jirutka.spring.exhandler.handlers.ErrorMessageRestExceptionHandler.java

protected String resolveMessage(String key, E exception, HttpServletRequest request) {

    String template = getMessage(key, request.getLocale());

    Map<String, Object> vars = new HashMap<>(2);
    vars.put("ex", exception);
    vars.put("req", request);

    return interpolateMessage(template, vars);
}

From source file:io.neba.core.spring.web.filter.NebaRequestContextFilter.java

private void initContextHolders(HttpServletRequest request, ServletRequestAttributes requestAttributes) {
    LocaleContextHolder.setLocale(request.getLocale(), this.threadContextInheritable);
    RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
    if (logger.isDebugEnabled()) {
        logger.debug("Bound request context to thread: " + request);
    }//from w  w  w  .  j  a va2  s  .  c  o  m
}

From source file:WelcomeServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    //Get the client's Locale
    Locale locale = request.getLocale();

    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle", locale);

    String welcome = bundle.getString("Welcome");

    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html><head><title>" + welcome + "</title></head><body>");

    out.println("<h2>" + welcome + "</h2>");

    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());

    out.println("</body></html>");
    out.close();//  ww w .j a v  a2s  .  c o m

}

From source file:eu.supersede.fe.locale.UserPreferredOrHeaderLocaleResolver.java

/**
 * Return the locale to be used for the given request.
 *///  ww w .  ja va2  s.c  o  m
@Override
public Locale resolveLocale(HttpServletRequest request) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Locale loc = request.getLocale();

    if ((authentication == null) || (authentication instanceof AnonymousAuthenticationToken)) {
        return loc;
    }

    if (SecurityContextHolder.getContext().getAuthentication() != null
            && SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null) {
        Object user = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (user instanceof DatabaseUser) {
            DatabaseUser dbUser = (DatabaseUser) user;
            String l = dbUser.getLocale();

            if (l != null && !l.equals("")) {
                loc = new Locale.Builder().setLanguage(l).build();
            }
        }
    }

    return loc;
}

From source file:CurrLocaleServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    //Get the client's Locale
    Locale locale = request.getLocale();
    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle", locale);
    String welcome = bundle.getString("Welcome");

    NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
    String formatted = nft.format(1000000);

    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>" + welcome + "</title></head><body>");

    out.println("<h2>" + bundle.getString("Hello") + " " + bundle.getString("and") + " " + welcome + "</h2>");

    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());

    out.println("<br /><br />");
    out.println(formatted);//from  w w  w. j  a v  a  2 s  .  co  m

    out.println("</body></html>");
    out.close();

}

From source file:httl.web.springmvc.HttlView.java

@Override
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    WebEngine.setRequestAndResponse(request, response);
    WebEngine.getEngine().getTemplate(getUrl(), request.getLocale(), model).render(model, response);
}

From source file:de.iew.demos.controllers.DemoListController.java

public DomainModelVisitor getNodeTransformer(HttpServletRequest request) {
    LocaleStringResolver localeStringResolver = new LocaleStringResolver();
    localeStringResolver.setLocale(request.getLocale());

    NodeToNodelModelTransformer nodeToNodelVisitorVisitor = new NodeToNodelModelTransformer();
    nodeToNodelVisitorVisitor.setStringResolver(localeStringResolver);
    return nodeToNodelVisitorVisitor;
}

From source file:org.zilverline.web.IndexDefaultsController.java

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    super.initBinder(request, binder);
    NumberFormat nf = NumberFormat.getInstance(request.getLocale());
    binder.registerCustomEditor(java.lang.Integer.class,
            new CustomNumberEditor(java.lang.Integer.class, nf, true));
}

From source file:org.saiku.reporting.backend.rest.SessionResource.java

@GET
@Consumes("application/x-www-form-urlencoded")
@Produces(MediaType.APPLICATION_JSON)//from   ww w .  ja v  a 2s . c  o  m
public Map<String, Object> getSession(@Context HttpServletRequest req) {

    Map<String, Object> sess = sessionService.getSession();
    try {
        String acceptLanguage = req.getLocale().getLanguage();
        if (StringUtils.isNotBlank(acceptLanguage)) {
            sess.put("language", acceptLanguage);
        }
    } catch (Exception e) {
        log.debug("Cannot get language!", e);
    }
    return sess;
}