001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.gwt;
029
030import com.google.gwt.core.client.GWT;
031import com.google.gwt.user.client.rpc.IsSerializable;
032
033/**
034 * GWT RPC Exception. Wrapper for exceptions thrown while processing a RPC request.<p>
035 *
036 * As of the current state of exception serialization within GWT,
037 * details of the original throwable are kept to be available on the client.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsRpcException extends Exception implements IsSerializable {
042
043    /** Serialization uid. */
044    private static final long serialVersionUID = 7582056307629544840L;
045
046    /** The original cause message. */
047    private String m_originalCauseMessage;
048
049    /** The original class name. */
050    private String m_originalClassName;
051
052    /** The original message. */
053    private String m_originalMessage;
054
055    /** The original stack trace. */
056    private StackTraceElement[] m_originalStackTrace;
057
058    /**
059     * Default constructor.<p>
060     */
061    public CmsRpcException() {
062
063        // empty
064    }
065
066    /**
067     * Default constructor.<p>
068     *
069     * @param t the cause
070     */
071    public CmsRpcException(Throwable t) {
072
073        super(t);
074        setOriginalStackTrace(t.getStackTrace());
075        setOriginalMessage(t.getLocalizedMessage());
076        setOriginalClassName(t.getClass().getName());
077        if (t.getCause() != null) {
078            setOriginalCauseMessage(t.getCause().getLocalizedMessage());
079        }
080    }
081
082    /**
083     * Override to make the method return something readable on the client side as well (because of its custom serializer).
084     *
085     * @see java.lang.Throwable#getMessage()
086     */
087    @Override
088    public String getMessage() {
089
090        if (GWT.isClient()) {
091            return getOriginalMessage();
092        } else {
093            return super.getMessage();
094        }
095    }
096
097    /**
098     * Returns the cause message.<p>
099     *
100     * @return the cause message
101     */
102    public String getOriginalCauseMessage() {
103
104        return m_originalCauseMessage;
105    }
106
107    /**
108     * Returns the original class name.<p>
109     *
110     * @return the original class name
111     */
112    public String getOriginalClassName() {
113
114        return m_originalClassName;
115    }
116
117    /**
118     * Returns the original message.<p>
119     *
120     * @return the original message
121     */
122    public String getOriginalMessage() {
123
124        return m_originalMessage;
125    }
126
127    /**
128     * Returns the original stack trace.<p>
129     *
130     * @return the original stack trace
131     */
132    public StackTraceElement[] getOriginalStackTrace() {
133
134        return m_originalStackTrace;
135    }
136
137    /**
138     * Sets the original class name.<p>
139     *
140     * @param originalClassName the original class name to set
141     */
142    public void setOriginalClassName(String originalClassName) {
143
144        m_originalClassName = originalClassName;
145    }
146
147    /**
148     * Sets the original message.<p>
149     *
150     * @param originalMessage the original message to set
151     */
152    public void setOriginalMessage(String originalMessage) {
153
154        m_originalMessage = originalMessage;
155    }
156
157    /**
158     * Sets the original cause message.<p>
159     *
160     * @param originalCauseMessage  the original cause message
161     */
162    protected void setOriginalCauseMessage(String originalCauseMessage) {
163
164        m_originalCauseMessage = originalCauseMessage;
165    }
166
167    /**
168     * Sets the original stack trace.<p>
169     *
170     * @param trace the original stack trace
171     */
172    protected void setOriginalStackTrace(StackTraceElement[] trace) {
173
174        m_originalStackTrace = trace;
175    }
176}