1 31 32 package org.opencms.jsp.util; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.i18n.CmsMessages; 36 import org.opencms.main.CmsException; 37 import org.opencms.main.CmsLog; 38 import org.opencms.main.CmsMultiException; 39 import org.opencms.main.I_CmsThrowable; 40 import org.opencms.main.OpenCms; 41 import org.opencms.util.CmsMacroResolver; 42 import org.opencms.util.CmsStringUtil; 43 44 import java.util.Locale ; 45 import java.util.Properties ; 46 47 56 public class CmsErrorBean { 57 58 59 public static final String ERRORPAGE = "org/opencms/jsp/util/errorpage.properties"; 60 61 62 private String m_buttons; 63 64 65 private CmsObject m_cms; 66 67 68 private String m_errorMessage; 69 70 71 private String m_hiddenParams; 72 73 74 private Locale m_locale; 75 76 77 private CmsMessages m_messages; 78 79 80 private String m_paramAction; 81 82 83 private Throwable m_throwable; 84 85 86 private String m_title; 87 88 94 public CmsErrorBean(CmsObject cms, Throwable throwable) { 95 96 m_cms = cms; 97 m_locale = cms.getRequestContext().getLocale(); 98 m_throwable = throwable; 99 m_messages = Messages.get().getBundle(m_locale); 100 } 101 102 107 public String getDefaultButtonsHtml() { 108 109 StringBuffer result = new StringBuffer (); 110 String closeLabel = m_messages.key(Messages.GUI_CLOSE_0, new Object [] {}); 111 String detailsLabel = m_messages.key(Messages.GUI_DETAILS_0, new Object [] {}); 112 result.append("<div class=\"dialogbuttons\" unselectable=\"on\">"); 113 result.append("<input name=\"close\" type=\"button\" value=\"").append(closeLabel).append( 114 "\" onclick=\"closeDialog();\" class=\"dialogbutton\">"); 115 result.append("<input name=\"details\" type=\"button\" value=\"").append(detailsLabel).append( 116 "\" class=\"dialogbutton\" onclick=\"toggleElement('errordetails');\">"); 117 result.append("</div>"); 118 return result.toString(); 119 } 120 121 126 public String getErrorMessage() { 127 128 StringBuffer result = new StringBuffer (512); 129 130 String reason = m_messages.key(Messages.GUI_REASON_0, new Object [] {}); 131 132 if (CmsStringUtil.isNotEmpty(m_errorMessage)) { 133 result.append(m_errorMessage); 134 result.append("\n").append(reason).append(": "); 135 } 136 137 result.append(getMessage(m_throwable)); 139 for (Throwable cause = m_throwable.getCause(); cause != null; cause = cause.getCause()) { 141 result.append("\n").append(reason).append(": "); 142 result.append(getMessage(cause)); 143 } 144 return result.toString(); 145 } 146 147 156 public String getMessage(Throwable t) { 157 158 if (t instanceof I_CmsThrowable && ((I_CmsThrowable)t).getMessageContainer() != null) { 159 StringBuffer result = new StringBuffer (256); 160 if (m_throwable instanceof CmsMultiException) { 161 CmsMultiException exc = (CmsMultiException)m_throwable; 162 String message = exc.getMessage(m_locale); 163 if (CmsStringUtil.isNotEmpty(message)) { 164 result.append(message); 165 result.append('\n'); 166 } 167 168 } 169 170 I_CmsThrowable cmsThrowable = (I_CmsThrowable)t; 171 result.append(cmsThrowable.getLocalizedMessage(m_locale)); 172 return result.toString(); 173 } else { 174 String message = t.getMessage(); 175 if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) { 176 message = m_messages.key(Messages.GUI_ERROR_UNKNOWN_0); 178 } 179 return message; 180 } 181 } 182 183 188 public void setButtons(String buttons) { 189 190 m_buttons = buttons; 191 } 192 193 198 public void setErrorMessage(String errorMessage) { 199 200 m_errorMessage = errorMessage; 201 } 202 203 208 public void setHiddenParams(String hiddenParams) { 209 210 m_hiddenParams = hiddenParams; 211 } 212 213 218 public void setParamAction(String paramAction) { 219 220 m_paramAction = paramAction; 221 } 222 223 228 public void setTitle(String title) { 229 230 m_title = title; 231 } 232 233 238 public String toHtml() { 239 240 CmsMacroResolver resolver = new CmsMacroResolver(); 241 if (CmsStringUtil.isEmpty(m_title)) { 242 m_title = m_messages.key(Messages.GUI_ERROR_0, new Object [] {}); 243 } 244 resolver.addMacro("title", m_title); 245 resolver.addMacro("label_error", m_messages.key(Messages.GUI_ERROR_0, new Object [] {})); 246 resolver.addMacro("errorstack", CmsException.getFormattedErrorstack(m_throwable)); 247 resolver.addMacro("message", CmsStringUtil.escapeHtml(getErrorMessage())); 248 resolver.addMacro("styleuri", OpenCms.getLinkManager().substituteLink( 249 m_cms, 250 "/system/workplace/commons/style/workplace.css")); 251 if (CmsStringUtil.isEmpty(m_buttons)) { 252 resolver.addMacro("buttons", getDefaultButtonsHtml()); 253 } else { 254 resolver.addMacro("buttons", m_buttons); 255 resolver.addMacro("paramaction", m_paramAction); 256 } 257 258 if (CmsStringUtil.isNotEmpty(m_hiddenParams)) { 259 resolver.addMacro("hiddenparams", m_hiddenParams); 260 } 261 resolver.addMacro("erroricon", OpenCms.getLinkManager().substituteLink( 262 m_cms, 263 "/system/workplace/resources/commons/error.png")); 264 Properties errorpage = new Properties (); 265 try { 266 errorpage.load(CmsErrorBean.class.getClassLoader().getResourceAsStream(ERRORPAGE)); 267 } catch (Throwable th) { 268 CmsLog.INIT.error(org.opencms.main.Messages.get().getBundle().key( 269 org.opencms.main.Messages.INIT_ERR_LOAD_HTML_PROPERTY_FILE_1, 270 ERRORPAGE), th); 271 } 272 return resolver.resolveMacros(errorpage.getProperty("ERRORPAGE")); 273 } 274 } | Popular Tags |