/
EndpointGroupDeserializer.java
158 lines (140 loc) · 6.35 KB
/
EndpointGroupDeserializer.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.definition.jackson.datatype.api.deser;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import io.gravitee.common.http.HttpHeader;
import io.gravitee.common.http.HttpHeaders;
import io.gravitee.definition.model.Endpoint;
import io.gravitee.definition.model.EndpointGroup;
import io.gravitee.definition.model.HttpClientOptions;
import io.gravitee.definition.model.HttpClientSslOptions;
import io.gravitee.definition.model.HttpProxy;
import io.gravitee.definition.model.LoadBalancer;
import io.gravitee.definition.model.services.Services;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class EndpointGroupDeserializer extends StdScalarDeserializer<EndpointGroup> {
private static final String DEFAULT_GROUP_NAME = "default";
public EndpointGroupDeserializer(Class<EndpointGroup> vc) {
super(vc);
}
@Override
public EndpointGroup deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
final EndpointGroup group = new EndpointGroup();
// Read group name
group.setName(node.path("name").asText(DEFAULT_GROUP_NAME));
// Read endpoints
final JsonNode nodeEndpoints = node.get("endpoints");
if (nodeEndpoints != null) {
final Set<Endpoint> endpoints = new LinkedHashSet<>(nodeEndpoints.size());
for (JsonNode jsonNode : nodeEndpoints) {
Endpoint endpoint = jsonNode.traverse(jp.getCodec()).readValueAs(Endpoint.class);
if (endpoint != null) {
boolean added = endpoints.add(endpoint);
if (!added) {
throw JsonMappingException.from(ctxt, "[api] API endpoint names must be unique");
}
}
}
group.setEndpoints(endpoints);
}
// Read load-balancing
JsonNode loadBalancingNode = node.get("load_balancing");
if (loadBalancingNode != null) {
LoadBalancer loadBalancer = loadBalancingNode.traverse(jp.getCodec()).readValueAs(LoadBalancer.class);
group.setLoadBalancer(loadBalancer);
}
JsonNode servicesNode = node.get("services");
if (servicesNode != null) {
Services services = servicesNode.traverse(jp.getCodec()).readValueAs(Services.class);
group.getServices().set(services.getAll());
}
JsonNode httpProxyNode = node.get("proxy");
if (httpProxyNode != null) {
HttpProxy httpProxy = httpProxyNode.traverse(jp.getCodec()).readValueAs(HttpProxy.class);
group.setHttpProxy(httpProxy);
}
JsonNode httpClientOptionsNode = node.get("http");
if (httpClientOptionsNode != null) {
HttpClientOptions httpClientOptions = httpClientOptionsNode.traverse(jp.getCodec()).readValueAs(HttpClientOptions.class);
group.setHttpClientOptions(httpClientOptions);
} else {
group.setHttpClientOptions(new HttpClientOptions());
}
JsonNode httpClientSslOptionsNode = node.get("ssl");
if (httpClientSslOptionsNode != null) {
HttpClientSslOptions httpClientSslOptions = httpClientSslOptionsNode
.traverse(jp.getCodec())
.readValueAs(HttpClientSslOptions.class);
group.setHttpClientSslOptions(httpClientSslOptions);
}
JsonNode headersNode = node.get("headers");
if (headersNode != null && !headersNode.isEmpty(null)) {
if (headersNode.isArray()) {
List<HttpHeader> headers = headersNode
.traverse(ctxt.getParser().getCodec())
.readValueAs(new TypeReference<ArrayList<HttpHeader>>() {});
group.setHeaders(headers);
} else {
Map<String, String> headers = headersNode
.traverse(ctxt.getParser().getCodec())
.readValueAs(new TypeReference<HashMap<String, String>>() {});
group.setHeaders(
headers.entrySet().stream().map(entry -> new HttpHeader(entry.getKey(), entry.getValue())).collect(Collectors.toList())
);
}
}
JsonNode hostHeaderNode = node.get("hostHeader");
if (hostHeaderNode != null) {
String hostHeader = hostHeaderNode.asText();
if (!hostHeader.trim().isEmpty()) {
if (group.getHeaders() == null) {
group.setHeaders(Collections.singletonList(new HttpHeader(HttpHeaders.HOST, hostHeader)));
} else {
Optional<HttpHeader> first = group
.getHeaders()
.stream()
.filter(httpHeader -> HttpHeaders.HOST.equals(httpHeader.getName()))
.findFirst();
if (first.isPresent()) {
first.get().setValue(hostHeader);
} else {
group.getHeaders().add(new HttpHeader(HttpHeaders.HOST, hostHeader));
}
}
}
}
return group;
}
}