1 17 18 19 20 package org.apache.lenya.ac.impl; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.apache.cocoon.environment.Request; 28 import org.apache.cocoon.sitemap.PatternException; 29 import org.apache.lenya.ac.AccessControlException; 30 import org.apache.regexp.RE; 31 import org.apache.regexp.RECompiler; 32 import org.apache.regexp.REProgram; 33 import org.apache.regexp.RESyntaxException; 34 35 38 public class BypassableAccessController extends DefaultAccessController { 39 40 43 public BypassableAccessController() { 44 } 45 46 private List publicMatchers = new ArrayList (); 47 48 51 public void configure(Configuration conf) throws ConfigurationException { 52 super.configure(conf); 53 54 getLogger().debug("Configuring bypass patterns"); 55 56 Configuration[] publics = conf.getChildren("public"); 57 58 for (int i = 0; i < publics.length; i++) { 59 String publicHref = publics[i].getValue(null); 60 61 try { 62 publicMatchers.add(preparePattern(publicHref)); 63 } catch (PatternException pe) { 64 throw new ConfigurationException("invalid pattern for public hrefs", pe); 65 } 66 67 if (getLogger().isDebugEnabled()) { 68 getLogger().debug("CONFIGURATION: public: " + publicHref); 69 } 70 } 71 72 } 73 74 80 protected REProgram preparePattern(String pattern) 81 throws PatternException { 82 if (pattern == null) { 83 throw new PatternException("null passed as a pattern", null); 84 } 85 86 if (pattern.length() == 0) { 87 pattern = "^$"; 88 89 if (getLogger().isWarnEnabled()) { 90 getLogger().warn("The empty pattern string was rewritten to '^$'" + 91 " to match for empty strings. If you intended" + 92 " to match all strings, please change your" + " pattern to '.*'"); 93 } 94 } 95 96 try { 97 RECompiler compiler = new RECompiler(); 98 REProgram program = compiler.compile(pattern); 99 100 return program; 101 } catch (RESyntaxException rse) { 102 getLogger().debug("Failed to compile the pattern '" + pattern + "'", rse); 103 throw new PatternException(rse.getMessage(), rse); 104 } 105 } 106 107 113 protected boolean preparedMatch(REProgram preparedPattern, String match) { 114 boolean result = false; 115 116 if (match != null) { 117 RE re = new RE(preparedPattern); 118 result = re.match(match); 119 } 120 return result; 121 } 122 123 126 public boolean authorize(Request request) 127 throws AccessControlException { 128 129 assert request != null; 130 131 boolean authorized = false; 132 133 String uri = request.getRequestURI(); 134 String context = request.getContextPath(); 135 if (context == null) { 136 context = ""; 137 } 138 uri = uri.substring(context.length()); 139 140 int i = 0; 142 while (!authorized && i < publicMatchers.size()) { 143 getLogger().debug("Trying pattern: [" + publicMatchers.get(i) + "] with URL [" + uri + "]"); 144 if (preparedMatch((REProgram) publicMatchers.get(i), uri)) { 145 if (getLogger().isDebugEnabled()) { 146 getLogger().debug("Permission granted for free: [" + uri + "]"); 147 } 148 authorized = true; 149 } 150 i++; 151 } 152 153 if (!authorized) { 154 authorized = super.authorize(request); 155 } 156 157 return authorized; 158 } 159 160 } 161 | Popular Tags |