001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.workplace.tools; 029 030import org.opencms.main.OpenCms; 031import org.opencms.security.CmsRole; 032import org.opencms.util.CmsMacroResolver; 033import org.opencms.util.CmsUUID; 034import org.opencms.util.I_CmsMacroResolver; 035import org.opencms.workplace.CmsWorkplace; 036 037import java.util.Arrays; 038import java.util.Collections; 039import java.util.Iterator; 040import java.util.List; 041 042/** 043 * Resolves special macros for the admin view.<p> 044 * 045 * Supported macros are:<p> 046 * <ul> 047 * <li>admin.userName|id</li> 048 * <li>admin.groupName|id</li> 049 * <li>admin.jobName|id</li> 050 * <li>admin.projectName|id</li> 051 * <li>admin.ouDescription|fqn</li> 052 * <li>admin.ouType|fqn</li> 053 * <li>admin.roleName|id</li> 054 * </ul><p> 055 * @since 6.0.0 056 */ 057public class CmsToolMacroResolver implements I_CmsMacroResolver { 058 059 /** Identifier for admin macros prefix. */ 060 public static final String PREFIX_ADMIN = "admin."; 061 062 /** Identifier for admin parameter names. */ 063 public static final String KEY_USERNAME = "userName."; 064 065 /** Identifier for admin parameter names. */ 066 public static final String KEY_GROUPNAME = "groupName."; 067 068 /** Identifier for admin parameter names. */ 069 public static final String KEY_JOBNAME = "jobName."; 070 071 /** Identifier for admin parameter names. */ 072 public static final String KEY_PROJECTNAME = "projectName."; 073 074 /** Identifier for admin parameter names. */ 075 public static final String KEY_OUDESCRIPTION = "ouDescription."; 076 077 /** Identifier for admin parameter names. */ 078 public static final String KEY_OUTYPE = "ouType."; 079 080 /** Identifier for admin parameter names. */ 081 public static final String KEY_ROLENAME = "roleName."; 082 083 /** Identified for admin parameter commands. */ 084 public static final String[] VALUE_NAME_ARRAY = { 085 KEY_USERNAME, 086 KEY_GROUPNAME, 087 KEY_JOBNAME, 088 KEY_PROJECTNAME, 089 KEY_OUDESCRIPTION, 090 KEY_OUTYPE, 091 KEY_ROLENAME}; 092 093 /** The admin commands wrapped in a List. */ 094 public static final List<String> VALUE_NAMES = Collections.unmodifiableList(Arrays.asList(VALUE_NAME_ARRAY)); 095 096 /** The workplace class for falling back, and use the cms context. */ 097 private CmsWorkplace m_wp; 098 099 /** 100 * Default private constructor.<p> 101 * 102 * @param wp the workplace instance 103 */ 104 public CmsToolMacroResolver(CmsWorkplace wp) { 105 106 m_wp = wp; 107 } 108 109 /** 110 * Resolves the macros in the given input using the provided parameters.<p> 111 * 112 * A macro in the form <code>${key}</code> in the content is replaced with it's assigned value 113 * returned by the <code>{@link I_CmsMacroResolver#getMacroValue(String)}</code> method of the given 114 * <code>{@link I_CmsMacroResolver}</code> instance.<p> 115 * 116 * If a macro is found that can not be mapped to a value by the given macro resolver, 117 * it is left untouched in the input.<p> 118 * 119 * @param input the input in which to resolve the macros 120 * @param wp the workplace class for falling back 121 * 122 * @return the input with the macros resolved 123 */ 124 public static String resolveMacros(String input, CmsWorkplace wp) { 125 126 return new CmsToolMacroResolver(wp).resolveMacros(input); 127 } 128 129 /** 130 * @see org.opencms.util.I_CmsMacroResolver#getMacroValue(java.lang.String) 131 */ 132 public String getMacroValue(String macro) { 133 134 if (!macro.startsWith(CmsToolMacroResolver.PREFIX_ADMIN)) { 135 // the key is not an admin macro, fallback 136 return m_wp.getMacroResolver().getMacroValue(macro); 137 } 138 macro = macro.substring(CmsToolMacroResolver.PREFIX_ADMIN.length()); 139 String id = null; 140 // validate macro command 141 Iterator<String> it = VALUE_NAMES.iterator(); 142 while (it.hasNext()) { 143 String cmd = it.next(); 144 if (macro.startsWith(cmd)) { 145 id = macro.substring(cmd.length()); 146 macro = cmd; 147 } 148 } 149 if (id == null) { 150 // macro command not found 151 return null; 152 } 153 try { 154 if (macro == CmsToolMacroResolver.KEY_USERNAME) { 155 return m_wp.getCms().readUser(new CmsUUID(id)).getSimpleName(); 156 } 157 if (macro == CmsToolMacroResolver.KEY_GROUPNAME) { 158 return m_wp.getCms().readGroup(new CmsUUID(id)).getSimpleName(); 159 } 160 if (macro == CmsToolMacroResolver.KEY_PROJECTNAME) { 161 return m_wp.getCms().readProject(new CmsUUID(id)).getSimpleName(); 162 } 163 if (macro == CmsToolMacroResolver.KEY_JOBNAME) { 164 return OpenCms.getScheduleManager().getJob(id).getJobName(); 165 } 166 if (macro == CmsToolMacroResolver.KEY_OUDESCRIPTION) { 167 return OpenCms.getOrgUnitManager().readOrganizationalUnit(m_wp.getCms(), id).getDisplayName( 168 m_wp.getLocale()); 169 } 170 if (macro == CmsToolMacroResolver.KEY_OUTYPE) { 171 if (OpenCms.getOrgUnitManager().readOrganizationalUnit(m_wp.getCms(), id).hasFlagWebuser()) { 172 return Messages.get().getBundle(m_wp.getLocale()).key(Messages.GUI_OU_TYPE_WEBUSER_0); 173 } 174 return Messages.get().getBundle(m_wp.getLocale()).key(Messages.GUI_OU_TYPE_NORMAL_0); 175 } 176 if (macro == CmsToolMacroResolver.KEY_ROLENAME) { 177 return CmsRole.valueOf(m_wp.getCms().readGroup(id)).getName( 178 m_wp.getCms().getRequestContext().getLocale()); 179 } 180 } catch (Exception e) { 181 // ignore 182 } 183 return null; 184 } 185 186 /** 187 * Resolves the macros in the given input.<p> 188 * 189 * Calls <code>{@link #resolveMacros(String)}</code> until no more macros can 190 * be resolved in the input. This way "nested" macros in the input are resolved as well.<p> 191 * 192 * @see org.opencms.util.I_CmsMacroResolver#resolveMacros(java.lang.String) 193 */ 194 public String resolveMacros(String input) { 195 196 String result = input; 197 198 if (input != null) { 199 String lastResult; 200 do { 201 // save result for next comparison 202 lastResult = result; 203 // resolve the macros 204 result = CmsMacroResolver.resolveMacros(result, this); 205 // if nothing changes then the final result is found 206 } while (!result.equals(lastResult)); 207 } 208 // return the result 209 return result; 210 } 211 212 /** 213 * @see org.opencms.util.I_CmsMacroResolver#isKeepEmptyMacros() 214 */ 215 public boolean isKeepEmptyMacros() { 216 217 return false; 218 } 219}