1 31 32 package org.opencms.workplace.tools; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProperty; 36 import org.opencms.file.CmsResource; 37 import org.opencms.i18n.CmsEncoder; 38 import org.opencms.jsp.CmsJspActionElement; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.util.CmsIdentifiableObjectContainer; 42 import org.opencms.util.CmsRequestUtil; 43 import org.opencms.util.CmsStringUtil; 44 import org.opencms.workplace.CmsDialog; 45 import org.opencms.workplace.CmsWorkplace; 46 47 import java.io.IOException ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 import java.util.Map ; 53 54 import javax.servlet.ServletException ; 55 56 import org.apache.commons.logging.Log; 57 58 70 public class CmsToolManager { 71 72 73 public static final String ADMINVIEW_ROOT_LOCATION = CmsWorkplace.PATH_WORKPLACE + "views/admin"; 74 75 76 public static final String HANDLERCLASS_PROPERTY = "admintoolhandler-class"; 77 78 79 public static final String NAVBAR_SEPARATOR = "\n > \n"; 80 81 82 public static final String ROOT_SEPARATOR = ":"; 83 84 85 public static final String ROOTKEY_DEFAULT = "admin"; 86 87 88 public static final String TOOLPATH_SEPARATOR = "/"; 89 90 91 public static final String VIEW_JSPPAGE_LOCATION = ADMINVIEW_ROOT_LOCATION + "/admin-main.jsp"; 92 93 94 private static final Log LOG = CmsLog.getLog(CmsToolManager.class); 95 96 97 private final CmsIdentifiableObjectContainer m_roots; 98 99 100 private final CmsIdentifiableObjectContainer m_tools; 101 102 103 private final CmsIdentifiableObjectContainer m_urls; 104 105 108 public CmsToolManager() { 109 110 m_roots = new CmsIdentifiableObjectContainer(true, false); 111 m_tools = new CmsIdentifiableObjectContainer(true, false); 112 m_urls = new CmsIdentifiableObjectContainer(false, false); 113 } 114 115 123 public static String linkForToolPath(CmsJspActionElement jsp, String toolPath) { 124 125 StringBuffer result = new StringBuffer (); 126 result.append(jsp.link(VIEW_JSPPAGE_LOCATION)); 127 result.append('?'); 128 result.append(CmsToolDialog.PARAM_PATH); 129 result.append('='); 130 result.append(CmsEncoder.encode(toolPath)); 131 return result.toString(); 132 } 133 134 147 public static String linkForToolPath(CmsJspActionElement jsp, String toolPath, Map params) { 148 149 if (params == null) { 150 return linkForToolPath(jsp, toolPath); 152 } 153 params.put(CmsToolDialog.PARAM_PATH, toolPath); 154 return CmsRequestUtil.appendParameters(jsp.link(VIEW_JSPPAGE_LOCATION), params, true); 155 } 156 157 162 public void addToolRoot(CmsToolRootHandler toolRoot) { 163 164 m_roots.addIdentifiableObject(toolRoot.getKey(), toolRoot); 165 } 166 167 172 public void configure(CmsObject cms) { 173 174 if (CmsLog.INIT.isInfoEnabled()) { 175 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_TOOLMANAGER_CREATED_0)); 176 } 177 if (m_roots.getObject(ROOTKEY_DEFAULT) == null) { 178 CmsToolRootHandler defToolRoot = new CmsToolRootHandler(); 179 defToolRoot.setKey(ROOTKEY_DEFAULT); 180 defToolRoot.setUri(CmsWorkplace.PATH_WORKPLACE + "admin/"); 181 defToolRoot.setName("${key." + Messages.GUI_ADMIN_VIEW_ROOT_NAME_0 + "}"); 182 defToolRoot.setHelpText("${key." + Messages.GUI_ADMIN_VIEW_ROOT_HELP_0 + "}"); 183 addToolRoot(defToolRoot); 184 } 185 m_tools.clear(); 186 m_urls.clear(); 187 Iterator it = getToolRoots().iterator(); 188 while (it.hasNext()) { 189 CmsToolRootHandler toolRoot = (CmsToolRootHandler)it.next(); 190 if (!cms.existsResource(toolRoot.getUri())) { 191 if (CmsLog.INIT.isInfoEnabled()) { 192 CmsLog.INIT.info(Messages.get().getBundle().key( 193 Messages.INIT_TOOLMANAGER_ROOT_SKIPPED_2, 194 toolRoot.getKey(), 195 toolRoot.getUri())); 196 } 197 continue; 198 } 199 try { 200 toolRoot.setup(cms, null, toolRoot.getUri()); 201 configureToolRoot(cms, toolRoot); 202 if (CmsLog.INIT.isInfoEnabled()) { 204 CmsLog.INIT.info(Messages.get().getBundle().key( 205 Messages.INIT_TOOLMANAGER_SETUP_1, 206 toolRoot.getKey())); 207 } 208 } catch (CmsException e) { 209 if (CmsLog.INIT.isWarnEnabled()) { 211 CmsLog.INIT.warn(Messages.get().getBundle().key( 212 Messages.INIT_TOOLMANAGER_SETUP_ERROR_1, 213 toolRoot.getKey()), e); 214 } 215 } 216 } 217 } 218 219 227 public String generateNavBar(String toolPath, CmsWorkplace wp) { 228 229 if (toolPath.equals(getBaseToolPath(wp))) { 230 return "<div class='pathbar'> </div>\n"; 231 } 232 CmsTool adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), toolPath); 233 String html = A_CmsHtmlIconButton.defaultButtonHtml( 234 wp.getJsp(), 235 CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT, 236 "nav" + adminTool.getId(), 237 adminTool.getHandler().getShortName(), 238 null, 239 false, 240 null, 241 null, 242 null); 243 String parent = toolPath; 244 while (!parent.equals(getBaseToolPath(wp))) { 245 parent = getParent(wp, parent); 246 adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), parent); 247 248 String id = "nav" + adminTool.getId(); 249 String link = linkForToolPath(wp.getJsp(), parent, adminTool.getHandler().getParameters(wp)); 250 String onClic = "openPage('" + link + "');"; 251 String buttonHtml = A_CmsHtmlIconButton.defaultButtonHtml( 252 wp.getJsp(), 253 CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT, 254 id, 255 adminTool.getHandler().getName(), 256 adminTool.getHandler().getHelpText(), 257 true, 258 null, 259 null, 260 onClic); 261 html = buttonHtml + NAVBAR_SEPARATOR + html; 262 } 263 264 return CmsToolMacroResolver.resolveMacros("<div class='pathbar'>\n" + html + " </div>\n", wp); 265 } 266 267 274 public String getBaseToolPath(CmsWorkplace wp) { 275 276 CmsToolUserData userData = getUserData(wp); 277 String path = TOOLPATH_SEPARATOR; 278 if (userData != null) { 279 path = userData.getBaseTool(getCurrentRoot(wp).getKey()); 280 } 281 return path; 282 } 283 284 291 public CmsToolRootHandler getCurrentRoot(CmsWorkplace wp) { 292 293 CmsToolUserData userData = getUserData(wp); 294 String root = ROOTKEY_DEFAULT; 295 if (userData != null && m_roots.getObject(userData.getRootKey()) != null) { 296 root = userData.getRootKey(); 297 } 298 return (CmsToolRootHandler)m_roots.getObject(root); 299 } 300 301 308 public CmsTool getCurrentTool(CmsWorkplace wp) { 309 310 return resolveAdminTool(getCurrentRoot(wp).getKey(), getCurrentToolPath(wp)); 311 } 312 313 320 public String getCurrentToolPath(CmsWorkplace wp) { 321 322 CmsToolUserData userData = getUserData(wp); 323 String path = getBaseToolPath(wp); 324 if (userData != null) { 325 path = userData.getCurrentToolPath(getCurrentRoot(wp).getKey()); 326 } 327 return path; 328 } 329 330 340 public String getParent(CmsWorkplace wp, String toolPath) { 341 342 if (toolPath.equals(getBaseToolPath(wp))) { 343 return toolPath; 344 } 345 int pos = toolPath.lastIndexOf(TOOLPATH_SEPARATOR); 346 return pos <= 0 ? TOOLPATH_SEPARATOR : toolPath.substring(0, pos); 347 } 348 349 354 public List getToolHandlers() { 355 356 return m_tools.elementList(); 357 } 358 359 364 public List getToolRoots() { 365 366 return m_roots.elementList(); 367 } 368 369 378 public List getToolsForPath(CmsWorkplace wp, String baseTool, boolean includeSubtools) { 379 380 List toolList = new ArrayList (); 381 String rootKey = getCurrentRoot(wp).getKey(); 382 Iterator itTools = m_tools.elementList().iterator(); 383 while (itTools.hasNext()) { 384 CmsTool tool = (CmsTool)itTools.next(); 385 String path = tool.getHandler().getPath(); 386 if (resolveAdminTool(rootKey, path) != tool) { 387 continue; 388 } 389 if (path.equals(TOOLPATH_SEPARATOR)) { 390 continue; 391 } 392 if (!path.startsWith(baseTool)) { 394 continue; 395 } 396 if (baseTool.equals(TOOLPATH_SEPARATOR) || path.startsWith(baseTool + TOOLPATH_SEPARATOR)) { 398 if (includeSubtools || path.indexOf(TOOLPATH_SEPARATOR, baseTool.length() + 1) < 0) { 400 toolList.add(tool); 401 } 402 } 403 } 404 return toolList; 405 } 406 407 414 public CmsToolUserData getUserData(CmsWorkplace wp) { 415 416 CmsToolUserData userData = wp.getSettings().getToolUserData(); 417 if (userData == null) { 418 userData = new CmsToolUserData(); 419 userData.setRootKey(ROOTKEY_DEFAULT); 420 Iterator it = getToolRoots().iterator(); 421 while (it.hasNext()) { 422 CmsToolRootHandler root = (CmsToolRootHandler)it.next(); 423 userData.setCurrentToolPath(root.getKey(), TOOLPATH_SEPARATOR); 424 userData.setBaseTool(root.getKey(), TOOLPATH_SEPARATOR); 425 } 426 wp.getSettings().setToolUserData(userData); 427 } 428 return userData; 429 } 430 431 438 public boolean hasToolPathForUrl(String url) { 439 440 List toolPaths = (List )m_urls.getObject(url); 441 return (toolPaths != null && !toolPaths.isEmpty()); 442 } 443 444 449 public synchronized void initParams(CmsToolDialog wp) { 450 451 setCurrentRoot(wp, wp.getParamRoot()); 452 setCurrentToolPath(wp, wp.getParamPath()); 453 setBaseToolPath(wp, wp.getParamBase()); 454 455 if (!getCurrentToolPath(wp).startsWith(getBaseToolPath(wp))) { 457 setCurrentToolPath(wp, getBaseToolPath(wp)); 458 } 459 wp.setParamPath(getCurrentToolPath(wp)); 460 wp.setParamBase(getBaseToolPath(wp)); 461 wp.setParamRoot(getCurrentRoot(wp).getKey()); 462 } 463 464 474 public void jspForwardPage(CmsWorkplace wp, String pagePath, Map params) throws IOException , ServletException { 475 476 Map newParams = new HashMap (); 477 if (pagePath.indexOf("?") > 0) { 479 String query = pagePath.substring(pagePath.indexOf("?")); 480 pagePath = pagePath.substring(0, pagePath.indexOf("?")); 481 Map reqParameters = CmsRequestUtil.createParameterMap(query); 482 newParams.putAll(reqParameters); 483 } 484 if (params != null) { 485 newParams.putAll(params); 486 } 487 488 if (!newParams.containsKey(CmsDialog.PARAM_CLOSELINK)) { 490 Map argMap = resolveAdminTool(getCurrentRoot(wp).getKey(), getCurrentToolPath(wp)).getHandler().getParameters( 491 wp); 492 newParams.put(CmsDialog.PARAM_CLOSELINK, linkForToolPath(wp.getJsp(), getCurrentToolPath(wp), argMap)); 493 } 494 wp.setForwarded(true); 495 CmsRequestUtil.forwardRequest( 497 wp.getJsp().link(pagePath), 498 CmsRequestUtil.createParameterMap(newParams), 499 wp.getJsp().getRequest(), 500 wp.getJsp().getResponse()); 501 } 502 503 513 public void jspForwardTool(CmsWorkplace wp, String toolPath, Map params) throws IOException , ServletException { 514 515 Map newParams; 516 if (params == null) { 517 newParams = new HashMap (); 518 } else { 519 newParams = new HashMap (params); 520 } 521 newParams.put(CmsToolDialog.PARAM_PATH, toolPath); 523 jspForwardPage(wp, VIEW_JSPPAGE_LOCATION, newParams); 524 } 525 526 534 public CmsTool resolveAdminTool(String rootKey, String toolPath) { 535 536 return (CmsTool)m_tools.getObject(rootKey + ROOT_SEPARATOR + toolPath); 537 } 538 539 545 public void setBaseToolPath(CmsWorkplace wp, String baseToolPath) { 546 547 if (CmsStringUtil.isEmpty(baseToolPath) || baseToolPath.trim().equals("null")) { 549 baseToolPath = getBaseToolPath(wp); 550 } 551 baseToolPath = repairPath(wp, baseToolPath); 552 CmsToolUserData userData = getUserData(wp); 554 userData.setBaseTool(userData.getRootKey(), baseToolPath); 555 } 556 557 563 public void setCurrentRoot(CmsWorkplace wp, String key) { 564 565 if (CmsStringUtil.isEmpty(key) || key.trim().equals("null")) { 567 key = getCurrentRoot(wp).getKey(); 568 } 569 CmsToolUserData userData = getUserData(wp); 571 userData.setRootKey(key); 572 } 573 574 580 public void setCurrentToolPath(CmsWorkplace wp, String currentToolPath) { 581 582 if (CmsStringUtil.isEmptyOrWhitespaceOnly(currentToolPath) || currentToolPath.trim().equals("null")) { 584 currentToolPath = getCurrentToolPath(wp); 585 } 586 currentToolPath = repairPath(wp, currentToolPath); 587 CmsToolUserData userData = getUserData(wp); 589 userData.setCurrentToolPath(userData.getRootKey(), currentToolPath); 590 } 591 592 599 private void configureToolRoot(CmsObject cms, CmsToolRootHandler toolRoot) throws CmsException { 600 601 List handlers = new ArrayList (); 602 603 handlers.add(toolRoot); 605 606 List resources = cms.readResourcesWithProperty(toolRoot.getUri(), HANDLERCLASS_PROPERTY); 609 Iterator itRes = resources.iterator(); 610 while (itRes.hasNext()) { 611 CmsResource res = (CmsResource)itRes.next(); 612 CmsProperty prop = cms.readPropertyObject(res.getRootPath(), HANDLERCLASS_PROPERTY, false); 613 if (!prop.isNullProperty()) { 614 try { 615 Class handlerClass = Class.forName(prop.getValue()); 617 I_CmsToolHandler handler = (I_CmsToolHandler)handlerClass.newInstance(); 618 619 if (!handler.setup(cms, toolRoot, res.getRootPath())) { 620 if (CmsLog.INIT.isWarnEnabled()) { 622 CmsLog.INIT.warn(Messages.get().getBundle().key( 623 Messages.INIT_TOOLMANAGER_TOOL_SETUP_ERROR_1, 624 res.getRootPath())); 625 } 626 } 627 628 handlers.add(handler); 630 if (CmsLog.INIT.isDebugEnabled()) { 632 if (!handler.getLink().equals(VIEW_JSPPAGE_LOCATION)) { 633 CmsLog.INIT.debug(Messages.get().getBundle().key( 634 Messages.INIT_TOOLMANAGER_NEWTOOL_FOUND_2, 635 handler.getPath(), 636 handler.getLink())); 637 } else { 638 CmsLog.INIT.debug(Messages.get().getBundle().key( 639 Messages.INIT_TOOLMANAGER_NEWTOOL_FOUND_2, 640 handler.getPath(), 641 res.getRootPath())); 642 } 643 } 644 } catch (Exception e) { 645 if (CmsLog.INIT.isWarnEnabled()) { 647 CmsLog.INIT.warn(Messages.get().getBundle().key( 648 Messages.INIT_TOOLMANAGER_TOOL_SETUP_ERROR_1, 649 res.getRootPath()), e); 650 } 651 } 652 } 653 } 654 registerHandlerList(cms, toolRoot, 1, handlers); 655 } 656 657 664 private void registerAdminTool(CmsObject cms, CmsToolRootHandler toolRoot, I_CmsToolHandler handler) { 665 666 String link = handler.getLink(); 667 if (link.indexOf("?") > 0) { 668 link = link.substring(0, link.indexOf("?")); 669 } 670 if (!cms.existsResource(link)) { 672 return; 673 } 674 675 if (!validatePath(toolRoot.getKey(), handler.getPath(), false)) { 677 if (CmsLog.INIT.isWarnEnabled()) { 679 CmsLog.INIT.warn(Messages.get().getBundle().key( 680 Messages.INIT_TOOLMANAGER_INCONSISTENT_PATH_2, 681 handler.getPath(), 682 handler.getLink())); 683 } 684 return; 685 } 686 687 String id = "tool" + m_tools.elementList().size(); 688 CmsTool tool = new CmsTool(id, handler); 689 690 try { 691 handler.isEnabled(cms); 693 handler.isVisible(cms); 694 } catch (Throwable ex) { 695 String message = Messages.get().getBundle().key( 696 Messages.INIT_TOOLMANAGER_INSTALL_ERROR_2, 697 handler.getPath(), 698 handler.getLink()); 699 if (CmsLog.INIT.isWarnEnabled()) { 700 CmsLog.INIT.warn(message); 701 } else if (CmsLog.INIT.isDebugEnabled()) { 702 CmsLog.INIT.debug(message, ex); 703 } 704 return; 705 } 706 707 try { 708 m_tools.addIdentifiableObject(toolRoot.getKey() + ROOT_SEPARATOR + handler.getPath(), tool); 710 m_urls.addIdentifiableObject(link, handler.getPath()); 712 } catch (Throwable ex) { 713 CmsLog.INIT.warn(Messages.get().getBundle().key( 714 Messages.INIT_TOOLMANAGER_DUPLICATED_ERROR_3, 715 handler.getPath(), 716 handler.getLink(), 717 resolveAdminTool(toolRoot.getKey(), handler.getPath()).getHandler().getLink())); 718 } 719 } 720 721 729 private void registerHandlerList(CmsObject cms, CmsToolRootHandler toolRoot, int len, List handlers) { 730 731 boolean found = false; 732 Iterator it = handlers.iterator(); 733 while (it.hasNext()) { 734 I_CmsToolHandler handler = (I_CmsToolHandler)it.next(); 735 int myLen = CmsStringUtil.splitAsArray(handler.getPath(), TOOLPATH_SEPARATOR).length; 736 if ((len == myLen && !handler.getPath().equals(TOOLPATH_SEPARATOR)) 737 || (len == 1 && handler.getPath().equals(TOOLPATH_SEPARATOR))) { 738 found = true; 739 registerAdminTool(cms, toolRoot, handler); 740 } 741 } 742 if (found) { 743 registerHandlerList(cms, toolRoot, len + 1, handlers); 744 } 745 746 } 747 748 756 private String repairPath(CmsWorkplace wp, String path) { 757 758 String rootKey = getCurrentRoot(wp).getKey(); 759 while (!validatePath(rootKey, path, true)) { 761 LOG.warn(Messages.get().getBundle().key(Messages.LOG_MISSING_ADMIN_TOOL_1, path)); 763 path = getParent(wp, path); 765 } 766 while (resolveAdminTool(rootKey, path) == null) { 768 LOG.warn(Messages.get().getBundle().key(Messages.LOG_MISSING_ADMIN_TOOL_1, path)); 770 path = getParent(wp, path); 772 } 773 774 CmsTool aTool = resolveAdminTool(rootKey, path); 776 while (!aTool.getHandler().isEnabled(wp.getCms())) { 777 if (aTool.getHandler().getLink().equals(VIEW_JSPPAGE_LOCATION)) { 778 break; 780 } 781 path = getParent(wp, path); 782 aTool = resolveAdminTool(rootKey, path); 783 } 784 785 return path; 786 } 787 788 797 private boolean validatePath(String rootKey, String toolPath, boolean full) { 798 799 if (toolPath.equals(TOOLPATH_SEPARATOR)) { 800 return true; 801 } 802 if (!toolPath.startsWith(TOOLPATH_SEPARATOR)) { 803 return false; 804 } 805 List groups = CmsStringUtil.splitAsList(toolPath, TOOLPATH_SEPARATOR); 806 Iterator itGroups = groups.iterator(); 807 String subpath = ""; 808 while (itGroups.hasNext()) { 809 String group = (String )itGroups.next(); 810 if (subpath.length() != TOOLPATH_SEPARATOR.length()) { 811 subpath += TOOLPATH_SEPARATOR + group; 812 } else { 813 subpath += group; 814 } 815 if (itGroups.hasNext() || full) { 816 try { 817 resolveAdminTool(rootKey, subpath).toString(); 819 } catch (Exception e) { 820 return false; 821 } 822 } 823 } 824 return true; 825 } 826 } | Popular Tags |