View Javadoc

1   package org.varienaja.util;
2   
3   import java.io.IOException;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.util.Map.Entry;
7   
8   import javax.servlet.FilterChain;
9   import javax.servlet.ServletException;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  
13  import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;
14  
15  /**
16   * The SelectiveOpenSessionInViewFilter selectively applies the 
17   * OpenSessionInViewFilter, depending on the requestparameters.
18   * @author Varienaja
19   */
20  public class SelectiveOpenSessionInViewFilter extends OpenSessionInViewFilter {
21  	private static final String KEYVALUELIST = "keyvaluelist";
22  	private Map<String,String> _filterMap;
23  	
24  	/**
25  	 *  Creates a new SelectiveOpenSessionInViewFilter. When configuring this
26  	 *  object, you must specify the keyvaluelist-property. This property should
27  	 *  contain a list of K=V; elements.
28  	 */
29  	public SelectiveOpenSessionInViewFilter() {
30  		addRequiredProperty(KEYVALUELIST);
31  		_filterMap = new HashMap<String,String>();
32  	}
33  
34  	/*
35  	 * Intercept the requests for the StreamService and deny them the normal
36  	 * opensessioninviewfilter-behaviour, if filtered.
37  	 * (non-Javadoc)
38  	 * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter#doFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
39  	 */
40  	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
41  		//MAJOR WTF BELOW
42  		//We're reading request parameters here. But the SetupRequestEncoding
43  		//class of Tapestry did not set the encoding yet. The encoding can only
44  		//be set once, and it has to be before parameters are read, otherwise
45  		//they are read as ASCII or something alike, which is not what we want.
46  		//So here's teh very superspecial MusiController-setting which corrects
47  		//everything: we just set the encoding to be UTF-8, since that is our
48  		//encoding in MusiController. We read our parameters right, and Tapestry
49  		//will decode them correctly later on as well. Everybody is happy.
50  		request.setCharacterEncoding("UTF-8");
51  		
52  		
53  		boolean matched = false;
54  		for (Entry<String,String> entry : _filterMap.entrySet()) {
55  			String values = request.getParameter(entry.getKey());
56  			if (entry.getValue().equals(values)) {
57  				matched = true;
58  				break;
59  			}
60  		}
61  		
62  		if (matched) {
63  			filterChain.doFilter(request, response);
64  		} else { 
65  			super.doFilterInternal(request, response, filterChain);
66  		}
67  	}
68  	
69  	/*
70  	 * (non-Javadoc)
71  	 * @see org.springframework.web.filter.GenericFilterBean#initFilterBean()
72  	 */
73  	protected void initFilterBean() throws ServletException {
74  		String cfg = getFilterConfig().getInitParameter(KEYVALUELIST);
75  		for (String pair : cfg.split(";")) {
76  			String[] items = pair.split("=");
77  			if (items.length==2) {
78  				_filterMap.put(items[0], items[1]);
79  			} else {
80  				throw new ServletException("Erroneous config of " + getClass().getName() + ", cannot interpret: " + cfg);
81  			}
82  		}
83  	}
84  }