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