1 package com.atlassian.seraph.config; 2 3 import com.atlassian.seraph.auth.RoleMapper; 4 import com.atlassian.seraph.auth.Authenticator; 5 import com.atlassian.seraph.auth.AuthenticationContext; 6 import com.atlassian.seraph.auth.AuthenticationContextImpl; 7 import com.atlassian.seraph.SecurityService; 8 import com.atlassian.seraph.Initable; 9 import com.atlassian.seraph.util.XMLUtils; 10 import com.atlassian.seraph.controller.SecurityController; 11 import com.atlassian.seraph.interceptor.Interceptor; 12 import com.opensymphony.util.ClassLoaderUtil; 13 import org.apache.log4j.Category; 14 import org.w3c.dom.Element ; 15 import org.w3c.dom.Node ; 16 import org.w3c.dom.NodeList ; 17 18 import javax.xml.parsers.DocumentBuilderFactory ; 19 import java.io.Serializable ; 20 import java.net.URL ; 21 import java.util.*; 22 23 28 public class SecurityConfigImpl implements Serializable , SecurityConfig 29 { 30 private static final Category log = Category.getInstance(SecurityConfigImpl.class); 31 private static SecurityConfigImpl instance = null; 32 33 public static final String DEFAULT_CONFIG_LOCATION = "seraph-config.xml"; 34 35 private String configFileLocation = "seraph-config.xml"; 36 protected Authenticator authenticator = null; 37 protected RoleMapper roleMapper = null; 38 protected SecurityController controller; 39 protected List services = null; 40 protected List interceptors = null; 41 42 private String loginURL; 43 private String logoutURL; 44 private String originalURLKey = "seraph_originalurl"; 45 private String cookieEncoding; 46 private String loginCookieKey = "seraph.os.cookie"; 47 private String linkLoginURL; 48 49 public SecurityConfigImpl(String configFileLocation) throws ConfigurationException 50 { 51 if (configFileLocation != null) 52 { 53 this.configFileLocation = configFileLocation; 54 log.debug("Config file location passed. Location: " + this.configFileLocation); 55 } 56 else 57 { 58 log.debug("Initialising securityConfig using default configFile: " + this.configFileLocation); 59 } 60 61 init(); 62 } 63 64 private void init() throws ConfigurationException 65 { 66 services = new ArrayList(); 67 interceptors = new ArrayList(); 68 69 try 70 { 71 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 72 URL fileUrl = ClassLoaderUtil.getResource(configFileLocation, this.getClass()); 73 74 if (fileUrl == null) 75 throw new IllegalArgumentException ("No such XML file: " + configFileLocation); 76 77 org.w3c.dom.Document doc = factory.newDocumentBuilder().parse(fileUrl.toString()); 79 Element rootEl = doc.getDocumentElement(); 80 81 configureParameters(rootEl); 82 configureAuthenticator(rootEl); 83 configureController(rootEl); 84 configureRoleMapper(rootEl); 85 configureServices(rootEl); 86 configureInterceptors(rootEl); 87 } 88 catch (Exception e) 89 { 90 e.printStackTrace(); 91 throw new ConfigurationException("Exception configuring from '"+configFileLocation+"': " + e); 92 } 93 } 94 95 protected void configureAuthenticator(Element rootEl) throws ConfigurationException 96 { 97 authenticator = (Authenticator) configureClass(rootEl, "authenticator"); 98 99 try 100 { 101 if (authenticator == null) 102 { 103 authenticator = (Authenticator) ClassLoaderUtil.loadClass(Authenticator.DEFAULT_AUTHENTICATOR, this.getClass()).newInstance(); 104 authenticator.init(new HashMap(), this); 105 } 106 } 107 catch (Exception e) 108 { 109 throw new ConfigurationException("Could not lookup class: " + Authenticator.DEFAULT_AUTHENTICATOR + ":" + e); 110 } 111 } 112 113 protected void configureController(Element rootEl) throws ConfigurationException 114 { 115 controller = (SecurityController) configureClass(rootEl, "controller"); 116 117 try 118 { 119 if (controller == null) 120 controller = (SecurityController) ClassLoaderUtil.loadClass(SecurityController.NULL_CONTROLLER, this.getClass()).newInstance(); 121 } 122 catch (Exception e) 123 { 124 throw new ConfigurationException("Could not lookup class: " + SecurityController.NULL_CONTROLLER + ":" + e); 125 } 126 } 127 128 protected void configureRoleMapper(Element rootEl) throws ConfigurationException 129 { 130 roleMapper = (RoleMapper) configureClass(rootEl, "rolemapper"); 131 } 132 133 private Initable configureClass(Element rootEl, String tagname) throws ConfigurationException 134 { 135 try 136 { 137 NodeList elementList = rootEl.getElementsByTagName(tagname); 138 139 for (int i = 0; i < elementList.getLength(); i++) 140 { 141 Element authEl = (Element ) elementList.item(i); 142 String clazz = authEl.getAttribute("class"); 143 144 Initable initable = (Initable) ClassLoaderUtil.loadClass(clazz, this.getClass()).newInstance(); 145 146 Map params = getInitParameters(authEl); 147 148 initable.init(params, this); 149 return initable; 150 } 151 } 152 catch (Exception e) 153 { 154 throw new ConfigurationException("Could not create: " + tagname + ": " + e); 155 } 156 157 return null; 158 } 159 160 private void configureParameters(Element rootEl) 161 { 162 NodeList nl = rootEl.getElementsByTagName("parameters"); 163 Element parametersEl = (Element ) nl.item(0); 164 Map globalParams = getInitParameters(parametersEl); 165 166 loginURL = (String ) globalParams.get("login.url"); 167 linkLoginURL = (String ) globalParams.get("link.login.url"); 168 logoutURL = (String ) globalParams.get("logout.url"); 169 170 if (globalParams.get("original.url.key") != null) 171 originalURLKey = (String ) globalParams.get("original.url.key"); 172 173 if (globalParams.get("cookie.encoding") != null) 174 cookieEncoding = (String ) globalParams.get("cookie.encoding"); 175 176 if (globalParams.get("login.cookie.key") != null) 177 loginCookieKey = (String ) globalParams.get("login.cookie.key"); 178 } 179 180 private void configureServices(Element rootEl) throws ConfigurationException 181 { 182 NodeList nl = rootEl.getElementsByTagName("services"); 183 184 if (nl != null && nl.getLength() > 0) 185 { 186 Element servicesEl = (Element ) nl.item(0); 187 NodeList serviceList = servicesEl.getElementsByTagName("service"); 188 189 for (int i = 0; i < serviceList.getLength(); i++) 190 { 191 Element serviceEl = (Element ) serviceList.item(i); 192 String serviceClazz = serviceEl.getAttribute("class"); 193 194 if (serviceClazz == null || "".equals(serviceClazz)) 195 throw new ConfigurationException("Service element with bad class attribute"); 196 197 try 198 { 199 log.debug("Adding seraph service of class: " + serviceClazz); 200 SecurityService service = (SecurityService) ClassLoaderUtil.loadClass(serviceClazz, this.getClass()).newInstance(); 201 202 Map serviceParams = getInitParameters(serviceEl); 203 204 service.init(serviceParams, this); 205 206 services.add(service); 207 } 208 catch (Exception e) 209 { 210 e.printStackTrace(); 211 throw new ConfigurationException("Could not getRequest service: " + serviceClazz + ". Exception: " + e); 212 } 213 } 214 } 215 } 216 217 protected void configureInterceptors(Element rootEl) throws ConfigurationException 218 { 219 NodeList nl = rootEl.getElementsByTagName("interceptors"); 220 221 if (nl != null && nl.getLength() > 0) 222 { 223 Element interceptorsEl = (Element ) nl.item(0); 224 NodeList interceptorList = interceptorsEl.getElementsByTagName("interceptor"); 225 226 for (int i = 0; i < interceptorList.getLength(); i++) 227 { 228 Element interceptorEl = (Element ) interceptorList.item(i); 229 String interceptorClazz = interceptorEl.getAttribute("class"); 230 231 if (interceptorClazz == null || "".equals(interceptorClazz)) 232 233 throw new ConfigurationException("Interceptor element with bad class attribute"); 234 235 try 236 { 237 log.debug("Adding interceptor of class: " + interceptorClazz); 238 Interceptor interceptor = (Interceptor) ClassLoaderUtil.loadClass(interceptorClazz, this.getClass()).newInstance(); 239 240 Map interceptorParams = getInitParameters(interceptorEl); 241 242 interceptor.init(interceptorParams, this); 243 244 interceptors.add(interceptor); 245 } 246 catch (Exception e) 247 { 248 e.printStackTrace(); 249 throw new ConfigurationException("Could not getRequest service: " + interceptorClazz + ". Exception: " + e); 250 } 251 } 252 } 253 } 254 255 protected Map getInitParameters(Element el) 256 { 257 Map params = new HashMap(); 258 259 NodeList nl = el.getElementsByTagName("init-param"); 260 261 for (int i = 0; i < nl.getLength(); i++) 262 { 263 Node initParam = nl.item(i); 264 String paramName = XMLUtils.getContainedText(initParam, "param-name"); 265 String paramValue = XMLUtils.getContainedText(initParam, "param-value"); 266 params.put(paramName, paramValue); 267 } 268 269 return params; 270 } 271 272 public void destroy() 273 { 274 for (Iterator iterator = services.iterator(); iterator.hasNext();) 275 { 276 SecurityService securityService = (SecurityService) iterator.next(); 277 securityService.destroy(); 278 } 279 280 for (Iterator iterator = interceptors.iterator(); iterator.hasNext();) 281 { 282 ((Interceptor) iterator.next()).destroy(); 283 } 284 285 return; 286 } 287 288 public void addInterceptor(Interceptor interceptor) 289 { 290 interceptors.add(interceptor); 291 } 292 293 public List getServices() 294 { 295 return services; 296 } 297 298 public String getLoginURL() 299 { 300 return loginURL; 301 } 302 303 public String getLinkLoginURL() 304 { 305 return linkLoginURL; 306 } 307 308 public String getLogoutURL() 309 { 310 return logoutURL; 311 } 312 313 public String getOriginalURLKey() 314 { 315 return originalURLKey; 316 } 317 318 public Authenticator getAuthenticator() 319 { 320 return authenticator; 321 } 322 323 public AuthenticationContext getAuthenticationContext() 324 { 325 return new AuthenticationContextImpl(); } 327 328 public SecurityController getController() 329 { 330 return controller; 331 } 332 333 336 public static SecurityConfigImpl getInstance(String configFileLocation) throws ConfigurationException 337 { 338 instance = new SecurityConfigImpl(configFileLocation); 339 return instance; 340 } 341 342 346 public static SecurityConfig getInstance() 347 { 348 if (instance == null) 349 { 350 try 351 { 352 if (instance == null) 353 { 354 instance = new SecurityConfigImpl(DEFAULT_CONFIG_LOCATION); 355 } 356 } 357 catch (ConfigurationException e) 358 { 359 log.error("Could not configure SecurityConfigImpl instance: " + e, e); 360 } 361 } 362 363 return instance; 364 } 365 366 public RoleMapper getRoleMapper() 367 { 368 return roleMapper; 369 } 370 371 public List getInterceptors(Class desiredInterceptorClass) 372 { 373 List result = new ArrayList(); 374 375 for (Iterator iterator = interceptors.iterator(); iterator.hasNext();) 376 { 377 Interceptor interceptor = (Interceptor) iterator.next(); 378 379 if (desiredInterceptorClass.isAssignableFrom(interceptor.getClass())) 380 { 381 result.add(interceptor); 382 } 383 } 384 385 return result; 386 } 387 388 public String getCookieEncoding() 389 { 390 return cookieEncoding; 391 } 392 393 public String getLoginCookieKey() 394 { 395 return loginCookieKey; 396 } 397 398 } 399 | Popular Tags |