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());
}
}
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;
}
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();
}
}
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());
}
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());
}
Aggregations