1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.web.spring;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21 import org.kuali.rice.coreservice.api.namespace.Namespace;
22 import org.kuali.rice.coreservice.api.namespace.NamespaceService;
23 import org.kuali.rice.ken.exception.ErrorList;
24 import org.kuali.rice.kim.api.group.Group;
25 import org.kuali.rice.kim.api.group.GroupService;
26 import org.kuali.rice.kim.api.identity.IdentityService;
27 import org.kuali.rice.kim.api.identity.principal.Principal;
28 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
30 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
31
32 import javax.servlet.http.HttpServletRequest;
33 import java.util.ArrayList;
34 import java.util.List;
35
36
37
38
39
40
41
42 public class BaseSendNotificationController extends MultiActionController {
43 private static final Logger LOG = Logger.getLogger(BaseSendNotificationController.class);
44
45 private static final String USER_RECIPS_PARAM = "userRecipients";
46 private static final String WORKGROUP_RECIPS_PARAM = "workgroupRecipients";
47 private static final String WORKGROUP_NAMESPACE_CODES_PARAM = "workgroupNamespaceCodes";
48 private static final String SPLIT_REGEX = "(%2C|,)";
49
50 private static IdentityService identityService;
51 private static GroupService groupService;
52 private static NamespaceService namespaceService;
53
54 protected static IdentityService getIdentityService() {
55 if ( identityService == null ) {
56 identityService = KimApiServiceLocator.getIdentityService();
57 }
58 return identityService;
59 }
60
61 protected static GroupService getGroupService() {
62 if ( groupService == null ) {
63 groupService = KimApiServiceLocator.getGroupService();
64 }
65 return groupService;
66 }
67
68 protected static NamespaceService getNamespaceService() {
69 if ( namespaceService == null ) {
70 namespaceService = CoreServiceApiServiceLocator.getNamespaceService();
71 }
72 return namespaceService;
73 }
74
75 protected String[] parseUserRecipients(HttpServletRequest request) {
76 return parseCommaSeparatedValues(request, USER_RECIPS_PARAM);
77 }
78
79 protected String[] parseWorkgroupRecipients(HttpServletRequest request) {
80 return parseCommaSeparatedValues(request, WORKGROUP_RECIPS_PARAM);
81 }
82
83 protected String[] parseWorkgroupNamespaceCodes(HttpServletRequest request) {
84 return parseCommaSeparatedValues(request, WORKGROUP_NAMESPACE_CODES_PARAM);
85 }
86
87 protected String[] parseCommaSeparatedValues(HttpServletRequest request, String param) {
88 String vals = request.getParameter(param);
89 if (vals != null) {
90 String[] split = vals.split(SPLIT_REGEX);
91 List<String> strs = new ArrayList<String>();
92 for (String component: split) {
93 if (StringUtils.isNotBlank(component)) {
94 strs.add(component.trim());
95 }
96 }
97 return strs.toArray(new String[strs.size()]);
98 } else {
99 return new String[0];
100 }
101 }
102
103 protected boolean isUserRecipientValid(String user, ErrorList errors) {
104 boolean valid = true;
105 Principal principal = getIdentityService().getPrincipalByPrincipalName(user);
106 if (principal == null) {
107 valid = false;
108 errors.addError("'" + user + "' is not a valid principal name");
109 }
110
111 return valid;
112 }
113
114 protected boolean isWorkgroupRecipientValid(String groupName, String namespaceCode, ErrorList errors) {
115 Namespace nSpace = getNamespaceService().getNamespace(namespaceCode);
116 if (nSpace == null) {
117 errors.addError((new StringBuilder()).append('\'').append(namespaceCode).append("' is not a valid namespace code").toString());
118 return false;
119 } else {
120 Group i = getGroupService().getGroupByNamespaceCodeAndName(namespaceCode, groupName);
121 if (i == null) {
122 errors.addError((new StringBuilder()).append('\'').append(groupName).append(
123 "' is not a valid group name for namespace code '").append(namespaceCode).append('\'').toString());
124 return false;
125 } else {
126 return true;
127 }
128 }
129 }
130 protected String getPrincipalIdFromIdOrName(String principalIdOrName) {
131 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalIdOrName);
132 if (principal == null) {
133 principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalIdOrName);
134 }
135 if (principal == null) {
136 throw new RiceIllegalArgumentException("Could not locate a principal as initiator with the given remoteUser of " + principalIdOrName);
137 }
138 return principal.getPrincipalId();
139 }
140 }