The road so far….

April 7, 2013

Spring Recepies : URI redirection

Filed under: java — Tags: , — Rahul Sharma @ 6:51 pm

We have a application which has gone through a short developments sprints. Things got added and then some got modified or removed etc. During this phase a number of service URLs got added/removed/modified. Now if such an application is getting crawled by Google, the you have a mess to clear about. Clearing such a mess is not easy, some URLs should give 301 to a relevant page which some should show 404 and 500 status errors. More over there can be other requirements from the application like domain/sub-domain redirections eg making http://www.myapp.com to myapp.com, ver1.myapp.com to ver2.myapp.com etc.

You could achieve the same by implementing servlet filters or if  you have a Spring mvc application then achieving this is just one interface away. Spring offers HandlerInterceptor interface which can be implemented in your application to achieve this.

Let us assume that you have a list of  URLs , in your database, on which you would like to redirect to a new location. Now in the preHandle method of the interceptor you would like to determine this and do the re-direction if required.


public class LocationInterceptor implements HandlerInterceptor {
 @Resource
 LocationRepository locRepo;

@Override
 public boolean preHandle(HttpServletRequest request,
 HttpServletResponse response, Object handler) throws Exception {
  Location loc=locRepo.findLocation(request.getRequestURI().toLowerCase())
  if(loc!=null){
   response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
   response.setHeader("Location", location.getCurrentLocation());
   return false;
 }
 return true;
 }

@Override
 public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,
 ModelAndView modelAndView) throws Exception {
 }

@Override
 public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)
 throws Exception {
 }

}

Now if you wanted to do redirections based on subdomaina/protcol or any other things, (this may be the case because you are running tomcat on port 80) then the same can be done in the preHandle method.

The same interface can be extended to perform varous other things.,like if you have some things that you would like to have on every page things can some model that you want on every page, you can add the same in the postHandle method.  We required list of countries and some metadata so we added both to the same. 


public class LocationInterceptor implements HandlerInterceptor {

..................
@Override
 public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,
 ModelAndView modelAndView) throws Exception {
 modelAndView.addObject("requiredCountries", loadCountries());
 modelAndView.addObject("metadata", loadMetadata());
 }

..........

}

In order to work properly you will be required to enable this interceptor to your application-servlet.xml.

<mvc:interceptors>
 <bean class="com.myapp.requesthandlers.LocationInterceptor" />
</mvc:interceptors>
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: