View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.security;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.content2bean.Content2BeanTransformer;
38  import info.magnolia.content2bean.PropertyTypeDescriptor;
39  import info.magnolia.content2bean.TransformationState;
40  import info.magnolia.content2bean.TypeDescriptor;
41  import info.magnolia.content2bean.impl.Content2BeanTransformerImpl;
42  import info.magnolia.objectfactory.ObservedComponentFactory;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import java.util.Arrays;
46  import java.util.Collections;
47  import java.util.HashMap;
48  import java.util.Map;
49  import java.util.Set;
50  import java.util.TreeSet;
51  
52  /**
53   *
54   * @author gjoseph
55   * @version $Revision: $ ($Author: $)
56   */
57  public class IPSecurityManagerImpl implements IPSecurityManager {
58      private static final String ALL = "*";
59      private Map<String, Rule> rules;
60  
61      public IPSecurityManagerImpl() {
62          this.rules = new HashMap<String, Rule>();
63      }
64  
65      public boolean isAllowed(HttpServletRequest req) {
66          final Rule rule = getRule(req.getRemoteAddr());
67          return rule != null && rule.allowsMethod(req.getMethod());
68      }
69  
70      public boolean isAllowed(String ip) {
71          return getRule(ip) != null;
72      }
73  
74      protected Rule getRule(String ip) {
75          if (rules.containsKey(ip)) {
76              return rules.get(ip);
77          } else {
78              return rules.get(ALL);
79          }
80      }
81  
82      public Map<String, Rule> getRules() {
83          return rules;
84      }
85  
86      public void setRules(Map<String, Rule> rules) {
87          this.rules = rules;
88      }
89  
90      public void addRule(String name, Rule rule) {
91          rules.put(name, rule);
92      }
93  
94      public static final class InstanceFactory extends ObservedComponentFactory<IPSecurityManager> {
95          public InstanceFactory() {
96              super(ContentRepository.CONFIG, "/server/IPConfig", IPSecurityManager.class);
97          }
98  
99          protected Content2BeanTransformer getContent2BeanTransformer() {
100             return new IPSecurityManagerTransformer();
101         }
102     }
103 
104     public static final class IPSecurityManagerTransformer extends Content2BeanTransformerImpl {
105 
106         public void setProperty(TransformationState state, PropertyTypeDescriptor descriptor, Map<String, Object> values) {
107             final Object currentBean = state.getCurrentBean();
108             if (currentBean instanceof IPSecurityManagerImpl) {
109                 final IPSecurityManagerImpl ipSecMan = (IPSecurityManagerImpl) currentBean;
110                 for (Object o : values.values()) {
111                     if (o instanceof Rule) {
112                         final Rule rule = (Rule) o;
113                         ipSecMan.addRule(rule.getIP(), rule);
114                     }
115                 }
116             }
117             super.setProperty(state, descriptor, values);
118         }
119 
120         protected TypeDescriptor onResolveType(TransformationState state,
121                 TypeDescriptor resolvedType) {
122             if (state.getLevel() == 2 && resolvedType == null) {
123                 return this.getTypeMapping().getTypeDescriptor(Rule.class);
124             }
125             return super.onResolveType(state, resolvedType);
126         }
127 
128     }
129 
130     public static final class Rule {
131         private String name;
132         private String ip;
133         private Set<String> methods;
134 
135         public Rule() {
136             this.methods = Collections.emptySet();
137         }
138 
139         public String getName() {
140             return name;
141         }
142 
143         public void setName(String name) {
144             this.name = name;
145         }
146 
147         public String getIP() {
148             return ip;
149         }
150 
151         public void setIP(String ip) {
152             this.ip = ip;
153         }
154 
155         public boolean allowsMethod(String s) {
156             return methods.contains(s);
157         }
158 
159         public String getMethods() {
160             throw new IllegalStateException("Just faking a getter for content2bean's sake.");
161         }
162 
163         public void setMethods(String methods) {
164             this.methods = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
165             this.methods.addAll(Arrays.asList(methods.split(",")));
166         }
167     }
168 
169 }