Skip to content

Commit d8d76a2

Browse files
committed
Load the cors header attributes from the config and not hard wired
1 parent bd31a04 commit d8d76a2

File tree

7 files changed

+75
-12
lines changed

7 files changed

+75
-12
lines changed

src/main/java/io/mapsmessaging/config/RestApiManagerConfig.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import io.mapsmessaging.configuration.ConfigurationProperties;
2424
import io.mapsmessaging.dto.rest.config.BaseConfigDTO;
2525
import io.mapsmessaging.dto.rest.config.RestApiManagerConfigDTO;
26+
import io.mapsmessaging.rest.handler.CorsHeaderManager;
2627
import io.mapsmessaging.utilities.configuration.ConfigurationManager;
2728
import java.io.IOException;
29+
import java.util.HashMap;
30+
import java.util.Map;
2831
import lombok.NoArgsConstructor;
2932

3033
@NoArgsConstructor
@@ -63,6 +66,16 @@ private RestApiManagerConfig(ConfigurationProperties properties) {
6366
if (properties.containsKey("static")) {
6467
this.staticConfig = new StaticConfig((ConfigurationProperties) properties.get("static"));
6568
}
69+
70+
corsHeaders = CorsHeaderManager.getInstance().getCorsHeaders();
71+
if(properties.containsKey("corsHeaders")) {
72+
Map<String, Object> corsHeadersProp = ((ConfigurationProperties) properties.get("corsHeaders")).getMap();
73+
for(Map.Entry<String, Object> entry : corsHeadersProp.entrySet()) {
74+
String key = entry.getKey();
75+
String value = entry.getValue().toString();
76+
corsHeaders.addHeader(key, value);
77+
}
78+
}
6679
}
6780

6881
@Override
@@ -146,7 +159,8 @@ public ConfigurationProperties toConfigurationProperties() {
146159
if (this.staticConfig != null) {
147160
properties.put("static", this.staticConfig.toConfigurationProperties());
148161
}
149-
162+
Map<String, Object> corsHeadersProp = new HashMap<>(CorsHeaderManager.getInstance().getCorsHeaders().getHeaders());
163+
properties.put("corsHeaders", corsHeadersProp);
150164
return properties;
151165
}
152166

src/main/java/io/mapsmessaging/dto/rest/config/RestApiManagerConfigDTO.java

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.mapsmessaging.config.network.impl.TlsConfig;
2222
import io.mapsmessaging.config.rest.StaticConfig;
23+
import io.mapsmessaging.rest.handler.CorsHeaders;
2324
import io.swagger.v3.oas.annotations.media.Schema;
2425
import lombok.Data;
2526
import lombok.EqualsAndHashCode;
@@ -87,4 +88,8 @@ public class RestApiManagerConfigDTO extends BaseConfigDTO {
8788

8889
@Schema(description = "Static configuration", implementation = StaticConfig.class)
8990
protected StaticConfig staticConfig;
91+
92+
@Schema(description = "CORS headers", implementation = CorsHeaders.class)
93+
protected CorsHeaders corsHeaders;
94+
9095
}

src/main/java/io/mapsmessaging/rest/handler/CorsEnabledStaticHttpHandler.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@
2424

2525
public class CorsEnabledStaticHttpHandler extends StaticHttpHandler {
2626

27-
private final CorsHeaders corsHeaders;
28-
2927
public CorsEnabledStaticHttpHandler(String... docRoots) {
3028
super(docRoots);
31-
corsHeaders = new CorsHeaders();
3229
}
3330

3431
@Override
3532
protected boolean handle(String uri, Request request, Response response) throws Exception {
36-
corsHeaders.updateResponseHeaders(response);
33+
CorsHeaderManager.getInstance().getCorsHeaders().updateResponseHeaders(response);
3734
return super.handle(uri, request, response);
3835
}
3936
}

src/main/java/io/mapsmessaging/rest/handler/CorsFilter.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
@Provider
2727
public class CorsFilter implements ContainerResponseFilter {
2828

29-
private final CorsHeaders corsHeaders = new CorsHeaders();
30-
3129
@Override
3230
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
33-
corsHeaders.addFilter(responseContext);
31+
CorsHeaderManager.getInstance().getCorsHeaders().addFilter(responseContext);
3432
}
3533
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright [ 2020 - 2024 ] [Matthew Buckton]
3+
* Copyright [ 2024 - 2024 ] [Maps Messaging B.V.]
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package io.mapsmessaging.rest.handler;
20+
21+
22+
import lombok.Getter;
23+
24+
@SuppressWarnings("java:S6548") // yes it is a singleton
25+
@Getter
26+
public class CorsHeaderManager {
27+
28+
private static class Holder {
29+
static final CorsHeaderManager INSTANCE = new CorsHeaderManager();
30+
}
31+
32+
public static CorsHeaderManager getInstance() {
33+
return Holder.INSTANCE;
34+
}
35+
36+
private final CorsHeaders corsHeaders;
37+
38+
private CorsHeaderManager() {
39+
corsHeaders = new CorsHeaders();
40+
}
41+
42+
}

src/main/java/io/mapsmessaging/rest/handler/CorsHeaders.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@
2121
import jakarta.ws.rs.container.ContainerResponseContext;
2222
import java.util.Map;
2323
import java.util.concurrent.ConcurrentHashMap;
24+
import lombok.Data;
2425
import org.glassfish.grizzly.http.server.Response;
2526

27+
@Data
2628
public class CorsHeaders {
2729

2830
private final Map<String, String> headers;
2931

3032
public CorsHeaders() {
3133
headers = new ConcurrentHashMap<>();
32-
headers.put("Access-Control-Allow-Origin", "*"); // Use "*" or specific domain
33-
headers.put("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
34-
headers.put("Access-Control-Allow-Headers", "Content-Type, Authorization");
35-
headers.put("Access-Control-Allow-Credentials", "true"); // Include if using cookies or authentication
3634
}
3735

36+
public void addHeader(String header, String value) {
37+
headers.put(header, value);
38+
}
3839

3940
public void updateResponseHeaders(Response response) {
4041
for(Map.Entry<String, String> header:headers.entrySet()) {

src/main/resources/RestApi.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,9 @@ RestApi:
7070
static:
7171
enabled: true
7272
directory: "{{MAPS_HOME}}/www/dist"
73+
74+
corsHeaders:
75+
Access-Control-Allow-Origin: "*"
76+
Access-Control-Allow-Methods: "GET, POST, PUT, DELETE, OPTIONS"
77+
Access-Control-Allow-Headers: "Content-Type, Authorization"
78+
Access-Control-Allow-Credentials: "true"

0 commit comments

Comments
 (0)