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.kew.routing.web;
017
018import javax.servlet.http.HttpServletRequest;
019import javax.servlet.http.HttpServletResponse;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.struts.action.ActionForm;
023import org.apache.struts.action.ActionForward;
024import org.apache.struts.action.ActionMapping;
025import org.kuali.rice.kew.api.WorkflowRuntimeException;
026import org.kuali.rice.kew.doctype.SecuritySession;
027import org.kuali.rice.kew.doctype.bo.DocumentType;
028import org.kuali.rice.kew.doctype.service.DocumentTypeService;
029import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
030import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
031import org.kuali.rice.kew.service.KEWServiceLocator;
032import org.kuali.rice.kew.api.KewApiConstants;
033import org.kuali.rice.kew.web.KewKualiAction;
034import org.kuali.rice.krad.UserSession;
035import org.kuali.rice.krad.util.GlobalVariables;
036
037
038/**
039 * A Struts Action for redirecting from the KEW web application to the appropriate
040 * Doc Handler for a document.
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044public class ClientAppDocHandlerRedirectAction extends KewKualiAction {
045
046    @Override
047        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
048        DocHandlerForm docHandlerForm = (DocHandlerForm) form;
049
050        String docHandler = null;
051
052        if (request.getParameter(KewApiConstants.DOCUMENT_ID_PARAMETER) != null) {
053            RouteHeaderService rhSrv = (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
054            DocumentRouteHeaderValue routeHeader = rhSrv.getRouteHeader(docHandlerForm.getDocId());
055
056            if (!KEWServiceLocator.getDocumentSecurityService().routeLogAuthorized(GlobalVariables.getUserSession().getPrincipalId(), routeHeader, new SecuritySession(GlobalVariables.getUserSession().getPrincipalId()))) {
057                return mapping.findForward("NotAuthorized");
058            }
059            docHandler = routeHeader.getDocumentType().getResolvedDocumentHandlerUrl();
060            if (StringUtils.isBlank(docHandler)) {
061                throw new WorkflowRuntimeException("Document Type '" + routeHeader.getDocumentType().getName() + "' does not have a document handler url set (attempted to open document handler url for document id " + routeHeader.getDocumentId() + ")");
062            }
063            if (!docHandler.contains("?")) {
064                docHandler += "?";
065            } else {
066                docHandler += "&";
067            }
068            docHandler += KewApiConstants.DOCUMENT_ID_PARAMETER + "=" + docHandlerForm.getDocId();
069        } else if (request.getParameter(KewApiConstants.DOCTYPE_PARAMETER) != null) {
070            DocumentTypeService documentTypeService = (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
071            DocumentType documentType = documentTypeService.findByName(docHandlerForm.getDocTypeName());
072            docHandler = documentType.getResolvedDocumentHandlerUrl();
073            if (StringUtils.isBlank(docHandler)) {
074                throw new WorkflowRuntimeException("Cannot find document handler url for document type '" + documentType.getName() + "'");
075            }
076            if (!docHandler.contains("?")) {
077                docHandler += "?";
078            } else {
079                docHandler += "&";
080            }
081            docHandler += KewApiConstants.DOCTYPE_PARAMETER + "=" + docHandlerForm.getDocTypeName();
082        } else {
083//TODO what should happen here if parms are missing; no proper ActionForward from here
084            throw new RuntimeException ("Cannot determine document handler");
085        }
086
087        docHandler += "&" + KewApiConstants.COMMAND_PARAMETER + "=" + docHandlerForm.getCommand();
088        if (getUserSession(request).isBackdoorInUse()) {
089            docHandler += "&" + KewApiConstants.BACKDOOR_ID_PARAMETER + "=" + getUserSession(request).getPrincipalName();
090        }
091        return new ActionForward(docHandler, true);
092    }
093
094    public static UserSession getUserSession(HttpServletRequest request) {
095        return GlobalVariables.getUserSession();
096    }
097}