Search in sources :

Example 1 with PolicyDefinitionBean

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;
}
Also used : PolicyType(io.apiman.manager.api.beans.policies.PolicyType) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with PolicyDefinitionBean

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);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) UpdatePolicyDefinitionBean(io.apiman.manager.api.beans.policies.UpdatePolicyDefinitionBean) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) PolicyDefinitionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionAlreadyExistsException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException)

Example 3 with PolicyDefinitionBean

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;
}
Also used : PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean)

Example 4 with PolicyDefinitionBean

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));
}
Also used : PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Test(org.junit.Test)

Example 5 with PolicyDefinitionBean

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;
    });
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) Date(java.util.Date) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean)

Aggregations

PolicyDefinitionBean (io.apiman.manager.api.beans.policies.PolicyDefinitionBean)28 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)16 PolicyDefinitionTemplateBean (io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean)12 Test (org.junit.Test)11 StorageException (io.apiman.manager.api.core.exceptions.StorageException)10 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)7 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)7 PolicyDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException)7 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)7 IOException (java.io.IOException)7 PoliciesBean (io.apiman.manager.api.es.beans.PoliciesBean)5 Date (java.util.Date)4 NewPluginBean (io.apiman.manager.api.beans.plugins.NewPluginBean)3 PluginBean (io.apiman.manager.api.beans.plugins.PluginBean)3 UpdatePolicyDefinitionBean (io.apiman.manager.api.beans.policies.UpdatePolicyDefinitionBean)3 InvalidPluginException (io.apiman.manager.api.core.exceptions.InvalidPluginException)3 PluginAlreadyExistsException (io.apiman.manager.api.rest.exceptions.PluginAlreadyExistsException)3 PluginNotFoundException (io.apiman.manager.api.rest.exceptions.PluginNotFoundException)3 PluginResourceNotFoundException (io.apiman.manager.api.rest.exceptions.PluginResourceNotFoundException)3 Plugin (io.apiman.common.plugin.Plugin)2