Search in sources :

Example 1 with IPolicyProbeResponse

use of io.apiman.gateway.engine.beans.IPolicyProbeResponse in project apiman by apiman.

the class ContractService method probePolicy.

// TODO make properly optimised query for this
public List<IPolicyProbeResponse> probePolicy(Long contractId, long policyId, String rawPayload) throws ClientNotFoundException, ContractNotFoundException {
    ContractBean contract = getContract(contractId);
    ApiVersionBean avb = contract.getApi();
    OrganizationBean apiOrg = avb.getApi().getOrganization();
    String apiKey = contract.getClient().getApikey();
    Set<String> gatewayIds = contract.getApi().getGateways().stream().map(ApiGatewayBean::getGatewayId).collect(Collectors.toSet());
    if (gatewayIds.size() == 0) {
        return List.of();
    }
    List<PolicyBean> policyChain = aggregateContractPolicies(contract);
    int idxFound = -1;
    for (int i = 0, policyChainSize = policyChain.size(); i < policyChainSize; i++) {
        PolicyBean policy = policyChain.get(i);
        if (policy.getId().equals(policyId)) {
            idxFound = i;
        }
    }
    if (idxFound == -1) {
        throw new IllegalArgumentException("Provided policy ID not found in contract " + policyId);
    }
    List<GatewayBean> gateways = tryAction(() -> storage.getGateways(gatewayIds));
    LOGGER.debug("Gateways for contract {0}: {1}", contractId, gateways);
    List<IPolicyProbeResponse> probeResponses = new ArrayList<>(gateways.size());
    for (GatewayBean gateway : gateways) {
        IGatewayLink link = gatewayLinkFactory.create(gateway);
        try {
            probeResponses.add(link.probe(apiOrg.getId(), avb.getApi().getId(), avb.getVersion(), idxFound, apiKey, rawPayload));
        } catch (GatewayAuthenticationException e) {
            throw new SystemErrorException(e);
        }
    }
    LOGGER.debug("Probe responses for contract {0}: {1}", contractId, probeResponses);
    return probeResponses;
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 2 with IPolicyProbeResponse

use of io.apiman.gateway.engine.beans.IPolicyProbeResponse in project apiman by apiman.

the class ApiResourceImpl method getPolicy.

private void getPolicy(Api api, int policyIdx, String probeConfigRaw, AsyncResponse response) {
    if (policyIdx < api.getApiPolicies().size()) {
        // Get API policy by index
        Policy policyConfig = api.getApiPolicies().get(policyIdx);
        IPolicyFactory policyFactory = getEngine().getPolicyFactory();
        // Load the policy (may not have been loaded yet, but is usually cached).
        policyFactory.loadPolicy(policyConfig.getPolicyImpl(), policyLoad -> {
            // Generate & load appropriate config for policy (is cached, so OK to do repeatedly).
            IPolicy policy = policyLoad.getResult();
            PolicyContextImpl policyContext = new PolicyContextImpl(getEngine().getComponentRegistry());
            ProbeContext probeContext = buildProbeContext(api, null, null, api.getEndpointType());
            // Probe it!
            if (policy instanceof IPolicyProbe) {
                IPolicyProbe<?, ?> policyWithProbe = (IPolicyProbe<?, ?>) policy;
                policyWithProbe.probe(probeConfigRaw, policyConfig.getPolicyJsonConfig(), probeContext, policyContext, probeResult -> {
                    IPolicyProbeResponse probeResponse = probeResult.getResult();
                    LOGGER.debug("Probe response for config {0} -> {1}", probeConfigRaw, probeResponse);
                    response.resume(Response.ok(probeResponse).build());
                });
            } else {
                response.resume(Response.status(Status.NOT_IMPLEMENTED.getStatusCode(), "Requested policy does not implement a policy probe").build());
            }
        });
    } else {
        response.resume(new IllegalArgumentException("Provided policy index out of bounds: " + policyIdx));
    }
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) Policy(io.apiman.gateway.engine.beans.Policy) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) IPolicy(io.apiman.gateway.engine.policy.IPolicy) ProbeContext(io.apiman.gateway.engine.policy.ProbeContext) IPolicyFactory(io.apiman.gateway.engine.policy.IPolicyFactory) IPolicyProbe(io.apiman.gateway.engine.policy.IPolicyProbe) PolicyContextImpl(io.apiman.gateway.engine.policy.PolicyContextImpl)

Example 3 with IPolicyProbeResponse

use of io.apiman.gateway.engine.beans.IPolicyProbeResponse in project apiman by apiman.

the class ApiResourceImpl method getPolicy.

private void getPolicy(ApiContract contract, int policyIdx, String probeConfigRaw, AsyncResponse response) {
    if (policyIdx < contract.getPolicies().size()) {
        // Get API policy by index
        Policy policyConfig = contract.getPolicies().get(policyIdx);
        IPolicyFactory policyFactory = getEngine().getPolicyFactory();
        // Load the policy (may not have been loaded yet, but is usually cached).
        policyFactory.loadPolicy(policyConfig.getPolicyImpl(), policyLoad -> {
            // Generate & load appropriate config for policy (is cached, so OK to do repeatedly).
            IPolicy policy = policyLoad.getResult();
            PolicyContextImpl policyContext = new PolicyContextImpl(getEngine().getComponentRegistry());
            Api api = contract.getApi();
            Client client = contract.getClient();
            ProbeContext probeContext = buildProbeContext(contract.getApi(), contract, client.getApiKey(), api.getEndpointType());
            // Probe it!
            if (policy instanceof IPolicyProbe) {
                IPolicyProbe<?, ?> policyWithProbe = (IPolicyProbe<?, ?>) policy;
                policyWithProbe.probe(probeConfigRaw, policyConfig.getPolicyJsonConfig(), probeContext, policyContext, probeResult -> {
                    IPolicyProbeResponse probeResponse = probeResult.getResult();
                    LOGGER.debug("Probe response for config {0} -> {1}", probeConfigRaw, probeResponse);
                    response.resume(Response.ok(ProbeRegistry.serialize(probeResponse)).build());
                });
            } else {
                response.resume(Response.status(Status.NOT_IMPLEMENTED.getStatusCode(), "Requested policy does not implement a policy probe").build());
            }
        });
    } else {
        response.resume(new IllegalArgumentException("Provided policy index out of bounds: " + policyIdx));
    }
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) Policy(io.apiman.gateway.engine.beans.Policy) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) IPolicy(io.apiman.gateway.engine.policy.IPolicy) ProbeContext(io.apiman.gateway.engine.policy.ProbeContext) IPolicyFactory(io.apiman.gateway.engine.policy.IPolicyFactory) IPolicyProbe(io.apiman.gateway.engine.policy.IPolicyProbe) Api(io.apiman.gateway.engine.beans.Api) Client(io.apiman.gateway.engine.beans.Client) PolicyContextImpl(io.apiman.gateway.engine.policy.PolicyContextImpl)

Example 4 with IPolicyProbeResponse

use of io.apiman.gateway.engine.beans.IPolicyProbeResponse in project apiman by apiman.

the class ApiResourceImpl method getPolicy.

private void getPolicy(ApiContract contract, int policyIdx, String probeConfigRaw, AsyncResponse response) {
    if (policyIdx < contract.getPolicies().size()) {
        // Get API policy by index
        Policy policyConfig = contract.getPolicies().get(policyIdx);
        IPolicyFactory policyFactory = engine.getPolicyFactory();
        // Load the policy (may not have been loaded yet, but is usually cached).
        policyFactory.loadPolicy(policyConfig.getPolicyImpl(), policyLoad -> {
            // Generate & load appropriate config for policy (is cached, so OK to do repeatedly).
            IPolicy policy = policyLoad.getResult();
            PolicyContextImpl policyContext = new PolicyContextImpl(engine.getComponentRegistry());
            Api api = contract.getApi();
            Client client = contract.getClient();
            ProbeContext probeContext = buildProbeContext(contract.getApi(), contract, client.getApiKey(), api.getEndpointType());
            // Probe it!
            if (policy instanceof IPolicyProbe) {
                IPolicyProbe<?, ?> policyWithProbe = (IPolicyProbe<?, ?>) policy;
                policyWithProbe.probe(probeConfigRaw, policyConfig.getPolicyJsonConfig(), probeContext, policyContext, probeResult -> {
                    IPolicyProbeResponse probeResponse = probeResult.getResult();
                    LOGGER.debug("Probe response for config {0} -> {1}", probeConfigRaw, probeResponse);
                    response.resume(Response.ok(probeResponse).build());
                });
            } else {
                response.resume(Response.status(Status.NOT_IMPLEMENTED.getStatusCode(), "Requested policy does not implement a policy probe").build());
            }
        });
    } else {
        response.resume(new IllegalArgumentException("Provided policy index out of bounds: " + policyIdx));
    }
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) Policy(io.apiman.gateway.engine.beans.Policy) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) IPolicy(io.apiman.gateway.engine.policy.IPolicy) ProbeContext(io.apiman.gateway.engine.policy.ProbeContext) IPolicyFactory(io.apiman.gateway.engine.policy.IPolicyFactory) IPolicyProbe(io.apiman.gateway.engine.policy.IPolicyProbe) Api(io.apiman.gateway.engine.beans.Api) Client(io.apiman.gateway.engine.beans.Client) PolicyContextImpl(io.apiman.gateway.engine.policy.PolicyContextImpl)

Example 5 with IPolicyProbeResponse

use of io.apiman.gateway.engine.beans.IPolicyProbeResponse in project apiman by apiman.

the class ApiResourceImpl method getPolicy.

private void getPolicy(Api api, int policyIdx, String probeConfigRaw, @Suspended AsyncResponse response) {
    if (policyIdx < api.getApiPolicies().size()) {
        // Get API policy by index
        Policy policyConfig = api.getApiPolicies().get(policyIdx);
        IPolicyFactory policyFactory = engine.getPolicyFactory();
        // Load the policy (may not have been loaded yet, but is usually cached).
        policyFactory.loadPolicy(policyConfig.getPolicyImpl(), policyLoad -> {
            // Generate & load appropriate config for policy (is cached, so OK to do repeatedly).
            IPolicy policy = policyLoad.getResult();
            PolicyContextImpl policyContext = new PolicyContextImpl(engine.getComponentRegistry());
            ProbeContext probeContext = buildProbeContext(api, null, null, api.getEndpointType());
            // Probe it!
            if (policy instanceof IPolicyProbe) {
                IPolicyProbe<?, ?> policyWithProbe = (IPolicyProbe<?, ?>) policy;
                policyWithProbe.probe(probeConfigRaw, policyConfig.getPolicyJsonConfig(), probeContext, policyContext, probeResult -> {
                    IPolicyProbeResponse probeResponse = probeResult.getResult();
                    LOGGER.debug("Probe response for config {0} -> {1}", probeConfigRaw, probeResponse);
                    response.resume(Response.ok(ProbeRegistry.serialize(probeResponse)).build());
                });
            } else {
                response.resume(Response.status(Status.NOT_IMPLEMENTED.getStatusCode(), "Requested policy does not implement a policy probe").build());
            }
        });
    } else {
        response.resume(new IllegalArgumentException("Provided policy index out of bounds: " + policyIdx));
    }
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) Policy(io.apiman.gateway.engine.beans.Policy) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) IPolicy(io.apiman.gateway.engine.policy.IPolicy) ProbeContext(io.apiman.gateway.engine.policy.ProbeContext) IPolicyFactory(io.apiman.gateway.engine.policy.IPolicyFactory) IPolicyProbe(io.apiman.gateway.engine.policy.IPolicyProbe) PolicyContextImpl(io.apiman.gateway.engine.policy.PolicyContextImpl)

Aggregations

IPolicyProbeResponse (io.apiman.gateway.engine.beans.IPolicyProbeResponse)5 Policy (io.apiman.gateway.engine.beans.Policy)4 IPolicy (io.apiman.gateway.engine.policy.IPolicy)4 IPolicyFactory (io.apiman.gateway.engine.policy.IPolicyFactory)4 IPolicyProbe (io.apiman.gateway.engine.policy.IPolicyProbe)4 PolicyContextImpl (io.apiman.gateway.engine.policy.PolicyContextImpl)4 ProbeContext (io.apiman.gateway.engine.policy.ProbeContext)4 Api (io.apiman.gateway.engine.beans.Api)2 Client (io.apiman.gateway.engine.beans.Client)2 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)1 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)1 ContractBean (io.apiman.manager.api.beans.contracts.ContractBean)1 NewContractBean (io.apiman.manager.api.beans.contracts.NewContractBean)1 GatewayBean (io.apiman.manager.api.beans.gateways.GatewayBean)1 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)1 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)1 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)1 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)1 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)1 ArrayList (java.util.ArrayList)1