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.jsp.util;
029
030import org.opencms.db.CmsUserSettings;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsUser;
033import org.opencms.i18n.CmsMessages;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.CmsMultiException;
037import org.opencms.main.I_CmsThrowable;
038import org.opencms.main.OpenCms;
039import org.opencms.util.CmsMacroResolver;
040import org.opencms.util.CmsStringUtil;
041
042import java.util.Locale;
043import java.util.Properties;
044
045/**
046 * Class to display the error dialog.<p>
047 *
048 * @since 6.0.0
049 */
050public class CmsErrorBean {
051
052    /** Name of the property file containing HTML fragments for setup wizard and error dialog.<p> */
053    public static final String ERRORPAGE = "org/opencms/jsp/util/errorpage.properties";
054
055    /** The html for the buttons. */
056    private String m_buttons;
057
058    /** The current CmsObject.<p> */
059    private CmsObject m_cms;
060
061    /** The optional error message. */
062    private String m_errorMessage;
063
064    /** The html code for the hidden parameters. */
065    private String m_hiddenParams;
066
067    /** The locale for the errorpage. */
068    private Locale m_locale;
069
070    /** Messages container. */
071    private CmsMessages m_messages;
072
073    /** The html code for the buttons. */
074    private String m_paramAction;
075
076    /** The exception that was caught.<p> */
077    private Throwable m_throwable;
078
079    /** The title for the error page. */
080    private String m_title;
081
082    /**
083     * Constructs a new error bean.<p>
084     *
085     * @param cms the current CmsObject
086     * @param throwable the exception that was caught
087     */
088    public CmsErrorBean(CmsObject cms, Throwable throwable) {
089
090        m_cms = cms;
091        // get the settings for system users to display errors in correct language
092        CmsUser user = cms.getRequestContext().getCurrentUser();
093        CmsUserSettings settings = new CmsUserSettings(user);
094        m_locale = settings.getLocale();
095        m_throwable = throwable;
096        m_messages = Messages.get().getBundle(m_locale);
097    }
098
099    /**
100     * Returns the html code for the buttons, when the errorpage is included from outside the workplace.<p>
101     *
102     * @return the default html for the buttons
103     */
104    public String getDefaultButtonsHtml() {
105
106        StringBuffer result = new StringBuffer();
107        String closeLabel = m_messages.key(Messages.GUI_CLOSE_0, new Object[] {});
108        String detailsLabel = m_messages.key(Messages.GUI_DETAILS_0, new Object[] {});
109        result.append("<div class=\"dialogbuttons\" unselectable=\"on\">");
110        result.append("<input name=\"close\" type=\"button\" value=\"").append(closeLabel).append(
111            "\" onclick=\"closeDialog();\" class=\"dialogbutton\">");
112        result.append("<input name=\"details\" type=\"button\" value=\"").append(detailsLabel).append(
113            "\" class=\"dialogbutton\" onclick=\"toggleElement('errordetails');\">");
114        result.append("</div>");
115        return result.toString();
116    }
117
118    /**
119     * Returns the error message to be displayed.<p>
120     *
121     * @return the error message to be displayed
122     */
123    public String getErrorMessage() {
124
125        StringBuffer result = new StringBuffer(512);
126
127        String reason = m_messages.key(Messages.GUI_REASON_0, new Object[] {});
128
129        if (CmsStringUtil.isNotEmpty(m_errorMessage)) {
130            result.append(m_errorMessage);
131            result.append("\n").append(reason).append(": ");
132        }
133
134        // if a localized message is already set as a parameter, append it.
135        result.append(getMessage(m_throwable));
136        // recursively append all error reasons to the message
137        for (Throwable cause = m_throwable.getCause(); cause != null; cause = cause.getCause()) {
138            result.append("\n").append(reason).append(": ");
139            result.append(getMessage(cause));
140        }
141        return result.toString();
142    }
143
144    /**
145     * Returns the localized Message, if the argument is a CmsException, or
146     * the message otherwise.<p>
147     *
148     * @param t the Throwable to get the message from
149     *
150     * @return returns the localized Message, if the argument is a CmsException, or
151     * the message otherwise
152     */
153    public String getMessage(Throwable t) {
154
155        if ((t instanceof I_CmsThrowable) && (((I_CmsThrowable)t).getMessageContainer() != null)) {
156            StringBuffer result = new StringBuffer(256);
157            if (m_throwable instanceof CmsMultiException) {
158                CmsMultiException exc = (CmsMultiException)m_throwable;
159                String message = exc.getMessage(m_locale);
160                if (CmsStringUtil.isNotEmpty(message)) {
161                    result.append(message);
162                    result.append('\n');
163                }
164
165            }
166
167            I_CmsThrowable cmsThrowable = (I_CmsThrowable)t;
168            result.append(cmsThrowable.getLocalizedMessage(m_locale));
169            return result.toString();
170        } else {
171            String message = t.getMessage();
172            if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) {
173                // no error message found (e.g. for NPE), provide default message text
174                message = m_messages.key(Messages.GUI_ERROR_UNKNOWN_0);
175            }
176            return message;
177        }
178    }
179
180    /**
181     * Sets the buttons.<p>
182     *
183     * @param buttons the buttons to set
184     */
185    public void setButtons(String buttons) {
186
187        m_buttons = buttons;
188    }
189
190    /**
191     * Sets the error message which can be displayed if no exception is there.<p>
192     *
193     * @param errorMessage the error message to set
194     */
195    public void setErrorMessage(String errorMessage) {
196
197        m_errorMessage = errorMessage;
198    }
199
200    /**
201     * Sets the hiddenParams.<p>
202     *
203     * @param hiddenParams the hiddenParams to set
204     */
205    public void setHiddenParams(String hiddenParams) {
206
207        m_hiddenParams = hiddenParams;
208    }
209
210    /**
211     * Sets the action parameter.<p>
212     *
213     * @param paramAction the action parameter to set
214     */
215    public void setParamAction(String paramAction) {
216
217        m_paramAction = paramAction;
218    }
219
220    /**
221     * Sets the title of the error page.<p>
222     *
223     * @param title of the error page
224     */
225    public void setTitle(String title) {
226
227        m_title = title;
228    }
229
230    /**
231     * Returns the html code for a errorpage.<p>
232     *
233     * @return the html for the errorpage
234     */
235    public String toHtml() {
236
237        CmsMacroResolver resolver = new CmsMacroResolver();
238        if (CmsStringUtil.isEmpty(m_title)) {
239            m_title = m_messages.key(Messages.GUI_ERROR_0, new Object[] {});
240        }
241        resolver.addMacro("title", m_title);
242        resolver.addMacro("label_error", m_messages.key(Messages.GUI_ERROR_0, new Object[] {}));
243        resolver.addMacro("errorstack", CmsException.getFormattedErrorstack(m_throwable));
244        resolver.addMacro("message", CmsStringUtil.escapeHtml(getErrorMessage()));
245        resolver.addMacro(
246            "styleuri",
247            OpenCms.getLinkManager().substituteLink(m_cms, "/system/workplace/commons/style/workplace.css"));
248        if (CmsStringUtil.isEmpty(m_buttons)) {
249            resolver.addMacro("buttons", getDefaultButtonsHtml());
250        } else {
251            resolver.addMacro("buttons", m_buttons);
252            resolver.addMacro("paramaction", m_paramAction);
253        }
254
255        if (CmsStringUtil.isNotEmpty(m_hiddenParams)) {
256            resolver.addMacro("hiddenparams", m_hiddenParams);
257        }
258        resolver.addMacro(
259            "erroricon",
260            OpenCms.getLinkManager().substituteLink(m_cms, "/system/workplace/resources/commons/error.png"));
261        Properties errorpage = new Properties();
262        try {
263            errorpage.load(CmsErrorBean.class.getClassLoader().getResourceAsStream(ERRORPAGE));
264        } catch (Throwable th) {
265            CmsLog.INIT.error(
266                org.opencms.main.Messages.get().getBundle().key(
267                    org.opencms.main.Messages.INIT_ERR_LOAD_HTML_PROPERTY_FILE_1,
268                    ERRORPAGE),
269                th);
270        }
271        return resolver.resolveMacros(errorpage.getProperty("ERRORPAGE"));
272    }
273}