-
Notifications
You must be signed in to change notification settings - Fork 214
/
CorsEnablingDirective.java
119 lines (104 loc) · 5.28 KB
/
CorsEnablingDirective.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.endpoints.directives;
import static org.apache.pekko.http.javadsl.server.Directives.complete;
import static org.apache.pekko.http.javadsl.server.Directives.concat;
import static org.apache.pekko.http.javadsl.server.Directives.extractRequest;
import static org.apache.pekko.http.javadsl.server.Directives.options;
import static org.apache.pekko.http.javadsl.server.Directives.respondWithHeaders;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.StreamSupport;
import org.eclipse.ditto.gateway.service.util.config.endpoints.HttpConfig;
import org.apache.pekko.http.javadsl.model.HttpHeader;
import org.apache.pekko.http.javadsl.model.HttpMethods;
import org.apache.pekko.http.javadsl.model.HttpResponse;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.http.javadsl.model.headers.AccessControlAllowHeaders;
import org.apache.pekko.http.javadsl.model.headers.AccessControlAllowMethods;
import org.apache.pekko.http.javadsl.model.headers.AccessControlAllowOrigin;
import org.apache.pekko.http.javadsl.model.headers.AccessControlRequestHeaders;
import org.apache.pekko.http.javadsl.model.headers.HttpOrigin;
import org.apache.pekko.http.javadsl.model.headers.HttpOriginRange;
import org.apache.pekko.http.javadsl.model.headers.HttpOriginRanges;
import org.apache.pekko.http.javadsl.model.headers.Origin;
import org.apache.pekko.http.javadsl.server.Directives;
import org.apache.pekko.http.javadsl.server.Route;
/**
* Custom Pekko Http directive enabling CORS for the wrapped Route.
*/
public final class CorsEnablingDirective {
private static final AccessControlAllowMethods ACCESS_CONTROL_ALLOW_METHODS =
AccessControlAllowMethods.create(HttpMethods.OPTIONS, HttpMethods.GET, HttpMethods.PUT, HttpMethods.PATCH,
HttpMethods.POST, HttpMethods.HEAD, HttpMethods.DELETE);
private final HttpConfig httpConfig;
private CorsEnablingDirective(final HttpConfig httpConfig) {
this.httpConfig = checkNotNull(httpConfig, "HTTP config");
}
/**
* Returns an instance of {@code CorsEnablingDirective}.
*
* @param httpConfig the configuration settings of the Gateway service's HTTP behaviour.
* @return the instance.
* @throws NullPointerException if {@code httpConfig} is {@code null}.
*/
public static CorsEnablingDirective getInstance(final HttpConfig httpConfig) {
return new CorsEnablingDirective(httpConfig);
}
/**
* Enables CORS - Cross-Origin Resource Sharing - for the wrapped {@code inner} Route.
*
* @param inner the inner route to be wrapped with the CORS enabling.
* @return the new route wrapping {@code inner} with the CORS enabling.
*/
public Route enableCors(final Supplier<Route> inner) {
return extractRequest(request -> {
if (!httpConfig.isEnableCors()) {
return inner.get();
}
final List<HttpHeader> corsHeaders = new ArrayList<>();
final Optional<Origin> originHeader = request.getHeader(Origin.class);
if (originHeader.isPresent()) {
// when the 'Origin' header was present:
// set the 'Access-Control-Allow-Origin' to be of that exact passed 'Origin' header:
final Origin origin = originHeader.get();
corsHeaders.add(AccessControlAllowOrigin.create(
HttpOriginRange.create(StreamSupport.stream(origin.getOrigins().spliterator(), false)
.toArray(HttpOrigin[]::new))
));
} else {
// otherwise, use 'Access-Control-Allow-Origin': *
corsHeaders.add(AccessControlAllowOrigin.create(HttpOriginRanges.ALL));
}
// allow all methods in 'Access-Control-Allow-Methods':
corsHeaders.add(ACCESS_CONTROL_ALLOW_METHODS);
// allow all headers which were requested with 'Access-Control-Request-Headers':
return Directives.optionalHeaderValueByType(AccessControlRequestHeaders.class, corsRequestHeaders -> {
corsRequestHeaders.ifPresent(toAdd ->
// add them as 'Access-Control-Allow-Headers':
corsHeaders.add(AccessControlAllowHeaders.create(
StreamSupport.stream(toAdd.getHeaders().spliterator(), false)
.toArray(String[]::new))
)
);
return concat(options(() -> complete(
HttpResponse.create().withStatus(StatusCodes.OK).addHeaders(corsHeaders))),
respondWithHeaders(corsHeaders, inner)
);
});
});
}
}