Search in sources :

Example 1 with PolicyDefinitionTemplateBean

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

the class PolicyTemplateUtil method generatePolicyDescription.

/**
 * Generates a dynamic description for the given policy and stores the
 * result on the policy bean instance.  This should be done prior
 * to returning the policybean back to the user for a REST call to the
 * management API.
 * @param policy the policy
 * @throws Exception any exception
 */
public static void generatePolicyDescription(PolicyBean policy) throws Exception {
    PolicyDefinitionBean def = policy.getDefinition();
    PolicyDefinitionTemplateBean templateBean = getTemplateBean(def);
    if (templateBean == null) {
        return;
    }
    // $NON-NLS-1$
    String cacheKey = def.getId() + "::" + templateBean.getLanguage();
    CompiledTemplate template = templateCache.get(cacheKey);
    if (template == null) {
        template = TemplateCompiler.compileTemplate(templateBean.getTemplate());
        templateCache.put(cacheKey, template);
    }
    try {
        // TODO hack to fix broken descriptions - this util should probably not know about encrypted data
        String jsonConfig = policy.getConfiguration();
        if (CurrentDataEncrypter.instance != null) {
            EntityType entityType = EntityType.Api;
            if (policy.getType() == PolicyType.Client) {
                entityType = EntityType.ClientApp;
            } else if (policy.getType() == PolicyType.Plan) {
                entityType = EntityType.Plan;
            }
            DataEncryptionContext ctx = new DataEncryptionContext(policy.getOrganizationId(), policy.getEntityId(), policy.getEntityVersion(), entityType);
            jsonConfig = CurrentDataEncrypter.instance.decrypt(jsonConfig, ctx);
        }
        Map<String, Object> configMap = mapper.readValue(jsonConfig, Map.class);
        configMap = new PolicyConfigMap(configMap);
        String desc = (String) TemplateRuntime.execute(template, configMap);
        policy.setDescription(desc);
    } catch (Exception e) {
        e.printStackTrace();
        // TODO properly log the error
        policy.setDescription(templateBean.getTemplate());
    }
}
Also used : PolicyDefinitionTemplateBean(io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean) EntityType(io.apiman.common.util.crypt.DataEncryptionContext.EntityType) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) DataEncryptionContext(io.apiman.common.util.crypt.DataEncryptionContext) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 2 with PolicyDefinitionTemplateBean

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

the class PolicyTemplateUtil method getTemplateBean.

/**
 * Determines the appropriate template bean to use given the current locale.
 * @param def
 */
private static PolicyDefinitionTemplateBean getTemplateBean(PolicyDefinitionBean def) {
    Locale currentLocale = Messages.i18n.getLocale();
    String lang = currentLocale.getLanguage();
    // $NON-NLS-1$
    String country = lang + "_" + currentLocale.getCountry();
    PolicyDefinitionTemplateBean nullBean = null;
    PolicyDefinitionTemplateBean langBean = null;
    PolicyDefinitionTemplateBean countryBean = null;
    for (PolicyDefinitionTemplateBean pdtb : def.getTemplates()) {
        if (pdtb.getLanguage() == null) {
            nullBean = pdtb;
        } else if (pdtb.getLanguage().equals(country)) {
            countryBean = pdtb;
            break;
        } else if (pdtb.getLanguage().equals(lang)) {
            langBean = pdtb;
        }
    }
    if (countryBean != null) {
        return countryBean;
    }
    if (langBean != null) {
        return langBean;
    }
    if (nullBean != null) {
        return nullBean;
    }
    return null;
}
Also used : Locale(java.util.Locale) PolicyDefinitionTemplateBean(io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean)

Example 3 with PolicyDefinitionTemplateBean

use of io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean 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 4 with PolicyDefinitionTemplateBean

use of io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean 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 5 with PolicyDefinitionTemplateBean

use of io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean 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

PolicyDefinitionTemplateBean (io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean)14 PolicyDefinitionBean (io.apiman.manager.api.beans.policies.PolicyDefinitionBean)12 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)10 Test (org.junit.Test)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 DataEncryptionContext (io.apiman.common.util.crypt.DataEncryptionContext)1 EntityType (io.apiman.common.util.crypt.DataEncryptionContext.EntityType)1 PolicyFormType (io.apiman.manager.api.beans.summary.PolicyFormType)1 StorageException (io.apiman.manager.api.core.exceptions.StorageException)1 PoliciesBean (io.apiman.manager.api.es.beans.PoliciesBean)1 IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 CompiledTemplate (org.mvel2.templates.CompiledTemplate)1