KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > RequestContextFactoryImpl


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.controller;
14
15 import java.util.Locale JavaDoc;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import javax.servlet.jsp.jstl.core.Config;
20
21 import org.apache.log4j.Logger;
22
23 import com.tonbeller.tbutils.res.Resources;
24 import com.tonbeller.tbutils.res.ResourcesFactory;
25 import com.tonbeller.wcf.convert.Converter;
26 import com.tonbeller.wcf.convert.ConverterFactory;
27 import com.tonbeller.wcf.format.Formatter;
28 import com.tonbeller.wcf.format.FormatterFactory;
29
30 /**
31  * Created on 05.11.2002
32  *
33  * @author av
34  */

35 public class RequestContextFactoryImpl implements RequestContextFactory {
36   Formatter formatter;
37   Converter converter;
38   Locale JavaDoc locale;
39   Resources resources;
40
41   String JavaDoc remoteUser;
42   String JavaDoc remoteDomain;
43
44   private static Logger logger = Logger.getLogger(RequestContextFactoryImpl.class);
45
46   public RequestContext createContext(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
47     initialize(request);
48     return new RequestContextImpl(this, request, response);
49   }
50
51   protected void initialize(HttpServletRequest JavaDoc request) {
52     if (locale == null) {
53       locale = ResourcesFactory.instance().getFixedLocale();
54       if (locale == null)
55         locale = request.getLocale();
56       if (locale == null)
57         locale = Locale.getDefault();
58       formatter = FormatterFactory.instance(locale);
59       converter = ConverterFactory.instance(formatter);
60       resources = Resources.instance(locale, getClass());
61
62       String JavaDoc rUser = request.getRemoteUser();
63       if (rUser != null) {
64         int slash = rUser.indexOf('/');
65         if (slash >= 0) {
66           remoteDomain = rUser.substring(0, slash);
67           remoteUser = rUser.substring(slash + 1);
68         } else {
69           remoteUser = rUser;
70           remoteDomain = null;
71         }
72         if (remoteUser != null)
73           remoteUser = remoteUser.trim();
74         if (remoteDomain != null)
75           remoteDomain = remoteDomain.trim();
76       }
77
78     }
79   }
80
81   /**
82    * change the locale
83    * @param locale the new locale or null to determine
84    * the locale from the next http request
85    * @deprecated
86    */

87   public void setLocale(Locale JavaDoc locale) {
88     this.locale = locale;
89     if (locale != null) {
90       formatter = FormatterFactory.instance(locale);
91       converter = ConverterFactory.instance(formatter);
92       resources = Resources.instance(locale, getClass());
93     }
94   }
95
96   /**
97    * change the locale including the Locale for JSTL >fmt:message> tags
98    *
99    * @param locale the new locale
100    * @param session current session
101    */

102   public void setLocale(HttpServletRequest JavaDoc request, Locale JavaDoc locale) {
103     this.locale = locale;
104     if (locale != null) {
105       formatter = FormatterFactory.instance(locale);
106       converter = ConverterFactory.instance(formatter);
107       resources = Resources.instance(locale, getClass());
108       if (logger.isInfoEnabled())
109         logger.info("setting locale to " + locale);
110       Config.set(request, Config.FMT_LOCALE, locale);
111     }
112   }
113
114   public Converter getConverter() {
115     return converter;
116   }
117
118   public Formatter getFormatter() {
119     return formatter;
120   }
121
122   public Locale JavaDoc getLocale() {
123     return locale;
124   }
125
126   public Resources getResources() {
127     return resources;
128   }
129
130   public String JavaDoc getRemoteDomain() {
131     return remoteDomain;
132   }
133
134   public String JavaDoc getRemoteUser() {
135     return remoteUser;
136   }
137
138 }
Popular Tags