1 package org.tigris.scarab.util; 2 3 48 49 import java.util.Enumeration ; 50 51 import org.apache.fulcrum.parser.ParameterParser; 52 import org.apache.fulcrum.parser.ValueParser; 53 import org.apache.fulcrum.pool.InitableRecyclable; 54 import org.apache.turbine.DynamicURI; 55 import org.apache.turbine.RunData; 56 import org.apache.turbine.Turbine; 57 import org.apache.turbine.tool.TemplateLink; 58 import org.tigris.scarab.om.Issue; 59 import org.tigris.scarab.om.Module; 60 import org.tigris.scarab.om.ModuleManager; 61 import org.tigris.scarab.om.ScarabUser; 62 import org.tigris.scarab.services.security.ScarabSecurity; 63 64 73 public class ScarabLink extends TemplateLink 74 implements InitableRecyclable, SkipFiltering 75 { 76 private RunData data; 77 private String label; 78 private String attributeText; 79 private String alternateText; 80 private String currentModuleId; 81 private String lastUsedModuleId; 82 private Module currentModule; 83 private Module lastUsedModule; 84 private boolean isOmitModule; 85 private boolean overrideSecurity; 86 87 91 public ScarabLink() 92 { 93 } 94 95 102 public void init(Object data) 103 { 104 super.init(data); 108 this.data = (RunData)data; 109 setAbsolute(false); 110 } 111 112 public void refresh() 113 { 114 super.refresh(); 115 setAbsolute(false); 116 label = null; 117 attributeText = null; 118 alternateText = null; 119 lastUsedModuleId = null; 120 lastUsedModule = null; 121 super.setPage(null); 122 super.removePathInfo(ScarabConstants.TEMPLATE); 123 isOmitModule = false; 124 overrideSecurity = false; 125 } 126 127 private void initCurrentModule() 128 { 129 if (currentModule == null) 130 { 131 ScarabUser user = (ScarabUser)data.getUser(); 132 if (user != null) 133 { 134 currentModule = user.getCurrentModule(); 135 } 136 } 137 } 138 139 144 public String getServerName() 145 { 146 initCurrentModule(); 147 String result = null; 148 if (currentModule != null) 149 { 150 result = currentModule.getHttpDomain(); 151 } 152 if (result == null || result.length() == 0) 153 { 154 result = super.getServerName(); 155 } 156 return result; 157 } 158 159 164 public int getServerPort() 165 { 166 initCurrentModule(); 167 int result = -1; 168 try 169 { 170 if (currentModule != null) 171 { 172 String port = currentModule.getPort(); 173 if (port.length() == 0) 174 { 175 result = super.getServerPort(); 176 } 177 else 178 { 179 result = Integer.parseInt(port); 180 } 181 } 182 } 183 catch (Exception e) 184 { 185 Log.get().debug("Could not parse port number", e); 186 } 187 if (result == -1) 188 { 189 result = super.getServerPort(); 190 } 191 return result; 192 } 193 194 199 public String getServerScheme() 200 { 201 initCurrentModule(); 202 String result = null; 203 try 204 { 205 if (currentModule != null) 206 { 207 result = currentModule.getScheme(); 208 } 209 } 210 catch (Exception e) 211 { 212 Log.get().debug("Could not get scheme parameter", e); 213 } 214 if (result == null || result.length() == 0) 215 { 216 result = super.getServerScheme(); 217 } 218 return result; 219 } 220 221 226 public String getScriptName() 227 { 228 initCurrentModule(); 229 String result = null; 230 try 231 { 232 if (currentModule != null) 233 { 234 result = currentModule.getScriptName(); 235 } 236 } 237 catch (Exception e) 238 { 239 Log.get().debug("Could not get script name parameter", e); 240 } 241 if (result == null || result.length() == 0) 242 { 243 result = super.getScriptName(); 244 } 245 return result; 246 } 247 248 254 public TemplateLink setPage(String t) 255 { 256 String moduleid = data.getParameters().getString(ScarabConstants.CURRENT_MODULE); 257 return setPage(t, moduleid); 258 } 259 260 266 public ScarabLink omitModule() 267 { 268 isOmitModule = true; 269 return this; 270 } 271 272 283 public ScarabLink overrideSecurity() 284 { 285 overrideSecurity = true; 286 return this; 287 } 288 289 297 protected TemplateLink setPage(String t, String moduleid) 298 { 299 300 this.currentModuleId = moduleid; 301 this.lastUsedModuleId = moduleid; 302 303 if (isSet(moduleid) && !isOmitModule) 304 { 305 addPathInfo(ScarabConstants.CURRENT_MODULE, moduleid); 306 } 307 308 String issueKey = data.getParameters() 309 .getString(ScarabConstants.REPORTING_ISSUE); 310 if (isSet(issueKey)) 311 { 312 addPathInfo(ScarabConstants.REPORTING_ISSUE, issueKey); 313 } 314 Object threadKey = ((ScarabUser)data.getUser()).getThreadKey(); 315 if (threadKey != null) 316 { 317 addPathInfo(ScarabConstants.THREAD_QUERY_KEY, threadKey); 318 } 319 String reportKey = data.getParameters() 320 .getString(ScarabConstants.CURRENT_REPORT); 321 if (isSet(reportKey)) 322 { 323 if (t.startsWith("report")) 324 { 325 addPathInfo(ScarabConstants.CURRENT_REPORT, reportKey); 326 } 327 else if (!t.startsWith("help")) 328 { 329 addPathInfo(ScarabConstants.REMOVE_CURRENT_REPORT, reportKey); 330 } 331 } 332 String historyScreen = data.getParameters() 334 .getString(ScarabConstants.HISTORY_SCREEN); 335 if (isSet(historyScreen)) 336 { 337 addPathInfo(ScarabConstants.HISTORY_SCREEN, historyScreen); 338 } 339 String adminMenu = data.getParameters() 341 .getString(ScarabConstants.CURRENT_ADMIN_MENU); 342 if (isSet(adminMenu)) 343 { 344 addPathInfo(ScarabConstants.CURRENT_ADMIN_MENU, adminMenu); 345 } 346 String debug = data.getParameters() 348 .getString(ScarabConstants.DEBUG); 349 if (isSet(debug)) 350 { 351 addPathInfo(ScarabConstants.DEBUG, debug); 352 } 353 354 super.setPage(t); 355 return this; 356 } 357 358 private boolean isSet(String s) 359 { 360 return s != null && s.length() > 0; 361 } 362 363 366 public String getCurrentView() 367 { 368 String temp = data.getParameters().getString(ScarabConstants.TEMPLATE, null); 369 if (temp != null) 370 { 371 temp = temp.replace(',', '/'); 372 } 373 return temp; 374 } 375 376 public ScarabLink setPathInfo(String key, String value) 377 { 378 removePathInfo(key); 379 addPathInfo(key, value); 380 return this; 381 } 382 383 public ScarabLink addPathInfo(String key, ParameterParser pp) 385 { 386 addPathInfo(key, pp); 387 return this; 388 } 389 390 397 public DynamicURI addPathInfo(String name, boolean value) 398 { 399 addPathInfo(name, (value ? "true" : "false")); 400 return this; 401 } 402 403 407 public ScarabLink addPathInfo(ValueParser pp) 408 { 409 Enumeration e = pp.keys(); 412 while (e.hasMoreElements()) 413 { 414 String key = (String )e.nextElement(); 415 if (!key.equalsIgnoreCase(Turbine.ACTION) && 416 !key.equalsIgnoreCase(Turbine.SCREEN) && 417 !key.equalsIgnoreCase(Turbine.TEMPLATE)) 418 { 419 String [] values = pp.getStrings(key); 420 for (int i=0; i<values.length; i++) 421 { 422 addPathInfo(key, values[i]); 423 } 424 } 425 } 426 return this; 427 } 428 429 438 public ScarabLink setLabel(String label) 439 { 440 this.label = label; 441 return this; 442 } 443 444 453 public ScarabLink setAttributeText(String attributeText) 454 { 455 this.attributeText = attributeText; 456 return this; 457 } 458 459 466 public ScarabLink setAlternateText(String alternateText) 467 { 468 this.alternateText = alternateText; 469 return this; 470 } 471 472 477 public String toString() 478 { 479 String tostring = null; 480 String alternateText = this.alternateText; 481 if (isAllowed()) 483 { 484 tostring = getLink(); 485 } 486 else 487 { 488 tostring = (alternateText == null) ? "" : alternateText; 489 } 490 refresh(); 491 return tostring; 492 } 493 494 501 public ScarabLink getIssueIdLink(Issue issue) 502 throws Exception 503 { 504 this.addPathInfo("id", issue.getUniqueId()); 505 return this; 506 } 507 508 517 public ScarabLink getIssueIdAbsoluteLink(Issue issue) 518 throws Exception 519 { 520 ScarabLink link = getIssueIdLink(issue); 521 link.setRelative(false).setEncodeUrl(false); 522 return link; 523 } 524 525 532 public boolean isAllowed() 533 { 534 boolean allowed = overrideSecurity || isAllowed(getPage()); 535 536 if (!allowed) 537 { 538 super.toString(); 540 refresh(); 541 } 542 543 return allowed; 544 } 545 546 551 public boolean isAllowed(String t) 552 { 553 if (t == null) 554 { 555 int count = pathInfo.size(); 557 for (int i = 0; i < count; i++) 558 { 559 Object [] pair = (Object []) pathInfo.get(i); 560 if ("id".equals(pair[0])) 561 { 562 t = "ViewIssue.vm"; 563 break; 564 } 565 } 566 if (t == null) 567 { 568 count = queryData.size(); 570 for (int i = 0; i < count; i++) 571 { 572 Object [] pair = (Object []) queryData.get(i); 573 if ("id".equals(pair[0])) 574 { 575 t = "ViewIssue.vm"; 576 break; 577 } 578 } 579 if (t == null) 580 { 581 return false; 582 } 583 } 584 } 585 boolean allowed = false; 586 try 587 { 588 String perm = ScarabSecurity.getScreenPermission(t); 589 if (perm != null) 590 { 591 initCurrentModule(); 592 593 if (currentModuleId != null) 594 { 595 if (currentModule == null || 596 !currentModule.getModuleId().toString() 597 .equals(currentModuleId)) 598 { 599 currentModule = ModuleManager 600 .getInstance(new Integer (currentModuleId)); 601 } 602 } 603 ScarabUser user = (ScarabUser)data.getUser(); 604 allowed = currentModule != null 605 && (user.hasLoggedIn() || AnonymousUserUtil.isUserAnonymous(user)) 606 && user.hasPermission(perm, currentModule); 607 } 608 else 609 { 610 allowed = true; 611 } 612 } 613 catch (Exception e) 614 { 615 allowed = false; 616 Log.get().info("Could not check permission due to: ", e); 617 } 618 return allowed; 619 } 620 621 private String getLink() 622 { 623 String s = null; 624 if (label != null && label.length() > 0) 625 { 626 StringBuffer sbuf = new StringBuffer (50); 627 sbuf.append("<a "); 628 if (attributeText != null && attributeText.length() > 0) 629 { 630 sbuf.append(attributeText); 631 sbuf.append(' '); 632 } 633 sbuf.append("href=\"") 634 .append(super.toString()) 635 .append("\">") 636 .append(label) 637 .append("</a>"); 638 s = sbuf.toString(); 639 } 640 else 641 { 642 s = super.toString(); 643 } 644 return s; 645 } 646 647 651 protected RunData getRunData() 652 { 653 return data; 654 } 655 656 662 private boolean disposed = false; 663 664 667 public void recycle() 668 { 669 disposed = false; 670 } 671 672 675 public void dispose() 676 { 677 data = null; 678 refresh(); 679 disposed = true; 680 } 681 682 687 public boolean isDisposed() 688 { 689 return disposed; 690 } 691 } 692 | Popular Tags |