1 37 38 package com.sun.j2ee.blueprints.signon.web; 39 40 import org.xml.sax.InputSource ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.NodeList ; 44 import org.w3c.dom.Node ; 45 46 import org.xml.sax.SAXException ; 47 import org.xml.sax.SAXParseException ; 48 import org.xml.sax.SAXException ; 49 50 51 import javax.xml.parsers.DocumentBuilderFactory ; 53 import javax.xml.parsers.DocumentBuilder ; 54 55 import java.net.URL ; 56 import java.util.ArrayList ; 57 import java.util.HashMap ; 58 59 60 65 66 public class ConfigFileSignOnDAO { 67 68 public static final String SIGNON_FORM_LOGIN_PAGE = "signon-form-login-page"; 70 public static final String SIGNON_FORM_ERROR_PAGE = "signon-form-error-page"; 71 public static final String SECURITY_CONSTRAINT = "security-constraint"; 72 public static final String WEB_RESOURCE_COLLECTION = "web-resource-collection"; 73 public static final String WEB_RESOURCE_NAME = "web-resource-name"; 74 public static final String URL_PATTERN = "url-pattern"; 75 public static final String AUTH_CONSTRAINT = "auth-constraint"; 76 public static final String ROLE_NAME = "role-name"; 77 78 private String signOnLoginPage = null; 79 private String signOnErrorPage = null; 80 private HashMap protectedResources = null; 81 82 public ConfigFileSignOnDAO (URL configURL) { 83 Element root = loadDocument (configURL); 84 protectedResources = getProtectedResources(root); 85 } 86 87 public String getSignOnPage() { 88 return signOnLoginPage; 89 } 90 91 public String getSignOnErrorPage() { 92 return signOnErrorPage; 93 } 94 95 public HashMap getProtectedResources() { 96 return protectedResources; 97 } 98 99 private Element loadDocument(URL url) { 100 Document doc = null; 101 try { 102 InputSource xmlInp = new InputSource (url.openStream()); 103 104 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 105 DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); 106 doc = parser.parse(xmlInp); 107 Element root = doc.getDocumentElement(); 108 root.normalize(); 109 return root; 110 } catch (SAXParseException err) { 111 System.err.println ("ConfigFileSignOnDAO ** Parsing error" + ", line " + 112 err.getLineNumber () + ", uri " + err.getSystemId ()); 113 System.err.println("ConfigFileSignOnDAO error: " + err.getMessage ()); 114 } catch (SAXException e) { 115 System.err.println("ConfigFileSignOnDAO error: " + e); 116 } catch (java.net.MalformedURLException mfx) { 117 System.err.println("ConfigFileSignOnDAO error: " + mfx); 118 } catch (java.io.IOException e) { 119 System.err.println("ConfigFileSignOnDAO error: " + e); 120 } catch (Exception pce) { 121 System.err.println("ConfigFileSignOnDAO error: " + pce); 122 } 123 return null; 124 } 125 126 private HashMap getProtectedResources(Element root) { 127 HashMap resources = new HashMap (); 128 signOnLoginPage = getTagValue(root, SIGNON_FORM_LOGIN_PAGE ).trim(); 130 signOnErrorPage = getTagValue(root, SIGNON_FORM_ERROR_PAGE ).trim(); 131 132 NodeList outterList = root.getElementsByTagName(SECURITY_CONSTRAINT); 134 for (int outterLoop = 0; outterLoop < outterList.getLength(); outterLoop++) { 135 Element element = (Element )outterList.item(outterLoop); 136 ArrayList roles = new ArrayList (); 138 NodeList roleList = element.getElementsByTagName(AUTH_CONSTRAINT); 139 for (int roleLoop = 0; (roleList != null) && roleLoop < roleList.getLength(); roleLoop++) { 140 Node roleNode = roleList.item(roleLoop); 141 String roleName = getSubTagValue(roleNode, ROLE_NAME); 142 if ((roleName != null) && !roleName.equals("")) roles.add(roleName); 143 } 144 145 NodeList list = element.getElementsByTagName(WEB_RESOURCE_COLLECTION); 146 for (int loop = 0; (list != null) && loop < list.getLength(); loop++) { 147 Node node = list.item(loop); 148 if (node != null) { 149 String resourceName = getSubTagValue(node, WEB_RESOURCE_NAME); 150 String urlPattern = getSubTagValue(node, URL_PATTERN); 151 ProtectedResource resource = new ProtectedResource(resourceName, urlPattern, roles); 152 if (!resources.containsKey(resourceName)) { 153 resources.put(resourceName, resource); 154 } else { 155 System.err.println("*** Non Fatal errror: Protected Resource " + resourceName + 156 " defined more than once in screen definitions file"); 157 } 158 } 159 } 160 } 161 return resources; 162 } 163 private String getSubTagAttribute(Element root, String tagName, String subTagName, String attribute) { 164 String returnString = ""; 165 NodeList list = root.getElementsByTagName(tagName); 166 for (int loop = 0; (list != null) && loop < list.getLength(); loop++) { 167 Node node = list.item(loop); 168 if (node != null) { 169 NodeList children = node.getChildNodes(); 170 for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) { 171 Node child = children.item(innerLoop); 172 if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) { 173 if (child instanceof Element ) { 174 return ((Element )child).getAttribute(attribute); 175 } 176 } 177 } } 179 } 180 return returnString; 181 } 182 183 private String getSubTagValue(Node node, String subTagName) { 184 String returnString = ""; 185 if (node != null) { 186 NodeList children = node.getChildNodes(); 187 for (int innerLoop =0; (children != null) && innerLoop < children.getLength(); innerLoop++) { 188 Node child = children.item(innerLoop); 189 if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) { 190 Node grandChild = child.getFirstChild(); 191 if (grandChild.getNodeValue() != null) return grandChild.getNodeValue(); 192 } 193 } } 195 return returnString; 196 } 197 198 private String getSubTagValue(Element root, String tagName, String subTagName) { 199 String returnString = ""; 200 NodeList list = root.getElementsByTagName(tagName); 201 for (int loop = 0; (list != null) && loop < list.getLength(); loop++) { 202 Node node = list.item(loop); 203 if (node != null) { 204 NodeList children = node.getChildNodes(); 205 for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) { 206 Node child = children.item(innerLoop); 207 if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) { 208 Node grandChild = child.getFirstChild(); 209 if (grandChild.getNodeValue() != null) return grandChild.getNodeValue(); 210 } 211 } } 213 } 214 return returnString; 215 } 216 217 private String getTagValue(Element root, String tagName) { 218 String returnString = ""; 219 NodeList list = root.getElementsByTagName(tagName); 220 for (int loop = 0; (list != null) && loop < list.getLength(); loop++) { 221 Node node = list.item(loop); 222 if (node != null) { 223 Node child = node.getFirstChild(); 224 if ((child != null) && child.getNodeValue() != null) return child.getNodeValue(); 225 } 226 } 227 return returnString; 228 } 229 } 230 231 232 | Popular Tags |