001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.edl.impl;
017
018import java.io.IOException;
019
020import javax.servlet.RequestDispatcher;
021import javax.servlet.ServletException;
022import javax.servlet.http.HttpServlet;
023import javax.servlet.http.HttpServletRequest;
024import javax.servlet.http.HttpServletResponse;
025
026import org.apache.commons.lang.StringUtils;
027import org.apache.logging.log4j.Logger;
028import org.apache.logging.log4j.LogManager;
029import org.kuali.rice.edl.impl.service.EdlServiceLocator;
030import org.kuali.rice.kew.api.WorkflowRuntimeException;
031import org.kuali.rice.kns.util.IncidentReportUtils;
032import org.kuali.rice.krad.UserSession;
033import org.kuali.rice.krad.exception.AuthenticationException;
034import org.kuali.rice.krad.util.GlobalVariables;
035import org.kuali.rice.krad.util.KRADConstants;
036import org.kuali.rice.krad.util.KRADUtils;
037import org.w3c.dom.Document;
038import org.w3c.dom.Element;
039
040
041/**
042 * Takes edl web requests.
043 * 
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 *
046 */
047public class EDLServlet extends HttpServlet {
048
049        private static final long serialVersionUID = -6344765194278430690L;
050
051        private static final Logger LOG = LogManager.getLogger(EDLServlet.class);
052
053        @Override
054        public void init() throws ServletException {
055                try {
056                        EdlServiceLocator.getEDocLiteService().initEDLGlobalConfig();
057                } catch (Exception e) {
058                        LOG.error("Error initializing EDL", e);
059                }
060
061        }
062
063        @Override
064        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
065                String documentId = null;
066                try {
067                    UserSession userSession = KRADUtils.getUserSessionFromRequest(request);
068                    if (userSession == null) {
069                        throw new AuthenticationException("Failed to locate a user session for request.");
070                    }
071                    GlobalVariables.setUserSession(userSession);
072                    
073                    RequestParser requestParser = new RequestParser(request);
074                    String inputCommand = requestParser.getParameterValue("command");
075                    if (StringUtils.equals(inputCommand, "initiate")){
076                        requestParser.setParameterValue("userAction","initiate");
077                    }
078                    String edlName = requestParser.getParameterValue("edlName");
079                    if (edlName == null) {
080                        edlName = requestParser.getParameterValue("docTypeName");//this is for 'WorkflowQuicklinks'
081                    }
082                    EDLController edlController = null;
083                    
084                    if (edlName == null) {
085                        documentId = requestParser.getParameterValue("docId");
086                        if (documentId == null) {
087                                String docFormKey = requestParser.getParameterValue(KRADConstants.DOC_FORM_KEY);
088                                if (docFormKey != null) {
089                                        Document document = (Document) GlobalVariables.getUserSession().retrieveObject(docFormKey);
090                                        Element documentState = EDLXmlUtils.getDocumentStateElement(document);
091                                        documentId = EDLXmlUtils.getChildElementTextValue(documentState, "docId");
092                                        requestParser.setAttribute(KRADConstants.DOC_FORM_KEY, docFormKey);
093                                }
094                                if (documentId == null) {
095                                        throw new WorkflowRuntimeException("No edl name or document id detected");
096                                }
097                        }
098                        requestParser.setAttribute("docId", documentId);
099                        edlController = EdlServiceLocator.getEDocLiteService().getEDLControllerUsingDocumentId(documentId);
100                    } else {
101                        edlController = EdlServiceLocator.getEDocLiteService().getEDLControllerUsingEdlName(edlName);
102                    }
103
104                    //TODO Fix this in a better way (reworking the command structure maybe?)
105                    //fix for KULRICE-4057 to make sure we don't destory docContent on empty command params
106                    if(inputCommand == null && requestParser.getParameterValue("docId") != null && !"POST".equals(request.getMethod())){
107                        //make sure these are the only params on the request (paging passed undefined input command as well...
108                        if(!(request.getParameterMap().size() > 2)){//ensures ONLY documentId was passed
109                                requestParser.setParameterValue("command", "displayDocSearchView");
110                                LOG.info("command parameter was not passed with the request, and only document ID was. Defaulted command to 'displayDocSearchView' to ensure docContent remains.");
111                        }
112                    }
113
114                    EDLControllerChain controllerChain = new EDLControllerChain();
115                    controllerChain.addEdlController(edlController);
116                        //TODO Do we not want to set the content type for the response?            
117                    controllerChain.renderEDL(requestParser, response);
118
119                } catch (Exception e) {
120                        LOG.error("Error processing EDL", e);
121                        outputError(request, response, e, documentId);
122                } finally {
123                    GlobalVariables.setUserSession(null);
124                }
125        }
126
127        private void outputError(HttpServletRequest request, HttpServletResponse response, Exception exception, String documentId) throws ServletException, IOException {
128                        IncidentReportUtils.populateRequestForIncidentReport(exception, "" + documentId, "eDoc Lite", request);
129                RequestDispatcher rd = getServletContext().getRequestDispatcher(request.getServletPath() + "/../../kr/kualiExceptionIncidentReport.do");
130                rd.forward(request, response);
131        }
132
133}