Search in sources :

Example 1 with Flow

use of io.gravitee.definition.model.flow.Flow in project gravitee-management-rest-api by gravitee-io.

the class FlowServiceImpl method convert.

private Flow convert(io.gravitee.repository.management.model.flow.Flow model) {
    Flow flow = new Flow();
    flow.setCondition(model.getCondition());
    flow.setEnabled(model.isEnabled());
    flow.setMethods(model.getMethods());
    flow.setName(model.getName());
    final PathOperator pathOperator = new PathOperator();
    pathOperator.setPath(model.getPath());
    pathOperator.setOperator(Operator.valueOf(model.getOperator().name()));
    flow.setPathOperator(pathOperator);
    flow.setPre(model.getPre().stream().map(this::convert).collect(Collectors.toList()));
    flow.setPost(model.getPost().stream().map(this::convert).collect(Collectors.toList()));
    flow.setConsumers(model.getConsumers().stream().map(this::convert).collect(Collectors.toList()));
    return flow;
}
Also used : Flow(io.gravitee.definition.model.flow.Flow)

Example 2 with Flow

use of io.gravitee.definition.model.flow.Flow in project gravitee-management-rest-api by gravitee-io.

the class APIV1toAPIV2Converter method createFlow.

@NotNull
private Flow createFlow(String path, Set<HttpMethod> methods) {
    // If contains all methods of HttpMethod enum or all methods without OTHER
    Set<HttpMethod> flowMethods = methods.containsAll(HTTP_METHODS) ? Collections.emptySet() : methods;
    final Flow flow = new Flow();
    flow.setName("");
    flow.setCondition("");
    flow.setEnabled(true);
    final PathOperator pathOperator = new PathOperator();
    pathOperator.setPath(path);
    pathOperator.setOperator(Operator.STARTS_WITH);
    flow.setPathOperator(pathOperator);
    flow.setMethods(flowMethods);
    return flow;
}
Also used : PathOperator(io.gravitee.definition.model.flow.PathOperator) HttpMethod(io.gravitee.common.http.HttpMethod) Flow(io.gravitee.definition.model.flow.Flow) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Flow

use of io.gravitee.definition.model.flow.Flow in project gravitee-management-rest-api by gravitee-io.

the class APIV1toAPIV2Converter method migratePathsToFlows.

/**
 * Migrate apiEntity.paths to Flow model.
 * @param paths, the map of paths to migrate
 * @param policies, the list of available policies, containing available scopes
 * @return the list of Flows
 */
private List<Flow> migratePathsToFlows(Map<String, List<Rule>> paths, Set<PolicyEntity> policies) {
    List<Flow> flows = new ArrayList<>();
    if (!CollectionUtils.isEmpty(paths)) {
        paths.forEach((pathKey, pathValue) -> {
            // if all rules for a path have the same set of HttpMethods, then we have a unique flow for this path.
            // else, we have a flow per rule in the path.
            boolean oneFlowPerPathMode = pathValue.stream().map(rule -> {
                Set<HttpMethod> methods = new HashSet<>(rule.getMethods());
                methods.retainAll(HTTP_METHODS);
                return methods;
            }).distinct().count() == 1;
            if (oneFlowPerPathMode) {
                // since, all HttpMethods are the same in this case, we can use `pathValue.getRules().get(0).getMethods()`
                final Flow flow = createFlow(pathKey, pathValue.get(0).getMethods());
                pathValue.forEach(rule -> {
                    configurePolicies(policies, rule, flow);
                });
                // reverse policies of the Post steps otherwise, flow are displayed in the wrong order into the policy studio
                Collections.reverse(flow.getPost());
                flows.add(flow);
            } else {
                pathValue.forEach(rule -> {
                    final Flow flow = createFlow(pathKey, rule.getMethods());
                    configurePolicies(policies, rule, flow);
                    // reverse policies of the Post steps otherwise, flow are displayed in the wrong order into the policy studio
                    Collections.reverse(flow.getPost());
                    flows.add(flow);
                });
            }
        });
    }
    return flows;
}
Also used : PATCH(io.gravitee.common.http.HttpMethod.PATCH) Arrays(java.util.Arrays) PUT(io.gravitee.common.http.HttpMethod.PUT) GET(io.gravitee.common.http.HttpMethod.GET) OPTIONS(io.gravitee.common.http.HttpMethod.OPTIONS) PathOperator(io.gravitee.definition.model.flow.PathOperator) Flow(io.gravitee.definition.model.flow.Flow) HEAD(io.gravitee.common.http.HttpMethod.HEAD) HashMap(java.util.HashMap) InvalidDataException(io.gravitee.rest.api.service.exceptions.InvalidDataException) Operator(io.gravitee.definition.model.flow.Operator) PlanStatus(io.gravitee.rest.api.model.PlanStatus) ArrayList(java.util.ArrayList) Rule(io.gravitee.definition.model.Rule) HashSet(java.util.HashSet) PolicyHelper.clearNullValues(io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues) POST(io.gravitee.common.http.HttpMethod.POST) Collections.reverseOrder(java.util.Collections.reverseOrder) Map(java.util.Map) JsonLoader(com.github.fge.jackson.JsonLoader) JsonNode(com.fasterxml.jackson.databind.JsonNode) Step(io.gravitee.definition.model.flow.Step) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) CONNECT(io.gravitee.common.http.HttpMethod.CONNECT) DELETE(io.gravitee.common.http.HttpMethod.DELETE) PlanEntity(io.gravitee.rest.api.model.PlanEntity) Set(java.util.Set) IOException(java.io.IOException) NavigableMap(java.util.NavigableMap) Collectors(java.util.stream.Collectors) FlowMode(io.gravitee.definition.model.FlowMode) List(java.util.List) Component(org.springframework.stereotype.Component) HttpMethod(io.gravitee.common.http.HttpMethod) DefinitionVersion(io.gravitee.definition.model.DefinitionVersion) TreeMap(java.util.TreeMap) CollectionUtils(org.springframework.util.CollectionUtils) PolicyEntity(io.gravitee.rest.api.model.PolicyEntity) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) TRACE(io.gravitee.common.http.HttpMethod.TRACE) Plan(io.gravitee.definition.model.Plan) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Flow(io.gravitee.definition.model.flow.Flow)

Example 4 with Flow

use of io.gravitee.definition.model.flow.Flow in project gravitee-management-rest-api by gravitee-io.

the class OAIToAPIV2Converter method createFlow.

private Flow createFlow(String path, Set<HttpMethod> methods) {
    final Flow flow = new Flow();
    flow.setName("");
    flow.setCondition("");
    flow.setEnabled(true);
    final PathOperator pathOperator = new PathOperator();
    pathOperator.setPath(path);
    pathOperator.setOperator(Operator.EQUALS);
    flow.setPathOperator(pathOperator);
    flow.setMethods(methods);
    return flow;
}
Also used : PathOperator(io.gravitee.definition.model.flow.PathOperator) Flow(io.gravitee.definition.model.flow.Flow)

Example 5 with Flow

use of io.gravitee.definition.model.flow.Flow in project gravitee-management-rest-api by gravitee-io.

the class SwaggerService_CreateAPIV2Test method validateRules.

@Override
protected void validateRules(SwaggerApiEntity api, String path, int expectedRuleSize, List<HttpMethod> firstRuleMethods, String firstRuleDescription) {
    List<Flow> flows = api.getFlows().stream().filter(flow1 -> flow1.getPath().equals(path)).collect(Collectors.toList());
    // assertEquals(1, flows.size());
    assertEquals(expectedRuleSize, flows.get(0).getPre().size());
    assertTrue(flows.get(0).getMethods().containsAll(firstRuleMethods));
    Step step = flows.get(0).getPre().get(0);
    assertNotNull(step);
    assertEquals(firstRuleDescription, step.getDescription());
}
Also used : Step(io.gravitee.definition.model.flow.Step) java.util(java.util) Flow(io.gravitee.definition.model.flow.Flow) RunWith(org.junit.runner.RunWith) IOException(java.io.IOException) Test(org.junit.Test) io.gravitee.definition.model(io.gravitee.definition.model) Collectors(java.util.stream.Collectors) HttpMethod(io.gravitee.common.http.HttpMethod) Arrays.asList(java.util.Arrays.asList) SwaggerApiEntity(io.gravitee.rest.api.model.api.SwaggerApiEntity) io.gravitee.rest.api.model(io.gravitee.rest.api.model) Assert(org.junit.Assert) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Step(io.gravitee.definition.model.flow.Step) Flow(io.gravitee.definition.model.flow.Flow)

Aggregations

Flow (io.gravitee.definition.model.flow.Flow)9 HttpMethod (io.gravitee.common.http.HttpMethod)4 PathOperator (io.gravitee.definition.model.flow.PathOperator)4 Collectors (java.util.stream.Collectors)4 DefinitionVersion (io.gravitee.definition.model.DefinitionVersion)3 Step (io.gravitee.definition.model.flow.Step)3 java.util (java.util)3 FlowMode (io.gravitee.definition.model.FlowMode)2 Rule (io.gravitee.definition.model.Rule)2 Operator (io.gravitee.definition.model.flow.Operator)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 io.gravitee.rest.api.model (io.gravitee.rest.api.model)2 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)2 SwaggerApiEntity (io.gravitee.rest.api.model.api.SwaggerApiEntity)2 PolicyHelper.clearNullValues (io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues)2 IOException (java.io.IOException)2 NotNull (org.jetbrains.annotations.NotNull)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1