001    package com.nimbusds.oauth2.sdk;
002    
003    
004    import java.net.URL;
005    
006    import net.jcip.annotations.Immutable;
007    
008    
009    /**
010     * Error object, used to encapsulate OAuth 2.0 and other errors. This class is 
011     * immutable.
012     *
013     * @author Vladimir Dzhuvinov
014     * @version $version$ (2013-01-30)
015     */
016    @Immutable
017    public class ErrorObject {
018            
019            
020            /**
021             * The error code, may not always be defined.
022             */
023            private final String code;
024    
025    
026            /**
027             * Optional error description.
028             */
029            private final String description;
030    
031    
032            /**
033             * Optional HTTP status code, 0 if not specified.
034             */
035            private final int httpStatusCode;
036    
037    
038            /**
039             * Optional URI of a web page that includes additional information 
040             * about the error.
041             */
042            private final URL uri;
043    
044    
045            /**
046             * Creates a new error with the specified code.
047             *
048             * @param code The error code, {@code null} if not specified.
049             */
050            public ErrorObject(final String code) {
051            
052                    this(code, null, 0, null);
053            }
054            
055            
056            /**
057             * Creates a new error with the specified code and description.
058             *
059             * @param code        The error code, {@code null} if not specified.
060             * @param description The error description, {@code null} if not
061             *                    specified.
062             */
063            public ErrorObject(final String code, final String description) {
064            
065                    this(code, description, 0, null);
066            }
067    
068    
069            /**
070             * Creates a new error with the specified code, description and HTTP 
071             * status code.
072             *
073             * @param code           The error code, {@code null} if not specified.
074             * @param description    The error description, {@code null} if not
075             *                       specified.
076             * @param httpStatusCode The HTTP status code, zero if not specified.
077             */
078            public ErrorObject(final String code, final String description, 
079                               final int httpStatusCode) {
080            
081                    this(code, description, httpStatusCode, null);
082            }
083    
084    
085            /**
086             * Creates a new error with the specified code, description, HTTP 
087             * status code and page URI.
088             *
089             * @param code           The error code, {@code null} if not specified.
090             * @param description    The error description, {@code null} if not
091             *                       specified.
092             * @param httpStatusCode The HTTP status code, zero if not specified.
093             * @param uri            The error page URI, {@code null} if not
094             *                       specified.
095             */
096            public ErrorObject(final String code, final String description, 
097                               final int httpStatusCode, final URL uri) {
098            
099                    this.code = code;
100                    this.description = description;
101                    this.httpStatusCode = httpStatusCode;
102                    this.uri = uri;
103            }
104    
105    
106            /**
107             * Gets the error code.
108             *
109             * @return The error code, {@code null} if not specified.
110             */
111            public String getCode() {
112    
113                    return code;
114            }
115            
116            
117            /**
118             * Gets the error description.
119             *
120             * @return The error description, {@code null} if not specified.
121             */
122            public String getDescription() {
123            
124                    return description;
125            }
126    
127    
128            /**
129             * Sets the error description.
130             *
131             * @param description The error description, {@code null} if not 
132             *                    specified.
133             *
134             * @return A copy of this error with the specified description.
135             */
136            public ErrorObject setDescription(final String description) {
137    
138                    return new ErrorObject(getCode(), description, getHTTPStatusCode(), getURI());
139            }
140    
141    
142            /**
143             * Appends the specified text to the error description.
144             *
145             * @param text The text to append to the error description, 
146             *             {@code null} if not specified.
147             *
148             * @return A copy of this error with the specified appended 
149             *         description.
150             */
151            public ErrorObject appendDescription(final String text) {
152    
153                    String newDescription;
154    
155                    if (getDescription() != null)
156                            newDescription = getDescription() + text;
157                    else
158                            newDescription = text;
159    
160                    return new ErrorObject(getCode(), newDescription, getHTTPStatusCode(), getURI());
161            }
162    
163    
164            /**
165             * Gets the HTTP status code.
166             *
167             * @return The HTTP status code, zero if not specified.
168             */
169            public int getHTTPStatusCode() {
170    
171                    return httpStatusCode;
172            }
173    
174    
175            /**
176             * Sets the HTTP status code.
177             *
178             * @param httpStatusCode  The HTTP status code, zero if not specified.
179             *
180             * @return A copy of this error with the specified HTTP status code.
181             */
182            public ErrorObject setHTTPStatusCode(final int httpStatusCode) {
183    
184                    return new ErrorObject(getCode(), getDescription(), httpStatusCode, getURI());
185            }
186    
187    
188            /**
189             * Gets the error page URI.
190             *
191             * @return The error page URI, {@code null} if not specified.
192             */
193            public URL getURI() {
194    
195                    return uri;
196            }
197    
198    
199            /**
200             * Sets the error page URI.
201             *
202             * @param uri The error page URI, {@code null} if not specified.
203             *
204             * @return A copy of this error with the specified page URI.
205             */
206            public ErrorObject setURI(final URL uri) {
207    
208                    return new ErrorObject(getCode(), getDescription(), getHTTPStatusCode(), uri);
209            }
210    
211    
212            /**
213             * @see #getCode
214             */
215            @Override
216            public String toString() {
217            
218                    if (code != null)
219                            return code;
220                    else
221                            return "null";
222            }
223    
224    
225            @Override
226            public int hashCode() {
227            
228                    if (code != null)
229                            return code.hashCode();
230                    else
231                            return "null".hashCode();
232            }
233    
234    
235            @Override
236            public boolean equals(final Object object) {
237            
238                    return object != null && 
239                           object instanceof ErrorObject && 
240                           this.toString().equals(object.toString());
241            }
242    }