1
|
|
package com.reallifedeveloper.common.resource; |
2
|
|
|
3
|
|
import org.checkerframework.checker.nullness.qual.Nullable; |
4
|
|
|
5
|
|
import jakarta.ws.rs.WebApplicationException; |
6
|
|
import jakarta.ws.rs.core.CacheControl; |
7
|
|
import jakarta.ws.rs.core.MediaType; |
8
|
|
import jakarta.ws.rs.core.Response; |
9
|
|
import jakarta.ws.rs.core.Response.ResponseBuilder; |
10
|
|
import jakarta.ws.rs.core.Response.Status; |
11
|
|
|
12
|
|
import com.reallifedeveloper.common.domain.ErrorHandling; |
13
|
|
|
14
|
|
/** |
15
|
|
* Utility class with helper methods for JAX-RS resources. |
16
|
|
* |
17
|
|
* @author RealLifeDeveloper |
18
|
|
*/ |
19
|
|
public final class ResourceUtil { |
20
|
|
|
21
|
|
/** |
22
|
|
* Since this is a utility class with only static methods, we hide the only constructor. |
23
|
|
*/ |
24
|
|
private ResourceUtil() { |
25
|
|
} |
26
|
|
|
27
|
|
/** |
28
|
|
* Gives a {@code WebApplicationException} with the given HTTP status and the given message as plain text in the response body. If |
29
|
|
* {@code message} is {@code null}, the entity in the response body will also be {@code null}. |
30
|
|
* |
31
|
|
* @param message the message to send back to the client, or {@code null} |
32
|
|
* @param status the HTTP status to send back to the client |
33
|
|
* |
34
|
|
* @return a {@code WebApplicationException} |
35
|
|
* |
36
|
|
* @throws IllegalArgumentException if {@code status} is {@code null} |
37
|
|
*/ |
38
|
|
public static WebApplicationException webApplicationException(@Nullable String message, Status status) { |
39
|
1
1. webApplicationException : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNull → SURVIVED
|
ErrorHandling.checkNull("status must not be null", status); |
40
|
|
ResponseBuilder responseBuilder = Response.status(status).type(MediaType.TEXT_PLAIN); |
41
|
1
1. webApplicationException : negated conditional → KILLED
|
if (message != null) { |
42
|
|
responseBuilder.entity(message); |
43
|
|
} |
44
|
1
1. webApplicationException : replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::webApplicationException → KILLED
|
return new WebApplicationException(responseBuilder.build()); |
45
|
|
} |
46
|
|
|
47
|
|
/** |
48
|
|
* Gives a {@code WebApplicationException} with the given HTTP status and the given {@code Throwable} as plain text in the response |
49
|
|
* body. If {@code cause} is {@code null}, the entity in the response body will also be {@code null}. |
50
|
|
* |
51
|
|
* @param cause the cause of the error, or {@code null} |
52
|
|
* @param status the HTTP status to send back to the client |
53
|
|
* |
54
|
|
* @return a {@code WebApplicationException} |
55
|
|
* |
56
|
|
* @throws IllegalArgumentException if {@code status} is {@code null} |
57
|
|
*/ |
58
|
|
public static WebApplicationException webApplicationException(@Nullable Throwable cause, Status status) { |
59
|
1
1. webApplicationException : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNull → SURVIVED
|
ErrorHandling.checkNull("status must not be null", status); |
60
|
1
1. webApplicationException : negated conditional → KILLED
|
String message = cause == null ? null : cause.toString(); |
61
|
1
1. webApplicationException : replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::webApplicationException → KILLED
|
return webApplicationException(message, status); |
62
|
|
} |
63
|
|
|
64
|
|
/** |
65
|
|
* Gives a {@code WebApplicationException} with HTTP status 400 and the given message as plain text in the body. |
66
|
|
* |
67
|
|
* @param message the message to send back to the client |
68
|
|
* |
69
|
|
* @return a {@code WebApplicationException} |
70
|
|
*/ |
71
|
|
public static WebApplicationException badRequest(@Nullable String message) { |
72
|
1
1. badRequest : replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::badRequest → KILLED
|
return webApplicationException(message, Status.BAD_REQUEST); |
73
|
|
} |
74
|
|
|
75
|
|
/** |
76
|
|
* Gives a {@code WebApplicationException} with HTTP status 404 and the given message as plain text in the body. |
77
|
|
* |
78
|
|
* @param message the message to send back to the client |
79
|
|
* |
80
|
|
* @return a {@code WebApplicationException} |
81
|
|
*/ |
82
|
|
public static WebApplicationException notFound(@Nullable String message) { |
83
|
1
1. notFound : replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::notFound → KILLED
|
return webApplicationException(message, Status.NOT_FOUND); |
84
|
|
} |
85
|
|
|
86
|
|
/** |
87
|
|
* Gives a {@code WebApplicationException} with HTTP status 500 and the given message as plain text in the body. |
88
|
|
* |
89
|
|
* @param message the message to send back to the client |
90
|
|
* |
91
|
|
* @return a {@code WebApplicationException} |
92
|
|
*/ |
93
|
|
public static WebApplicationException serverError(@Nullable String message) { |
94
|
1
1. serverError : replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::serverError → KILLED
|
return webApplicationException(message, Status.INTERNAL_SERVER_ERROR); |
95
|
|
} |
96
|
|
|
97
|
|
/** |
98
|
|
* Gives a {@code CacheControl} object with the {@code max-age} directive set to the given value. |
99
|
|
* <p> |
100
|
|
* You would normally use this method like this: |
101
|
|
* |
102
|
|
* <pre> |
103
|
|
* Response response = Response.ok(representation).cacheControl(cacheControl(10)).build(); |
104
|
|
* </pre> |
105
|
|
* |
106
|
|
* @param maxAgeInSeconds the value of the {@code max-age} directive |
107
|
|
* |
108
|
|
* @return a {@code CacheControl} object with the {@code max-age} directive set |
109
|
|
*/ |
110
|
|
public static CacheControl cacheControl(int maxAgeInSeconds) { |
111
|
|
CacheControl cacheControl = new CacheControl(); |
112
|
1
1. cacheControl : removed call to jakarta/ws/rs/core/CacheControl::setMaxAge → KILLED
|
cacheControl.setMaxAge(maxAgeInSeconds); |
113
|
1
1. cacheControl : replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::cacheControl → KILLED
|
return cacheControl; |
114
|
|
} |
115
|
|
|
116
|
|
/** |
117
|
|
* Gives a {@code CacheControl} object with directives set to prevent caching. The directives a set as follows: |
118
|
|
* <ul> |
119
|
|
* <li>{@code no-cache=true} |
120
|
|
* <li>{@code no-store=true} |
121
|
|
* <li>{@code must-revalidate=true} |
122
|
|
* </ul> |
123
|
|
* |
124
|
|
* @return a {@code CacheControl} object with directives set to prevent caching |
125
|
|
*/ |
126
|
|
public static CacheControl noCache() { |
127
|
|
CacheControl cacheControl = new CacheControl(); |
128
|
1
1. noCache : removed call to jakarta/ws/rs/core/CacheControl::setNoCache → KILLED
|
cacheControl.setNoCache(true); |
129
|
1
1. noCache : removed call to jakarta/ws/rs/core/CacheControl::setNoStore → KILLED
|
cacheControl.setNoStore(true); |
130
|
1
1. noCache : removed call to jakarta/ws/rs/core/CacheControl::setMustRevalidate → KILLED
|
cacheControl.setMustRevalidate(true); |
131
|
1
1. noCache : replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::noCache → KILLED
|
return cacheControl; |
132
|
|
} |
133
|
|
|
134
|
|
} |
| | Mutations |
39 |
|
1.1 Location : webApplicationException Killed by : none removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNull → SURVIVED
Covering tests
Covered by tests:
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionStringNullStatus()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:serverError()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:badRequest()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionString()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionNullThrowable()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:notFound()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:badRequestNull()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:notFoundNull()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:serverErrorNull()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionThrowable()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionNullString()]
- com.reallifedeveloper.common.resource.ResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceTest]/[method:handleErrorNullPointerException()]
- com.reallifedeveloper.common.resource.ResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceTest]/[method:handleErrorFileNotFoundException()]
- com.reallifedeveloper.common.resource.documentation.DocumentationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.documentation.DocumentationResourceTest]/[method:getDocumentationNamedDocumentDoesNotExist()]
- com.reallifedeveloper.common.resource.documentation.DocumentationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.documentation.DocumentationResourceTest]/[method:getDocumentationDefaultDocumentDoesNotExist()]
- com.reallifedeveloper.common.resource.notification.NotificationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.notification.NotificationResourceTest]/[method:getCurrentNotificationLogServiceThrowsIllegalArgumentException()]
- com.reallifedeveloper.common.resource.notification.NotificationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.notification.NotificationResourceTest]/[method:getCurrentNotificationLogServiceThrowsNullPointerException()]
- com.reallifedeveloper.common.resource.notification.NotificationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.notification.NotificationResourceTest]/[method:getNotificationLogNullNotificationLogString()]
- com.reallifedeveloper.common.resource.notification.NotificationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.notification.NotificationResourceTest]/[method:getNotificationLogMalformedNotificationLogString()]
- com.reallifedeveloper.common.resource.notification.NotificationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.notification.NotificationResourceTest]/[method:getNotificationLogServiceThrowsIllegalArgumentException()]
- com.reallifedeveloper.common.resource.notification.NotificationResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.notification.NotificationResourceTest]/[method:getNotificationLogServiceThrowsNullPointerException()]
- com.reallifedeveloper.common.resource.ResourceTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceTest]/[method:handleErrorIllegalArgumentException()]
|
41 |
|
1.1 Location : webApplicationException Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:serverError()] negated conditional → KILLED
|
44 |
|
1.1 Location : webApplicationException Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:serverError()] replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::webApplicationException → KILLED
|
59 |
|
1.1 Location : webApplicationException Killed by : none removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNull → SURVIVED
Covering tests
Covered by tests:
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionThrowableNullStatus()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionNullThrowable()]
- com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionThrowable()]
|
60 |
|
1.1 Location : webApplicationException Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionNullThrowable()] negated conditional → KILLED
|
61 |
|
1.1 Location : webApplicationException Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:webApplicationExceptionNullThrowable()] replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::webApplicationException → KILLED
|
72 |
|
1.1 Location : badRequest Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:badRequestNull()] replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::badRequest → KILLED
|
83 |
|
1.1 Location : notFound Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:notFound()] replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::notFound → KILLED
|
94 |
|
1.1 Location : serverError Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:serverError()] replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::serverError → KILLED
|
112 |
|
1.1 Location : cacheControl Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:cacheControl()] removed call to jakarta/ws/rs/core/CacheControl::setMaxAge → KILLED
|
113 |
|
1.1 Location : cacheControl Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:cacheControl()] replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::cacheControl → KILLED
|
128 |
|
1.1 Location : noCache Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:noCache()] removed call to jakarta/ws/rs/core/CacheControl::setNoCache → KILLED
|
129 |
|
1.1 Location : noCache Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:noCache()] removed call to jakarta/ws/rs/core/CacheControl::setNoStore → KILLED
|
130 |
|
1.1 Location : noCache Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:noCache()] removed call to jakarta/ws/rs/core/CacheControl::setMustRevalidate → KILLED
|
131 |
|
1.1 Location : noCache Killed by : com.reallifedeveloper.common.resource.ResourceUtilTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.common.resource.ResourceUtilTest]/[method:noCache()] replaced return value with null for com/reallifedeveloper/common/resource/ResourceUtil::noCache → KILLED
|