View Javadoc

1   /* Copyright 2004 Acegi Technology Pty Limited
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  
16  package org.acegisecurity.intercept.web;
17  
18  import org.acegisecurity.ConfigAttributeDefinition;
19  import org.acegisecurity.ConfigAttributeEditor;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.springframework.util.StringUtils;
25  
26  import java.beans.PropertyEditorSupport;
27  
28  import java.io.BufferedReader;
29  import java.io.IOException;
30  import java.io.StringReader;
31  
32  
33  /***
34   * Property editor to assist with the setup of a {@link
35   * FilterInvocationDefinitionSource}.
36   * 
37   * <p>
38   * The class creates and populates a {@link
39   * RegExpBasedFilterInvocationDefinitionMap} or {@link
40   * PathBasedFilterInvocationDefinitionMap} (depending on the type of patterns
41   * presented).
42   * </p>
43   * 
44   * <P>
45   * By default the class treats presented patterns as regular expressions. If
46   * the keyword <code>PATTERN_TYPE_APACHE_ANT</code> is present (case
47   * sensitive), patterns will be treated as Apache Ant paths rather than
48   * regular expressions.
49   * </p>
50   *
51   * @author Ben Alex
52   * @version $Id: FilterInvocationDefinitionSourceEditor.java,v 1.3 2005/11/17 00:55:50 benalex Exp $
53   */
54  public class FilterInvocationDefinitionSourceEditor
55      extends PropertyEditorSupport {
56      //~ Static fields/initializers =============================================
57  
58      private static final Log logger = LogFactory.getLog(FilterInvocationDefinitionSourceEditor.class);
59  
60      //~ Methods ================================================================
61  
62      public void setAsText(String s) throws IllegalArgumentException {
63          FilterInvocationDefinitionMap source = new RegExpBasedFilterInvocationDefinitionMap();
64  
65          if ((s == null) || "".equals(s)) {
66              // Leave target object empty
67          } else {
68              // Check if we need to override the default definition map
69              if (s.lastIndexOf("PATTERN_TYPE_APACHE_ANT") != -1) {
70                  source = new PathBasedFilterInvocationDefinitionMap();
71  
72                  if (logger.isDebugEnabled()) {
73                      logger.debug(("Detected PATTERN_TYPE_APACHE_ANT directive; using Apache Ant style path expressions"));
74                  }
75              }
76  
77              BufferedReader br = new BufferedReader(new StringReader(s));
78              int counter = 0;
79              String line;
80  
81              while (true) {
82                  counter++;
83  
84                  try {
85                      line = br.readLine();
86                  } catch (IOException ioe) {
87                      throw new IllegalArgumentException(ioe.getMessage());
88                  }
89  
90                  if (line == null) {
91                      break;
92                  }
93  
94                  line = line.trim();
95  
96                  if (logger.isDebugEnabled()) {
97                      logger.debug("Line " + counter + ": " + line);
98                  }
99  
100                 if (line.startsWith("//")) {
101                     continue;
102                 }
103 
104                 if (line.equals("CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON")) {
105                     if (logger.isDebugEnabled()) {
106                         logger.debug("Line " + counter
107                             + ": Instructing mapper to convert URLs to lowercase before comparison");
108                     }
109 
110                     source.setConvertUrlToLowercaseBeforeComparison(true);
111 
112                     continue;
113                 }
114 
115                 if (line.lastIndexOf('=') == -1) {
116                     continue;
117                 }
118 
119                 // Tokenize the line into its name/value tokens
120                 String[] nameValue = StringUtils.delimitedListToStringArray(line,
121                         "=");
122                 String name = nameValue[0];
123                 String value = nameValue[1];
124 
125                 // Convert value to series of security configuration attributes
126                 ConfigAttributeEditor configAttribEd = new ConfigAttributeEditor();
127                 configAttribEd.setAsText(value);
128 
129                 ConfigAttributeDefinition attr = (ConfigAttributeDefinition) configAttribEd
130                     .getValue();
131 
132                 // Register the regular expression and its attribute
133                 source.addSecureUrl(name, attr);
134             }
135         }
136 
137         setValue(source);
138     }
139 }