Skip to content

Commit

Permalink
fix(): refactor CorsUtil from sonar issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed May 17, 2024
1 parent f100803 commit 70f0404
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class CorsUtils {
public static final String WS = "ws";
public static final String WSS = "wss";

public CorsUtils() {}
public CorsUtils() {
globalCorsMeta = new CorsMeta();
}

public static int getPort(String scheme, int port) {
if (port == -1) {
Expand All @@ -46,23 +48,18 @@ public static int getPort(String scheme, int port) {
return port;
}

public CorsMeta resolveGlobalMeta(Configuration config) {

public void resolveGlobalMeta(Configuration config) {
// Get the CORS configuration properties from the configuration object.
String allowOrigins = config.getString(RestConstants.ALLOWED_ORIGINS);
String allowMethods = config.getString(RestConstants.ALLOWED_METHODS);
String allowHeaders = config.getString(RestConstants.ALLOWED_HEADERS);
String exposeHeaders = config.getString(RestConstants.EXPOSED_HEADERS);
String maxAge = config.getString(RestConstants.MAX_AGE);
// Create a new CorsMeta object and set the properties.
CorsMeta meta = new CorsMeta();
meta.setAllowedOrigins(parseList(allowOrigins));
meta.setAllowedMethods(parseList(allowMethods));
meta.setAllowedHeaders(parseList(allowHeaders));
meta.setExposedHeaders(parseList(exposeHeaders));
meta.setMaxAge(maxAge == null ? null : Long.valueOf(maxAge));
// Return the CorsMeta object.
return meta;
globalCorsMeta.setAllowedOrigins(parseList(allowOrigins));
globalCorsMeta.setAllowedMethods(parseList(allowMethods));
globalCorsMeta.setAllowedHeaders(parseList(allowHeaders));
globalCorsMeta.setExposedHeaders(parseList(exposeHeaders));
globalCorsMeta.setMaxAge(maxAge == null ? null : Long.valueOf(maxAge));
}

@Nullable
Expand All @@ -77,7 +74,7 @@ public CorsMeta getGlobalCorsMeta() {
if (globalCorsMeta == null) {
Configuration globalConfiguration =
ConfigurationUtils.getGlobalConfiguration(ApplicationModel.defaultModel());
globalCorsMeta = resolveGlobalMeta(globalConfiguration);
resolveGlobalMeta(globalConfiguration);
}
return globalCorsMeta;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void testResolveGlobalMetaInCommon() {
Mockito.when(config.getString(RestConstants.EXPOSED_HEADERS)).thenReturn("Content-Type,Authorization");
Mockito.when(config.getString(RestConstants.MAX_AGE)).thenReturn("3600");
Mockito.when(config.getString(RestConstants.MAX_AGE)).thenReturn("3600");
CorsMeta meta = utils.resolveGlobalMeta(config);
utils.resolveGlobalMeta(config);
CorsMeta meta = utils.getGlobalCorsMeta();
Assertions.assertTrue(meta.getAllowedOrigins().contains("http://localhost:8080"));
Assertions.assertTrue(meta.getAllowedMethods().contains("GET"));
Assertions.assertTrue(meta.getAllowedMethods().contains("POST"));
Expand All @@ -57,8 +58,8 @@ void testResolveGlobalMetaWithNullConfig() {
Mockito.when(config.getString(RestConstants.ALLOWED_HEADERS)).thenReturn(null);
Mockito.when(config.getString(RestConstants.EXPOSED_HEADERS)).thenReturn(null);
Mockito.when(config.getString(RestConstants.MAX_AGE)).thenReturn(null);

CorsMeta meta = utils.resolveGlobalMeta(config);
utils.resolveGlobalMeta(config);
CorsMeta meta = utils.getGlobalCorsMeta();
Assertions.assertNull(meta.getMaxAge());
Assertions.assertNull(meta.getAllowedOrigins());
Assertions.assertNull(meta.getAllowedMethods());
Expand Down

0 comments on commit 70f0404

Please sign in to comment.