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.back.shared;
019    
020    import javax.xml.bind.annotation.XmlRootElement;
021    
022    /**
023     * {@link Response} implementation for sending an error back to the app-server.
024     * It can optionally wrap a {@link Throwable} to provide more precise information
025     * (the type) than just a message.
026     *
027     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
028     */
029    @XmlRootElement
030    public class ErrorResponse extends Response
031    {
032            private static final long serialVersionUID = 1L;
033    
034            private String type;
035            private String message;
036    
037            /**
038             * Create an empty instance of <code>ErrorResponse</code>.
039             * Only used for serialisation/deserialisation.
040             */
041            public ErrorResponse() { }
042    
043            /**
044             * Create an instance of <code>ErrorResponse</code> in order to reply the given <code>request</code>.
045             *
046             * @param request the request to be replied.
047             * @param errorMessage a description of what went wrong.
048             */
049            public ErrorResponse(Request request, String errorMessage) {
050                    super(request);
051                    this.message = errorMessage;
052            }
053    
054            /**
055             * Create an instance of <code>ErrorResponse</code> in order to reply the given <code>request</code>.
056             *
057             * @param request the request to be replied.
058             * @param throwable the error to be wrapped and sent back to the app-server instead of a normal response.
059             */
060            public ErrorResponse(Request request, Throwable throwable) {
061                    super(request);
062                    this.type = throwable.getClass().getName();
063                    this.message = throwable.getMessage();
064            }
065    
066            /**
067             * Get the error-type. This is a fully qualified class-name of the {@link Throwable}-sub-class
068             * passed to {@link #ErrorResponse(Request, Throwable)} or {@link #setType(String) otherwise set}.
069             * @return the error-type or <code>null</code>.
070             * @see #setType(String)
071             */
072            public String getType() {
073                    return type;
074            }
075            /**
076             * Set the error-type.
077             * @param type the error-type or <code>null</code>. If not <code>null</code>, this must be a fully
078             * qualified class-name of the {@link Throwable}-sub-class.
079             * @see #getType()
080             */
081            public void setType(String type) {
082                    this.type = type;
083            }
084    
085            /**
086             * Get the error-message. If an exception was wrapped by this <code>ErrorResponse</code> instance,
087             * it will be the result of {@link Throwable#getMessage()}.
088             * @return the error-message.
089             * @see #setMessage(String)
090             */
091            public String getMessage() {
092                    return message;
093            }
094            /**
095             * Set the error-message, i.e. a description of what went wrong and prevented successful handling
096             * of the request.
097             * @param errorMessage the error message.
098             * @see #getMessage()
099             */
100            public void setMessage(String errorMessage) {
101                    this.message = errorMessage;
102            }
103    }