use of io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException 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.rest.exceptions.PolicyDefinitionNotFoundException in project apiman by apiman.
the class PluginResourceImpl method getPolicyForm.
/**
* @see IPluginResource#getPolicyForm(java.lang.Long, java.lang.String)
*/
@Override
public String getPolicyForm(Long pluginId, String policyDefId) throws PluginNotFoundException, PluginResourceNotFoundException, PolicyDefinitionNotFoundException {
// No permission check is needed
PluginBean pbean;
PolicyDefinitionBean pdBean;
try {
pbean = storage.getPlugin(pluginId);
if (pbean == null) {
throw ExceptionFactory.pluginNotFoundException(pluginId);
}
pdBean = storage.getPolicyDefinition(policyDefId);
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
PluginCoordinates coordinates = new PluginCoordinates(pbean.getGroupId(), pbean.getArtifactId(), pbean.getVersion(), pbean.getClassifier(), pbean.getType());
try {
if (pdBean == null) {
throw ExceptionFactory.policyDefNotFoundException(policyDefId);
}
if (pdBean.getPluginId() == null || !pdBean.getPluginId().equals(pbean.getId())) {
throw ExceptionFactory.pluginNotFoundException(pluginId);
}
if (pdBean.getFormType() == PolicyFormType.JsonSchema && pdBean.getForm() != null) {
String formPath = pdBean.getForm();
if (!formPath.startsWith("/")) {
// $NON-NLS-1$
// $NON-NLS-1$
formPath = "META-INF/apiman/policyDefs/" + formPath;
} else {
formPath = formPath.substring(1);
}
Plugin plugin = pluginRegistry.loadPlugin(coordinates);
PluginClassLoader loader = plugin.getLoader();
InputStream resource = null;
try {
resource = loader.getResourceAsStream(formPath);
if (resource == null) {
throw ExceptionFactory.pluginResourceNotFoundException(formPath, coordinates);
}
StringWriter writer = new StringWriter();
IOUtils.copy(resource, writer);
return writer.toString();
} finally {
IOUtils.closeQuietly(resource);
}
} else {
throw ExceptionFactory.pluginResourceNotFoundException(null, coordinates);
}
} catch (AbstractRestException e) {
throw e;
} catch (Throwable t) {
throw new SystemErrorException(t);
}
}
use of io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException in project apiman by apiman.
the class PolicyDefinitionResourceImpl method delete.
/**
* @see IPolicyDefinitionResource#delete(java.lang.String)
*/
@Override
public void delete(String policyDefinitionId) throws PolicyDefinitionNotFoundException, NotAuthorizedException {
securityContext.checkAdminPermissions();
try {
PolicyDefinitionBean pdb = storage.getPolicyDefinition(policyDefinitionId);
if (pdb == null) {
throw ExceptionFactory.policyDefNotFoundException(policyDefinitionId);
}
if (pdb.getPluginId() != null) {
// $NON-NLS-1$
throw new SystemErrorException(Messages.i18n.format("CannotDeletePluginPolicyDef"));
}
storage.deletePolicyDefinition(pdb);
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException in project apiman by apiman.
the class PolicyDefinitionResourceImpl method update.
/**
* @see IPolicyDefinitionResource#update(java.lang.String, io.apiman.manager.api.beans.policies.UpdatePolicyDefinitionBean)
*/
@Override
public void update(String policyDefinitionId, UpdatePolicyDefinitionBean bean) throws PolicyDefinitionNotFoundException, NotAuthorizedException {
securityContext.checkAdminPermissions();
try {
PolicyDefinitionBean pdb = storage.getPolicyDefinition(policyDefinitionId);
if (pdb == null) {
throw ExceptionFactory.policyDefNotFoundException(policyDefinitionId);
}
if (pdb.getPluginId() != null) {
// $NON-NLS-1$
throw new SystemErrorException(Messages.i18n.format("CannotUpdatePluginPolicyDef"));
}
if (bean.getName() != null)
pdb.setName(bean.getName());
if (bean.getDescription() != null)
pdb.setDescription(bean.getDescription());
if (bean.getIcon() != null)
pdb.setIcon(bean.getIcon());
storage.updatePolicyDefinition(pdb);
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException in project apiman by apiman.
the class OrganizationResourceImpl method doCreatePolicy.
/**
* 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)
*/
private PolicyBean doCreatePolicy(String organizationId, String entityId, String entityVersion, NewPolicyBean bean, PolicyType type) throws PolicyDefinitionNotFoundException {
if (bean.getDefinitionId() == null) {
// $NON-NLS-1$
throw ExceptionFactory.policyDefNotFoundException("null");
}
PolicyDefinitionBean def;
try {
storage.beginTx();
def = storage.getPolicyDefinition(bean.getDefinitionId());
if (def == null) {
throw ExceptionFactory.policyDefNotFoundException(bean.getDefinitionId());
}
storage.rollbackTx();
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
int newIdx;
try {
newIdx = query.getMaxPolicyOrderIndex(organizationId, entityId, entityVersion, type) + 1;
} catch (StorageException e) {
throw new SystemErrorException(e);
}
try {
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);
storage.beginTx();
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));
storage.commitTx();
PolicyTemplateUtil.generatePolicyDescription(policy);
// $NON-NLS-1$
log.debug(String.format("Created client policy: %s", policy));
return policy;
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
Aggregations