<$BlogRSDURL$>
  Tuesday, October 18, 2005

Preferred Locale on AppFuse

The solution provided here are based on locale issues on AppFuse JIRA, specifically, APF-141 which is further refined in APF-142.

Here are the steps to change the request-based locale to user-preferred locale, i.e., implement a session-based locale on AppFuse:
1. Add session-scope PREFERRED_LOCALE and optional request scope DEFAULT_LOCALE constants.
2. Use LoginServlet to set locale session. The parameter, locale, is passed from loginForm.jsp, a selected locale from a dropdown for example.

public final class LoginServlet extends HttpServlet {
...
public void execute(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
...
request.getSession().setAttribute(Constants.PREFERRED_LOCALE, request.getParameter("locale"));
...
}
}

3. Persist preferredLocale for user in ActionFilter:

public class ActionFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain)
throws IOException, ServletException {
...
if ((username != null) && (user == null)) {
...
String preferredLocale = session.getAttribute(Constants.PREFERRED_LOCALE).toString();
if (preferredLocale != null) {
if (preferredLocale "") {
if (user.getPreferredLocale() null) {
user.setPreferredLocale(Constants.DEFAULT_LOCALE);
}
} else {
user.setPreferredLocale(preferredLocale);
}
}
}
session.setAttribute(Constants.PREFERRED_LOCALE, user.getPreferredLocale());
}
}

4. Add locale filter, LocaleFilter:

public class LocaleFilter implements Filter {
private static final transient Log log = LogFactory.getLog(LocaleFilter.class);
public void init(FilterConfig filterConfig) {
}

public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpSession session = servletRequest.getSession(false);

if (log.isDebugEnabled()) {
log.debug(servletRequest.getRequestURL());
}
Locale preferredLocale = null;

if (session!=null){
if (session.getAttribute(Constants.PREFERRED_LOCALE) != null) {
String localeKey = session.getAttribute(Constants.PREFERRED_LOCALE).toString();
if (localeKey != null) {
if (localeKey.equals("")) {
String[] keys = Constants.DEFAULT_LOCALE.split("-");
preferredLocale = new Locale(keys[0],keys[1]);
} else {
String[] keys = localeKey.split("-");
preferredLocale = new Locale(keys[0],keys[1]);
}
}
}
}
if (null != preferredLocale
&& !(request instanceof LocaleResponseWrapper)) {
request = new LocaleResponseWrapper(servletRequest, preferredLocale);
}

chain.doFilter(request, response);
}
}

5. The associated LocaleResponseWrapper is equivalent to LocaleRequestWrapper:

public class LocaleResponseWrapper extends HttpServletRequestWrapper {
private final Locale preferredLocale;
private Enumeration locales;

public LocaleResponseWrapper(HttpServletRequest request, Locale sessionLocale) {
super(request);
preferredLocale = sessionLocale;
}

public Locale getLocale() {
if (null != preferredLocale) {
return preferredLocale;
} else {
return super.getLocale();
}
}
public Enumeration getLocales() {
if (null != preferredLocale) {
return setLocales();
} else {
return super.getLocales();
}
}
private Enumeration setLocales() {
if (null == locales) {
List l = Collections.list(super.getLocales());
l.add(0, preferredLocale);
locales = Collections.enumeration(l);
}
return locales;
}
}

6. Put the locale filter in web.xml or appfuse/metadata/web/filters.xml:

<filter>
<filter-name>localeFilter</filter-name>
<display-name>Locale Filter</display-name>
<filter-class>org.appfuse.webapp.filter.LocaleFilter</filter-class>
</filter>

7. Modify JSP’s: add page-scope locale setting:

<fmt:setLocale value="${preferredLocale}" />


Now AppFuse is powered by preferred locale!
 

8 Comments/評論:

Anonymous Anonymous said...

Sorry for my post .Where i can watch more info about?

1:37 PM  
Anonymous Anonymous said...

[B]NZBsRus.com[/B]
Lose Idle Downloads With NZB Downloads You Can Swiftly Search Movies, PC Games, Music, Applications and Download Them @ Alarming Speeds

[URL=http://www.nzbsrus.com][B]NZB Search[/B][/URL]

11:15 AM  
Anonymous Anonymous said...

ego ops slow environment per working added wo

3:52 PM  
Anonymous Anonymous said...

You could easily be making money online in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat forums[/URL], Don’t feel silly if you don't know what blackhat is. Blackhat marketing uses not-so-popular or misunderstood methods to produce an income online.

12:27 PM  
Anonymous Anonymous said...

[url=http://www.casino-online.gd]casino[/url], also known as accepted casinos or Internet casinos, are online versions of distinguished ("confrere and mortar") casinos. Online casinos consign gamblers to palm false after subdivide in and wager on casino games from start to have a bite the Internet.
Online casinos habitually plonk down aside on the supermarket odds and payback percentages that are comparable to land-based casinos. Some online casinos control higher payback percentages as a pharmaceutical with a position affair gismo games, and some dispatch payout note audits on their websites. Assuming that the online casino is using an correctly programmed unspecific consolidate up generator, catalogue games like blackjack take an established congress edge. The payout slice as a replacement representing these games are established sooner than the rules of the game.
Uncountable online casinos limit not at cuttingly or earn their software from companies like Microgaming, Realtime Gaming, Playtech, Supranational Ploy Technology and CryptoLogic Inc.

11:46 PM  
Anonymous Anonymous said...

The Honest Debt Collection Practices Act (FDCPA) established by the Ftc (FTC) was created in response so that you can consumer grievances of excessively aggressive plus abusive habits by bill collectors [url=http://www.paydayloansddbs.co.uk]payday loans[/url] payday loans You're now free to connect with those of your wants which you could not think about just because of your restricted salary http://www.paydayloansvs.co.uk

2:07 AM  
Anonymous Anonymous said...

Police investigated the topic extensively, who lived in institutions, like Nursing homes, skewing the results toward fitter people. [url=http://surf-navi.jp/surfpatrol/isokokids/2005/bbs/bbs17.cgi]more[/url] homepage Asked how he would celebrate beingness voted tried to colza her while jogging in fundamental Park -- but reported this two months later the fact. http://www.lannahealth.com/webboard/index.php?topic=55203.new#new

3:06 PM  
Anonymous Anonymous said...

It has to be tacit that cosh is for the most part a Portion-based Put up no-deposit bonus, you can signboard it up unbent aside and use the fillip to do the prove. [url=http://ukpaydayloans.blog.co.uk/]payday loans[/url] pay day loans Exploitation ecard is a beneficial or the use of drugs like cocaine or opium. http://www.bbvcpaydayloans.co.uk/

10:31 PM  

Post a Comment /投評論

<< Home

Software Culture
  03/01/2004 - 04/01/2004
  04/01/2004 - 05/01/2004
  05/01/2004 - 06/01/2004
  06/01/2004 - 07/01/2004
  08/01/2004 - 09/01/2004
  11/01/2004 - 12/01/2004
  07/01/2005 - 08/01/2005
  09/01/2005 - 10/01/2005
  10/01/2005 - 11/01/2005
  01/01/2006 - 02/01/2006
  03/01/2006 - 04/01/2006
ColdFusion
日常毒藥與養料
  Smart talk always right?
  Drools on AppFuse
  Braille, braille
  可愛提示
  The Dynamic SRC of IMG
  Preferred Locale on AppFuse
  告訴你為什麼程式不 work
  絲綢之路 2000:致命病毒
  AJAX on AppFuse
  1918
  Meet Mr. Writely
  網際網路的最後一頁
  小螞蟻最短篇
  健檢, e檢
  Open source ColdFusion
  八月半個
  自行其是
  Rich DHTML client
  cfspring, seriously
  三百萬民主補給站
  敏督利小插曲
  迷上喬治亞
  說故事
  Where are they?
  宿夢
  An Architect's View
  Martin Fowler
  Loud Thinking
  Raible Designs   fullasagoog
Home


Powered by Blogger