001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.keymanager.front.shared;
019    
020    import java.io.Serializable;
021    
022    import javax.xml.bind.annotation.XmlRootElement;
023    
024    /**
025     * DTO for sending an error back to the client (<code>KeyManagerAPI</code>).
026     * It can optionally wrap a {@link Throwable} to provide more precise information
027     * (the type) than just a message.
028     *
029     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
030     */
031    @XmlRootElement
032    public class Error implements Serializable
033    {
034            private static final long serialVersionUID = 1L;
035    
036            private String rootType;
037            private String rootMessage;
038    
039            private String type;
040            private String message;
041    
042            /**
043             * Create an empty instance of <code>Error</code>.
044             * Only used for serialisation/deserialisation.
045             */
046            public Error() { }
047    
048            /**
049             * Create an instance of <code>Error</code> wrapping a {@link Throwable}.
050             * @param throwable the error to be wrapped and sent back to the client instead of a normal response.
051             */
052            public Error(Throwable throwable) {
053                    this.type = throwable.getClass().getName();
054                    this.message = throwable.getMessage();
055    
056                    Throwable r = throwable;
057                    while (r.getCause() != null)
058                            r = r.getCause();
059    
060                    if (r != throwable) {
061                            this.rootType = r.getClass().getName();
062                            this.rootMessage = r.getMessage();
063                    }
064            }
065    
066            /**
067             * Create an instance of <code>Error</code> with an error message.
068             * @param message the message describing what went wrong.
069             */
070            public Error(String message) {
071                    this.message = message;
072            }
073    
074            /**
075             * Get the fully qualified class-name of the root-{@link Throwable} in the exception's cause-chain.
076             * @return the fully qualified class-name of the root-{@link Throwable}. Can be <code>null</code>.
077             */
078            public String getRootType() {
079                    return rootType;
080            }
081            /**
082             * Get the fully qualified class-name of the root-{@link Throwable} in the exception's cause-chain.
083             * @param rootType the fully qualified class-name of the root-{@link Throwable} or <code>null</code>.
084             */
085            public void setRootType(String rootType) {
086                    this.rootType = rootType;
087            }
088            /**
089             * Get the root-{@link Throwable}'s exception-{@link Throwable#getMessage() message}.
090             * @return the message of the root-{@link Throwable} in the exception's cause-chain.
091             */
092            public String getRootMessage() {
093                    return rootMessage;
094            }
095            /**
096             * Set the root-{@link Throwable}'s exception-{@link Throwable#getMessage() message}.
097             * @param rootMessage the message of the root-{@link Throwable} in the exception's cause-chain.
098             */
099            public void setRootMessage(String rootMessage) {
100                    this.rootMessage = rootMessage;
101            }
102            /**
103             * Get the fully qualified class-name of the wrapped {@link Throwable}.
104             * @return the fully qualified class-name of the wrapped {@link Throwable}.
105             */
106            public String getType() {
107                    return type;
108            }
109            /**
110             * Set the fully qualified class-name of the wrapped {@link Throwable}.
111             * @param type the fully qualified class-name of the wrapped {@link Throwable}.
112             */
113            public void setType(String type) {
114                    this.type = type;
115            }
116            /**
117             * Get the error-message. If this <code>Error</code> wraps a {@link Throwable}, this is
118             * {@link Throwable#getMessage()}
119             * @return the error-message.
120             */
121            public String getMessage() {
122                    return message;
123            }
124            /**
125             * Set the error-message.
126             * @param message the error-message.
127             */
128            public void setMessage(String message) {
129                    this.message = message;
130            }
131    }