View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26  
27  */
28  package net.sf.jguard.core;
29  
30  import net.sf.jguard.core.filters.Filterable;
31  import net.sf.jguard.core.filters.FilterChainImpl;
32  import net.sf.jguard.core.filters.Filter;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Map;
36  
37  
38  
39  import net.sf.jguard.core.authentication.AccessContext;
40  import net.sf.jguard.core.authentication.manager.AuthenticationManager;
41  import net.sf.jguard.core.authentication.manager.AuthenticationManagerFactory;
42  import net.sf.jguard.core.filters.PolicyEnforcementPointFilter;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  
47  /**
48   * Policy Enforcement Point.
49   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
50   * @since 1.1
51   */
52  public class PolicyEnforcementPoint implements Cloneable{
53  
54      private static final Logger logger = LoggerFactory.getLogger(PolicyEnforcementPoint.class.getName());
55      
56      private List<Filter> filters;
57  
58      private PolicyEnforcementPoint(){
59          
60      }
61  
62      public PolicyEnforcementPoint(String authenticationBindingsFactoryImpl,Map<PolicyEnforcementPointOptions,String> options){
63         
64         PolicyEnforcementPointFilter pepFilter = new PolicyEnforcementPointFilter(authenticationBindingsFactoryImpl,options);
65         filters = new ArrayList<Filter>();
66         AuthenticationManager authenticationManager = AuthenticationManagerFactory.getAuthenticationManager();
67         if(authenticationManager!=null && authenticationManager instanceof Filterable){
68             logger.debug("adding authenticationManager filter");
69             addFilter(((Filterable)authenticationManager).getFilter());
70         }
71         addFilter(pepFilter);
72      }
73      
74           
75      /**
76       * check if user is authenticated, and check its access rights.
77       * @param context
78       */
79  	public void process(AccessContext context){
80  		try{
81                      FilterChainImpl filterChain = new FilterChainImpl(filters);
82                      filterChain.doFilter(context);
83                  }catch(Throwable t){
84                      logger.error(t.getMessage(),t);
85                      throw new IllegalStateException(" an exception has occured ");
86                  }
87  
88  	}
89   
90     public void addFilter(Filter filter){
91          filters.add(filter);
92     }
93  
94     public void addFilter(int index,Filter filter){
95          filters.add(index,filter);
96     }
97  
98     public Object clone(){
99         PolicyEnforcementPoint p = new PolicyEnforcementPoint();
100        p.filters = new ArrayList<Filter>(filters);
101        return p;
102    }
103 }