Q:

How to intercept request for specific file type?

Our application is a Java/Spring3/Spring MVC/Hibernate based app.
We have some resources that are stored in various different places on the server machine. The locations are stored in the database. Basically when the web application requests a file from a uri like /<our-app>/page/file.kml when need to intercept this call, ignore the requested uri, lookup the actual location of the file and return that as the response.
In our servlet-context.xml we have some interceptors;
<interceptors>
    <interceptor>
        <mapping path="/page/**" />
        <beans:bean class="com.ourapp.AuthenticationInterceptor" />
    </interceptor>
    <interceptor>
        <mapping path="/page/*.kml" />
        <beans:bean class="com.ourapp.KmlInterceptor" />
    </interceptor>
</interceptors>

The first intercept is for our authentication and works great. Basically makes sure the user is logged in for ANY request.
The second interceptor is what we've setup to try and intercept requests to KML files from geoXML3. The interceptor doesn't seem to be firing? (ie the KmlInterceptor.preHandle doesn't get called?).
Are we doing the correct mapping there?
Is this the way to intercept requests for specific file types and return the actual file retrieved from somewhere else?

A:

In actual fact, we moved away from trying to use an interceptor and just used a normal @RequestMapping annotation instead;
@RequestMapping(value = "/page/location/*.kml", method = RequestMethod.GET)
public void getKMLFile(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        // Getting the filename (ie splitting off the /page/location/ part)
        String uri = httpRequest.getRequestURI();
        String[] parts = uri.split("/");
        String alais = parts[parts.length - 1];

        // Our app specific code finding the file from the db location 
        Resource resource = resourceService.getResourceByAlias(alais);
        File file = resource.getAbsFile();

        // Putting the file onto the httpResponse 
        InputStream is = new FileInputStream(file);
        IOUtils.copy(is, httpResponse.getOutputStream());
        httpResponse.flushBuffer();

    } catch (IOException e) {
        throw new RuntimeException("IOError writing file to output stream");
    }
}

