Re: multi-language url rewiting. Is it possible?

Splash Forums PrettyFaces Users multi-language url rewiting. Is it possible? Re: multi-language url rewiting. Is it possible?

#23436

francescanye
Member

The only way to get it to work was using the redirect, thanks.

The initial redirection to the welcome file is the only item I’m still having concern on. Fixed redirection to the italian content welcome file was not an acceptable solution, so I tried something different. I removed the <welcome-file-list> element completely from the web.xml file and I developed a simple filter:

<!--  Default page -->
<filter>
<filter-name>DefaultPageFilter</filter-name>
<filter-class>com.authorization.DefaultPageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>DefaultPageFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>

and inserted just before the pretty-faces filter:

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Request and Response
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
// Session
HttpSession session = httpRequest.getSession();
// Get the localeChanger bean
ChangeLocaleBean clb = (ChangeLocaleBean)session
.getAttribute("localeChanger");
String lang = "en";
if(clb == null){
// here the localeChanger is null: no info ubout the language,
// so the language is taken from the request
Locale loc = httpRequest.getLocale();
if(loc != null)
lang = loc.getLanguage().toString();
} else {
// there is a session parameter about the language: we use
// that one
lang = clb.getLanguageCode();
}
String url = httpRequest.getRequestURL().toString();
if (httpRequest.getQueryString() != null) {
url = url + "?" + httpRequest.getQueryString();
}

StringBuilder redirectUrl = new StringBuilder(url);
redirectUrl.append(lang).append("/main/welcome");

// redirect 301
httpResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
httpResponse.setHeader("Location", redirectUrl.toString());

httpResponse.setHeader("Cache-Control",
"no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpResponse.setDateHeader("Expires", 0); // Proxies.
}

which will be called on / path only and remap it into the welcome page with the content in the appropriate language:

If the language has already been set in the session (flag link clicked), use that language,

otherwise use the language coming from the user request.

Furthermore, in order to avoid the user to go to the “back-end” welcome page I’ve used:

<rewrite match="/pages/main/welcome.jsf"
substitute="/"
redirect="301" />

which triggers the filter just in case.

It works as expected, but I wonder if it is the right way to do it. Could I use pretty-faces features only instead of implementing a filter? Am I misusing pretty-faces?