Changeset 1407

Show
Ignore:
Timestamp:
14/12/09 09:37:12 (3 months ago)
Author:
karel
Message:

Allow configuring a default locale and timezone.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/modules/kauri-locale-assignment/src/main/java/org/kauriproject/i18n/locale_assignment/LocaleAssigner.java

    r1327 r1407  
    1616package org.kauriproject.i18n.locale_assignment; 
    1717 
     18import java.util.Locale; 
     19 
     20import javax.annotation.PostConstruct; 
     21 
     22import org.kauriproject.conf.Conf; 
     23import org.kauriproject.runtime.rapi.ConfRegistry; 
    1824import org.kauriproject.runtime.rapi.FilterFactory; 
    1925import org.kauriproject.runtime.rapi.KauriModule; 
    2026import org.restlet.routing.Filter; 
    2127 
    22 import javax.annotation.PostConstruct; 
    23  
    2428public class LocaleAssigner { 
    2529    private KauriModule kauriModule; 
    26  
    27     public LocaleAssigner(KauriModule kauriModule) { 
     30     
     31    private ConfRegistry confRegistry; 
     32     
     33    public LocaleAssigner(KauriModule kauriModule, ConfRegistry confRegistry) { 
    2834        this.kauriModule = kauriModule; 
     35        this.confRegistry = confRegistry; 
    2936    } 
    3037 
    3138    @PostConstruct 
    3239    public void init() { 
    33         kauriModule.addRootFilter(new LocaleAssignmentFilterFactory()); 
     40        Conf conf = confRegistry.getConfiguration("defaults", true); 
     41         
     42        boolean ignoreClientInfo = conf.getAttributeAsBoolean("ignoreClientInfo", false); 
     43 
     44        Conf localeConf = conf.getChild("defaultLocale", false); 
     45        Locale defaultLocale = Locale.getDefault(); 
     46        if (localeConf != null) { 
     47            String language = localeConf.getAttribute("language", ""); 
     48            String country = localeConf.getAttribute("country", ""); 
     49            String variant = localeConf.getAttribute("variant", ""); 
     50            defaultLocale = new Locale(language, country, variant); 
     51        } 
     52 
     53        String defaultTimeZoneId = conf.getChild("defaultTimeZoneId").getValue("UTC"); 
     54 
     55        kauriModule.addRootFilter(new LocaleAssignmentFilterFactory(ignoreClientInfo, defaultLocale, defaultTimeZoneId)); 
    3456    } 
    3557 
    3658    private static class LocaleAssignmentFilterFactory implements FilterFactory { 
     59         
     60        private boolean ignoreClientInfo; 
     61        private Locale defaultLocale; 
     62        private String defaultTimeZoneId; 
     63         
     64        public LocaleAssignmentFilterFactory(boolean ignoreClientInfo, Locale defaultLocale, String defaultTimeZoneId) { 
     65            this.ignoreClientInfo = ignoreClientInfo; 
     66            this.defaultLocale = defaultLocale; 
     67            this.defaultTimeZoneId = defaultTimeZoneId; 
     68        } 
    3769        public Filter createFilter() { 
    38             return new LocaleAssignmentFilter(); 
     70            return new LocaleAssignmentFilter(ignoreClientInfo, defaultLocale, defaultTimeZoneId); 
    3971        } 
    4072    } 
  • trunk/modules/kauri-locale-assignment/src/main/java/org/kauriproject/i18n/locale_assignment/LocaleAssignmentFilter.java

    r1327 r1407  
    1616package org.kauriproject.i18n.locale_assignment; 
    1717 
    18 import org.restlet.routing.Filter; 
    19 import org.restlet.data.*; 
     18import java.util.List; 
     19import java.util.Locale; 
     20 
    2021import org.kauriproject.i18n.LocaleHelper; 
    2122import org.kauriproject.i18n.LocaleSettings; 
    22  
    23 import java.util.Locale; 
    24 import java.util.List; 
     23import org.restlet.data.Form; 
     24import org.restlet.data.Language; 
     25import org.restlet.data.Preference; 
     26import org.restlet.data.Request; 
     27import org.restlet.data.Response; 
     28import org.restlet.routing.Filter; 
    2529 
    2630public class LocaleAssignmentFilter extends Filter { 
    27     private Locale defaultLocale = new Locale("en"); // TODO should become configurable 
    28     private String defaultTimeZoneId = "UTC"; // TODO should become configurable 
    29  
     31     
     32    private boolean ignoreClientInfo; 
     33     
     34    private Locale defaultLocale; 
     35     
     36    private String defaultTimeZoneId; 
     37     
     38    public LocaleAssignmentFilter(boolean ignoreClientInfo, Locale defaultLocale, String defaultTimeZoneId) { 
     39        this.ignoreClientInfo = ignoreClientInfo; 
     40        this.defaultLocale = defaultLocale; 
     41        this.defaultTimeZoneId = defaultTimeZoneId; 
     42    } 
     43     
    3044    @Override 
    3145    protected int doHandle(Request request, Response response) { 
     
    5064        // Then, check for accepted languages 
    5165        // 
    52         if (locale == null) { 
     66        if (locale == null && !ignoreClientInfo) { 
    5367            List<Preference<Language>> acceptedLanguages = request.getClientInfo().getAcceptedLanguages(); 
    5468            if (!acceptedLanguages.isEmpty()) { 
  • trunk/modules/kauri-locale-assignment/src/main/kauri/spring/services.xml

    r999 r1407  
    1616  <context:annotation-config/> 
    1717 
    18   <kauri:module restletContext="context" classLoader="classloader" handle="module"/> 
    19  
     18  <kauri:module restletContext="context" classLoader="classloader" handle="module" conf="confRegistry"/> 
     19   
    2020  <bean class="org.kauriproject.i18n.locale_assignment.LocaleAssigner"> 
    2121    <constructor-arg ref="module"/> 
     22    <constructor-arg ref="confRegistry"/> 
    2223  </bean> 
    23  
     24   
    2425</beans>