Java Code Examples for javax.servlet.ServletRequest#getLocale()

The following examples show how to use javax.servlet.ServletRequest#getLocale() . 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: I18nFilter.java    From tech-gallery with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
    throws IOException, ServletException {
  I18n i18n = I18n.getInstance();

  // Checks if appengine is the one making the request. In this case, do nothing.
  String appEngineHeader = ((HttpServletRequest) request).getHeader("X-Appengine-Peer");
  if (appEngineHeader == null) {
    if (request.getParameter("lang") == null) {
      Locale acceptLanguage = request.getLocale();
      i18n.setLanguage(acceptLanguage.getLanguage());
      i18n.setCountry(acceptLanguage.getCountry());
    } else {
      String[] lang = request.getParameter("lang").split("-");
      String language = lang[0];
      String country = lang[1];
      i18n.setCountry(country);
      i18n.setLanguage(language);
    }
  }

  filterChain.doFilter(request, response);
}
 
Example 2
Source File: PortletModeDropDownTag.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
/**
   * Obtains decoration name for a portlet managed mode from the portlet's resource bundle
   * as defined in PLT.8.4 of the JSR-286 spec using the key 
   * javax.portlet.app.custom-portlet-mode.<custom mode>.decoration-name where
   * custom mode is the name of the custom mode as defined in portlet.xml
   * (//portlet-app/custom-portlet-mode/portlet-mode element). If the decoration
   * name is not found in the resource bundle, this method returns the uppercased
   * mode name.
   * 
   * @param driverConfig the driver config object found in the session.
   * @param mode the portlet managed custom mode that will be searched for decoration name
   * in the resource bundle.
   * @return the decoration name for a portlet managed mode in the resource bundle
   * using the key javax.portlet.app.custom-portlet-mode.<custom mode>.decoration-name 
   * where custom mode is the name of the custom mode as defined in portlet.xml
   * (//portlet-app/custom-portlet-mode/portlet-mode element). If the decoration
   * name is not found in the resource bundle, the uppercased
   * mode name is returned.
   */
  private String getCustomModeDecorationName(DriverConfiguration driverConfig, 
  		PortletMode mode) {
  	//decoration name is mode name by default
String decorationName = mode.toString().toUpperCase();
ResourceBundle bundle;
StringBuffer res;
try {
	PortletConfig config = driverConfig.getPortletConfig(evaluatedPortletId);
	ServletRequest request = pageContext.getRequest();
	Locale defaultLocale = request.getLocale();
	bundle = config.getResourceBundle(defaultLocale);
	res = new StringBuffer();
	res.append("javax.portlet.app.custom-portlet-mode.");
	res.append(mode.toString());
	res.append(".decoration-name");
	decorationName = bundle.getString(res.toString());
} catch (Exception e) {
	LOG.debug("Problem finding decoration-name for custom mode: " + mode.toString());
}
return decorationName;
  }
 
Example 3
Source File: LocaleFilter.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
        ServletException {
    Locale locale = request.getLocale();
    AbstractMessages.setLocale(locale);
    try {
        chain.doFilter(request, response);
    } finally {
        AbstractMessages.clearLocale();
    }
}
 
Example 4
Source File: PortletTitleTag.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
/**
 * Method invoked when the start tag is encountered. This method retrieves the portlet title and print it to the
 * page.
 * 
 * @see org.apache.pluto.container.services.PortalCallbackService#setTitle(HttpServletRequest, PortletWindow, String)
 * @see org.apache.pluto.driver.services.container.PortalCallbackServiceImpl#setTitle(HttpServletRequest,
 *      PortletWindow, String)
 * 
 * @throws JspException
 */
@SuppressWarnings("unchecked")
public int doStartTag() throws JspException {

   // Ensure that the portlet title tag resides within a portlet tag.
   PortletTag parentTag = (PortletTag) TagSupport.findAncestorWithClass(this, PortletTag.class);
   if (parentTag == null) {
      throw new JspException("Portlet title tag may only reside " + "within a pluto:portlet tag.");
   }

   // Print out the portlet title to page.
   try {

      Map<String, String> titles = (Map<String, String>) pageContext.getRequest().getAttribute(AttributeKeys.PORTLET_TITLE);

      String portletId = parentTag.getEvaluatedPortletId();

      String title = null;
      if (titles != null) {
         title = titles.get(portletId);
      }

      if (title == null) {

         PortletWindowConfig windowConfig = PortletWindowConfig.fromId(portletId);

         try {
            ServletContext servletContext = pageContext.getServletContext();
            DriverConfiguration driverConfig = (DriverConfiguration) servletContext.getAttribute(AttributeKeys.DRIVER_CONFIG);
            PortletConfig config = driverConfig.getPortletConfig(portletId);
            ServletRequest request = pageContext.getRequest();
            Locale defaultLocale = request.getLocale();
            ResourceBundle bundle = config.getResourceBundle(defaultLocale);
            title = bundle.getString("javax.portlet.title");
         } catch (Throwable th) {
            if (LOG.isDebugEnabled()) {
               StringBuilder txt = new StringBuilder(128);
               txt.append("Could not obtain title for: " + windowConfig.getPortletName() + "\n");
               StringWriter sw = new StringWriter();
               PrintWriter pw = new PrintWriter(sw);
               th.printStackTrace(pw);
               pw.flush();
               txt.append(sw.toString());
               LOG.warn(txt.toString());
            }
         }

         if (title == null) {
            title = "[ " + windowConfig.getPortletName() + " ]";
         }
      }

      pageContext.getOut().print(title);
   } catch (IOException ex) {
      throw new JspException(ex);
   }
   return SKIP_BODY;
}