View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.test.web;
17  
18  import javax.servlet.ServletContext;
19  
20  import org.kuali.rice.kim.api.identity.principal.Principal;
21  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22  import org.kuali.rice.krad.UserSession;
23  import org.kuali.rice.krad.util.KRADConstants;
24  import org.springframework.mock.web.MockHttpServletRequest;
25  
26  
27  /**
28   * Subclass of MockHttpServletRequest that initializes the request with a user session
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class WorkflowServletRequest extends MockHttpServletRequest {
32      public WorkflowServletRequest() {
33          super();
34      }
35      public WorkflowServletRequest(ServletContext context, String method, String requestURI) {
36          super(context, method, requestURI);
37      }
38      public WorkflowServletRequest(ServletContext context) {
39          super(context);
40      }
41      public WorkflowServletRequest(String method, String requestURI) {
42          super(method, requestURI);
43      }
44  
45      public WorkflowServletRequest(String user) {
46          setUser(user);
47      }
48  
49      public void setUser(String user) {
50          Principal wfuser;
51          if (user == null) {
52              wfuser = null;
53          } else {
54              wfuser = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(user);
55          }
56          setWorkflowUser(wfuser);
57      }
58  
59      public String getUser() {
60          Principal user = getWorkflowUser();
61          if (user == null) return null;
62          return user.getPrincipalName();
63      }
64  
65      public void setBackdoorId(String backdoorId) {
66          UserSession session = getUserSession();
67          if (session == null) {
68              throw new IllegalStateException("Session must be set before backdoor id is set");
69          }
70          session.setBackdoorUser(backdoorId);
71      }
72  
73      public void setWorkflowUser(Principal user) {
74          if (user == null) {
75              setUserSession(null);
76          } else {
77              setUserSession(new UserSession(user.getPrincipalName()));
78          }
79      }
80  
81      public Principal getWorkflowUser() {
82          UserSession session = getUserSession();
83          if (session == null) return null;
84          return KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(session.getLoggedInUserPrincipalName());
85      }
86  
87      public String getBackdoorPrincipalId() {
88          UserSession session = getUserSession();
89          if (session == null) return null;
90          return session.getPrincipalId();
91      }
92  
93      public void setUserSession(UserSession userSession) {
94          getSession().setAttribute(KRADConstants.USER_SESSION_KEY, userSession);
95      }
96  
97      public UserSession getUserSession() {
98          return (UserSession) getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
99      }
100 }