Search in sources :

Example 11 with Policy

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

the class ActionService method publishApi.

/**
 * Publishes an API to the gateway.
 */
public void publishApi(String orgId, String apiId, String apiVersion) throws ActionException, NotAuthorizedException {
    ApiVersionBean versionBean;
    try {
        versionBean = storage.getApiVersion(orgId, apiId, apiVersion);
    } catch (ApiVersionNotFoundException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ApiNotFound"));
    } catch (StorageException e) {
        throw new RuntimeException(e);
    }
    // Validate that it's ok to perform this action - API must be Ready.
    if (!versionBean.isPublicAPI() && versionBean.getStatus() != ApiStatus.Ready) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
    }
    if (versionBean.isPublicAPI()) {
        if (versionBean.getStatus() == ApiStatus.Retired || versionBean.getStatus() == ApiStatus.Created) {
            // $NON-NLS-1$
            throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
        }
        if (versionBean.getStatus() == ApiStatus.Published) {
            Date modOn = versionBean.getModifiedOn();
            Date publishedOn = versionBean.getPublishedOn();
            int c = modOn.compareTo(publishedOn);
            if (c <= 0) {
                // $NON-NLS-1$
                throw ExceptionFactory.actionException(Messages.i18n.format("ApiRePublishNotRequired"));
            }
        }
    }
    Api gatewayApi = new Api();
    gatewayApi.setEndpoint(versionBean.getEndpoint());
    gatewayApi.setEndpointType(versionBean.getEndpointType().toString());
    if (versionBean.getEndpointContentType() != null) {
        gatewayApi.setEndpointContentType(versionBean.getEndpointContentType().toString());
    }
    gatewayApi.setEndpointProperties(versionBean.getEndpointProperties());
    gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
    gatewayApi.setApiId(versionBean.getApi().getId());
    gatewayApi.setVersion(versionBean.getVersion());
    gatewayApi.setPublicAPI(versionBean.isPublicAPI());
    gatewayApi.setParsePayload(versionBean.isParsePayload());
    gatewayApi.setKeysStrippingDisabled(versionBean.getDisableKeysStrip());
    try {
        if (versionBean.isPublicAPI()) {
            List<Policy> policiesToPublish = new ArrayList<>();
            List<PolicySummaryBean> apiPolicies = query.getPolicies(orgId, apiId, apiVersion, PolicyType.Api);
            for (PolicySummaryBean policySummaryBean : apiPolicies) {
                PolicyBean apiPolicy = storage.getPolicy(PolicyType.Api, orgId, apiId, apiVersion, policySummaryBean.getId());
                Policy policyToPublish = new Policy();
                policyToPublish.setPolicyJsonConfig(apiPolicy.getConfiguration());
                policyToPublish.setPolicyImpl(apiPolicy.getDefinition().getPolicyImpl());
                policiesToPublish.add(policyToPublish);
            }
            gatewayApi.setApiPolicies(policiesToPublish);
        }
    } catch (StorageException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
    }
    // Publish the API to all relevant gateways
    try {
        Set<ApiGatewayBean> gateways = versionBean.getGateways();
        if (gateways == null) {
            // $NON-NLS-1$
            throw new PublishingException("No gateways specified for API!");
        }
        for (ApiGatewayBean apiGatewayBean : gateways) {
            IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
            gatewayLink.publishApi(gatewayApi);
            gatewayLink.close();
        }
        versionBean.setStatus(ApiStatus.Published);
        versionBean.setPublishedOn(new Date());
        ApiBean api = storage.getApi(orgId, apiId);
        if (api == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new PublishingException("Error: could not find API - " + orgId + "=>" + apiId);
        }
        if (api.getNumPublished() == null) {
            api.setNumPublished(1);
        } else {
            api.setNumPublished(api.getNumPublished() + 1);
        }
        storage.updateApi(api);
        storage.updateApiVersion(versionBean);
        storage.createAuditEntry(AuditUtils.apiPublished(versionBean, securityContext));
    } catch (Exception e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
    }
    LOGGER.debug(// $NON-NLS-1$
    String.format(// $NON-NLS-1$
    "Successfully published API %s on specified gateways: %s", versionBean.getApi().getName(), versionBean.getApi()));
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) Date(java.util.Date) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) Api(io.apiman.gateway.engine.beans.Api) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 12 with Policy

use of io.apiman.gateway.engine.beans.Policy 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 13 with Policy

use of io.apiman.gateway.engine.beans.Policy 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 14 with Policy

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

the class ApiRequestExecutorImpl method resolvePropertyReplacements.

/**
 * Resolve property replacements for list of policies
 */
private void resolvePropertyReplacements(List<Policy> apiPolicies) {
    if (apiPolicies != null) {
        for (Policy policy : apiPolicies) {
            String config = policy.getPolicyJsonConfig();
            config = resolveProperties(config);
            policy.setPolicyJsonConfig(config);
        }
    }
}
Also used : IDataPolicy(io.apiman.gateway.engine.policy.IDataPolicy) Policy(io.apiman.gateway.engine.beans.Policy) IPolicy(io.apiman.gateway.engine.policy.IPolicy)

Example 15 with Policy

use of io.apiman.gateway.engine.beans.Policy 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)

Aggregations

Policy (io.apiman.gateway.engine.beans.Policy)17 IPolicy (io.apiman.gateway.engine.policy.IPolicy)7 ArrayList (java.util.ArrayList)7 Api (io.apiman.gateway.engine.beans.Api)6 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)6 StorageException (io.apiman.manager.api.core.exceptions.StorageException)5 IPolicyProbeResponse (io.apiman.gateway.engine.beans.IPolicyProbeResponse)4 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)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 PolicySummaryBean (io.apiman.manager.api.beans.summary.PolicySummaryBean)4 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)4 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)3 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)3 PolicyType (io.apiman.manager.api.beans.policies.PolicyType)3 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)3 ActionException (io.apiman.manager.api.rest.exceptions.ActionException)3 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)3