Search in sources :

Example 21 with PolicyBean

use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.

the class StorageExporter method exportApis.

private void exportApis(String orgId) {
    try {
        writer.startApis();
        Iterator<ApiBean> apiIter = storage.getAllApis(orgId);
        while (apiIter.hasNext()) {
            ApiBean apiBean = apiIter.next();
            writer.startApi(apiBean);
            writer.startApiVersions();
            Iterator<ApiVersionBean> versionIter = storage.getAllApiVersions(orgId, apiBean.getId());
            while (versionIter.hasNext()) {
                ApiVersionBean versionBean = versionIter.next();
                // $NON-NLS-1$
                logger.info(Messages.i18n.format("StorageExporter.ExportingApiVersion") + versionBean);
                writer.startApiVersion(versionBean);
                writer.startApiPolicies();
                Iterator<PolicyBean> policyIter = storage.getAllPolicies(orgId, apiBean.getId(), versionBean.getVersion(), PolicyType.Api);
                while (policyIter.hasNext()) {
                    PolicyBean policyBean = policyIter.next();
                    // $NON-NLS-1$
                    logger.info(Messages.i18n.format("StorageExporter.ExportingApiPolicy") + policyBean);
                    writer.writeApiPolicy(policyBean);
                }
                writer.endApiPolicies();
                // Api definition
                if (versionBean.getDefinitionType() != null && versionBean.getDefinitionType() != None) {
                    logger.info(Messages.i18n.format("StorageExporter.ExportingApiDefinition", versionBean.getDefinitionType()));
                    InputStream data = storage.getApiDefinition(orgId, apiBean.getId(), versionBean.getVersion());
                    if (data != null) {
                        writer.writeApiDefinition(data);
                    }
                }
                writer.endApiVersion();
            }
            writer.endApiVersions();
            writer.endApi();
        }
        writer.endApis();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ApiBean(io.apiman.manager.api.beans.apis.ApiBean) InputStream(java.io.InputStream) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 22 with PolicyBean

use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.

the class StorageImportDispatcher method publishApis.

/**
 * Publishes any apis that were imported in the "Published" state.
 * @throws StorageException
 */
private void publishApis() throws StorageException {
    // $NON-NLS-1$
    logger.info(Messages.i18n.format("StorageExporter.PublishingApis"));
    try {
        for (EntityInfo info : apisToPublish) {
            // $NON-NLS-1$
            logger.info(Messages.i18n.format("StorageExporter.PublishingApi", info));
            ApiVersionBean versionBean = storage.getApiVersion(info.organizationId, info.id, info.version);
            Api gatewayApi = new Api();
            gatewayApi.setEndpoint(versionBean.getEndpoint());
            gatewayApi.setEndpointType(versionBean.getEndpointType().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());
            if (versionBean.isPublicAPI()) {
                List<Policy> policiesToPublish = new ArrayList<>();
                Iterator<PolicyBean> apiPolicies = storage.getAllPolicies(info.organizationId, info.id, info.version, PolicyType.Api);
                while (apiPolicies.hasNext()) {
                    PolicyBean apiPolicy = apiPolicies.next();
                    Policy policyToPublish = new Policy();
                    policyToPublish.setPolicyJsonConfig(apiPolicy.getConfiguration());
                    policyToPublish.setPolicyImpl(apiPolicy.getDefinition().getPolicyImpl());
                    policiesToPublish.add(policyToPublish);
                }
                gatewayApi.setApiPolicies(policiesToPublish);
            }
            // Publish the api to all relevant gateways
            Set<ApiGatewayBean> gateways = versionBean.getGateways();
            if (gateways == null) {
                // $NON-NLS-1$
                throw new RuntimeException("No gateways specified for api!");
            }
            for (ApiGatewayBean apiGatewayBean : gateways) {
                IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
                gatewayLink.publishApi(gatewayApi);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ImportNotNeededException(io.apiman.manager.api.exportimport.exceptions.ImportNotNeededException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) Api(io.apiman.gateway.engine.beans.Api) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 23 with PolicyBean

use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.

the class PolicyTemplateUtilTest method testGeneratePolicyDescription_i18n.

/**
 * Test method for {@link io.apiman.manager.api.core.util.PolicyTemplateUtil#generatePolicyDescription(io.apiman.manager.api.beans.policies.PolicyBean)}.
 * @throws Exception any exception
 */
@Test
public void testGeneratePolicyDescription_i18n() throws Exception {
    PolicyBean policy = new PolicyBean();
    PolicyDefinitionBean def = new PolicyDefinitionBean();
    // $NON-NLS-1$
    def.setId("i18n");
    PolicyDefinitionTemplateBean template = new PolicyDefinitionTemplateBean();
    template.setLanguage(null);
    // $NON-NLS-1$
    template.setTemplate("Default language message.");
    def.getTemplates().add(template);
    template = new PolicyDefinitionTemplateBean();
    // $NON-NLS-1$
    template.setLanguage("en");
    // $NON-NLS-1$
    template.setTemplate("English language message.");
    def.getTemplates().add(template);
    template = new PolicyDefinitionTemplateBean();
    // $NON-NLS-1$
    template.setLanguage("en_US");
    // $NON-NLS-1$
    template.setTemplate("English (US) language message.");
    def.getTemplates().add(template);
    policy.setDefinition(def);
    // $NON-NLS-1$
    policy.setConfiguration("{}");
    try {
        AbstractMessages.setLocale(Locale.ENGLISH);
        PolicyTemplateUtil.generatePolicyDescription(policy);
        // $NON-NLS-1$
        Assert.assertEquals("English language message.", policy.getDescription());
        AbstractMessages.setLocale(Locale.US);
        PolicyTemplateUtil.generatePolicyDescription(policy);
        // $NON-NLS-1$
        Assert.assertEquals("English (US) language message.", policy.getDescription());
    } finally {
        AbstractMessages.clearLocale();
    }
}
Also used : PolicyDefinitionTemplateBean(io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) Test(org.junit.Test)

Example 24 with PolicyBean

use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.

the class PolicyTemplateUtilTest method testGeneratePolicyDescription_arrays.

/**
 * Test method for {@link io.apiman.manager.api.core.util.PolicyTemplateUtil#generatePolicyDescription(io.apiman.manager.api.beans.policies.PolicyBean)}.
 * @throws Exception any exception
 */
@Test
public void testGeneratePolicyDescription_arrays() throws Exception {
    PolicyBean policy = new PolicyBean();
    PolicyDefinitionBean def = new PolicyDefinitionBean();
    // $NON-NLS-1$
    def.setId("arrays");
    PolicyDefinitionTemplateBean template = new PolicyDefinitionTemplateBean();
    // $NON-NLS-1$
    template.setTemplate("@{messages[0]} @{messages[1]}");
    def.getTemplates().add(template);
    policy.setDefinition(def);
    // $NON-NLS-1$
    policy.setConfiguration("{ \"messages\" : [ \"Hello\", \"World\" ] }");
    PolicyTemplateUtil.generatePolicyDescription(policy);
    // $NON-NLS-1$
    Assert.assertEquals("Hello World", policy.getDescription());
}
Also used : PolicyDefinitionTemplateBean(io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) Test(org.junit.Test)

Example 25 with PolicyBean

use of io.apiman.manager.api.beans.policies.PolicyBean in project apiman by apiman.

the class PolicyTemplateUtilTest method testGeneratePolicyDescription_basicauth.

/**
 * Test method for {@link io.apiman.manager.api.core.util.PolicyTemplateUtil#generatePolicyDescription(io.apiman.manager.api.beans.policies.PolicyBean)}.
 * @throws Exception any exception
 */
@Test
public void testGeneratePolicyDescription_basicauth() throws Exception {
    PolicyBean policy = new PolicyBean();
    PolicyDefinitionBean def = new PolicyDefinitionBean();
    // $NON-NLS-1$
    def.setId("basicauth");
    PolicyDefinitionTemplateBean template = new PolicyDefinitionTemplateBean();
    // $NON-NLS-1$
    template.setTemplate("Access to the API is protected by BASIC Authentication through the @{realm} authentication realm.  @if{forwardIdentityHttpHeader != null}Successfully authenticated requests will include '@{forwardIdentityHttpHeader}' as a custom HTTP header to the back end API.@end{}");
    def.getTemplates().add(template);
    policy.setDefinition(def);
    // $NON-NLS-1$
    policy.setConfiguration("{ \"realm\" : \"Example\", \"forwardIdentityHttpHeader\" : \"X-Authenticated-Identity\" }");
    PolicyTemplateUtil.generatePolicyDescription(policy);
    // $NON-NLS-1$
    Assert.assertEquals("Access to the API is protected by BASIC Authentication through the Example authentication realm.  Successfully authenticated requests will include 'X-Authenticated-Identity' as a custom HTTP header to the back end API.", policy.getDescription());
}
Also used : PolicyDefinitionTemplateBean(io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) Test(org.junit.Test)

Aggregations

PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)64 NewPolicyBean (io.apiman.manager.api.beans.policies.NewPolicyBean)31 StorageException (io.apiman.manager.api.core.exceptions.StorageException)30 UpdatePolicyBean (io.apiman.manager.api.beans.policies.UpdatePolicyBean)29 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)22 Date (java.util.Date)22 OrganizationNotFoundException (io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException)19 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)19 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)18 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)18 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)18 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)18 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)18 PlanNotFoundException (io.apiman.manager.api.rest.exceptions.PlanNotFoundException)18 PlanVersionNotFoundException (io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException)18 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)17 ClientNotFoundException (io.apiman.manager.api.rest.exceptions.ClientNotFoundException)17 InvalidClientStatusException (io.apiman.manager.api.rest.exceptions.InvalidClientStatusException)17 InvalidNameException (io.apiman.manager.api.rest.exceptions.InvalidNameException)17 InvalidVersionException (io.apiman.manager.api.rest.exceptions.InvalidVersionException)17