Java Code Examples for javax.servlet.http.HttpServletRequest#getLocales()

The following examples show how to use javax.servlet.http.HttpServletRequest#getLocales() . 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: AcceptHeaderLocaleResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private Locale findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales) {
	Enumeration<Locale> requestLocales = request.getLocales();
	Locale languageMatch = null;
	while (requestLocales.hasMoreElements()) {
		Locale locale = requestLocales.nextElement();
		if (supportedLocales.contains(locale)) {
			if (languageMatch == null || languageMatch.getLanguage().equals(locale.getLanguage())) {
				// Full match: language + country, possibly narrowed from earlier language-only match
				return locale;
			}
		}
		else if (languageMatch == null) {
			// Let's try to find a language-only match as a fallback
			for (Locale candidate : supportedLocales) {
				if (!StringUtils.hasLength(candidate.getCountry()) &&
						candidate.getLanguage().equals(locale.getLanguage())) {
					languageMatch = candidate;
					break;
				}
			}
		}
	}
	return languageMatch;
}
 
Example 2
Source File: AcceptHeaderLocaleResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private Locale findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales) {
	Enumeration<Locale> requestLocales = request.getLocales();
	Locale languageMatch = null;
	while (requestLocales.hasMoreElements()) {
		Locale locale = requestLocales.nextElement();
		if (supportedLocales.contains(locale)) {
			if (languageMatch == null || languageMatch.getLanguage().equals(locale.getLanguage())) {
				// Full match: language + country, possibly narrowed from earlier language-only match
				return locale;
			}
		}
		else if (languageMatch == null) {
			// Let's try to find a language-only match as a fallback
			for (Locale candidate : supportedLocales) {
				if (!StringUtils.hasLength(candidate.getCountry()) &&
						candidate.getLanguage().equals(locale.getLanguage())) {
					languageMatch = candidate;
					break;
				}
			}
		}
	}
	return languageMatch;
}
 
Example 3
Source File: AwsProxyHttpServletRequestTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void getLocales_validAcceptHeaderMultipleLocales_expectFullLocaleList() {
    AwsProxyRequestBuilder req = getRequestWithHeaders();
    req.header(HttpHeaders.ACCEPT_LANGUAGE, "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5");
    HttpServletRequest servletRequest = getRequest(req, null, null);
    Enumeration<Locale> locales = servletRequest.getLocales();
    List<Locale> localesList = new ArrayList<>();
    while (locales.hasMoreElements()) {
        localesList.add(locales.nextElement());
    }
    assertEquals(5, localesList.size());
    assertEquals(new Locale("fr-CH"), localesList.get(0));
    assertEquals(new Locale("fr"), localesList.get(1));
    assertEquals(new Locale("en"), localesList.get(2));
    assertEquals(new Locale("de"), localesList.get(3));
    assertEquals(new Locale("*"), localesList.get(4));

    assertNotNull(servletRequest.getLocale());
    assertEquals(new Locale("fr-CH"), servletRequest.getLocale());
}
 
Example 4
Source File: AwsProxyHttpServletRequestTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void getLocales_validAcceptHeaderMultipleLocales_expectFullLocaleListOrdered() {
    AwsProxyRequestBuilder req = getRequestWithHeaders();
    req.header(HttpHeaders.ACCEPT_LANGUAGE, "fr-CH, en;q=0.8, de;q=0.7, *;q=0.5, fr;q=0.9");
    HttpServletRequest servletRequest = getRequest(req, null, null);
    Enumeration<Locale> locales = servletRequest.getLocales();
    List<Locale> localesList = new ArrayList<>();
    while (locales.hasMoreElements()) {
        localesList.add(locales.nextElement());
    }
    assertEquals(5, localesList.size());
    assertEquals(new Locale("fr-CH"), localesList.get(0));
    assertEquals(new Locale("fr"), localesList.get(1));
    assertEquals(new Locale("en"), localesList.get(2));
    assertEquals(new Locale("de"), localesList.get(3));
    assertEquals(new Locale("*"), localesList.get(4));
}
 
Example 5
Source File: ConfigManager.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Gets the list of all Locales specified in the request, whether set as Locales or
 * as a request parameter or session attribute
 * @param request the request to parse
 * @return a List of Locales found
 */
private static List<Locale> getLocalesFromRequest(HttpServletRequest request) {
    List<Locale> locales = new ArrayList<Locale>();

    String language = request.getParameter(LANGUAGE_ATTRIBUTE_NAME);
    _log.debug("language from request = " + language);

    if (StringUtil.isNullOrEmpty(language)) {
        language = (String) request.getSession().getAttribute(LANGUAGE_ATTRIBUTE_NAME);
        _log.debug("language from session = " + language);
    }

    if (!StringUtil.isNullOrEmpty(language)) {
        locales.add(new Locale(language, language));
    }

    Enumeration localesEnum = request.getLocales();
    while (localesEnum.hasMoreElements()) {
        locales.add((Locale) localesEnum.nextElement());
    }
    return locales;
}
 
Example 6
Source File: HostManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated Use {@link StringManager#getManager(String, Enumeration)}.
 *             This method will be removed in Tomcat 8.
 */
@Deprecated
protected StringManager getStringManager(HttpServletRequest req) {
    Enumeration<Locale> requestedLocales = req.getLocales();
    while (requestedLocales.hasMoreElements()) {
        Locale locale = requestedLocales.nextElement();
        StringManager result = StringManager.getManager(Constants.Package,
                locale);
        if (result.getLocale().equals(locale)) {
            return result;
        }
    }
    // Return the default
    return sm;
}
 
Example 7
Source File: ManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated Use {@link StringManager#getManager(String, Enumeration)}.
 *             This method will be removed in Tomcat 8.
 */
@Deprecated
protected StringManager getStringManager(HttpServletRequest req) {
    Enumeration<Locale> requestedLocales = req.getLocales();
    while (requestedLocales.hasMoreElements()) {
        Locale locale = requestedLocales.nextElement();
        StringManager result = StringManager.getManager(Constants.Package,
                locale);
        if (result.getLocale().equals(locale)) {
            return result;
        }
    }
    // Return the default
    return sm;
}
 
Example 8
Source File: AcceptHeaderLocaleResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Locale findSupportedLocale(HttpServletRequest request) {
	Enumeration<Locale> requestLocales = request.getLocales();
	while (requestLocales.hasMoreElements()) {
		Locale locale = requestLocales.nextElement();
		if (getSupportedLocales().contains(locale)) {
			return locale;
		}
	}
	return null;
}
 
Example 9
Source File: AwsProxyHttpServletRequestTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void getLocales_emptyAcceptHeader_expectDefaultLocale() {
    AwsProxyRequestBuilder req = getRequestWithHeaders();
    HttpServletRequest servletRequest = getRequest(req, null, null);
    Enumeration<Locale> locales = servletRequest.getLocales();
    int localesNo = 0;
    while (locales.hasMoreElements()) {
        Locale defaultLocale = locales.nextElement();
        assertEquals(Locale.getDefault(), defaultLocale);
        localesNo++;
    }
    assertEquals(1, localesNo);
}
 
Example 10
Source File: AwsProxyHttpServletRequestTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void getLocales_validAcceptHeader_expectSingleLocale() {
    AwsProxyRequestBuilder req = getRequestWithHeaders();
    req.header(HttpHeaders.ACCEPT_LANGUAGE, "fr-CH");
    HttpServletRequest servletRequest = getRequest(req, null, null);
    Enumeration<Locale> locales = servletRequest.getLocales();
    int localesNo = 0;
    while (locales.hasMoreElements()) {
        Locale defaultLocale = locales.nextElement();
        assertEquals(new Locale("fr-CH"), defaultLocale);
        localesNo++;
    }
    assertEquals(1, localesNo);
}
 
Example 11
Source File: HostManagerServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated Use {@link StringManager#getManager(String, Enumeration)}.
 *             This method will be removed in Tomcat 8.
 */
@Deprecated
protected StringManager getStringManager(HttpServletRequest req) {
    Enumeration<Locale> requestedLocales = req.getLocales();
    while (requestedLocales.hasMoreElements()) {
        Locale locale = requestedLocales.nextElement();
        StringManager result = StringManager.getManager(Constants.Package,
                locale);
        if (result.getLocale().equals(locale)) {
            return result;
        }
    }
    // Return the default
    return sm;
}
 
Example 12
Source File: ManagerServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated Use {@link StringManager#getManager(String, Enumeration)}.
 *             This method will be removed in Tomcat 8.
 */
@Deprecated
protected StringManager getStringManager(HttpServletRequest req) {
    Enumeration<Locale> requestedLocales = req.getLocales();
    while (requestedLocales.hasMoreElements()) {
        Locale locale = requestedLocales.nextElement();
        StringManager result = StringManager.getManager(Constants.Package,
                locale);
        if (result.getLocale().equals(locale)) {
            return result;
        }
    }
    // Return the default
    return sm;
}
 
Example 13
Source File: ServletRequestCopy.java    From onedev with MIT License 4 votes vote down vote up
public ServletRequestCopy(HttpServletRequest request) {
	this.servletPath = request.getServletPath();
	this.contextPath = request.getContextPath();
	this.pathInfo = request.getPathInfo();
	this.requestUri = request.getRequestURI();
	this.requestURL = request.getRequestURL();
	this.method = request.getMethod();
	this.serverName = request.getServerName();
	this.serverPort = request.getServerPort();
	this.protocol = request.getProtocol();
	this.scheme = request.getScheme();
	
	
	/*
	 * have to comment out below two lines as otherwise web socket will
	 * report UnSupportedOperationException upon connection
	 */
	//this.characterEncoding = request.getCharacterEncoding();
	//this.contentType = request.getContentType();
	//this.requestedSessionId = request.getRequestedSessionId();
	this.characterEncoding = null;
	this.contentType = null;
	this.requestedSessionId = null;
	
	this.locale = request.getLocale();
	this.locales = request.getLocales();
	this.isSecure = request.isSecure();
	this.remoteUser = request.getRemoteUser();
	this.remoteAddr = request.getRemoteAddr();
	this.remoteHost = request.getRemoteHost();
	this.remotePort = request.getRemotePort();
	this.localAddr = request.getLocalAddr();
	this.localName = request.getLocalName();
	this.localPort = request.getLocalPort();
	this.pathTranslated = request.getPathTranslated();
	this.principal = request.getUserPrincipal();

	HttpSession session = request.getSession(true);
	httpSession = new HttpSessionCopy(session);

	String s;
	Enumeration<String> e = request.getHeaderNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		Enumeration<String> headerValues = request.getHeaders(s);
		this.headers.put(s, headerValues);
	}

	e = request.getAttributeNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		attributes.put(s, request.getAttribute(s));
	}

	e = request.getParameterNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		parameters.put(s, request.getParameterValues(s));
	}
}
 
Example 14
Source File: LoginToContinueMechanism.java    From tomee with Apache License 2.0 4 votes vote down vote up
static void saveRequest(final HttpServletRequest request) throws IOException {
    SavedRequest saved = new SavedRequest();
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            saved.addCookie(cookies[i]);
        }
    }
    Enumeration<String> names = request.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        Enumeration<String> values = request.getHeaders(name);
        while (values.hasMoreElements()) {
            String value = values.nextElement();
            saved.addHeader(name, value);
        }
    }
    Enumeration<Locale> locales = request.getLocales();
    while (locales.hasMoreElements()) {
        Locale locale = locales.nextElement();
        saved.addLocale(locale);
    }

    int maxSavePostSize = MAX_SAVE_POST_SIZE;
    if (maxSavePostSize != 0) {
        ByteChunk body = new ByteChunk();
        body.setLimit(maxSavePostSize);

        byte[] buffer = new byte[4096];
        int bytesRead;
        InputStream is = request.getInputStream();

        while ( (bytesRead = is.read(buffer) ) >= 0) {
            body.append(buffer, 0, bytesRead);
        }

        // Only save the request body if there is something to save
        if (body.getLength() > 0) {
            saved.setContentType(request.getContentType());
            saved.setBody(body);
        }
    }

    saved.setMethod(request.getMethod());
    saved.setQueryString(request.getQueryString());
    saved.setRequestURI(request.getRequestURI());
    saved.setRequestURL(request.getRequestURL().toString());

    // Stash the SavedRequest in our session for later use
    request.getSession().setAttribute(ORIGINAL_REQUEST, saved);
}