001: package com.technoetic.xplanner.security.config;
002:
003: import org.apache.commons.digester.Digester;
004: import org.xml.sax.SAXException;
005:
006: import javax.servlet.http.HttpServletRequest;
007: import java.io.FileInputStream;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.ArrayList;
011: import java.util.Iterator;
012:
013: public class SecurityConfiguration {
014: private ArrayList securityConstraints = new ArrayList();
015: private ArrayList securityRoles = new ArrayList();
016: private WebResourceCollection securityBypass;
017:
018: public void addSecurityConstraint(SecurityConstraint constraint) {
019: securityConstraints.add(constraint);
020: }
021:
022: public void addSecurityRole(SecurityRole securityRole) {
023: securityRoles.add(securityRole);
024: }
025:
026: public ArrayList getSecurityConstraints() {
027: return securityConstraints;
028: }
029:
030: public WebResourceCollection getSecurityBypass() {
031: return securityBypass;
032: }
033:
034: public void setSecurityBypass(WebResourceCollection securityBypass) {
035: this .securityBypass = securityBypass;
036: }
037:
038: public boolean isAuthorized(HttpServletRequest request) {
039: if (!isSecureRequest(request)) {
040: return true;
041: } else {
042: Iterator securityConstraints = getSecurityConstraints()
043: .iterator();
044: while (securityConstraints.hasNext()) {
045: SecurityConstraint securityConstraint = (SecurityConstraint) securityConstraints
046: .next();
047: if (securityConstraint.isApplicable(request)) {
048: return securityConstraint.isAuthorized(request);
049: }
050: }
051: return true;
052: }
053: }
054:
055: public boolean isSecureRequest(HttpServletRequest request) {
056: return !(securityBypass != null && securityBypass
057: .matches(request));
058: }
059:
060: public ArrayList getSecurityRoles() {
061: return securityRoles;
062: }
063:
064: public static SecurityConfiguration load(String filename)
065: throws SAXException, IOException {
066: return load(new FileInputStream(filename));
067: }
068:
069: public static SecurityConfiguration load(InputStream in)
070: throws SAXException, IOException {
071: Digester digester = new Digester();
072: digester.setValidating(false);
073: digester.setClassLoader(SecurityConfiguration.class
074: .getClassLoader());
075:
076: digester.addObjectCreate("security",
077: SecurityConfiguration.class);
078:
079: digester.addObjectCreate("security/security-constraint",
080: SecurityConstraint.class);
081: digester.addBeanPropertySetter(
082: "security/security-constraint/display-name",
083: "displayName");
084: digester.addSetNext("security/security-constraint",
085: "addSecurityConstraint");
086:
087: digester.addObjectCreate("security/security-bypass",
088: WebResourceCollection.class);
089: digester.addCallMethod("security/security-bypass/url-pattern",
090: "addUrlPattern", 0);
091: digester.addSetNext("security/security-bypass",
092: "setSecurityBypass");
093:
094: digester.addObjectCreate(
095: "security/security-constraint/web-resource-collection",
096: WebResourceCollection.class);
097: digester
098: .addBeanPropertySetter(
099: "security/security-constraint/web-resource-collection/web-resource-name",
100: "name");
101: digester
102: .addCallMethod(
103: "security/security-constraint/web-resource-collection/url-pattern",
104: "addUrlPattern", 0);
105: digester.addSetNext(
106: "security/security-constraint/web-resource-collection",
107: "addWebResourceCollection");
108:
109: digester.addObjectCreate(
110: "security/security-constraint/auth-constraint",
111: AuthConstraint.class);
112: digester
113: .addCallMethod(
114: "security/security-constraint/auth-constraint/role-name",
115: "addRoleName", 0);
116: digester.addSetNext(
117: "security/security-constraint/auth-constraint",
118: "addAuthConstraint");
119:
120: digester.addObjectCreate("security/security-role",
121: SecurityRole.class);
122: digester.addBeanPropertySetter(
123: "security/security-role/role-name", "name");
124: digester
125: .addSetNext("security/security-role", "addSecurityRole");
126:
127: return (SecurityConfiguration) digester.parse(in);
128: }
129: }
|