Skip to content

Commit c9e8814

Browse files
committed
Create HTTP status enum
1 parent a1f46ef commit c9e8814

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Inrupt Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to use,
7+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8+
* Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
package com.inrupt.client.solid;
22+
23+
import java.util.Arrays;
24+
import java.util.Optional;
25+
26+
enum HttpStatus {
27+
BAD_REQUEST(BadRequestException.STATUS_CODE, "Bad Request"),
28+
UNAUTHORIZED(UnauthorizedException.STATUS_CODE, "Unauthorized"),
29+
FORBIDDEN(ForbiddenException.STATUS_CODE, "Forbidden"),
30+
NOT_FOUND(NotFoundException.STATUS_CODE, "Not Found"),
31+
METHOD_NOT_ALLOWED(MethodNotAllowedException.STATUS_CODE, "Method Not Allowed"),
32+
NOT_ACCEPTABLE(NotAcceptableException.STATUS_CODE, "Not Acceptable"),
33+
CONFLICT(ConflictException.STATUS_CODE, "Conflict"),
34+
GONE(GoneException.STATUS_CODE, "Gone"),
35+
PRECONDITION_FAILED(PreconditionFailedException.STATUS_CODE, "Precondition Failed"),
36+
UNSUPPORTED_MEDIA_TYPE(UnsupportedMediaTypeException.STATUS_CODE, "Unsupported Media Type"),
37+
TOO_MANY_REQUESTS(TooManyRequestsException.STATUS_CODE, "Too Many Requests"),
38+
INTERNAL_SERVER_ERROR(InternalServerErrorException.STATUS_CODE, "Internal Server Error");
39+
40+
final int code;
41+
final String message;
42+
43+
HttpStatus(int code, String message) {
44+
this.code = code;
45+
this.message = message;
46+
}
47+
48+
static String getStatusMessage(int statusCode) {
49+
Optional<HttpStatus> knownStatus = Arrays.stream(HttpStatus.values()).filter(status -> status.code == statusCode).findFirst();
50+
if (knownStatus.isPresent()) {
51+
return knownStatus.get().message;
52+
}
53+
// If the status is unknown, default to 400 for client errors and 500 for server errors
54+
if (statusCode > 499) {
55+
return INTERNAL_SERVER_ERROR.message;
56+
}
57+
return BAD_REQUEST.message;
58+
}
59+
}

0 commit comments

Comments
 (0)