KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > signon > web > ConfigFileSignOnDAO


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37
38 package com.sun.j2ee.blueprints.signon.web;
39
40 import org.xml.sax.InputSource JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45
46 import org.xml.sax.SAXException JavaDoc;
47 import org.xml.sax.SAXParseException JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49
50
51 // jaxp 1.0.1 imports
52
import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
53 import javax.xml.parsers.DocumentBuilder JavaDoc;
54
55 import java.net.URL JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.HashMap JavaDoc;
58
59
60 /**
61  * This class provides the data bindings for the screendefinitions.xml
62  * and the requestmappings.xml file.
63  * The data obtained is maintained by the ScreenFlowManager
64  */

65
66 public class ConfigFileSignOnDAO {
67
68     // xml tag constants
69
public static final String JavaDoc SIGNON_FORM_LOGIN_PAGE = "signon-form-login-page";
70     public static final String JavaDoc SIGNON_FORM_ERROR_PAGE = "signon-form-error-page";
71     public static final String JavaDoc SECURITY_CONSTRAINT = "security-constraint";
72     public static final String JavaDoc WEB_RESOURCE_COLLECTION = "web-resource-collection";
73     public static final String JavaDoc WEB_RESOURCE_NAME = "web-resource-name";
74     public static final String JavaDoc URL_PATTERN = "url-pattern";
75     public static final String JavaDoc AUTH_CONSTRAINT = "auth-constraint";
76     public static final String JavaDoc ROLE_NAME = "role-name";
77
78     private String JavaDoc signOnLoginPage = null;
79     private String JavaDoc signOnErrorPage = null;
80     private HashMap JavaDoc protectedResources = null;
81
82     public ConfigFileSignOnDAO (URL JavaDoc configURL) {
83         Element JavaDoc root = loadDocument (configURL);
84         protectedResources = getProtectedResources(root);
85     }
86
87     public String JavaDoc getSignOnPage() {
88         return signOnLoginPage;
89     }
90
91     public String JavaDoc getSignOnErrorPage() {
92         return signOnErrorPage;
93     }
94
95     public HashMap JavaDoc getProtectedResources() {
96         return protectedResources;
97     }
98
99     private Element JavaDoc loadDocument(URL JavaDoc url) {
100         Document JavaDoc doc = null;
101         try {
102             InputSource JavaDoc xmlInp = new InputSource JavaDoc(url.openStream());
103
104             DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
105             DocumentBuilder JavaDoc parser = docBuilderFactory.newDocumentBuilder();
106             doc = parser.parse(xmlInp);
107             Element JavaDoc root = doc.getDocumentElement();
108             root.normalize();
109             return root;
110         } catch (SAXParseException JavaDoc 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 JavaDoc e) {
115             System.err.println("ConfigFileSignOnDAO error: " + e);
116         } catch (java.net.MalformedURLException JavaDoc mfx) {
117             System.err.println("ConfigFileSignOnDAO error: " + mfx);
118         } catch (java.io.IOException JavaDoc e) {
119             System.err.println("ConfigFileSignOnDAO error: " + e);
120         } catch (Exception JavaDoc pce) {
121             System.err.println("ConfigFileSignOnDAO error: " + pce);
122         }
123         return null;
124     }
125
126     private HashMap JavaDoc getProtectedResources(Element JavaDoc root) {
127         HashMap JavaDoc resources = new HashMap JavaDoc();
128         // get the signon page and signon error page
129
signOnLoginPage = getTagValue(root, SIGNON_FORM_LOGIN_PAGE ).trim();
130         signOnErrorPage = getTagValue(root, SIGNON_FORM_ERROR_PAGE ).trim();
131
132         // get protected pages //
133
NodeList JavaDoc outterList = root.getElementsByTagName(SECURITY_CONSTRAINT);
134         for (int outterLoop = 0; outterLoop < outterList.getLength(); outterLoop++) {
135             Element JavaDoc element = (Element JavaDoc)outterList.item(outterLoop);
136             // get roles that can see this page
137
ArrayList JavaDoc roles = new ArrayList JavaDoc();
138             NodeList JavaDoc roleList = element.getElementsByTagName(AUTH_CONSTRAINT);
139             for (int roleLoop = 0; (roleList != null) && roleLoop < roleList.getLength(); roleLoop++) {
140                         Node JavaDoc roleNode = roleList.item(roleLoop);
141                         String JavaDoc roleName = getSubTagValue(roleNode, ROLE_NAME);
142                         if ((roleName != null) && !roleName.equals("")) roles.add(roleName);
143             }
144
145             NodeList JavaDoc list = element.getElementsByTagName(WEB_RESOURCE_COLLECTION);
146             for (int loop = 0; (list != null) && loop < list.getLength(); loop++) {
147                 Node JavaDoc node = list.item(loop);
148                 if (node != null) {
149                     String JavaDoc resourceName = getSubTagValue(node, WEB_RESOURCE_NAME);
150                     String JavaDoc 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 JavaDoc getSubTagAttribute(Element JavaDoc root, String JavaDoc tagName, String JavaDoc subTagName, String JavaDoc attribute) {
164         String JavaDoc returnString = "";
165         NodeList JavaDoc list = root.getElementsByTagName(tagName);
166         for (int loop = 0; (list != null) && loop < list.getLength(); loop++) {
167             Node JavaDoc node = list.item(loop);
168             if (node != null) {
169                 NodeList JavaDoc children = node.getChildNodes();
170                 for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
171                     Node JavaDoc child = children.item(innerLoop);
172                     if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
173                         if (child instanceof Element JavaDoc) {
174                             return ((Element JavaDoc)child).getAttribute(attribute);
175                         }
176                     }
177                 } // end inner loop
178
}
179         }
180         return returnString;
181     }
182
183     private String JavaDoc getSubTagValue(Node JavaDoc node, String JavaDoc subTagName) {
184         String JavaDoc returnString = "";
185         if (node != null) {
186             NodeList JavaDoc children = node.getChildNodes();
187             for (int innerLoop =0; (children != null) && innerLoop < children.getLength(); innerLoop++) {
188                 Node JavaDoc child = children.item(innerLoop);
189                 if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
190                     Node JavaDoc grandChild = child.getFirstChild();
191                     if (grandChild.getNodeValue() != null) return grandChild.getNodeValue();
192                 }
193             } // end inner loop
194
}
195         return returnString;
196     }
197
198     private String JavaDoc getSubTagValue(Element JavaDoc root, String JavaDoc tagName, String JavaDoc subTagName) {
199         String JavaDoc returnString = "";
200         NodeList JavaDoc list = root.getElementsByTagName(tagName);
201         for (int loop = 0; (list != null) && loop < list.getLength(); loop++) {
202             Node JavaDoc node = list.item(loop);
203             if (node != null) {
204                 NodeList JavaDoc children = node.getChildNodes();
205                 for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) {
206                     Node JavaDoc child = children.item(innerLoop);
207                     if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) {
208                         Node JavaDoc grandChild = child.getFirstChild();
209                         if (grandChild.getNodeValue() != null) return grandChild.getNodeValue();
210                     }
211                 } // end inner loop
212
}
213         }
214         return returnString;
215     }
216
217     private String JavaDoc getTagValue(Element JavaDoc root, String JavaDoc tagName) {
218         String JavaDoc returnString = "";
219         NodeList JavaDoc list = root.getElementsByTagName(tagName);
220         for (int loop = 0; (list != null) && loop < list.getLength(); loop++) {
221             Node JavaDoc node = list.item(loop);
222             if (node != null) {
223                 Node JavaDoc 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