1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.forgerock.api.commons;
17
18 import org.forgerock.api.models.ApiDescription;
19 import org.forgerock.api.models.ApiError;
20 import org.forgerock.api.models.Errors.Builder;
21 import org.forgerock.util.i18n.LocalizableString;
22
23
24 public final class CommonsApi {
25
26 public static final ApiDescription COMMONS_API_DESCRIPTION = buildCommonsApi();
27
28
29 private static final String BASE_ERRORS_REF = "frapi:common#/errors/";
30
31
32 public static final String BAD_REQUEST_REF = BASE_ERRORS_REF + "badRequest";
33
34
35 public static final String UNAUTHORIZED_REF = BASE_ERRORS_REF + "unauthorized";
36
37
38 public static final String PAYMENT_REQUIRED_REF = BASE_ERRORS_REF + "paymentRequired";
39
40
41 public static final String FORBIDDEN_REF = BASE_ERRORS_REF + "forbidden";
42
43
44 public static final String NOT_FOUND_REF = BASE_ERRORS_REF + "notFound";
45
46
47 public static final String METHOD_NOT_ALLOWED_REF = BASE_ERRORS_REF + "methodNotAllowed";
48
49
50 public static final String NOT_ACCEPTABLE_REF = BASE_ERRORS_REF + "notAcceptable";
51
52
53 public static final String PROXY_AUTH_REQUIRED_REF = BASE_ERRORS_REF + "proxyAuthRequired";
54
55
56 public static final String REQUEST_TIMEOUT_REF = BASE_ERRORS_REF + "requestTimeout";
57
58
59 public static final String CONFLICT_REF = BASE_ERRORS_REF + "conflict";
60
61
62 public static final String GONE_REF = BASE_ERRORS_REF + "gone";
63
64
65 public static final String LENGTH_REQUIRED_REF = BASE_ERRORS_REF + "lengthRequired";
66
67
68 public static final String VERSION_MISMATCH_REF = BASE_ERRORS_REF + "versionMismatch";
69
70
71 public static final String PRECONDITION_FAILED_REF = BASE_ERRORS_REF + "preconditionFailed";
72
73
74 public static final String REQUEST_ENTITY_TOO_LARGE_REF = BASE_ERRORS_REF + "requestEntityTooLarge";
75
76
77 public static final String REQUEST_URI_TOO_LARGE_REF = BASE_ERRORS_REF + "requestUriTooLarge";
78
79
80 public static final String UNSUPPORTED_MEDIA_TYPE_REF = BASE_ERRORS_REF + "unsupportedMediaType";
81
82
83 public static final String RANGE_NOT_SATISFIABLE_REF = BASE_ERRORS_REF + "rangeNotSatisfiable";
84
85
86 public static final String EXPECTATION_FAILED_REF = BASE_ERRORS_REF + "expectationFailed";
87
88
89 public static final String VERSION_REQUIRED_REF = BASE_ERRORS_REF + "versionRequired";
90
91
92 public static final String PRECONDITION_REQUIRED_REF = BASE_ERRORS_REF + "preconditionRequired";
93
94
95 public static final String INTERNAL_SERVER_ERROR_REF = BASE_ERRORS_REF + "internalServerError";
96
97
98 public static final String NOT_SUPPORTED_REF = BASE_ERRORS_REF + "notSupported";
99
100
101 public static final String BAD_GATEWAY_REF = BASE_ERRORS_REF + "badGateway";
102
103
104 public static final String UNAVAILABLE_REF = BASE_ERRORS_REF + "unavailable";
105
106
107 public static final String GATEWAY_TIMEOUT_REF = BASE_ERRORS_REF + "gatewayTimeout";
108
109
110 public static final String HTTP_VERSION_NOT_SUPPORTED_REF = BASE_ERRORS_REF + "httpVersionNotSupported";
111
112
113 public enum Errors {
114
115
116
117 BAD_REQUEST (400, "badRequest"),
118
119 UNAUTHORIZED (401, "unauthorized"),
120
121 PAYMENT_REQUIRED (402, "paymentRequired"),
122
123 FORBIDDEN (403, "forbidden"),
124
125 NOT_FOUND (404, "notFound"),
126
127 METHOD_NOT_ALLOWED (405, "methodNotAllowed"),
128
129 NOT_ACCEPTABLE (406, "notAcceptable"),
130
131 PROXY_AUTH_REQUIRED (407, "proxyAuthRequired"),
132
133 REQUEST_TIMEOUT (408, "requestTimeout"),
134
135 CONFLICT (409, "conflict"),
136
137 GONE (410, "gone"),
138
139 LENGTH_REQUIRED (411, "lengthRequired"),
140
141 VERSION_MISMATCH (412, "versionMismatch"),
142
143 PRECONDITION_FAILED (412, "preconditionFailed"),
144
145 REQUEST_ENTITY_TOO_LARGE (413, "requestEntityTooLarge"),
146
147 REQUEST_URI_TOO_LARGE (414, "requestUriTooLarge"),
148
149 UNSUPPORTED_MEDIA_TYPE (415, "unsupportedMediaType"),
150
151 RANGE_NOT_SATISFIABLE (416, "rangeNotSatisfiable"),
152
153 EXPECTATION_FAILED (417, "expectationFailed"),
154
155 VERSION_REQUIRED (428, "versionRequired"),
156
157 PRECONDITION_REQUIRED (428, "preconditionRequired"),
158
159 INTERNAL_SERVER_ERROR (500, "internalServerError"),
160
161 NOT_SUPPORTED (501, "notSupported"),
162
163 BAD_GATEWAY (502, "badGateway"),
164
165 UNAVAILABLE (503, "unavailable"),
166
167 GATEWAY_TIMEOUT (504, "gatewayTimeout"),
168
169 HTTP_VERSION_NOT_SUPPORTED (505, "httpVersionNotSupported");
170
171 private String camelCaseName;
172 private String reference;
173 private int code;
174 private String translationKey;
175
176 private Errors(int errorCode, String camelCaseName) {
177 this.code = errorCode;
178 this.camelCaseName = camelCaseName;
179 this.reference = BASE_ERRORS_REF + camelCaseName;
180 this.translationKey = "error." + camelCaseName + ".description";
181 }
182
183
184
185
186
187
188 public String getReference() {
189 return reference;
190 }
191
192 private ApiError toApiError() {
193 return ApiError.apiError().code(code).description(i18n(translationKey)).build();
194 }
195 }
196
197 private static ApiDescription buildCommonsApi() {
198 final Builder commonErrors = org.forgerock.api.models.Errors.errors();
199 for (Errors error : Errors.values()) {
200 commonErrors.put(error.camelCaseName, error.toApiError());
201 }
202
203 return ApiDescription
204 .apiDescription()
205 .id("frapi:common")
206 .description(i18n("commonApi.description"))
207 .version("1.0.0")
208 .errors(commonErrors.build())
209 .build();
210 }
211
212 private static LocalizableString i18n(String translationKey) {
213 String value = "i18n:org/forgerock/api/commons/frapiCommons#" + translationKey;
214 return new LocalizableString(value, CommonsApi.class.getClassLoader());
215 }
216
217 private CommonsApi() {
218
219 }
220 }