View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.interceptors;
20  
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
30  
31  /***
32   * @author alf
33   *
34   */
35  public abstract class ExcludingInterceptorBase extends
36  		HandlerInterceptorAdapter
37  {
38  	protected Set<String> m_exclusions;
39  
40  	public ExcludingInterceptorBase()
41  	{
42  		m_exclusions = Collections.emptySet();
43  	}
44  	
45  	@Override
46  	public final boolean preHandle(HttpServletRequest request,
47  			HttpServletResponse response, Object handler) throws Exception
48  	{
49  		if (excluded(request.getServletPath()))
50  		{
51  			return true;
52  		}
53  		else
54  		{
55  			return excludingPreHandle(request, response, handler);
56  		}
57  	}
58  	
59  	public abstract boolean excludingPreHandle(HttpServletRequest request,
60  			HttpServletResponse response, Object handler) throws Exception;
61  
62  	protected boolean excluded(String servletPath) {
63  		assert servletPath != null : "Error -- null servlet path";
64  		int lastSlash = servletPath.lastIndexOf('/');
65  		
66  		String servletName;
67  		
68  		if (lastSlash >= 0 && lastSlash+1 < servletPath.length())
69  		{
70  			servletName = servletPath.substring(lastSlash+1);
71  		}
72  		else
73  		{
74  			// servletPath ends with '/' or contains no '/' -- just fall back to the whole path.
75  			servletName = servletPath;
76  		}
77  		
78  		return m_exclusions.contains(servletName);
79  	}
80  
81  	public void setExclusions(List<String> exclusions) {
82  		m_exclusions = new HashSet<String>(exclusions);
83  	}
84  
85  }