Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 MVC + RESTful url + Internationalization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    @xcarlos:
    Thank you for your reply. It would be very nice if you could do that :-).

    Comment


    • #17
      Well, here it is. Couldn't test the code after removing non relevant stuff, but I believe it works. Of course there may be bugs or things which could be improved

      Servlet filter

      Code:
      public class LanguageFilter implements Filter {
      
      	private ServletContext servletContext;
      	private LocaleResolver localeResolver;
      
      	@Override
      	public void init(FilterConfig filterConfig) throws ServletException {
      		this.servletContext = filterConfig.getServletContext();
      		// Grab your Locale Resolver here and assign it to the
      		// instance variable (with context.getBean() or whatever...)
      	}
      
      	@Override
      	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
      		throws IOException, ServletException {
      
      		HttpServletRequest request = (HttpServletRequest) req;
      		HttpServletResponse response = new LangResponseWrapper((HttpServletResponse) resp, request);
      		RequestDispatcher dispatcher = null;
      
      		String path = request.getRequestURI().substring(request.getContextPath().length());
      		if (path.startsWith("/")) {
      			path = path.substring(1);
      		}
      
      		String tokens = path.split("/");
      
      		if (tokens.length > 0 && tokens[0].length() == 2) {
      			String language = tokens[0];
      			// You may want to check if supplied language code is supported...
      			localeResolver.setLocale(request, response, new Locale(language)); 
      
      			StringBuilder cleanUrl = new StringBuilder("/");
      			for (int i = 1; i < tokens.length; i++) {
      				cleanUrl.append(tokens[i]);
      				if (i < tokens.length - 1) {
      					cleanUrl.append("/");
      				}
      			}
      			dispatcher = servletContext.getRequestDispatcher(cleanUrl.toString());
      		}
      
      		if (dispatcher != null) {
      			dispatcher.forward(request, response);
      		} else {
      			chain.doFilter(request, response);
      		}
      	}
      }
      Response wrapper

      Code:
      public class LangResponseWrapper extends HttpServletResponseWrapper {
      	
      	private HttpServletRequest request;
      	
      	public LanguageResponseWrapper(HttpServletResponse response, HttpServletRequest request) {
      		super(response);
      		this.request = request;
      	}
      
      	@Override
      	public String encodeUrl(String url) {
      
      		String path = url;
      		
      		if (path.startsWith(request.getContextPath())) {
      			path = path.substring(request.getContextPath().length());
      		}
      		
      		if (path.startsWith("/")) {
      			path = path.substring(1);
      		}
      
      		String [] tokens = path.split("/");
      
      		if (tokens.length > 0 && tokens[0].length() != 2) {
      			String currentLanguage = LocaleContextHolder.getLocale().getLanguage();
      			StringBuilder localizedUrl = new StringBuilder();
      			localizedUrl.append(request.getContextPath()).append("/")
      				.append(currentLanguage).append("/").append(path);
      			return super.encodeUrl(localizedUrl.toString());
      		} else {
      			return super.encodeUrl(url);
      		}
      
      	}
      
      	@Override
      	public String encodeURL(String url) {
      		return encodeUrl(url);
      	}
      
      }
      For the Locale Resolver, I used a standard SessionLocaleResolver.
      Just don't forget to map your filter to /* inside web.xml, write your links using <c:url/> and I think you're ready to go.

      Hope that helps!

      Comment


      • #18
        @xcarlos: Thank you very much.

        I will test it and i will give you a feedback!

        Comment

        Working...
        X