KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > realm > EditRealmAction


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
17 package org.apache.webapp.admin.realm;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28 import org.apache.struts.action.Action;
29 import org.apache.struts.action.ActionErrors;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.util.MessageResources;
34
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.ObjectInstance JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.JMException JavaDoc;
39
40 import org.apache.webapp.admin.ApplicationServlet;
41 import org.apache.webapp.admin.LabelValueBean;
42 import org.apache.webapp.admin.Lists;
43 import org.apache.webapp.admin.TomcatTreeBuilder;
44
45 /**
46  * A generic <code>Action</code> that sets up <em>Edit
47  * Realm </em> transactions, based on the type of Realm.
48  *
49  * @author Manveen Kaur
50  * @version $Revision: 1.10 $ $Date: 2004/10/18 06:37:54 $
51  */

52
53 public class EditRealmAction extends Action {
54
55
56     /**
57      * The MBeanServer we will be interacting with.
58      */

59     private MBeanServer JavaDoc mBServer = null;
60
61
62     // --------------------------------------------------------- Public Methods
63

64     /**
65      * Process the specified HTTP request, and create the corresponding HTTP
66      * response (or forward to another web component that will create it).
67      * Return an <code>ActionForward</code> instance describing where and how
68      * control should be forwarded, or <code>null</code> if the response has
69      * already been completed.
70      *
71      * @param mapping The ActionMapping used to select this instance
72      * @param actionForm The optional ActionForm bean for this request (if any)
73      * @param request The HTTP request we are processing
74      * @param response The HTTP response we are creating
75      *
76      * @exception IOException if an input/output error occurs
77      * @exception ServletException if a servlet exception occurs
78      */

79     public ActionForward execute(ActionMapping mapping,
80                                  ActionForm form,
81                                  HttpServletRequest JavaDoc request,
82                                  HttpServletResponse JavaDoc response)
83         throws IOException JavaDoc, ServletException JavaDoc {
84
85         // Acquire the resources that we need
86
HttpSession JavaDoc session = request.getSession();
87         Locale JavaDoc locale = getLocale(request);
88         MessageResources resources = getResources(request);
89
90         // Acquire a reference to the MBeanServer containing our MBeans
91
try {
92             mBServer = ((ApplicationServlet) getServlet()).getServer();
93         } catch (Throwable JavaDoc t) {
94             throw new ServletException JavaDoc
95             ("Cannot acquire MBeanServer reference", t);
96         }
97
98         // Set up the object names of the MBeans we are manipulating
99
ObjectName JavaDoc rname = null;
100         StringBuffer JavaDoc sb = null;
101         try {
102             rname = new ObjectName JavaDoc(request.getParameter("select"));
103         } catch (Exception JavaDoc e) {
104             String JavaDoc message =
105                 resources.getMessage(locale, "error.realmName.bad",
106                                      request.getParameter("select"));
107             getServlet().log(message);
108             response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
109             return (null);
110         }
111
112        String JavaDoc realmType = null;
113        String JavaDoc attribute = null;
114
115        // Find what type of Realm this is
116
try {
117             attribute = "className";
118             String JavaDoc className = (String JavaDoc)
119                 mBServer.getAttribute(rname, attribute);
120             int period = className.lastIndexOf(".");
121             if (period >= 0)
122                 realmType = className.substring(period + 1);
123         } catch (Throwable JavaDoc t) {
124           getServlet().log
125                 (resources.getMessage(locale, "users.error.attribute.get",
126                                       attribute), t);
127             response.sendError
128                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
129                  resources.getMessage(locale, "users.error.attribute.get",
130                                       attribute));
131             return (null);
132         }
133
134         // Forward to the appropriate realm display page
135

136         if ("UserDatabaseRealm".equalsIgnoreCase(realmType)) {
137                setUpUserDatabaseRealm(rname, request, response);
138         } else if ("MemoryRealm".equalsIgnoreCase(realmType)) {
139                setUpMemoryRealm(rname, request, response);
140         } else if ("JDBCRealm".equalsIgnoreCase(realmType)) {
141                setUpJDBCRealm(rname, request, response);
142         } else if ("JNDIRealm".equalsIgnoreCase(realmType)) {
143                setUpJNDIRealm(rname, request, response);
144         } else if ("DataSourceRealm".equalsIgnoreCase(realmType)) {
145                 setUpDataSourceRealm(rname, request, response);
146         }
147
148         return (mapping.findForward(realmType));
149
150     }
151
152     private void setUpUserDatabaseRealm(ObjectName JavaDoc rname,
153                                         HttpServletRequest JavaDoc request,
154                                         HttpServletResponse JavaDoc response)
155     throws IOException JavaDoc {
156         // Fill in the form values for display and editing
157
MessageResources resources = getResources(request);
158         HttpSession JavaDoc session = request.getSession();
159         Locale JavaDoc locale = getLocale(request);
160         UserDatabaseRealmForm realmFm = new UserDatabaseRealmForm();
161         session.setAttribute("userDatabaseRealmForm", realmFm);
162         realmFm.setAdminAction("Edit");
163         realmFm.setObjectName(rname.toString());
164         String JavaDoc realmType = "UserDatabaseRealm";
165         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
166         String JavaDoc host = rname.getKeyProperty("host");
167         String JavaDoc context = rname.getKeyProperty("path");
168         if (host!=null) {
169             sb.append("Host (" + host + ") > ");
170         }
171         if (context!=null) {
172             sb.append("Context (" + context + ") > ");
173         }
174         sb.append(resources.getMessage(locale, "server.service.treeBuilder.realm"));
175         realmFm.setNodeLabel(sb.toString());
176         realmFm.setRealmType(realmType);
177         realmFm.setAllowDeletion(allowDeletion(rname,request));
178
179         String JavaDoc attribute = null;
180         try {
181
182             // Copy scalar properties
183
attribute = "resourceName";
184             realmFm.setResource
185                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
186
187         } catch (Throwable JavaDoc t) {
188             getServlet().log
189                 (resources.getMessage(locale, "users.error.attribute.get",
190                                       attribute), t);
191             response.sendError
192                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
193                  resources.getMessage(locale, "users.error.attribute.get",
194                                       attribute));
195         }
196     }
197
198     private void setUpMemoryRealm(ObjectName JavaDoc rname, HttpServletRequest JavaDoc request,
199                                         HttpServletResponse JavaDoc response)
200     throws IOException JavaDoc {
201         // Fill in the form values for display and editing
202
MessageResources resources = getResources(request);
203         HttpSession JavaDoc session = request.getSession();
204         Locale JavaDoc locale = getLocale(request);
205         MemoryRealmForm realmFm = new MemoryRealmForm();
206         session.setAttribute("memoryRealmForm", realmFm);
207         realmFm.setAdminAction("Edit");
208         realmFm.setObjectName(rname.toString());
209         String JavaDoc realmType = "MemoryRealm";
210         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
211         sb.append(resources.getMessage(locale, "server.service.treeBuilder.realm"));
212         sb.append(" (");
213         sb.append(realmType);
214         sb.append(")");
215         realmFm.setNodeLabel(sb.toString());
216         realmFm.setRealmType(realmType);
217         realmFm.setAllowDeletion(allowDeletion(rname,request));
218
219         String JavaDoc attribute = null;
220         try {
221
222             // Copy scalar properties
223
attribute = "pathname";
224             realmFm.setPathName
225                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
226
227         } catch (Throwable JavaDoc t) {
228             getServlet().log
229                 (resources.getMessage(locale, "users.error.attribute.get",
230                                       attribute), t);
231             response.sendError
232                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
233                  resources.getMessage(locale, "users.error.attribute.get",
234                                       attribute));
235         }
236     }
237
238     private void setUpJDBCRealm(ObjectName JavaDoc rname, HttpServletRequest JavaDoc request,
239                                         HttpServletResponse JavaDoc response)
240     throws IOException JavaDoc {
241         // Fill in the form values for display and editing
242
MessageResources resources = getResources(request);
243         HttpSession JavaDoc session = request.getSession();
244         Locale JavaDoc locale = getLocale(request);
245         JDBCRealmForm realmFm = new JDBCRealmForm();
246         session.setAttribute("jdbcRealmForm", realmFm);
247         realmFm.setAdminAction("Edit");
248         realmFm.setObjectName(rname.toString());
249         String JavaDoc realmType = "JDBCRealm";
250         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
251         sb.append(resources.getMessage(locale, "server.service.treeBuilder.realm"));
252         sb.append(" (");
253         sb.append(realmType);
254         sb.append(")");
255         realmFm.setNodeLabel(sb.toString());
256         realmFm.setRealmType(realmType);
257         realmFm.setAllowDeletion(allowDeletion(rname,request));
258
259         String JavaDoc attribute = null;
260         try {
261
262             // Copy scalar properties
263
attribute = "digest";
264             realmFm.setDigest
265                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
266             attribute = "driverName";
267             realmFm.setDriver
268                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
269             attribute = "roleNameCol";
270             realmFm.setRoleNameCol
271                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
272             attribute = "userNameCol";
273             realmFm.setUserNameCol
274                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
275             attribute = "userCredCol";
276             realmFm.setPasswordCol
277                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
278             attribute = "userTable";
279             realmFm.setUserTable
280                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
281             attribute = "userRoleTable";
282             realmFm.setRoleTable
283                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
284             attribute = "connectionName";
285             realmFm.setConnectionName
286                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
287             attribute = "connectionPassword";
288             realmFm.setConnectionPassword
289                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
290             attribute = "connectionURL";
291             realmFm.setConnectionURL
292                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
293
294         } catch (Throwable JavaDoc t) {
295             getServlet().log
296                 (resources.getMessage(locale, "users.error.attribute.get",
297                                       attribute), t);
298             response.sendError
299                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
300                  resources.getMessage(locale, "users.error.attribute.get",
301                                       attribute));
302         }
303     }
304
305     private void setUpJNDIRealm(ObjectName JavaDoc rname, HttpServletRequest JavaDoc request,
306                                         HttpServletResponse JavaDoc response)
307     throws IOException JavaDoc {
308         // Fill in the form values for display and editing
309
MessageResources resources = getResources(request);
310         HttpSession JavaDoc session = request.getSession();
311         Locale JavaDoc locale = getLocale(request);
312         JNDIRealmForm realmFm = new JNDIRealmForm();
313         session.setAttribute("jndiRealmForm", realmFm);
314         realmFm.setAdminAction("Edit");
315         realmFm.setObjectName(rname.toString());
316         String JavaDoc realmType = "JNDIRealm";
317         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
318         sb.append(resources.getMessage(locale, "server.service.treeBuilder.realm"));
319         sb.append(" (");
320         sb.append(realmType);
321         sb.append(")");
322         realmFm.setNodeLabel(sb.toString());
323         realmFm.setRealmType(realmType);
324         realmFm.setSearchVals(Lists.getBooleanValues());
325         realmFm.setAllowDeletion(allowDeletion(rname,request));
326
327         String JavaDoc attribute = null;
328         try {
329
330             // Copy scalar properties
331
attribute = "digest";
332             realmFm.setDigest
333                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
334             attribute = "userSubtree";
335             realmFm.setUserSubtree
336                     (((Boolean JavaDoc) mBServer.getAttribute(rname, attribute)).toString());
337             attribute = "roleSubtree";
338             realmFm.setRoleSubtree
339                     (((Boolean JavaDoc) mBServer.getAttribute(rname, attribute)).toString());
340             attribute = "userRoleName";
341             realmFm.setUserRoleName
342                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
343             attribute = "roleName";
344             realmFm.setRoleName
345                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
346             attribute = "roleBase";
347             realmFm.setRoleBase
348                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
349             attribute = "roleSearch";
350             realmFm.setRolePattern
351                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
352             attribute = "contextFactory";
353             realmFm.setContextFactory
354                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
355             attribute = "userPassword";
356             realmFm.setUserPassword
357                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
358             attribute = "userPattern";
359             realmFm.setUserPattern
360                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
361             attribute = "userSearch";
362             realmFm.setUserSearch
363                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
364             attribute = "connectionName";
365             realmFm.setConnectionName
366                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
367             attribute = "connectionPassword";
368             realmFm.setConnectionPassword
369                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
370             attribute = "connectionURL";
371             realmFm.setConnectionURL
372                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
373
374         } catch (Throwable JavaDoc t) {
375             getServlet().log
376                 (resources.getMessage(locale, "users.error.attribute.get",
377                                       attribute), t);
378             response.sendError
379                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
380                  resources.getMessage(locale, "users.error.attribute.get",
381                                       attribute));
382         }
383     }
384
385     private void setUpDataSourceRealm(ObjectName JavaDoc rname, HttpServletRequest JavaDoc request,
386                                         HttpServletResponse JavaDoc response)
387     throws IOException JavaDoc {
388         // Fill in the form values for display and editing
389
MessageResources resources = getResources(request);
390         HttpSession JavaDoc session = request.getSession();
391         Locale JavaDoc locale = getLocale(request);
392         DataSourceRealmForm realmFm = new DataSourceRealmForm();
393         session.setAttribute("dataSourceRealmForm", realmFm);
394         realmFm.setAdminAction("Edit");
395         realmFm.setObjectName(rname.toString());
396         String JavaDoc realmType = "DataSourceRealm";
397         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
398         sb.append(resources.getMessage(locale, "server.service.treeBuilder.realm"));
399         sb.append(" (");
400         sb.append(realmType);
401         sb.append(")");
402         realmFm.setNodeLabel(sb.toString());
403         realmFm.setRealmType(realmType);
404         realmFm.setAllowDeletion(allowDeletion(rname,request));
405         realmFm.setBooleanVals(Lists.getBooleanValues());
406
407         String JavaDoc attribute = null;
408         try {
409
410             // Copy scalar properties
411
attribute = "dataSourceName";
412             realmFm.setDataSourceName
413                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
414             attribute = "digest";
415             realmFm.setDigest
416                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
417             attribute = "localDataSource";
418             realmFm.setLocalDataSource
419                 (((Boolean JavaDoc) mBServer.getAttribute(rname, attribute)).toString());
420             attribute = "roleNameCol";
421             realmFm.setRoleNameCol
422                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
423             attribute = "userCredCol";
424             realmFm.setUserCredCol
425                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
426             attribute = "userNameCol";
427             realmFm.setUserNameCol
428                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
429             attribute = "userRoleTable";
430             realmFm.setUserRoleTable
431                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
432             attribute = "userTable";
433             realmFm.setUserTable
434                 ((String JavaDoc) mBServer.getAttribute(rname, attribute));
435
436         } catch (Throwable JavaDoc t) {
437             getServlet().log
438                 (resources.getMessage(locale, "users.error.attribute.get",
439                                       attribute), t);
440             response.sendError
441                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
442                  resources.getMessage(locale, "users.error.attribute.get",
443                                       attribute));
444         }
445     }
446
447     /*
448      * Check if "delete this realm" operation should be enabled.
449      * this operation is not allowed in case the realm is under service,
450      * host or context that the admin app runs on.
451      * return "true" if deletion is allowed.
452      */

453
454     private String JavaDoc allowDeletion(ObjectName JavaDoc rname, HttpServletRequest JavaDoc request) {
455
456      boolean retVal = true;
457      try{
458         // admin app's values
459
String JavaDoc adminService = Lists.getAdminAppService(
460                               mBServer, rname.getDomain(),request);
461         String JavaDoc adminHost = request.getServerName();
462         String JavaDoc adminContext = request.getContextPath();
463
464         //String thisService = rname.getKeyProperty("service");
465
String JavaDoc domain = rname.getDomain();
466         String JavaDoc thisHost = rname.getKeyProperty("host");
467         String JavaDoc thisContext = rname.getKeyProperty("path");
468
469         // realm is under context
470
if (thisContext!=null) {
471             retVal = !(thisContext.equalsIgnoreCase(adminContext));
472         } else if (thisHost != null) {
473             // realm is under host
474
retVal = !(thisHost.equalsIgnoreCase(adminHost));
475         } else {
476             // XXX FIXME
477
// realm is under service
478
return "false";
479             //retVal = !(thisService.equalsIgnoreCase(adminService));
480
}
481
482      } catch (Exception JavaDoc e) {
483            getServlet().log("Error getting admin service, host or context", e);
484      }
485         return new Boolean JavaDoc(retVal).toString();
486     }
487 }
488
Popular Tags