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