001/**
002 * Copyright 2005-2016 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.log4j.Logger;
028import org.kuali.rice.edl.impl.service.EdlServiceLocator;
029import org.kuali.rice.kew.api.WorkflowRuntimeException;
030import org.kuali.rice.kns.util.IncidentReportUtils;
031import org.kuali.rice.krad.UserSession;
032import org.kuali.rice.krad.exception.AuthenticationException;
033import org.kuali.rice.krad.util.GlobalVariables;
034import org.kuali.rice.krad.util.KRADConstants;
035import org.kuali.rice.krad.util.KRADUtils;
036import org.w3c.dom.Document;
037import org.w3c.dom.Element;
038
039
040/**
041 * Takes edl web requests.
042 * 
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 *
045 */
046public class EDLServlet extends HttpServlet {
047
048        private static final long serialVersionUID = -6344765194278430690L;
049
050        private static final Logger LOG = Logger.getLogger(EDLServlet.class);
051
052        @Override
053        public void init() throws ServletException {
054                try {
055                        EdlServiceLocator.getEDocLiteService().initEDLGlobalConfig();
056                } catch (Exception e) {
057                        LOG.error("Error initializing EDL", e);
058                }
059
060        }
061
062        @Override
063        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
064                String documentId = null;
065                try {
066                    UserSession userSession = KRADUtils.getUserSessionFromRequest(request);
067                    if (userSession == null) {
068                        throw new AuthenticationException("Failed to locate a user session for request.");
069                    }
070                    GlobalVariables.setUserSession(userSession);
071                    
072                    RequestParser requestParser = new RequestParser(request);
073                    String inputCommand = requestParser.getParameterValue("command");
074                    if (StringUtils.equals(inputCommand, "initiate")){
075                        requestParser.setParameterValue("userAction","initiate");
076                    }
077                    String edlName = requestParser.getParameterValue("edlName");
078                    if (edlName == null) {
079                        edlName = requestParser.getParameterValue("docTypeName");//this is for 'WorkflowQuicklinks'
080                    }
081                    EDLController edlController = null;
082                    
083                    if (edlName == null) {
084                        documentId = requestParser.getParameterValue("docId");
085                        if (documentId == null) {
086                                String docFormKey = requestParser.getParameterValue(KRADConstants.DOC_FORM_KEY);
087                                if (docFormKey != null) {
088                                        Document document = (Document) GlobalVariables.getUserSession().retrieveObject(docFormKey);
089                                        Element documentState = EDLXmlUtils.getDocumentStateElement(document);
090                                        documentId = EDLXmlUtils.getChildElementTextValue(documentState, "docId");
091                                        requestParser.setAttribute(KRADConstants.DOC_FORM_KEY, docFormKey);
092                                }
093                                if (documentId == null) {
094                                        throw new WorkflowRuntimeException("No edl name or document id detected");
095                                }
096                        }
097                        requestParser.setAttribute("docId", documentId);
098                        edlController = EdlServiceLocator.getEDocLiteService().getEDLControllerUsingDocumentId(documentId);
099                    } else {
100                        edlController = EdlServiceLocator.getEDocLiteService().getEDLControllerUsingEdlName(edlName);
101                    }
102
103                    //TODO Fix this in a better way (reworking the command structure maybe?)
104                    //fix for KULRICE-4057 to make sure we don't destory docContent on empty command params
105                    if(inputCommand == null && requestParser.getParameterValue("docId") != null && !"POST".equals(request.getMethod())){
106                        //make sure these are the only params on the request (paging passed undefined input command as well...
107                        if(!(request.getParameterMap().size() > 2)){//ensures ONLY documentId was passed
108                                requestParser.setParameterValue("command", "displayDocSearchView");
109                                LOG.info("command parameter was not passed with the request, and only document ID was. Defaulted command to 'displayDocSearchView' to ensure docContent remains.");
110                        }
111                    }
112
113                    EDLControllerChain controllerChain = new EDLControllerChain();
114                    controllerChain.addEdlController(edlController);
115                        //TODO Do we not want to set the content type for the response?            
116                    controllerChain.renderEDL(requestParser, response);
117
118                } catch (Exception e) {
119                        LOG.error("Error processing EDL", e);
120                        outputError(request, response, e, documentId);
121                } finally {
122                    GlobalVariables.setUserSession(null);
123                }
124        }
125
126        private void outputError(HttpServletRequest request, HttpServletResponse response, Exception exception, String documentId) throws ServletException, IOException {
127                        IncidentReportUtils.populateRequestForIncidentReport(exception, "" + documentId, "eDoc Lite", request);
128                RequestDispatcher rd = getServletContext().getRequestDispatcher(request.getServletPath() + "/../../kr/kualiExceptionIncidentReport.do");
129                rd.forward(request, response);
130        }
131
132}