1
2
3
4
5
6 package gov.bnl.gums.service;
7
8 import gov.bnl.gums.configuration.ConfigElement;
9 import gov.bnl.gums.configuration.Configuration;
10 import gov.bnl.gums.groupToAccount.*;
11 import gov.bnl.gums.hostToGroup.*;
12 import gov.bnl.gums.util.StringUtil;
13
14 import javax.servlet.http.HttpServletRequest;
15
16 import java.lang.reflect.Method;
17 import java.rmi.Remote;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.regex.Pattern;
25
26
27
28
29
30 public class ConfigurationWebToolkit implements Remote {
31 static Pattern configParamPattern = Pattern.compile("^ce_.*");
32
33 static public Configuration removeUncheckedG2AMappings(HttpServletRequest request, Configuration configuration) {
34 int i = 0;
35
36 Configuration newConfiguration = (Configuration)configuration.clone();
37
38 for (Object obj : configuration.getGroupToAccountMappings().values()) {
39 GroupToAccountMapping g2aMapping = (GroupToAccountMapping)obj;
40 if (request.getParameter("g"+i)==null)
41 newConfiguration.removeGroupToAccountMapping(g2aMapping.getName(), false);
42 i++;
43 }
44
45 return newConfiguration;
46 }
47
48 @SuppressWarnings("unchecked")
49 static public ConfigElement parseConfigElement(HttpServletRequest request) throws Exception {
50 String className = request.getParameter("class").trim();
51 Class<ConfigElement> configElementClass = (Class<ConfigElement>)Class.forName(className);
52 ConfigElement configElement = configElementClass.newInstance();
53
54 Set<?> keySet = request.getParameterMap().keySet();
55 Iterator<?> it = keySet.iterator();
56 while(it.hasNext()) {
57 String htmlKey = it.next().toString();
58 if (!configParamPattern.matcher(htmlKey).matches())
59 continue;
60 String key = htmlKey.substring(3);
61 String capFirstLetter = key.substring(0,1).toUpperCase();
62 String setterName = "set" + capFirstLetter + key.substring(1, key.length());
63 Method method = null;
64 try {
65 method = configElementClass.getMethod(setterName, String.class);
66 } catch (Exception e) {
67 continue;
68 }
69
70 assert(method.getParameterTypes().length == 1);
71 Class paramClassType = method.getParameterTypes()[0];
72
73 if (paramClassType.equals(String.class))
74 method.invoke(configElement, request.getParameter(htmlKey));
75 else if (paramClassType.equals(Boolean.class) || paramClassType.equals(boolean.class))
76 method.invoke(configElement, Boolean.parseBoolean(request.getParameter(htmlKey)));
77 else if (paramClassType.newInstance() instanceof List) {
78 List<String> list = new ArrayList<String>();
79 String strParam;
80 int i = 0;
81 while ((strParam = request.getParameter(key + new Integer(i++).toString())) != null)
82 list.add(strParam);
83 if (i==0) {
84
85 String newKey = key.substring(0,1);
86 while ((strParam = request.getParameter(newKey + new Integer(i++).toString())) != null)
87 list.add(strParam);
88 }
89 method.invoke(configElement, list);
90 }
91 else if (paramClassType.newInstance() instanceof Set) {
92 Set<String> list = new HashSet<String>();
93 String strParam;
94 int i = 0;
95 while ((strParam = request.getParameter(key + new Integer(i).toString())) != null)
96 list.add(strParam);
97 if (i==0) {
98
99 String conciseKey = key.substring(0,2);
100 while ((strParam = request.getParameter(conciseKey + new Integer(i++).toString())) != null)
101 list.add(strParam);
102 }
103 method.invoke(configElement, list);
104 }
105 }
106
107 return configElement;
108 }
109
110 static public String getHostToGroupReferences(Configuration configuration, String g2AMappingName) {
111 String retStr = null;
112 Collection<HostToGroupMapping> h2GMappings = configuration.getHostToGroupMappings();
113 Iterator<HostToGroupMapping> it = h2GMappings.iterator();
114 while (it.hasNext()) {
115 HostToGroupMapping h2GMapping = (HostToGroupMapping)it.next();
116 Iterator<String> it2 = StringUtil.toList(h2GMapping.getGroupToAccountMappings()).iterator();
117 while (it2.hasNext()) {
118 String thisG2AMapping = (String)it2.next();
119 if (thisG2AMapping.equals(g2AMappingName)) {
120 if (retStr==null)
121 retStr = "";
122 retStr += "\"" + h2GMapping.getName() + "\", ";
123 break;
124 }
125 }
126 }
127 if(retStr!=null)
128 retStr = retStr.substring(0, retStr.length()-2);
129 return retStr;
130 }
131
132 static public String getGroupToAccountMappingReferences(Configuration configuration, String name, String className) {
133 String retStr = null;
134 Collection<GroupToAccountMapping> g2AMappings = configuration.getGroupToAccountMappings().values();
135 Iterator<GroupToAccountMapping> it = g2AMappings.iterator();
136 while (it.hasNext()) {
137 GroupToAccountMapping g2AMapping = (GroupToAccountMapping)it.next();
138 if(className.equals("gov.bnl.gums.account.AccountMapper")) {
139 Iterator<String> it2 = StringUtil.toList(g2AMapping.getAccountMappers()).iterator();
140 while (it2.hasNext()) {
141 String thisAccountMapper = (String)it2.next().trim();
142 if (thisAccountMapper.equals(name)) {
143 if (retStr==null)
144 retStr = "";
145 retStr += g2AMapping.getName() + ", ";
146 break;
147 }
148 }
149 }
150 else if(className.equals("gov.bnl.gums.userGroup.UserGroup")) {
151 Iterator<String> it2 = StringUtil.toList(g2AMapping.getUserGroups()).iterator();
152 while (it2.hasNext()) {
153 String thisUserGroup = (String)it2.next();
154 if (thisUserGroup.equals(name)) {
155 if (retStr==null)
156 retStr = "";
157 retStr += g2AMapping.getName() + ", ";
158 break;
159 }
160 }
161 }
162 }
163 if(retStr!=null)
164 retStr = retStr.substring(0, retStr.length()-2);
165 return retStr;
166 }
167
168 static public String createSelectBox(String name, Collection<?> items, String selected, String javascript, boolean includeEmptySlot) {
169 String retStr = "<select name=\""+name+"\" " + (javascript!=null?javascript:"") + ">";
170 if (includeEmptySlot)
171 retStr += "<option " + (selected==null?"selected":"") + "></option>";
172 Iterator<?> it = items.iterator();
173 while(it.hasNext())
174 {
175 Object item = it.next();
176 String strVal;
177 if (item instanceof ConfigElement)
178 strVal = ((ConfigElement)item).getName();
179 else if (item instanceof Class)
180 strVal = ((Class<?>)item).getName();
181 else
182 strVal = item.toString();
183 if (strVal.equals("_test") || strVal.equals("/DC=com/DC=example/OU=Services/CN=example.site.com"))
184 continue;
185 if (strVal.equals(selected))
186 retStr += "<option selected>" + strVal + "</option>";
187 else
188 retStr += "<option>" + strVal + "</option>";
189 }
190 retStr += "</select> \n";
191 return retStr;
192 }
193
194 static public Object blankNull(Object obj) {
195 return (obj == null) ? "" : obj;
196 }
197
198 static public String createOrderedList(String name, Collection<?> items, String javascript) {
199
200 return "";
201 }
202
203 static public String createDoSubmit(Collection<ConfigElement> items, HttpServletRequest request) {
204 String str =
205 "<script language=\"javascript\">"+
206 "String.prototype.trim = function() { return this.replace(/^\\s+|\\s+$/g, \"\"); };"+
207 "document.forms[0].elements['ce_name'].value = document.forms[0].elements['ce_name'].value.trim();"+
208 "function doSubmit(str) {";
209
210 if ("add".equals(request.getParameter("command")) || "add".equals(request.getParameter("originalCommand"))) {
211 str += "if ( document.forms[0].elements['ce_name'].value == '' ){ alert('First field cannot be empty'); return false; }";
212
213 Iterator<ConfigElement> it = items.iterator();
214 while(it.hasNext())
215 str += "if ( document.forms[0].elements['ce_name'].value == '" + it.next().getName() + "'){ alert('Name already exists - please choose another name'); return false; }";
216 }
217
218 str +=
219 "document.forms[0].elements['command'].value='save'; return true;"+
220 "return false;"+
221 "}"+
222 "</script>";
223
224 return str;
225 }
226
227 static public String stripVo(String voGroup) {
228 if(voGroup.length()>0 && voGroup.charAt(0)=='/') {
229 String subStr = voGroup.substring(1);
230 int index = subStr.indexOf("/");
231 if (index!=-1)
232 return subStr.substring(index+1);
233 }
234 return voGroup;
235 }
236
237 static public String stripGroup(String voGroup) {
238 if(voGroup.length()>0 && voGroup.charAt(0)=='/') {
239 String subStr = voGroup.substring(1);
240 int index = subStr.indexOf("/");
241 if (index!=-1)
242 return subStr.substring(0, index);
243 }
244 return voGroup;
245 }
246 }