1 package com.atlassian.seraph.service; 2 3 import com.atlassian.seraph.util.PathMapper; 4 import com.atlassian.seraph.util.CachedPathMapper; 5 import com.atlassian.seraph.SecurityService; 6 import com.atlassian.seraph.config.SecurityConfig; 7 import com.opensymphony.util.ClassLoaderUtil; 8 import org.apache.log4j.Category; 9 import org.apache.commons.collections.LRUMap; 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.NodeList ; 12 import webwork.config.Configuration; 13 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.xml.parsers.DocumentBuilderFactory ; 16 import java.net.URL ; 17 import java.util.*; 18 19 37 public class WebworkService implements SecurityService 38 { 39 private static final Category log = Category.getInstance(WebworkService.class); 40 private final String ROLES_REQUIRED_ATTR = "roles-required"; 41 42 43 private PathMapper actionMapper = new CachedPathMapper(new LRUMap(500), new LRUMap(10)); 48 49 private Map rolesMap = new HashMap(); 51 52 private String extension = "action"; 54 55 public void init(Map params, SecurityConfig config) 56 { 57 try 58 { 59 extension = (String ) params.get("action.extension"); 60 61 configureActionMapper(extension); 62 } 63 catch (Exception e) 64 { 65 e.printStackTrace(); 66 } 67 } 68 69 private void configureActionMapper(String extension) 70 { 71 try 72 { 73 String actionResourcePath = (String ) Configuration.get("webwork.configuration.xml"); 74 75 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 76 URL fileUrl = ClassLoaderUtil.getResource(actionResourcePath + ".xml", this.getClass()); 77 78 if (fileUrl == null) 79 fileUrl = ClassLoaderUtil.getResource("/" + actionResourcePath + ".xml", this.getClass()); 80 81 if (fileUrl == null) 82 throw new IllegalArgumentException ("No such XML file:/" + actionResourcePath + ".xml"); 83 84 org.w3c.dom.Document doc = factory.newDocumentBuilder().parse(fileUrl.toString()); 86 87 NodeList actions = doc.getElementsByTagName("action"); 89 90 String rootRolesRequired = overrideRoles(null, doc.getDocumentElement()); 91 92 for (int i = 0; i < actions.getLength(); i++) 94 { 95 Element action = (Element ) actions.item(i); 96 String actionName = action.getAttribute("name"); 97 String actionAlias = action.getAttribute("alias"); 98 final String actionRolesRequired = overrideRoles(rootRolesRequired, action); 99 100 if (actionRolesRequired != null) 101 { 102 103 if (actionAlias != null) 104 { 105 actionMapper.put(actionAlias, "/" + actionAlias + "." + extension); 106 rolesMap.put(actionAlias, actionRolesRequired); 107 actionMapper.put(actionAlias + "!*", "/" + actionAlias + "!*." + extension); 108 rolesMap.put(actionAlias + "!*", actionRolesRequired); 109 } 110 111 if (actionName != null) 112 { 113 actionMapper.put(actionName, "/" + actionName + "." + extension); 114 rolesMap.put(actionName, actionRolesRequired); 115 actionMapper.put(actionName + "!*", "/" + actionName + "!*." + extension); 116 rolesMap.put(actionName + "!*", actionRolesRequired); 117 } 118 } 119 120 NodeList commands = action.getElementsByTagName("command"); 122 for (int j = 0; j < commands.getLength(); j++) 123 { 124 Element command = (Element ) commands.item(j); 125 String cmdRolesRequired = overrideRoles(actionRolesRequired, command); 126 127 String commandAlias = command.getAttribute("alias"); 128 129 if (commandAlias != null && cmdRolesRequired != null) 130 { 131 actionMapper.put(commandAlias, "/" + commandAlias + "." + extension); 132 rolesMap.put(commandAlias, cmdRolesRequired); 133 } 134 } 135 } 136 } 137 catch (Exception ex) 138 { 139 log.error("Exception: " + ex, ex); 140 } 141 } 142 143 144 147 private String overrideRoles(String rolesRequired, Element action) 148 { 149 if (action.hasAttribute(ROLES_REQUIRED_ATTR)) 150 { 151 return action.getAttribute(ROLES_REQUIRED_ATTR); 152 } 153 else 154 { 155 return rolesRequired; 156 } 157 } 158 159 169 170 public void destroy() 171 { 172 } 173 174 public Set getRequiredRoles(HttpServletRequest request) 175 { 176 Set requiredRoles = new HashSet(); 177 178 String currentURL = request.getRequestURI(); 179 180 int lastSlash = currentURL.lastIndexOf('/'); 181 String targetURL; 182 183 if (lastSlash > -1) 185 { 186 targetURL = currentURL.substring(lastSlash); 187 } 188 else 189 { 190 targetURL = currentURL; 191 } 192 193 String actionMatch = actionMapper.get(targetURL); 194 195 if (actionMatch != null) 196 { 197 String rolesStr = (String ) rolesMap.get(actionMatch); 198 199 StringTokenizer st = new StringTokenizer(rolesStr, ", "); 200 while (st.hasMoreTokens()) 201 { 202 requiredRoles.add(st.nextToken()); 203 } 204 } 205 206 return requiredRoles; 207 } 208 } 209 | Popular Tags |