Skip to content

Commit bbf1266

Browse files
committed
refactor: Move problem+json to HttpContants
1 parent 69a1c83 commit bbf1266

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

hivemq-edge/src/main/java/com/hivemq/api/error/ApiExceptionMapper.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.hivemq.api.error;
1717

1818
import com.hivemq.api.model.ApiErrorMessage;
19+
import com.hivemq.http.HttpConstants;
1920
import jakarta.ws.rs.core.MediaType;
2021
import jakarta.ws.rs.core.Response;
2122
import jakarta.ws.rs.ext.ExceptionMapper;
@@ -32,10 +33,6 @@
3233
*/
3334
@Provider
3435
public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
35-
public static final @NotNull String APPLICATION_PROBLEM_JSON_CHARSET_UTF_8 =
36-
"application/problem+json;charset=utf-8";
37-
public static final @NotNull MediaType APPLICATION_PROBLEM_JSON_TYPE =
38-
new MediaType("application", "problem+json", "utf-8");
3936
private static final @NotNull Logger logger = LoggerFactory.getLogger(ApiExceptionMapper.class);
4037

4138
@Override
@@ -47,7 +44,7 @@ public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
4744
apiError.setFieldName(exception.getFieldName());
4845
return Response.status(exception.getHttpStatusCode())
4946
.entity(apiError)
50-
.type(APPLICATION_PROBLEM_JSON_TYPE)
47+
.type(HttpConstants.APPLICATION_PROBLEM_JSON_TYPE)
5148
.build();
5249
}
5350
}

hivemq-edge/src/main/java/com/hivemq/http/HttpConstants.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.hivemq.http;
1717

1818
import com.hivemq.http.error.ErrorType;
19+
import jakarta.ws.rs.core.MediaType;
20+
import org.jetbrains.annotations.NotNull;
1921

2022
import java.nio.charset.Charset;
2123
import java.nio.charset.StandardCharsets;
@@ -28,27 +30,29 @@ enum METHOD {
2830
GET, POST, HEAD, PUT, DELETE, OPTIONS, CONNECT, TRACE, PATCH
2931
}
3032

31-
String SLASH = "/";
32-
String HTTP = "http";
33-
String HTTPS = "https";
34-
String PROTOCOL_SEP = "://";
35-
String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
36-
String HTTP_URL_REGEX = "https?:\\/\\/(?:w{1,3}\\.)?[^\\s.]+(?:\\.[a-z]+)*(?::\\d+)?((?:\\/\\w+)|(?:-\\w+))*\\/?(?![^<]*(?:<\\/\\w+>|\\/?>))";
37-
Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
38-
String CONTENT_TYPE_HEADER = "Content-Type";
39-
String USER_AGENT_HEADER = "User-Agent";
40-
String CONTENT_ENCODING_HEADER = "Content-Encoding";
41-
String LOCATION_HEADER = "Location";
42-
String AUTH_HEADER = "Authorization";
43-
String BASIC_AUTH_CHALLENGE_HEADER = "WWW-Authenticate";
44-
String BEARER_TOKEN_HEADER = "Bearer %s";
45-
String BASIC_AUTH_HEADER = "Basic %s";
46-
String BASIC_AUTH_REALM = "Basic realm=\"%s\"";
47-
String HTML_MIME_TYPE = "text/html";
48-
String PLAIN_MIME_TYPE = "text/plain";
49-
String JSON_MIME_TYPE = "application/json";
50-
String BASE64_ENCODED_VALUE = "data:%s;base64,%s";
51-
String DEFAULT_MIME_TYPE = HTML_MIME_TYPE;
33+
@NotNull String SLASH = "/";
34+
@NotNull String HTTP = "http";
35+
@NotNull String HTTPS = "https";
36+
@NotNull String PROTOCOL_SEP = "://";
37+
@NotNull String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
38+
@NotNull String HTTP_URL_REGEX = "https?:\\/\\/(?:w{1,3}\\.)?[^\\s.]+(?:\\.[a-z]+)*(?::\\d+)?((?:\\/\\w+)|(?:-\\w+))*\\/?(?![^<]*(?:<\\/\\w+>|\\/?>))";
39+
@NotNull Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
40+
@NotNull String CONTENT_TYPE_HEADER = "Content-Type";
41+
@NotNull String USER_AGENT_HEADER = "User-Agent";
42+
@NotNull String CONTENT_ENCODING_HEADER = "Content-Encoding";
43+
@NotNull String LOCATION_HEADER = "Location";
44+
@NotNull String AUTH_HEADER = "Authorization";
45+
@NotNull String BASIC_AUTH_CHALLENGE_HEADER = "WWW-Authenticate";
46+
@NotNull String BEARER_TOKEN_HEADER = "Bearer %s";
47+
@NotNull String BASIC_AUTH_HEADER = "Basic %s";
48+
@NotNull String BASIC_AUTH_REALM = "Basic realm=\"%s\"";
49+
@NotNull String HTML_MIME_TYPE = "text/html";
50+
@NotNull String PLAIN_MIME_TYPE = "text/plain";
51+
@NotNull String JSON_MIME_TYPE = "application/json";
52+
@NotNull String BASE64_ENCODED_VALUE = "data:%s;base64,%s";
53+
@NotNull String DEFAULT_MIME_TYPE = HTML_MIME_TYPE;
54+
@NotNull String APPLICATION_PROBLEM_JSON_CHARSET_UTF_8 = "application/problem+json;charset=utf-8";
55+
@NotNull MediaType APPLICATION_PROBLEM_JSON_TYPE = new MediaType("application", "problem+json", "utf-8");
5256

5357
int SC_CONTINUE = 100;
5458
int SC_SWITCHING_PROTOCOLS = 101;
@@ -92,7 +96,7 @@ enum METHOD {
9296
int SC_GATEWAY_TIMEOUT = 504;
9397
int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
9498

95-
Map<String,String> MIME_MAP = new HashMap(){{
99+
@NotNull Map<String,String> MIME_MAP = new HashMap<>(){{
96100
put("appcache", "text/cache-manifest");
97101
put("css", "text/css");
98102
put("woff", "font/woff");

hivemq-edge/src/main/java/com/hivemq/util/ErrorResponseUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.hivemq.api.error.ApiExceptionMapper;
1919
import com.hivemq.edge.api.model.ApiError;
20+
import com.hivemq.http.HttpConstants;
2021
import com.hivemq.http.error.ProblemDetails;
2122
import jakarta.ws.rs.core.Response;
2223
import org.jetbrains.annotations.NotNull;
@@ -28,14 +29,14 @@ public class ErrorResponseUtil {
2829
public static @NotNull Response errorResponse(final @NotNull ApiError error) {
2930
return Response.status(error.getStatus())
3031
.entity(error)
31-
.type(ApiExceptionMapper.APPLICATION_PROBLEM_JSON_TYPE)
32+
.type(HttpConstants.APPLICATION_PROBLEM_JSON_TYPE)
3233
.build();
3334
}
3435

3536
public static @NotNull Response errorResponse(final @NotNull ProblemDetails errors) {
3637
return Response.status(errors.getStatus())
3738
.entity(errors)
38-
.type(ApiExceptionMapper.APPLICATION_PROBLEM_JSON_TYPE)
39+
.type(HttpConstants.APPLICATION_PROBLEM_JSON_TYPE)
3940
.build();
4041
}
4142
}

hivemq-edge/src/test/java/com/hivemq/api/auth/BearerTokenAuthTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.hivemq.api.auth.handler.impl.BearerTokenAuthenticationHandler;
2525
import com.hivemq.api.auth.jwt.JwtAuthenticationProvider;
2626
import com.hivemq.api.config.ApiJwtConfiguration;
27-
import com.hivemq.api.error.ApiExceptionMapper;
2827
import com.hivemq.api.resources.impl.AuthenticationResourceImpl;
2928
import com.hivemq.bootstrap.ioc.Injector;
3029
import com.hivemq.edge.api.model.ApiBearerToken;
@@ -140,7 +139,7 @@ public void testAuthenticateInvalidUser() throws IOException {
140139
READ_TIMEOUT);
141140
assertThat(response.getStatusCode()).as("Resource should NOT be accepted").isEqualTo(401);
142141
assertThat(response.getContentType()).as("API authenticate response should be json")
143-
.isEqualTo(ApiExceptionMapper.APPLICATION_PROBLEM_JSON_CHARSET_UTF_8);
142+
.isEqualTo(HttpConstants.APPLICATION_PROBLEM_JSON_CHARSET_UTF_8);
144143
assertThat(mapper.readValue(response.getResponseBody(), ProblemDetails.class)
145144
.getErrors()
146145
.get(0)

hivemq-edge/src/test/java/com/hivemq/api/auth/ChainedAuthTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.hivemq.api.auth.jwt.JwtAuthenticationProvider;
2727
import com.hivemq.api.auth.provider.IUsernamePasswordProvider;
2828
import com.hivemq.api.config.ApiJwtConfiguration;
29-
import com.hivemq.api.error.ApiExceptionMapper;
3029
import com.hivemq.api.resources.impl.AuthenticationResourceImpl;
3130
import com.hivemq.edge.api.model.ApiBearerToken;
3231
import com.hivemq.edge.api.model.UsernamePasswordCredentials;
@@ -141,7 +140,7 @@ public void testAuthenticateInvalidUser() throws IOException {
141140
READ_TIMEOUT);
142141
assertThat(response.getStatusCode()).as("Resource should NOT be accepted").isEqualTo(401);
143142
assertThat(response.getContentType()).as("API authenticate response should be json")
144-
.isEqualTo(ApiExceptionMapper.APPLICATION_PROBLEM_JSON_CHARSET_UTF_8);
143+
.isEqualTo(HttpConstants.APPLICATION_PROBLEM_JSON_CHARSET_UTF_8);
145144
assertThat(mapper.readValue(response.getResponseBody(), ProblemDetails.class)
146145
.getErrors()
147146
.get(0)

0 commit comments

Comments
 (0)