Get all available @RequestMapping values ​​from controller in filter

I am writing an application using java and Spring MVC. My scope is that my url will contain smth language, for example http://localhost:8080/School/en/xxx/

.... In my application, I use the following code:

Controller-servlet.xml

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

      

web.xml

<filter>
    <filter-name>URLFilter</filter-name>
    <filter-class>filter.URLFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>URLFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

      

Filter

public void doFilter(ServletRequest hsRequest, ServletResponse hsResponse, FilterChain filterChain) throws IOException, ServletException {
    final HttpServletRequest request = (HttpServletRequest) hsRequest;
    final HttpServletResponse response = (HttpServletResponse) hsResponse;

    String servletPath = request.getRequestURI().substring(request.getContextPath().length());
    List<String> langList = new ArrayList<String>();
    langList.add("/it/");
    langList.add("/en/");
    for (String lang : langList) {
        if (StringUtils.startsWithIgnoreCase(servletPath, lang)) {
            filterChain.doFilter(hsRequest, hsResponse);
            return;
        }
    }
    String string = request.getContextPath() + "/en".concat(servletPath);
    response.sendRedirect(string);
}

      

interceptor:

public class LangInterceptor extends HandlerInterceptorAdapter {

@Autowired
SessionLocaleResolver sessionLocaleResolver;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String language = request.getServletPath().substring(1, 3);
    Locale locale = new Locale(language);
    sessionLocaleResolver.setLocale(request, response, locale);
    return true;
}

      

I am assuming the following behavior:

  • The user enters the url, for example: "http: // localhost: 8080 / School /" - the filter doesn't find "/ it /" or "/ en /" and adds "/ en /" by default than a redirect to http: // localhost: 8080 / School / en /, where it will be found to be ok next time.
  • User enters url: http: // localhost: 8080 / School / add-filter doesn't find "/ it /" or "/ en /" and adds "/ en /" by default after redirecting to http: // localhost: 8080 / School / en / add where to find such url is ok.
  • The user enters a URL, for example: http: // localhost: 8080 / School / en1 / add - the filter does not find "/ it /" or "/ en /" and adds "/ en /" by default after redirecting to http: // localhost: 8080 / School / en / ru1 / add where no such url was found and redirect to 404.jsp which is NOT OK.

Therefore, before redirecting, I need to check what follows in the url after "/ en" /, so I need a list of values @RequestMapping

of @Controller

. I tried to use RequestMappingHandlerMapping

but it is not possible because at the filter level beans are not initialized.

How can I solve this problem?

+3


source to share





All Articles