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