views:

74

answers:

1

I have Spring 3.0 setup with annotated controllers and it is finding my controller and executing it as it should when I send an url to it from a browser . But when rewriting the url with a filter to the exact same that works in the browser I get No mapping found for HTTP request with URI [/test/lookup]. The filter is loaded by a org.springframework.web.filter.DelegatingFilterProxy.

What have I missed in the filter?

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.stereotype.Component;


@Component("urlLookupFilter")
public class UrlLookupFilter implements Filter {

    private class ModifiedRequest extends HttpServletRequestWrapper {

        private String mRequestURI;
        private String mQueryString;

        public ModifiedRequest(HttpServletRequest request) {
            super(request);
        }

        @Override
        public String getRequestURI() {
            return mRequestURI;
        }

        public void setRequestUri(String s) {
            this.mRequestURI = s;
        }

        @Override
        public String getQueryString() {
            return mQueryString;
        }

        public void setQueryString(String s) {
            this.mQueryString = s;
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest oRequest = (HttpServletRequest) request;

        if (oRequest.getRequestURI().endsWith("test")) {
            ModifiedRequest mRequest = new ModifiedRequest(oRequest);
            mRequest.setRequestUri(oRequest.getContextPath() + "/lookup");
            mRequest.setQueryString(oRequest.getRequestURI().substring(oRequest.getContextPath().length()));
            chain.doFilter(mRequest, response);
            return;
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
+2  A: 

As I mentioned in my answer to your previous question, just over-riding getRequestURI is not enough, you would need to override several different path-related methods in the request, such as getPathInfo, getServletContextPath, etc etc. It's really not a good idea, but if you really want to do this, you'll have to go through the Spring source and find which of the methods on the request are used to do the mapping, and make sure you're over-riding that one.

skaffman
Thanks. I concurr with the fact that's it's really not a good idea. If someone gets the same bright idea as me in the future, just don't do it! ;)
NA