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 java.util.Stack;
31
32
33
34
35
36
37
38
39
40
41 public class EDLControllerChain {
42
43 private static final Logger LOG = Logger.getLogger(EDLControllerChain.class);
44
45 private Stack<EDLController> edlControllers = new Stack<EDLController>();
46
47 public void renderEDL(RequestParser requestParser, HttpServletResponse response) throws Exception {
48 EDLController edlController = edlControllers.pop();
49 edlController.setEdlContext(getInitialEDLContext(edlController, requestParser, edlController.getStyle().newTransformer()));
50
51 Document dom = edlController.notifyComponents();
52 if (edlControllers.isEmpty()) {
53 transform(edlController.getEdlContext(), dom, response);
54 } else {
55 renderEDL(requestParser, response);
56 }
57 }
58
59 public void addEdlController(EDLController edlController) {
60 edlControllers.add(edlController);
61 }
62
63 private void transform(EDLContext edlContext, Document dom, HttpServletResponse response) throws Exception {
64 if (StringUtils.isNotBlank(edlContext.getRedirectUrl())) {
65 response.sendRedirect(edlContext.getRedirectUrl());
66 return;
67 }
68 response.setContentType("text/html; charset=UTF-8");
69 Transformer transformer = edlContext.getTransformer();
70
71 transformer.setOutputProperty("indent", "yes");
72 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
73 String user = null;
74 String loggedInUser = null;
75 if (edlContext.getUserSession() != null) {
76 Person wu = edlContext.getUserSession().getPerson();
77 if (wu != null) user = wu.getPrincipalId();
78 wu = edlContext.getUserSession().getPerson();
79 if (wu != null) loggedInUser = wu.getPrincipalId();
80 }
81 transformer.setParameter("user", user);
82 transformer.setParameter("loggedInUser", loggedInUser);
83 if (LOG.isDebugEnabled()) {
84 LOG.debug("Transforming dom " + XmlJotter.jotNode(dom, true));
85 }
86 transformer.transform(new DOMSource(dom), new StreamResult(response.getOutputStream()));
87 }
88
89 private EDLContext getInitialEDLContext(EDLController edlController, RequestParser requestParser, Transformer transformer) {
90 EDLContext edlContext = new EDLContext();
91 edlContext.setEdlControllerChain(this);
92 edlContext.setEdocLiteAssociation(edlController.getEdocLiteAssociation());
93 edlContext.setUserSession(GlobalVariables.getUserSession());
94 edlContext.setTransformer(transformer);
95 edlContext.setRequestParser(requestParser);
96 return edlContext;
97 }
98
99 }