001 /**
002 * Copyright 2005-2011 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 */
016 package org.kuali.rice.kew.test.web;
017
018 import javax.servlet.ServletContext;
019
020 import org.kuali.rice.kim.api.identity.principal.Principal;
021 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
022 import org.kuali.rice.krad.UserSession;
023 import org.kuali.rice.krad.util.KRADConstants;
024 import org.springframework.mock.web.MockHttpServletRequest;
025
026
027 /**
028 * Subclass of MockHttpServletRequest that initializes the request with a user session
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031 public class WorkflowServletRequest extends MockHttpServletRequest {
032 public WorkflowServletRequest() {
033 super();
034 }
035 public WorkflowServletRequest(ServletContext context, String method, String requestURI) {
036 super(context, method, requestURI);
037 }
038 public WorkflowServletRequest(ServletContext context) {
039 super(context);
040 }
041 public WorkflowServletRequest(String method, String requestURI) {
042 super(method, requestURI);
043 }
044
045 public WorkflowServletRequest(String user) {
046 setUser(user);
047 }
048
049 public void setUser(String user) {
050 Principal wfuser;
051 if (user == null) {
052 wfuser = null;
053 } else {
054 wfuser = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(user);
055 }
056 setWorkflowUser(wfuser);
057 }
058
059 public String getUser() {
060 Principal user = getWorkflowUser();
061 if (user == null) return null;
062 return user.getPrincipalName();
063 }
064
065 public void setBackdoorId(String backdoorId) {
066 UserSession session = getUserSession();
067 if (session == null) {
068 throw new IllegalStateException("Session must be set before backdoor id is set");
069 }
070 session.setBackdoorUser(backdoorId);
071 }
072
073 public void setWorkflowUser(Principal user) {
074 if (user == null) {
075 setUserSession(null);
076 } else {
077 setUserSession(new UserSession(user.getPrincipalName()));
078 }
079 }
080
081 public Principal getWorkflowUser() {
082 UserSession session = getUserSession();
083 if (session == null) return null;
084 return KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(session.getLoggedInUserPrincipalName());
085 }
086
087 public String getBackdoorPrincipalId() {
088 UserSession session = getUserSession();
089 if (session == null) return null;
090 return session.getPrincipalId();
091 }
092
093 public void setUserSession(UserSession userSession) {
094 getSession().setAttribute(KRADConstants.USER_SESSION_KEY, userSession);
095 }
096
097 public UserSession getUserSession() {
098 return (UserSession) getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
099 }
100 }