This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gateway): Manage group of endpoints
Closes gravitee-io/issues#756
- Loading branch information
Showing
18 changed files
with
373 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...ain/java/io/gravitee/definition/jackson/datatype/api/deser/EndpointGroupDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (C) 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.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; | ||
import io.gravitee.definition.model.Endpoint; | ||
import io.gravitee.definition.model.EndpointGroup; | ||
import io.gravitee.definition.model.EndpointType; | ||
import io.gravitee.definition.model.LoadBalancer; | ||
import io.gravitee.definition.model.endpoint.HttpEndpoint; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author David BRASSELY (david.brassely at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
public class EndpointGroupDeserializer extends StdScalarDeserializer<EndpointGroup> { | ||
|
||
private final static String DEFAULT_GROUP_NAME = "default"; | ||
|
||
public EndpointGroupDeserializer(Class<EndpointGroup> vc) { | ||
super(vc); | ||
} | ||
|
||
@Override | ||
public EndpointGroup deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { | ||
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) { | ||
EndpointType type = EndpointType.valueOf( | ||
jsonNode.path("type").asText(EndpointType.HTTP.name()).toUpperCase()); | ||
|
||
Endpoint endpoint; | ||
switch (type) { | ||
case HTTP: | ||
endpoint = jsonNode.traverse(jp.getCodec()).readValueAs(HttpEndpoint.class); | ||
break; | ||
default: | ||
endpoint = jsonNode.traverse(jp.getCodec()).readValueAs(HttpEndpoint.class); | ||
break; | ||
} | ||
|
||
if (endpoint != null) { | ||
boolean added = endpoints.add(endpoint); | ||
if (!added) { | ||
throw ctxt.mappingException("[api] API must have single endpoint names"); | ||
} | ||
} | ||
} | ||
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); | ||
} | ||
|
||
return group; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...rc/main/java/io/gravitee/definition/jackson/datatype/api/ser/EndpointGroupSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (C) 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.ser; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; | ||
import io.gravitee.definition.model.Endpoint; | ||
import io.gravitee.definition.model.EndpointGroup; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author David BRASSELY (david.brassely at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
public class EndpointGroupSerializer extends StdScalarSerializer<EndpointGroup> { | ||
|
||
public EndpointGroupSerializer(Class<EndpointGroup> vc) { | ||
super(vc); | ||
} | ||
|
||
@Override | ||
public void serialize(EndpointGroup group, JsonGenerator jgen, SerializerProvider provider) throws IOException { | ||
jgen.writeStartObject(); | ||
|
||
jgen.writeStringField("name", group.getName()); | ||
|
||
|
||
final Set<Endpoint> endpoints = group.getEndpoints(); | ||
|
||
if (endpoints != null) { | ||
jgen.writeArrayFieldStart("endpoints"); | ||
endpoints.forEach(endpoint -> { | ||
try { | ||
jgen.writeObject(endpoint); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
jgen.writeEndArray(); | ||
} | ||
|
||
if (group.getLoadBalancer() != null) { | ||
jgen.writeObjectField("load_balancing", group.getLoadBalancer()); | ||
} | ||
|
||
jgen.writeEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.