use of io.apiman.manager.api.beans.policies.PolicyDefinitionBean in project apiman by apiman.
the class EsMarshalling method unmarshallPolicies.
/**
* Unmarshals the given map source into a bean.
* @param source the source
* @return the policy beans
*/
@SuppressWarnings("unchecked")
public static PoliciesBean unmarshallPolicies(Map<String, Object> source) {
if (source == null) {
return null;
}
PoliciesBean bean = new PoliciesBean();
bean.setOrganizationId(asString(source.get("organizationId")));
bean.setEntityId(asString(source.get("entityId")));
bean.setEntityVersion(asString(source.get("entityVersion")));
bean.setType(asEnum(source.get("type"), PolicyType.class));
List<Map<String, Object>> policies = (List<Map<String, Object>>) source.get("policies");
if (policies != null) {
for (Map<String, Object> policyMap : policies) {
PolicyBean policy = new PolicyBean();
policy.setOrganizationId(bean.getOrganizationId());
policy.setEntityId(bean.getEntityId());
policy.setEntityVersion(bean.getEntityVersion());
policy.setType(bean.getType());
policy.setConfiguration(asString(policyMap.get("configuration")));
policy.setCreatedBy(asString(policyMap.get("createdBy")));
policy.setCreatedOn(asDate(policyMap.get("createdOn")));
PolicyDefinitionBean def = new PolicyDefinitionBean();
def.setId(asString(policyMap.get("definitionId")));
// Note: this is a placeholder that needs to be resolved later.
policy.setDefinition(def);
policy.setId(asLong(policyMap.get("id")));
policy.setModifiedBy(asString(policyMap.get("modifiedBy")));
policy.setModifiedOn(asDate(policyMap.get("modifiedOn")));
policy.setName(asString(policyMap.get("name")));
policy.setOrderIndex(asInt(policyMap.get("orderIndex")));
bean.getPolicies().add(policy);
}
}
postMarshall(bean);
return bean;
}
use of io.apiman.manager.api.beans.policies.PolicyDefinitionBean in project apiman by apiman.
the class PolicyDefinitionResourceImpl method get.
/**
* @see IPolicyDefinitionResource#get(java.lang.String)
*/
@Override
public PolicyDefinitionBean get(String policyDefinitionId) throws PolicyDefinitionNotFoundException {
// No permission check is needed
try {
storage.beginTx();
PolicyDefinitionBean bean = storage.getPolicyDefinition(policyDefinitionId);
if (bean == null) {
throw ExceptionFactory.policyDefNotFoundException(policyDefinitionId);
}
storage.commitTx();
return bean;
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.policies.PolicyDefinitionBean in project apiman by apiman.
the class EsStorage method getPolicy.
/**
* @see io.apiman.manager.api.core.IStorage#getPolicy(io.apiman.manager.api.beans.policies.PolicyType, java.lang.String, java.lang.String, java.lang.String, java.lang.Long)
*/
@Override
public PolicyBean getPolicy(PolicyType type, String organizationId, String entityId, String version, Long id) throws StorageException {
String docType = getPoliciesDocType(type);
String pid = id(organizationId, entityId, version);
Map<String, Object> source = getEntity(docType, pid);
if (source == null) {
return null;
}
PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
if (policies == null)
return null;
List<PolicyBean> policyBeans = policies.getPolicies();
if (policyBeans != null) {
for (PolicyBean policyBean : policyBeans) {
if (policyBean.getId().equals(id)) {
PolicyDefinitionBean def = getPolicyDefinition(policyBean.getDefinition().getId());
policyBean.setDefinition(def);
return policyBean;
}
}
}
return null;
}
use of io.apiman.manager.api.beans.policies.PolicyDefinitionBean in project apiman by apiman.
the class EsMarshallingTest method testMarshallPolicyDefinitionBean.
/**
* Test method for {@link io.apiman.manager.api.es.EsMarshalling#marshall(io.apiman.manager.api.beans.policies.PolicyDefinitionBean)}.
*/
@Test
public void testMarshallPolicyDefinitionBean() throws Exception {
PolicyDefinitionBean bean = createBean(PolicyDefinitionBean.class);
XContentBuilder builder = EsMarshalling.marshall(bean);
Assert.assertEquals("{\"id\":\"ID\",\"name\":\"NAME\",\"description\":\"DESCRIPTION\",\"form\":\"FORM\",\"formType\":\"Default\",\"icon\":\"ICON\",\"pluginId\":17,\"policyImpl\":\"POLICYIMPL\",\"deleted\":false,\"templates\":[{\"language\":\"LANGUAGE\",\"template\":\"TEMPLATE\"}]}", Strings.toString(builder));
}
use of io.apiman.manager.api.beans.policies.PolicyDefinitionBean in project apiman by apiman.
the class PolicyService method createPolicy.
/**
* Creates a policy for the given entity (supports creating policies for clients,
* APIs, and plans).
*
* @param organizationId
* @param entityId
* @param entityVersion
* @param bean
* @return the stored policy bean (with updated information)
*/
public PolicyBean createPolicy(String organizationId, String entityId, String entityVersion, NewPolicyBean bean, PolicyType type) throws PolicyDefinitionNotFoundException {
return tryAction(() -> {
if (bean.getDefinitionId() == null) {
// $NON-NLS-1$
throw ExceptionFactory.policyDefNotFoundException("null");
}
PolicyDefinitionBean def = storage.getPolicyDefinition(bean.getDefinitionId());
if (def == null) {
throw ExceptionFactory.policyDefNotFoundException(bean.getDefinitionId());
}
int newIdx = query.getMaxPolicyOrderIndex(organizationId, entityId, entityVersion, type) + 1;
PolicyBean policy = new PolicyBean();
policy.setId(null);
policy.setDefinition(def);
policy.setName(def.getName());
policy.setConfiguration(bean.getConfiguration());
policy.setCreatedBy(securityContext.getCurrentUser());
policy.setCreatedOn(new Date());
policy.setModifiedBy(securityContext.getCurrentUser());
policy.setModifiedOn(new Date());
policy.setOrganizationId(organizationId);
policy.setEntityId(entityId);
policy.setEntityVersion(entityVersion);
policy.setType(type);
policy.setOrderIndex(newIdx);
if (type == PolicyType.Client) {
ClientVersionBean cvb = storage.getClientVersion(organizationId, entityId, entityVersion);
cvb.setModifiedBy(securityContext.getCurrentUser());
cvb.setModifiedOn(new Date());
storage.updateClientVersion(cvb);
} else if (type == PolicyType.Api) {
ApiVersionBean avb = storage.getApiVersion(organizationId, entityId, entityVersion);
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
storage.updateApiVersion(avb);
} else if (type == PolicyType.Plan) {
PlanVersionBean pvb = storage.getPlanVersion(organizationId, entityId, entityVersion);
pvb.setModifiedBy(securityContext.getCurrentUser());
pvb.setModifiedOn(new Date());
storage.updatePlanVersion(pvb);
}
storage.createPolicy(policy);
storage.createAuditEntry(AuditUtils.policyAdded(policy, type, securityContext));
PolicyTemplateUtil.generatePolicyDescription(policy);
// $NON-NLS-1$
LOGGER.debug(String.format("Created client policy: %s", policy));
return policy;
});
}
Aggregations