| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.web; |
| 16 | |
| 17 | import java.io.IOException; |
| 18 | |
| 19 | import javax.servlet.Filter; |
| 20 | import javax.servlet.FilterChain; |
| 21 | import javax.servlet.FilterConfig; |
| 22 | import javax.servlet.ServletException; |
| 23 | import javax.servlet.ServletRequest; |
| 24 | import javax.servlet.ServletResponse; |
| 25 | import javax.servlet.http.HttpServletRequest; |
| 26 | |
| 27 | import org.apache.hivemind.Registry; |
| 28 | import org.apache.hivemind.servlet.HiveMindFilter; |
| 29 | |
| 30 | /** |
| 31 | * Servlet Filter that allows retrieval of HiveMind <code>Registry</code> from |
| 32 | * any code location executed in the current thread without needing access to |
| 33 | * the <code>ServletRequest</code>. |
| 34 | * <p> |
| 35 | * It should be put in second position right after the standard |
| 36 | * <code>HiveMindFilter</code>. |
| 37 | * <p> |
| 38 | * It copies the current <code>Registry</code> to a <code>ThreadLocal</code>, |
| 39 | * making it accessible through a static method with no argument. |
| 40 | * <p> |
| 41 | * You should use this Filter <b>only</b> if you have some code that needs to |
| 42 | * access HiveMind <code>Registry</code>, but cannot have access to the current |
| 43 | * <code>ServletRequest</code>, eg if you use SecurityFilter (from Max Cooper), |
| 44 | * and your SecurityRealm needs to access the <code>Registry</code> (generally |
| 45 | * it would need to). |
| 46 | * |
| 47 | * @author Jean-Francois Poilpret |
| 48 | */ |
| 49 | public class HiveMindRegistryPublishFilter implements Filter |
| 50 | { |
| 51 | /** |
| 52 | * Gets the HiveMind <code>Registry</code> assigned to the current thread. |
| 53 | * No argument is required. |
| 54 | */ |
| 55 | static public Registry getRegistry() |
| 56 | { |
| 57 | return _localRegistry.get(); |
| 58 | } |
| 59 | |
| 60 | public void init(FilterConfig config) throws ServletException |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | public void destroy() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 69 | throws IOException, ServletException |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | _localRegistry.set(HiveMindFilter.getRegistry((HttpServletRequest) request)); |
| 74 | chain.doFilter(request, response); |
| 75 | } |
| 76 | finally |
| 77 | { |
| 78 | _localRegistry.set(null); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static protected ThreadLocal<Registry> _localRegistry = new ThreadLocal<Registry>(); |
| 83 | } |