Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
feat(gateway): Manage group of endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld committed Jun 4, 2018
1 parent 98d7900 commit 3df3ca5
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 125 deletions.
Expand Up @@ -51,6 +51,7 @@ public ApiModule() {
addDeserializer(Properties.class, new PropertiesDeserializer(Properties.class));
addDeserializer(Property.class, new PropertyDeserializer(Property.class));
addDeserializer(Cors.class, new CorsDeserializer(Cors.class));
addDeserializer(EndpointGroup.class, new EndpointGroupDeserializer(EndpointGroup.class));

// then serializers:
addSerializer(Api.class, new ApiSerializer(Api.class));
Expand All @@ -68,5 +69,6 @@ public ApiModule() {
addSerializer(Properties.class, new PropertiesSerializer(Properties.class));
addSerializer(Property.class, new PropertySerializer(Property.class));
addSerializer(Cors.class, new CorsSerializer(Cors.class));
addSerializer(EndpointGroup.class, new EndpointGroupSerializer(EndpointGroup.class));
}
}
@@ -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;
}
}
Expand Up @@ -16,13 +16,14 @@
package io.gravitee.definition.jackson.datatype.api.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
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.*;
import io.gravitee.definition.model.endpoint.HttpEndpoint;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -51,45 +52,19 @@ public Proxy deserialize(JsonParser jp, DeserializationContext ctxt)
}

final JsonNode nodeEndpoints = node.get("endpoints");
final JsonNode nodeGroups = node.get("groups");

if (nodeEndpoints != null && nodeEndpoints.isArray()) {
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");
}
}
}

proxy.setEndpoints(endpoints);
createDefaultEndpointGroup(node, jp.getCodec(), proxy);
} else if (nodeGroups != null && nodeGroups.isArray()) {
createEndpointGroups(node, jp.getCodec(), proxy);
}

JsonNode stripContextNode = node.get("strip_context_path");
if (stripContextNode != null) {
proxy.setStripContextPath(stripContextNode.asBoolean(false));
}

JsonNode loadBalancingNode = node.get("load_balancing");
if (loadBalancingNode != null) {
LoadBalancer loadBalancer = loadBalancingNode.traverse(jp.getCodec()).readValueAs(LoadBalancer.class);
proxy.setLoadBalancer(loadBalancer);
}

JsonNode failoverNode = node.get("failover");
if (failoverNode != null) {
Failover failover = failoverNode.traverse(jp.getCodec()).readValueAs(Failover.class);
Expand All @@ -112,6 +87,25 @@ public Proxy deserialize(JsonParser jp, DeserializationContext ctxt)
return proxy;
}

private void createDefaultEndpointGroup(JsonNode node, ObjectCodec codec, Proxy proxy) throws IOException {
final EndpointGroup group = node.traverse(codec).readValueAs(EndpointGroup.class);
group.setName("default-group");
proxy.setGroups(Collections.singleton(group));
}

private void createEndpointGroups(JsonNode node, ObjectCodec codec, Proxy proxy) throws IOException {
final JsonNode nodeGroups = node.get("groups");

Set<EndpointGroup> groups = new LinkedHashSet<>(nodeGroups.size());

for (JsonNode jsonNode : nodeGroups) {
EndpointGroup group = jsonNode.traverse(codec).readValueAs(EndpointGroup.class);
groups.add(group);
}

proxy.setGroups(groups);
}

private String formatContextPath(String contextPath) {
String [] parts = contextPath.split("/");
StringBuilder finalPath = new StringBuilder("/");
Expand Down
@@ -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();
}
}
Expand Up @@ -18,7 +18,7 @@
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 io.gravitee.definition.model.Proxy;

import java.io.IOException;
Expand All @@ -43,24 +43,20 @@ public void serialize(Proxy proxy, JsonGenerator jgen, SerializerProvider provid
jgen.writeStringField("loggingMode", proxy.getLoggingMode().name());
}

final Set<Endpoint> endpoints = proxy.getEndpoints();
final Set<EndpointGroup> groups = proxy.getGroups();

if (endpoints != null) {
jgen.writeArrayFieldStart("endpoints");
endpoints.forEach(endpoint -> {
if (groups != null) {
jgen.writeArrayFieldStart("groups");
groups.forEach(group -> {
try {
jgen.writeObject(endpoint);
jgen.writeObject(group);
} catch (IOException e) {
e.printStackTrace();
}
});
jgen.writeEndArray();
}

if (proxy.getLoadBalancer() != null) {
jgen.writeObjectField("load_balancing", proxy.getLoadBalancer());
}

if (proxy.getFailover() != null) {
jgen.writeObjectField("failover", proxy.getFailover());
}
Expand Down

0 comments on commit 3df3ca5

Please sign in to comment.