1 18 19 package org.apache.struts.config; 20 21 import java.io.Serializable ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.apache.commons.beanutils.BeanUtils; 32 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.util.WildcardHelper; 35 36 43 public class ActionConfigMatcher implements Serializable { 44 45 48 private static final Log log = 49 LogFactory.getLog(ActionConfigMatcher.class); 50 51 54 private static final WildcardHelper wildcard = new WildcardHelper(); 55 56 59 private List compiledPaths; 60 61 70 public ActionConfigMatcher(ActionConfig[] configs) { 71 compiledPaths = new ArrayList (); 72 int[] pattern; 73 String path; 74 for (int x = 0; x < configs.length; x++) { 75 path = configs[x].getPath(); 76 if (path != null && path.indexOf('*') > -1) { 77 if (path.length() > 0 && path.charAt(0) == '/') { 78 path = path.substring(1); 79 } 80 if (log.isDebugEnabled()) { 81 log.debug("Compiling action config path '" + path + "'"); 82 } 83 pattern = wildcard.compilePattern(path); 84 compiledPaths.add(new Mapping(pattern, configs[x])); 85 } 86 } 87 } 88 89 95 public ActionConfig match(String path) { 96 97 ActionConfig config = null; 98 if (compiledPaths.size() > 0) { 99 if (log.isDebugEnabled()) { 100 log.debug("Attempting to match '" + path 101 + "' to a wildcard pattern"); 102 } 103 if (path.length() > 0 && path.charAt(0) == '/') { 104 path = path.substring(1); 105 } 106 Mapping m; 107 HashMap vars = new HashMap (); 108 for (Iterator i = compiledPaths.iterator(); i.hasNext();) { 109 m = (Mapping) i.next(); 110 if (wildcard.match(vars, path, m.getPattern())) { 111 config = convertActionConfig( 112 path, 113 (ActionConfig) m.getActionConfig(), 114 vars); 115 } 116 } 117 } 118 119 return config; 120 } 121 122 132 protected ActionConfig convertActionConfig(String path, 133 ActionConfig orig, Map vars) { 134 ActionConfig config = null; 135 136 try { 137 config = (ActionConfig) BeanUtils.cloneBean(orig); 138 } 139 catch (Exception ex) { 140 log.warn("Unable to clone action config, recommend not using " 141 + "wildcards", ex); 142 return null; 143 } 144 145 config.setName(convertParam(orig.getName(), vars)); 146 if (path.length() == 0 || path.charAt(0) != '/') { 147 path = "/" + path; 148 } 149 config.setPath(path); 150 config.setType(convertParam(orig.getType(), vars)); 151 config.setRoles(convertParam(orig.getRoles(), vars)); 152 config.setParameter(convertParam(orig.getParameter(), vars)); 153 config.setAttribute(convertParam(orig.getAttribute(), vars)); 154 config.setForward(convertParam(orig.getForward(), vars)); 155 config.setInclude(convertParam(orig.getInclude(), vars)); 156 config.setInput(convertParam(orig.getInput(), vars)); 157 158 ForwardConfig[] fConfigs = orig.findForwardConfigs(); 159 ForwardConfig cfg; 160 for (int x = 0; x < fConfigs.length; x++) { 161 cfg = new ActionForward(); 162 cfg.setContextRelative(fConfigs[x].getContextRelative()); 163 cfg.setName(fConfigs[x].getName()); 164 cfg.setPath(convertParam(fConfigs[x].getPath(), vars)); 165 cfg.setRedirect(fConfigs[x].getRedirect()); 166 config.removeForwardConfig(fConfigs[x]); 167 config.addForwardConfig(cfg); 168 } 169 170 ExceptionConfig[] exConfigs = orig.findExceptionConfigs(); 171 for (int x = 0; x < exConfigs.length; x++) { 172 config.addExceptionConfig(exConfigs[x]); 173 } 174 175 config.freeze(); 176 177 return config; 178 } 179 180 187 protected String convertParam(String val, Map vars) { 188 if (val == null) { 189 return null; 190 } else if (val.indexOf("{") == -1) { 191 return val; 192 } 193 194 Map.Entry entry; 195 StringBuffer key = new StringBuffer ("{0}"); 196 StringBuffer ret = new StringBuffer (val); 197 String keyTmp; 198 int x; 199 for (Iterator i = vars.entrySet().iterator(); i.hasNext();) { 200 entry = (Map.Entry ) i.next(); 201 key.setCharAt(1, ((String ) entry.getKey()).charAt(0)); 202 keyTmp = key.toString(); 203 204 while ((x = ret.toString().indexOf(keyTmp)) > -1) { 206 ret.replace(x, x + 3, (String ) entry.getValue()); 207 } 208 } 209 return ret.toString(); 210 } 211 212 215 private class Mapping implements Serializable { 216 217 218 private int[] pattern; 219 220 221 private ActionConfig config; 222 223 229 public Mapping(int[] pattern, ActionConfig config) { 230 this.pattern = pattern; 231 this.config = config; 232 } 233 234 239 public int[] getPattern() { 240 return this.pattern; 241 } 242 243 248 public ActionConfig getActionConfig() { 249 return this.config; 250 } 251 } 252 } 253 254 | Popular Tags |