Search in sources :

Example 1 with PolicyBean

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

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

the class EsStorage method updatePolicy.

/**
 * @see io.apiman.manager.api.core.IStorage#updatePolicy(io.apiman.manager.api.beans.policies.PolicyBean)
 */
@Override
public void updatePolicy(PolicyBean policy) throws StorageException {
    String docType = getPoliciesDocType(policy.getType());
    String pid = id(policy.getOrganizationId(), policy.getEntityId(), policy.getEntityVersion());
    Map<String, Object> source = getEntity(docType, pid);
    if (source == null) {
        // $NON-NLS-1$
        throw new StorageException("Policy not found.");
    }
    PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
    List<PolicyBean> policyBeans = policies.getPolicies();
    boolean found = false;
    if (policyBeans != null) {
        for (PolicyBean policyBean : policyBeans) {
            if (policyBean.getId().equals(policy.getId())) {
                policyBean.setConfiguration(policy.getConfiguration());
                policyBean.setModifiedBy(policy.getModifiedBy());
                policyBean.setModifiedOn(policy.getModifiedOn());
                found = true;
                break;
            }
        }
    }
    if (found) {
        updateEntity(docType, pid, EsMarshalling.marshall(policies));
    } else {
        // $NON-NLS-1$
        throw new StorageException("Policy not found.");
    }
}
Also used : PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 3 with PolicyBean

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

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

the class EsStorage method deletePolicy.

/**
 * @see io.apiman.manager.api.core.IStorage#deletePolicy(io.apiman.manager.api.beans.policies.PolicyBean)
 */
@Override
public void deletePolicy(PolicyBean policy) throws StorageException {
    String docType = getPoliciesDocType(policy.getType());
    String pid = id(policy.getOrganizationId(), policy.getEntityId(), policy.getEntityVersion());
    Map<String, Object> source = getEntity(docType, pid);
    if (source == null) {
        // $NON-NLS-1$
        throw new StorageException("Policy not found.");
    }
    PoliciesBean policies = EsMarshalling.unmarshallPolicies(source);
    // $NON-NLS-1$
    if (policies == null)
        throw new StorageException("Policy not found.");
    List<PolicyBean> policyBeans = policies.getPolicies();
    boolean found = false;
    if (policyBeans != null) {
        for (PolicyBean policyBean : policyBeans) {
            if (policyBean.getId().equals(policy.getId())) {
                policies.getPolicies().remove(policyBean);
                found = true;
                break;
            }
        }
    }
    if (found) {
        updateEntity(docType, pid, EsMarshalling.marshall(policies));
    } else {
        // $NON-NLS-1$
        throw new StorageException("Policy not found.");
    }
}
Also used : PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 5 with PolicyBean

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

the class EsStorage method reorderPolicies.

/**
 * @see io.apiman.manager.api.core.IStorage#reorderPolicies(io.apiman.manager.api.beans.policies.PolicyType, java.lang.String, java.lang.String, java.lang.String, java.util.List)
 */
@Override
public void reorderPolicies(PolicyType type, String organizationId, String entityId, String entityVersion, List<Long> newOrder) throws StorageException {
    String docType = getPoliciesDocType(type);
    String pid = id(organizationId, entityId, entityVersion);
    Map<String, Object> source = getEntity(docType, pid);
    if (source == null) {
        return;
    }
    PoliciesBean policiesBean = EsMarshalling.unmarshallPolicies(source);
    List<PolicyBean> policies = policiesBean.getPolicies();
    List<PolicyBean> reordered = new ArrayList<>(policies.size());
    for (Long policyId : newOrder) {
        ListIterator<PolicyBean> iterator = policies.listIterator();
        while (iterator.hasNext()) {
            PolicyBean policyBean = iterator.next();
            if (policyBean.getId().equals(policyId)) {
                iterator.remove();
                reordered.add(policyBean);
                break;
            }
        }
    }
    // remaining at the end of the list.
    for (PolicyBean policyBean : policies) {
        reordered.add(policyBean);
    }
    policiesBean.setPolicies(reordered);
    updateEntity(docType, pid, EsMarshalling.marshall(policiesBean));
}
Also used : PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) PoliciesBean(io.apiman.manager.api.es.beans.PoliciesBean)

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