You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2011/06/20 16:29:49 UTC

svn commit: r1137640 - in /sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n: DefaultLocaleResolver.java impl/I18NFilter.java

Author: fmeschbe
Date: Mon Jun 20 14:29:49 2011
New Revision: 1137640

URL: http://svn.apache.org/viewvc?rev=1137640&view=rev
Log:
SLING-1905 Make DefaultLocaleResolver (using servlet container's getLocales() method to resolve the locales) available for extension and use the wrapped request object instead of the I18nFilter provided wrapper to call the LocaleResolver.

Added:
    sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java   (with props)
Modified:
    sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java

Added: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java?rev=1137640&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java (added)
+++ sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java Mon Jun 20 14:29:49 2011
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.i18n;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+
+/**
+ * The <code>DefaultLocaleResolver</code> resolves the request's Locale by
+ * calling the <code>ServletRequest.getLocales()</code> method, which generally
+ * will be the Servlet Container's implementation of this method and thus be
+ * based on the client's <code>Accept-Language</code> header.
+ */
+public class DefaultLocaleResolver implements LocaleResolver {
+
+    /**
+     * Return the Locales provided by the
+     * <code>ServletRequest.getLocales()</code> method collected in a
+     * <code>List</code>.
+     */
+    public List<Locale> resolveLocale(SlingHttpServletRequest request) {
+        Enumeration<?> locales = request.getLocales();
+        ArrayList<Locale> localeList = new ArrayList<Locale>();
+        while (locales.hasMoreElements()) {
+            localeList.add((Locale) locales.nextElement());
+        }
+        return localeList;
+    }
+
+}

Propchange: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Modified: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java?rev=1137640&r1=1137639&r2=1137640&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java (original)
+++ sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java Mon Jun 20 14:29:49 2011
@@ -19,7 +19,6 @@
 package org.apache.sling.i18n.impl;
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
@@ -33,7 +32,6 @@ import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
@@ -44,6 +42,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.sling.SlingFilterScope;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
+import org.apache.sling.i18n.DefaultLocaleResolver;
 import org.apache.sling.i18n.LocaleResolver;
 import org.apache.sling.i18n.ResourceBundleProvider;
 import org.osgi.framework.Constants;
@@ -64,21 +63,7 @@ public class I18NFilter implements Filte
     /** default log */
     private static final Logger log = LoggerFactory.getLogger(I18NFilter.class.getName());
 
-    private static LocaleResolver DEFAULT_LOCALE_RESOLVER = new LocaleResolver() {
-        public List<Locale> resolveLocale(SlingHttpServletRequest request) {
-
-            // unwrap the request which is a I18NSlingHttpServletRequest
-            request = ((SlingHttpServletRequestWrapper) request).getSlingRequest();
-
-            Enumeration<?> locales = request.getLocales();
-            List<Locale> localeList = new ArrayList<Locale>();
-            while (locales.hasMoreElements()) {
-                localeList.add((Locale) locales.nextElement());
-            }
-
-            return localeList;
-        }
-    };
+    private static LocaleResolver DEFAULT_LOCALE_RESOLVER = new DefaultLocaleResolver();
 
     @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
     private LocaleResolver localeResolver = DEFAULT_LOCALE_RESOLVER;
@@ -187,7 +172,7 @@ public class I18NFilter implements Filte
 
         private List<Locale> getLocaleList() {
             if (localeList == null) {
-                localeList = localeResolver.resolveLocale(this);
+                localeList = localeResolver.resolveLocale(this.getSlingRequest());
             }
 
             return localeList;