1 16 17 package org.apache.webapp.admin.realm; 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Locale ; 23 import java.util.ArrayList ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.http.HttpSession ; 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 ; 36 import javax.management.ObjectInstance ; 37 import javax.management.ObjectName ; 38 import javax.management.JMException ; 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 52 53 public class EditRealmAction extends Action { 54 55 56 59 private MBeanServer mBServer = null; 60 61 62 64 79 public ActionForward execute(ActionMapping mapping, 80 ActionForm form, 81 HttpServletRequest request, 82 HttpServletResponse response) 83 throws IOException , ServletException { 84 85 HttpSession session = request.getSession(); 87 Locale locale = getLocale(request); 88 MessageResources resources = getResources(request); 89 90 try { 92 mBServer = ((ApplicationServlet) getServlet()).getServer(); 93 } catch (Throwable t) { 94 throw new ServletException 95 ("Cannot acquire MBeanServer reference", t); 96 } 97 98 ObjectName rname = null; 100 StringBuffer sb = null; 101 try { 102 rname = new ObjectName (request.getParameter("select")); 103 } catch (Exception e) { 104 String 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 realmType = null; 113 String attribute = null; 114 115 try { 117 attribute = "className"; 118 String className = (String ) 119 mBServer.getAttribute(rname, attribute); 120 int period = className.lastIndexOf("."); 121 if (period >= 0) 122 realmType = className.substring(period + 1); 123 } catch (Throwable 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 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 rname, 153 HttpServletRequest request, 154 HttpServletResponse response) 155 throws IOException { 156 MessageResources resources = getResources(request); 158 HttpSession session = request.getSession(); 159 Locale locale = getLocale(request); 160 UserDatabaseRealmForm realmFm = new UserDatabaseRealmForm(); 161 session.setAttribute("userDatabaseRealmForm", realmFm); 162 realmFm.setAdminAction("Edit"); 163 realmFm.setObjectName(rname.toString()); 164 String realmType = "UserDatabaseRealm"; 165 StringBuffer sb = new StringBuffer (""); 166 String host = rname.getKeyProperty("host"); 167 String 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 attribute = null; 180 try { 181 182 attribute = "resourceName"; 184 realmFm.setResource 185 ((String ) mBServer.getAttribute(rname, attribute)); 186 187 } catch (Throwable 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 rname, HttpServletRequest request, 199 HttpServletResponse response) 200 throws IOException { 201 MessageResources resources = getResources(request); 203 HttpSession session = request.getSession(); 204 Locale locale = getLocale(request); 205 MemoryRealmForm realmFm = new MemoryRealmForm(); 206 session.setAttribute("memoryRealmForm", realmFm); 207 realmFm.setAdminAction("Edit"); 208 realmFm.setObjectName(rname.toString()); 209 String realmType = "MemoryRealm"; 210 StringBuffer sb = new StringBuffer (); 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 attribute = null; 220 try { 221 222 attribute = "pathname"; 224 realmFm.setPathName 225 ((String ) mBServer.getAttribute(rname, attribute)); 226 227 } catch (Throwable 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 rname, HttpServletRequest request, 239 HttpServletResponse response) 240 throws IOException { 241 MessageResources resources = getResources(request); 243 HttpSession session = request.getSession(); 244 Locale locale = getLocale(request); 245 JDBCRealmForm realmFm = new JDBCRealmForm(); 246 session.setAttribute("jdbcRealmForm", realmFm); 247 realmFm.setAdminAction("Edit"); 248 realmFm.setObjectName(rname.toString()); 249 String realmType = "JDBCRealm"; 250 StringBuffer sb = new StringBuffer (); 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 attribute = null; 260 try { 261 262 attribute = "digest"; 264 realmFm.setDigest 265 ((String ) mBServer.getAttribute(rname, attribute)); 266 attribute = "driverName"; 267 realmFm.setDriver 268 ((String ) mBServer.getAttribute(rname, attribute)); 269 attribute = "roleNameCol"; 270 realmFm.setRoleNameCol 271 ((String ) mBServer.getAttribute(rname, attribute)); 272 attribute = "userNameCol"; 273 realmFm.setUserNameCol 274 ((String ) mBServer.getAttribute(rname, attribute)); 275 attribute = "userCredCol"; 276 realmFm.setPasswordCol 277 ((String ) mBServer.getAttribute(rname, attribute)); 278 attribute = "userTable"; 279 realmFm.setUserTable 280 ((String ) mBServer.getAttribute(rname, attribute)); 281 attribute = "userRoleTable"; 282 realmFm.setRoleTable 283 ((String ) mBServer.getAttribute(rname, attribute)); 284 attribute = "connectionName"; 285 realmFm.setConnectionName 286 ((String ) mBServer.getAttribute(rname, attribute)); 287 attribute = "connectionPassword"; 288 realmFm.setConnectionPassword 289 ((String ) mBServer.getAttribute(rname, attribute)); 290 attribute = "connectionURL"; 291 realmFm.setConnectionURL 292 ((String ) mBServer.getAttribute(rname, attribute)); 293 294 } catch (Throwable 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 rname, HttpServletRequest request, 306 HttpServletResponse response) 307 throws IOException { 308 MessageResources resources = getResources(request); 310 HttpSession session = request.getSession(); 311 Locale locale = getLocale(request); 312 JNDIRealmForm realmFm = new JNDIRealmForm(); 313 session.setAttribute("jndiRealmForm", realmFm); 314 realmFm.setAdminAction("Edit"); 315 realmFm.setObjectName(rname.toString()); 316 String realmType = "JNDIRealm"; 317 StringBuffer sb = new StringBuffer (); 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 attribute = null; 328 try { 329 330 attribute = "digest"; 332 realmFm.setDigest 333 ((String ) mBServer.getAttribute(rname, attribute)); 334 attribute = "userSubtree"; 335 realmFm.setUserSubtree 336 (((Boolean ) mBServer.getAttribute(rname, attribute)).toString()); 337 attribute = "roleSubtree"; 338 realmFm.setRoleSubtree 339 (((Boolean ) mBServer.getAttribute(rname, attribute)).toString()); 340 attribute = "userRoleName"; 341 realmFm.setUserRoleName 342 ((String ) mBServer.getAttribute(rname, attribute)); 343 attribute = "roleName"; 344 realmFm.setRoleName 345 ((String ) mBServer.getAttribute(rname, attribute)); 346 attribute = "roleBase"; 347 realmFm.setRoleBase 348 ((String ) mBServer.getAttribute(rname, attribute)); 349 attribute = "roleSearch"; 350 realmFm.setRolePattern 351 ((String ) mBServer.getAttribute(rname, attribute)); 352 attribute = "contextFactory"; 353 realmFm.setContextFactory 354 ((String ) mBServer.getAttribute(rname, attribute)); 355 attribute = "userPassword"; 356 realmFm.setUserPassword 357 ((String ) mBServer.getAttribute(rname, attribute)); 358 attribute = "userPattern"; 359 realmFm.setUserPattern 360 ((String ) mBServer.getAttribute(rname, attribute)); 361 attribute = "userSearch"; 362 realmFm.setUserSearch 363 ((String ) mBServer.getAttribute(rname, attribute)); 364 attribute = "connectionName"; 365 realmFm.setConnectionName 366 ((String ) mBServer.getAttribute(rname, attribute)); 367 attribute = "connectionPassword"; 368 realmFm.setConnectionPassword 369 ((String ) mBServer.getAttribute(rname, attribute)); 370 attribute = "connectionURL"; 371 realmFm.setConnectionURL 372 ((String ) mBServer.getAttribute(rname, attribute)); 373 374 } catch (Throwable 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 rname, HttpServletRequest request, 386 HttpServletResponse response) 387 throws IOException { 388 MessageResources resources = getResources(request); 390 HttpSession session = request.getSession(); 391 Locale locale = getLocale(request); 392 DataSourceRealmForm realmFm = new DataSourceRealmForm(); 393 session.setAttribute("dataSourceRealmForm", realmFm); 394 realmFm.setAdminAction("Edit"); 395 realmFm.setObjectName(rname.toString()); 396 String realmType = "DataSourceRealm"; 397 StringBuffer sb = new StringBuffer (); 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 attribute = null; 408 try { 409 410 attribute = "dataSourceName"; 412 realmFm.setDataSourceName 413 ((String ) mBServer.getAttribute(rname, attribute)); 414 attribute = "digest"; 415 realmFm.setDigest 416 ((String ) mBServer.getAttribute(rname, attribute)); 417 attribute = "localDataSource"; 418 realmFm.setLocalDataSource 419 (((Boolean ) mBServer.getAttribute(rname, attribute)).toString()); 420 attribute = "roleNameCol"; 421 realmFm.setRoleNameCol 422 ((String ) mBServer.getAttribute(rname, attribute)); 423 attribute = "userCredCol"; 424 realmFm.setUserCredCol 425 ((String ) mBServer.getAttribute(rname, attribute)); 426 attribute = "userNameCol"; 427 realmFm.setUserNameCol 428 ((String ) mBServer.getAttribute(rname, attribute)); 429 attribute = "userRoleTable"; 430 realmFm.setUserRoleTable 431 ((String ) mBServer.getAttribute(rname, attribute)); 432 attribute = "userTable"; 433 realmFm.setUserTable 434 ((String ) mBServer.getAttribute(rname, attribute)); 435 436 } catch (Throwable 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 453 454 private String allowDeletion(ObjectName rname, HttpServletRequest request) { 455 456 boolean retVal = true; 457 try{ 458 String adminService = Lists.getAdminAppService( 460 mBServer, rname.getDomain(),request); 461 String adminHost = request.getServerName(); 462 String adminContext = request.getContextPath(); 463 464 String domain = rname.getDomain(); 466 String thisHost = rname.getKeyProperty("host"); 467 String thisContext = rname.getKeyProperty("path"); 468 469 if (thisContext!=null) { 471 retVal = !(thisContext.equalsIgnoreCase(adminContext)); 472 } else if (thisHost != null) { 473 retVal = !(thisHost.equalsIgnoreCase(adminHost)); 475 } else { 476 return "false"; 479 } 481 482 } catch (Exception e) { 483 getServlet().log("Error getting admin service, host or context", e); 484 } 485 return new Boolean (retVal).toString(); 486 } 487 } 488 | Popular Tags |