1 13 package com.tonbeller.wcf.controller; 14 15 import java.util.Locale ; 16 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 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 35 public class RequestContextFactoryImpl implements RequestContextFactory { 36 Formatter formatter; 37 Converter converter; 38 Locale locale; 39 Resources resources; 40 41 String remoteUser; 42 String remoteDomain; 43 44 private static Logger logger = Logger.getLogger(RequestContextFactoryImpl.class); 45 46 public RequestContext createContext(HttpServletRequest request, HttpServletResponse response) { 47 initialize(request); 48 return new RequestContextImpl(this, request, response); 49 } 50 51 protected void initialize(HttpServletRequest 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 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 87 public void setLocale(Locale 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 102 public void setLocale(HttpServletRequest request, Locale 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 getLocale() { 123 return locale; 124 } 125 126 public Resources getResources() { 127 return resources; 128 } 129 130 public String getRemoteDomain() { 131 return remoteDomain; 132 } 133 134 public String getRemoteUser() { 135 return remoteUser; 136 } 137 138 } | Popular Tags |