View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Contains a stack of EDLControllers.  Allows EDL components to add new controllers to the chain runtime.  The idea
36   * being that this is how you would page between edls.  Uses the template associated with the last edl controller
37   * to render the dom of the last edl controller.
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
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 }