1 22 23 24 package org.jboss.web.rewrite; 25 26 import java.util.ArrayList ; 27 import java.util.Map ; 28 import java.util.regex.Matcher ; 29 30 public class Substitution { 31 32 public abstract class SubstitutionElement { 33 public abstract String evaluate(Matcher rule, Matcher cond, Resolver resolver); 34 } 35 36 public class StaticElement extends SubstitutionElement { 37 public String value; 38 39 public String evaluate 40 (Matcher rule, Matcher cond, Resolver resolver) { 41 return value; 42 } 43 44 } 45 46 public class RewriteRuleBackReferenceElement extends SubstitutionElement { 47 public int n; 48 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 49 return rule.group(n); 50 } 51 } 52 53 public class RewriteCondBackReferenceElement extends SubstitutionElement { 54 public int n; 55 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 56 return cond.group(n); 57 } 58 } 59 60 public class ServerVariableElement extends SubstitutionElement { 61 public String key; 62 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 63 return resolver.resolve(key); 64 } 65 } 66 67 public class ServerVariableEnvElement extends SubstitutionElement { 68 public String key; 69 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 70 return resolver.resolveEnv(key); 71 } 72 } 73 74 public class ServerVariableSslElement extends SubstitutionElement { 75 public String key; 76 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 77 return resolver.resolveSsl(key); 78 } 79 } 80 81 public class ServerVariableHttpElement extends SubstitutionElement { 82 public String key; 83 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 84 return resolver.resolveHttp(key); 85 } 86 } 87 88 public class MapElement extends SubstitutionElement { 89 public RewriteMap map = null; 90 public String key; 91 public String defaultValue = null; 92 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 93 String result = map.lookup(key); 94 if (result == null) { 95 result = defaultValue; 96 } 97 return result; 98 } 99 } 100 101 protected SubstitutionElement[] elements = null; 102 103 protected String sub = null; 104 public String getSub() { return sub; } 105 public void setSub(String sub) { this.sub = sub; } 106 107 public void parse(Map maps) { 108 109 ArrayList elements = new ArrayList (); 110 int pos = 0; 111 int percentPos = 0; 112 int dollarPos = 0; 113 114 while (pos < sub.length()) { 115 percentPos = sub.indexOf('%', pos); 116 dollarPos = sub.indexOf('$', pos); 117 if (percentPos == -1 && dollarPos == -1) { 119 StaticElement newElement = new StaticElement(); 121 newElement.value = sub.substring(pos, sub.length()); 122 pos = sub.length(); 123 elements.add(newElement); 124 } else if (percentPos == -1 || ((dollarPos != -1) && (dollarPos < percentPos))) { 125 if (dollarPos + 1 == sub.length()) { 127 throw new IllegalArgumentException (sub); 128 } 129 if (pos < dollarPos) { 130 StaticElement newElement = new StaticElement(); 132 newElement.value = sub.substring(pos, dollarPos); 133 pos = dollarPos; 134 elements.add(newElement); 135 } 136 if (Character.isDigit(sub.charAt(dollarPos + 1))) { 137 RewriteRuleBackReferenceElement newElement = new RewriteRuleBackReferenceElement(); 139 newElement.n = Character.digit(sub.charAt(dollarPos + 1), 10); 140 pos = dollarPos + 2; 141 elements.add(newElement); 142 } else { 143 MapElement newElement = new MapElement(); 145 int open = sub.indexOf('{', dollarPos); 146 int colon = sub.indexOf(':', dollarPos); 147 int def = sub.indexOf('|', dollarPos); 148 int close = sub.indexOf('}', dollarPos); 149 if (!(-1 < open && open < colon && colon < close)) { 150 throw new IllegalArgumentException (sub); 151 } 152 newElement.map = (RewriteMap) maps.get(sub.substring(open + 1, colon)); 153 if (newElement.map == null) { 154 throw new IllegalArgumentException (sub + ": No map: " + sub.substring(open + 1, colon)); 155 } 156 if (def > -1) { 157 if (!(colon < def && def < close)) { 158 throw new IllegalArgumentException (sub); 159 } 160 newElement.key = sub.substring(colon + 1, def); 161 newElement.defaultValue = sub.substring(def + 1, close); 162 } else { 163 newElement.key = sub.substring(colon + 1, close); 164 } 165 pos = close + 1; 166 elements.add(newElement); 167 } 168 } else { 169 if (percentPos + 1 == sub.length()) { 171 throw new IllegalArgumentException (sub); 172 } 173 if (pos < percentPos) { 174 StaticElement newElement = new StaticElement(); 176 newElement.value = sub.substring(pos, percentPos); 177 pos = percentPos; 178 elements.add(newElement); 179 } 180 if (Character.isDigit(sub.charAt(percentPos + 1))) { 181 RewriteCondBackReferenceElement newElement = new RewriteCondBackReferenceElement(); 183 newElement.n = Character.digit(sub.charAt(percentPos + 1), 10); 184 pos = percentPos + 2; 185 elements.add(newElement); 186 } else { 187 SubstitutionElement newElement = null; 189 int open = sub.indexOf('{', percentPos); 190 int colon = sub.indexOf(':', percentPos); 191 int close = sub.indexOf('}', percentPos); 192 if (!(-1 < open && open < close)) { 193 throw new IllegalArgumentException (sub); 194 } 195 if (colon > -1) { 196 if (!(open < colon && colon < close)) { 197 throw new IllegalArgumentException (sub); 198 } 199 String type = sub.substring(open + 1, colon); 200 if (type.equals("ENV")) { 201 newElement = new ServerVariableEnvElement(); 202 ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close); 203 } else if (type.equals("SSL")) { 204 newElement = new ServerVariableSslElement(); 205 ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close); 206 } else if (type.equals("HTTP")) { 207 newElement = new ServerVariableHttpElement(); 208 ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close); 209 } else { 210 throw new IllegalArgumentException (sub + ": Bad type: " + type); 211 } 212 } else { 213 newElement = new ServerVariableElement(); 214 ((ServerVariableElement) newElement).key = sub.substring(open + 1, close); 215 } 216 pos = close + 1; 217 elements.add(newElement); 218 } 219 } 220 } 221 222 this.elements = (SubstitutionElement[]) elements.toArray(new SubstitutionElement[0]); 223 224 } 225 226 229 348 349 356 public String evaluate(Matcher rule, Matcher cond, Resolver resolver) { 357 StringBuffer buf = new StringBuffer (); 358 for (int i = 0; i < elements.length; i++) { 359 buf.append(elements[i].evaluate(rule, cond, resolver)); 360 } 361 return buf.toString(); 362 } 363 364 } 365 | Popular Tags |