001: package de.schlund.pfixcore.util;
002:
003: import java.io.StringWriter;
004: import java.util.Arrays;
005: import java.util.HashSet;
006: import java.util.Map;
007:
008: import javax.xml.parsers.DocumentBuilder;
009: import javax.xml.parsers.DocumentBuilderFactory;
010: import javax.xml.transform.OutputKeys;
011: import javax.xml.transform.Transformer;
012: import javax.xml.transform.TransformerFactory;
013: import javax.xml.transform.dom.DOMSource;
014: import javax.xml.transform.stream.StreamResult;
015:
016: import org.apache.log4j.Logger;
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.Node;
020:
021: import de.schlund.pfixcore.auth.Authentication;
022: import de.schlund.pfixcore.auth.Role;
023: import de.schlund.pfixcore.generator.IWrapper;
024: import de.schlund.pfixcore.generator.IWrapperInfo;
025: import de.schlund.pfixcore.workflow.Context;
026: import de.schlund.pfixcore.workflow.ContextImpl;
027: import de.schlund.pfixcore.workflow.PageRequest;
028: import de.schlund.pfixcore.workflow.context.AccessibilityChecker;
029: import de.schlund.pfixcore.workflow.context.RequestContextImpl; //import de.schlund.pfixxml.SPDocument;
030: import de.schlund.pfixxml.config.IWrapperConfig;
031: import de.schlund.pfixxml.config.PageRequestConfig;
032: import de.schlund.pfixxml.util.ExtensionFunctionUtils;
033: import de.schlund.pfixxml.util.Xml;
034: import de.schlund.pfixxml.util.XsltVersion;
035:
036: /**
037: * Describe class TransformerCallback here.
038: *
039: *
040: * Created: Tue Jul 4 14:45:43 2006
041: *
042: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
043: * @version 1.0
044: */
045: public class TransformerCallback {
046:
047: private final static Logger LOG = Logger
048: .getLogger(TransformerCallback.class);
049: private static DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
050: .newInstance();
051:
052: // public static void setNoStore(SPDocument spdoc) {
053: // spdoc.setNostore(true);
054: // }
055:
056: public static int isAccessible(RequestContextImpl requestcontext,
057: String pagename) throws Exception {
058: try {
059: ContextImpl context = requestcontext.getParentContext();
060: if (context.getContextConfig().getPageRequestConfig(
061: pagename) != null) {
062: AccessibilityChecker check = (AccessibilityChecker) context;
063: boolean retval;
064: if (context.getContextConfig().isSynchronized()) {
065: synchronized (context) {
066: retval = check.isPageAccessible(pagename);
067: }
068: } else {
069: retval = check.isPageAccessible(pagename);
070: }
071: if (retval) {
072: return 1;
073: } else {
074: return 0;
075: }
076: }
077: return -1;
078: } catch (Exception x) {
079: ExtensionFunctionUtils.setExtensionFunctionError(x);
080: throw x;
081: }
082: }
083:
084: public static int isVisited(RequestContextImpl requestcontext,
085: String pagename) throws Exception {
086: try {
087: ContextImpl context = requestcontext.getParentContext();
088: if (context.getContextConfig().getPageRequestConfig(
089: pagename) != null) {
090: AccessibilityChecker check = (AccessibilityChecker) context;
091: boolean retval;
092: if (context.getContextConfig().isSynchronized()) {
093: synchronized (context) {
094: retval = check.isPageAlreadyVisited(pagename);
095: }
096: } else {
097: retval = check.isPageAlreadyVisited(pagename);
098: }
099: if (retval) {
100: return 1;
101: } else {
102: return 0;
103: }
104: }
105: return -1;
106: } catch (Exception x) {
107: ExtensionFunctionUtils.setExtensionFunctionError(x);
108: throw x;
109: }
110: }
111:
112: public static String getToken(RequestContextImpl requestContext,
113: String tokenName) throws Exception {
114: try {
115: tokenName = tokenName.trim();
116: if (tokenName.contains(":"))
117: throw new IllegalArgumentException(
118: "Illegal token name: " + tokenName);
119: String token = requestContext.getParentContext().getToken(
120: tokenName);
121: return token;
122: } catch (Exception x) {
123: ExtensionFunctionUtils.setExtensionFunctionError(x);
124: throw x;
125: }
126: }
127:
128: public static boolean requiresToken(
129: RequestContextImpl requestContext, String pageName)
130: throws Exception {
131: try {
132: ContextImpl context = requestContext.getParentContext();
133: PageRequestConfig pageConfig = context.getContextConfig()
134: .getPageRequestConfig(pageName);
135: if (pageConfig != null)
136: return pageConfig.requiresToken();
137: return false;
138: } catch (Exception x) {
139: ExtensionFunctionUtils.setExtensionFunctionError(x);
140: throw x;
141: }
142: }
143:
144: public static Node getIWrapperInfo(
145: RequestContextImpl requestContext, Node docNode,
146: String pageName, String prefix) {
147: try {
148: ContextImpl context = requestContext.getParentContext();
149: XsltVersion xsltVersion = Xml.getXsltVersion(docNode);
150: if (pageName == null || pageName.equals("")) {
151: PageRequest pg = requestContext.getCurrentPageRequest();
152: if (pg != null)
153: pageName = pg.getName();
154: else
155: throw new IllegalArgumentException(
156: "Missing page name");
157: }
158: PageRequestConfig pageConfig = context.getContextConfig()
159: .getPageRequestConfig(pageName);
160: if (pageConfig != null) {
161: Map<String, ? extends IWrapperConfig> iwrappers = pageConfig
162: .getIWrappers();
163: Class<? extends IWrapper> iwrpClass = null;
164: IWrapperConfig iwrpConfig = iwrappers.get(prefix);
165: if (iwrpConfig != null) {
166: iwrpClass = (Class<? extends IWrapper>) iwrpConfig
167: .getWrapperClass();
168: } else if (pageConfig.getAuthWrapperPrefix() != null
169: && pageConfig.getAuthWrapperPrefix().equals(
170: prefix)) {
171: iwrpClass = (Class<? extends IWrapper>) pageConfig
172: .getAuthWrapperClass();
173: } else {
174: Map<String, Class<? extends IWrapper>> auxWrappers = pageConfig
175: .getAuxWrappers();
176: iwrpClass = (Class<? extends IWrapper>) auxWrappers
177: .get(prefix);
178: }
179: if (iwrpClass != null)
180: return IWrapperInfo.getDocument(iwrpClass,
181: xsltVersion);
182: }
183: return null;
184: } catch (RuntimeException x) {
185: ExtensionFunctionUtils.setExtensionFunctionError(x);
186: throw x;
187: }
188: }
189:
190: public static Node getIWrappers(RequestContextImpl requestContext,
191: Node docNode, String pageName) throws Exception {
192: try {
193: ContextImpl context = requestContext.getParentContext();
194: XsltVersion xsltVersion = Xml.getXsltVersion(docNode);
195: DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
196: Document doc = db.newDocument();
197: Element root = doc.createElement("iwrappers");
198: doc.appendChild(root);
199: if (pageName == null || pageName.equals("")) {
200: PageRequest pg = requestContext.getCurrentPageRequest();
201: if (pg != null)
202: pageName = pg.getName();
203: else
204: throw new IllegalArgumentException(
205: "Missing page name");
206: }
207: PageRequestConfig pageConfig = context.getContextConfig()
208: .getPageRequestConfig(pageName);
209: if (pageConfig != null) {
210: if (pageConfig.getAuthWrapperPrefix() != null) {
211: Element elem = doc.createElement("iwrapper");
212: elem.setAttribute("type", "auth");
213: elem.setAttribute("prefix", pageConfig
214: .getAuthWrapperPrefix());
215: elem
216: .setAttribute("class", pageConfig
217: .getAuthWrapperClass().getClass()
218: .getName());
219: root.appendChild(elem);
220: }
221: Map<String, Class<? extends IWrapper>> auxWrappers = pageConfig
222: .getAuxWrappers();
223: for (String prefix : auxWrappers.keySet()) {
224: Element elem = doc.createElement("iwrapper");
225: elem.setAttribute("type", "aux");
226: elem.setAttribute("prefix", prefix);
227: elem.setAttribute("class", auxWrappers.get(prefix)
228: .getClass().getName());
229: root.appendChild(elem);
230: }
231: Map<String, ? extends IWrapperConfig> iwrappers = pageConfig
232: .getIWrappers();
233: for (String prefix : iwrappers.keySet()) {
234: Element elem = doc.createElement("iwrapper");
235: elem.setAttribute("prefix", prefix);
236: elem.setAttribute("class", iwrappers.get(prefix)
237: .getWrapperClass().getName());
238: elem.setAttribute("activeignore", ""
239: + iwrappers.get(prefix).isActiveIgnore());
240: elem.setAttribute("alwaysretrieve", ""
241: + iwrappers.get(prefix).isAlwaysRetrieve());
242: root.appendChild(elem);
243: }
244: }
245: if (LOG.isDebugEnabled()) {
246: TransformerFactory tf = TransformerFactory
247: .newInstance();
248: Transformer t = tf.newTransformer();
249: t.setOutputProperty(OutputKeys.INDENT, "yes");
250: StringWriter writer = new StringWriter();
251: t.transform(new DOMSource(doc),
252: new StreamResult(writer));
253: LOG.debug(writer.toString());
254: }
255: Node iwrpDoc = Xml.parse(xsltVersion, doc);
256: return iwrpDoc;
257: } catch (Exception x) {
258: ExtensionFunctionUtils.setExtensionFunctionError(x);
259: throw x;
260: }
261: }
262:
263: public static boolean hasRole(RequestContextImpl requestContext,
264: String roleName) throws Exception {
265: try {
266: ContextImpl context = requestContext.getParentContext();
267: Authentication auth = context.getAuthentication();
268: if (auth != null) {
269: return auth.hasRole(roleName);
270: }
271: return false;
272: } catch (Exception x) {
273: ExtensionFunctionUtils.setExtensionFunctionError(x);
274: throw x;
275: }
276: }
277:
278: public static Node getAllDefinedRoles(
279: RequestContextImpl requestContext, Node docNode)
280: throws Exception {
281: try {
282: Context context = requestContext.getParentContext();
283: XsltVersion xsltVersion = Xml.getXsltVersion(docNode);
284: DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
285: Document doc = db.newDocument();
286: Element root = doc.createElement("roles");
287: doc.appendChild(root);
288: Map<String, Role> configuredroles = context
289: .getContextConfig().getRoles();
290: HashSet<Role> currentroles = new HashSet<Role>();
291: if (context.getAuthentication() != null
292: && context.getAuthentication().getRoles() != null) {
293: currentroles.addAll(Arrays.asList(context
294: .getAuthentication().getRoles()));
295: }
296:
297: for (Role role : configuredroles.values()) {
298: Element elem = doc.createElement("role");
299: elem.setAttribute("name", role.getName());
300: elem.setAttribute("initial", Boolean.toString(role
301: .isInitial()));
302: if (currentroles.contains(role)) {
303: elem.setAttribute("current", "true");
304: } else {
305: elem.setAttribute("current", "false");
306: }
307: root.appendChild(elem);
308: }
309:
310: if (LOG.isDebugEnabled()) {
311: TransformerFactory tf = TransformerFactory
312: .newInstance();
313: Transformer t = tf.newTransformer();
314: t.setOutputProperty(OutputKeys.INDENT, "yes");
315: StringWriter writer = new StringWriter();
316: t.transform(new DOMSource(doc),
317: new StreamResult(writer));
318: LOG.debug(writer.toString());
319: }
320: Node iwrpDoc = Xml.parse(xsltVersion, doc);
321: return iwrpDoc;
322: } catch (Exception x) {
323: ExtensionFunctionUtils.setExtensionFunctionError(x);
324: throw x;
325: }
326: }
327: }
|