1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.edl.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.util.xml.XmlJotter;
21 import org.kuali.rice.kim.api.identity.Person;
22 import org.kuali.rice.krad.util.GlobalVariables;
23 import org.w3c.dom.Document;
24
25 import javax.servlet.http.HttpServletResponse;
26 import javax.xml.transform.OutputKeys;
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.dom.DOMSource;
29 import javax.xml.transform.stream.StreamResult;
30 import javax.xml.xpath.XPathFactory;
31 import java.util.Stack;
32
33
34
35
36
37
38
39
40
41
42 public class EDLControllerChain {
43
44 private static final Logger LOG = Logger.getLogger(EDLControllerChain.class);
45
46 private Stack<EDLController> edlControllers = new Stack<EDLController>();
47
48 public void renderEDL(RequestParser requestParser, HttpServletResponse response) throws Exception {
49 EDLController edlController = edlControllers.pop();
50 edlController.setEdlContext(getInitialEDLContext(edlController, requestParser, edlController.getStyle().newTransformer()));
51
52 Document dom = edlController.notifyComponents();
53 if (edlControllers.isEmpty()) {
54 transform(edlController.getEdlContext(), dom, response);
55 } else {
56 renderEDL(requestParser, response);
57 }
58 }
59
60 public void addEdlController(EDLController edlController) {
61 edlControllers.add(edlController);
62 }
63
64 private void transform(EDLContext edlContext, Document dom, HttpServletResponse response) throws Exception {
65 if (StringUtils.isNotBlank(edlContext.getRedirectUrl())) {
66 response.sendRedirect(edlContext.getRedirectUrl());
67 return;
68 }
69 response.setContentType("text/html; charset=UTF-8");
70 Transformer transformer = edlContext.getTransformer();
71
72 transformer.setOutputProperty("indent", "yes");
73 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
74 String user = null;
75 String loggedInUser = null;
76 if (edlContext.getUserSession() != null) {
77 Person wu = edlContext.getUserSession().getPerson();
78 if (wu != null) user = wu.getPrincipalId();
79 wu = edlContext.getUserSession().getPerson();
80 if (wu != null) loggedInUser = wu.getPrincipalId();
81 }
82 transformer.setParameter("user", user);
83 transformer.setParameter("loggedInUser", loggedInUser);
84 if (LOG.isDebugEnabled()) {
85 LOG.debug("Transforming dom " + XmlJotter.jotNode(dom, true));
86 }
87 transformer.transform(new DOMSource(dom), new StreamResult(response.getOutputStream()));
88 }
89
90 private EDLContext getInitialEDLContext(EDLController edlController, RequestParser requestParser, Transformer transformer) {
91 EDLContext edlContext = new EDLContext();
92 edlContext.setEdlControllerChain(this);
93 edlContext.setEdocLiteAssociation(edlController.getEdocLiteAssociation());
94 edlContext.setUserSession(GlobalVariables.getUserSession());
95 edlContext.setTransformer(transformer);
96 edlContext.setRequestParser(requestParser);
97 edlContext.setXpath(XPathFactory.newInstance().newXPath());
98 return edlContext;
99 }
100
101 }