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