-
Notifications
You must be signed in to change notification settings - Fork 11
/
BaseAPIRouteBuilder.java
68 lines (59 loc) · 3.01 KB
/
BaseAPIRouteBuilder.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
package at.rovo.awsxray.routes.api;
import at.rovo.awsxray.exceptions.APIException;
import at.rovo.awsxray.routes.beans.PrepareErrorResponse;
import at.rovo.awsxray.security.BasicAuthFailedHandler;
import org.apache.camel.CamelAuthorizationException;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
public abstract class BaseAPIRouteBuilder extends RouteBuilder {
/**
* Configures basic exception handler and a common Camel Rest DSL configuration for inheriting
* Rest endpoints defined by sub classes.
*/
@Override
public void configure() throws Exception {
onException(APIException.class)
.handled(true)
.bean(PrepareErrorResponse.class)
.log("Handled error ${exception.message}");
onException(CamelAuthorizationException.class, BadCredentialsException.class, AuthenticationException.class)
.handled(true)
.logExhausted(false)
.bean(BasicAuthFailedHandler.class)
.log("Handled basic auth failure");
onException(Exception.class)
.handled(true)
.bean(PrepareErrorResponse.class)
.log("Handled generic error ${exception.stacktrace}");
restConfiguration()
.component("jetty")
.scheme("https")
.host("0.0.0.0")
.port("{{api.port}}")
.contextPath("/api")
.endpointProperty("matchOnUriPrefix", "true")
.endpointProperty("sendServerVersion", "false")
// .componentProperty("filtersRef", "#tracingFilters")
.jsonDataFormat("hal+json")
// enable CORS header in the HTTP response
.enableCORS(true)
.corsHeaderProperty("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
.corsHeaderProperty("Access-Control-Allow-Headers",
"Origin, Accept, Content-Type, Accept-Encoding, Accept-Language, "
+ "X-Requested-With, Transfer-Encoding, Authorization, X-APP-KEY, "
+ "Access-Control-Request-Method, Access-Control-Request-Headers")
// we do want to make use of the streaming API which sends out the response in small chunks
// instead of all at once. This, however, will replace the Content-Length header field with
// a Transfer-Encoding: chunked header field. A chunk length of 0 indicates the end of the
// stream
.endpointProperty("chunked", "true");
this.defineRoute();
}
/**
* Defines an custom API route services have to specify.
*
* @throws Exception All thrown exceptions while executing the route
*/
protected abstract void defineRoute() throws Exception;
}