Search in sources :

Example 6 with Locale

use of java.util.Locale in project tomcat by apache.

the class FormAuthenticator method restoreRequest.

/**
     * Restore the original request from information stored in our session.
     * If the original request is no longer present (because the session
     * timed out), return <code>false</code>; otherwise, return
     * <code>true</code>.
     *
     * @param request The request to be restored
     * @param session The session containing the saved information
     * @return <code>true</code> if the request was successfully restored
     * @throws IOException if an IO error occurred during the process
     */
protected boolean restoreRequest(Request request, Session session) throws IOException {
    // Retrieve and remove the SavedRequest object from our session
    SavedRequest saved = (SavedRequest) session.getNote(Constants.FORM_REQUEST_NOTE);
    session.removeNote(Constants.FORM_REQUEST_NOTE);
    session.removeNote(Constants.FORM_PRINCIPAL_NOTE);
    if (saved == null) {
        return false;
    }
    // Swallow any request body since we will be replacing it
    // Need to do this before headers are restored as AJP connector uses
    // content length header to determine how much data needs to be read for
    // request body
    byte[] buffer = new byte[4096];
    InputStream is = request.createInputStream();
    while (is.read(buffer) >= 0) {
    // Ignore request body
    }
    // Modify our current request to reflect the original one
    request.clearCookies();
    Iterator<Cookie> cookies = saved.getCookies();
    while (cookies.hasNext()) {
        request.addCookie(cookies.next());
    }
    String method = saved.getMethod();
    MimeHeaders rmh = request.getCoyoteRequest().getMimeHeaders();
    rmh.recycle();
    boolean cacheable = "GET".equalsIgnoreCase(method) || "HEAD".equalsIgnoreCase(method);
    Iterator<String> names = saved.getHeaderNames();
    while (names.hasNext()) {
        String name = names.next();
        // BZ 43687
        if (!("If-Modified-Since".equalsIgnoreCase(name) || (cacheable && "If-None-Match".equalsIgnoreCase(name)))) {
            Iterator<String> values = saved.getHeaderValues(name);
            while (values.hasNext()) {
                rmh.addValue(name).setString(values.next());
            }
        }
    }
    request.clearLocales();
    Iterator<Locale> locales = saved.getLocales();
    while (locales.hasNext()) {
        request.addLocale(locales.next());
    }
    request.getCoyoteRequest().getParameters().recycle();
    request.getCoyoteRequest().getParameters().setQueryStringEncoding(request.getConnector().getURIEncoding());
    ByteChunk body = saved.getBody();
    if (body != null) {
        request.getCoyoteRequest().action(ActionCode.REQ_SET_BODY_REPLAY, body);
        // Set content type
        MessageBytes contentType = MessageBytes.newInstance();
        // If no content type specified, use default for POST
        String savedContentType = saved.getContentType();
        if (savedContentType == null && "POST".equalsIgnoreCase(method)) {
            savedContentType = "application/x-www-form-urlencoded";
        }
        contentType.setString(savedContentType);
        request.getCoyoteRequest().setContentType(contentType);
    }
    request.getCoyoteRequest().method().setString(method);
    return true;
}
Also used : Cookie(javax.servlet.http.Cookie) Locale(java.util.Locale) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) InputStream(java.io.InputStream) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) MimeHeaders(org.apache.tomcat.util.http.MimeHeaders)

Example 7 with Locale

use of java.util.Locale in project tomcat by apache.

the class Util method message.

static String message(ELContext context, String name, Object... props) {
    Locale locale = null;
    if (context != null) {
        locale = context.getLocale();
    }
    if (locale == null) {
        locale = Locale.getDefault();
        if (locale == null) {
            return "";
        }
    }
    ResourceBundle bundle = ResourceBundle.getBundle("javax.el.LocalStrings", locale);
    try {
        String template = bundle.getString(name);
        if (props != null) {
            template = MessageFormat.format(template, props);
        }
        return template;
    } catch (MissingResourceException e) {
        return "Missing Resource: '" + name + "' for Locale " + locale.getDisplayName();
    }
}
Also used : Locale(java.util.Locale) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Example 8 with Locale

use of java.util.Locale in project tomcat by apache.

the class StringManager method getManager.

/**
     * Retrieve the StringManager for a list of Locales. The first StringManager
     * found will be returned.
     *
     * @param packageName      The package for which the StringManager was
     *                         requested
     * @param requestedLocales The list of Locales
     *
     * @return the found StringManager or the default StringManager
     */
public static StringManager getManager(String packageName, Enumeration<Locale> requestedLocales) {
    while (requestedLocales.hasMoreElements()) {
        Locale locale = requestedLocales.nextElement();
        StringManager result = getManager(packageName, locale);
        if (result.getLocale().equals(locale)) {
            return result;
        }
    }
    // Return the default
    return getManager(packageName);
}
Also used : Locale(java.util.Locale)

Example 9 with Locale

use of java.util.Locale in project tomcat by apache.

the class TestRequest method getLocaleMultipleHeaders02.

/*
     * Reverse header order of getLocaleMultipleHeaders01() and make sure the
     * result is the same.
     */
@Test
public void getLocaleMultipleHeaders02() throws Exception {
    TesterRequest req = new TesterRequest();
    req.addHeader("accept-language", "en-gb");
    req.addHeader("accept-language", "en;q=0.5");
    Locale actual = req.getLocale();
    Locale expected = Locale.forLanguageTag("en-gb");
    Assert.assertEquals(expected, actual);
}
Also used : Locale(java.util.Locale) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 10 with Locale

use of java.util.Locale in project tomcat by apache.

the class TestAcceptLanguage method bug56848.

@Test
public void bug56848() throws Exception {
    List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("zh-hant-CN;q=0.5,zh-hans-TW;q=0.05"));
    Assert.assertEquals(2, actual.size());
    Locale.Builder b = new Locale.Builder();
    b.setLanguage("zh").setRegion("CN").setScript("hant");
    Locale l1 = b.build();
    b.clear().setLanguage("zh").setRegion("TW").setScript("hans");
    Locale l2 = b.build();
    Assert.assertEquals(l1, actual.get(0).getLocale());
    Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
    Assert.assertEquals(l2, actual.get(1).getLocale());
    Assert.assertEquals(Q0_050, actual.get(1).getQuality(), 0.0001);
}
Also used : Locale(java.util.Locale) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

Locale (java.util.Locale)2214 Test (org.junit.Test)262 ArrayList (java.util.ArrayList)179 HashMap (java.util.HashMap)108 ResourceBundle (java.util.ResourceBundle)83 SimpleDateFormat (java.text.SimpleDateFormat)78 Date (java.util.Date)77 MissingResourceException (java.util.MissingResourceException)68 IOException (java.io.IOException)67 TimeZone (java.util.TimeZone)63 File (java.io.File)50 Map (java.util.Map)49 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)49 Calendar (java.util.Calendar)46 Configuration (android.content.res.Configuration)41 LocaleList (android.os.LocaleList)39 HashSet (java.util.HashSet)39 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)39 HttpSession (javax.servlet.http.HttpSession)38 Resources (android.content.res.Resources)37