use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.
the class OrganizationResourceImpl method reorderPlanPolicies.
/**
* @see IOrganizationResource#reorderPlanPolicies(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.policies.PolicyChainBean)
*/
@Override
public void reorderPlanPolicies(String organizationId, String planId, String version, PolicyChainBean policyChain) throws OrganizationNotFoundException, PlanVersionNotFoundException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.planEdit, organizationId);
// Make sure the plan version exists
PlanVersionBean pvb = getPlanVersionInternal(organizationId, planId, version);
try {
storage.beginTx();
List<Long> newOrder = new ArrayList<>(policyChain.getPolicies().size());
for (PolicySummaryBean psb : policyChain.getPolicies()) {
newOrder.add(psb.getId());
}
storage.reorderPolicies(PolicyType.Plan, organizationId, planId, version, newOrder);
storage.createAuditEntry(AuditUtils.policiesReordered(pvb, PolicyType.Plan, securityContext));
pvb.setModifiedBy(securityContext.getCurrentUser());
pvb.setModifiedOn(new Date());
storage.updatePlanVersion(pvb);
storage.commitTx();
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.
the class OrganizationResourceImpl method getApiPolicyChain.
/**
* @see IOrganizationResource#getApiPolicyChain(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public PolicyChainBean getApiPolicyChain(String organizationId, String apiId, String version, String planId) throws ApiVersionNotFoundException, PlanNotFoundException {
// No permission check is needed, because this would break All APIs UI
// Try to get the API first - will throw an exception if not found.
ApiVersionBean avb = getApiVersion(organizationId, apiId, version);
try {
String planVersion = null;
Set<ApiPlanBean> plans = avb.getPlans();
if (plans != null) {
for (ApiPlanBean apiPlanBean : plans) {
if (apiPlanBean.getPlanId().equals(planId)) {
planVersion = apiPlanBean.getVersion();
break;
}
}
}
if (planVersion == null) {
throw ExceptionFactory.planNotFoundException(planId);
}
// Hide sensitive data and set only needed data for the UI
List<PolicySummaryBean> apiPolicies = RestHelper.hideSensitiveDataFromPolicySummaryBeanList(securityContext, query.getPolicies(organizationId, apiId, version, PolicyType.Api));
List<PolicySummaryBean> planPolicies = RestHelper.hideSensitiveDataFromPolicySummaryBeanList(securityContext, query.getPolicies(organizationId, planId, planVersion, PolicyType.Plan));
PolicyChainBean chain = new PolicyChainBean();
chain.getPolicies().addAll(planPolicies);
chain.getPolicies().addAll(apiPolicies);
return chain;
} catch (StorageException e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.
the class OrganizationResourceImpl method createApiVersion.
/**
* @see IOrganizationResource#createApiVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.apis.NewApiVersionBean)
*/
@Override
public ApiVersionBean createApiVersion(String organizationId, String apiId, NewApiVersionBean bean) throws ApiNotFoundException, NotAuthorizedException, InvalidVersionException, ApiVersionAlreadyExistsException {
securityContext.checkPermissions(PermissionType.apiEdit, organizationId);
FieldValidator.validateVersion(bean.getVersion());
ApiVersionBean newVersion;
try {
GatewaySummaryBean gateway = getSingularGateway();
storage.beginTx();
ApiBean api = getApiFromStorage(organizationId, apiId);
if (storage.getApiVersion(organizationId, apiId, bean.getVersion()) != null) {
throw ExceptionFactory.apiVersionAlreadyExistsException(apiId, bean.getVersion());
}
newVersion = createApiVersionInternal(bean, api, gateway);
storage.commitTx();
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
if (bean.isClone() && bean.getCloneVersion() != null) {
try {
ApiVersionBean cloneSource = getApiVersion(organizationId, apiId, bean.getCloneVersion());
// Clone primary attributes of the API version unless those attributes
// were included in the NewApiVersionBean. In other words, information
// sent as part of the "create version" payload take precedence over the
// cloned attributes.
UpdateApiVersionBean updatedApi = new UpdateApiVersionBean();
if (bean.getEndpoint() == null) {
updatedApi.setEndpoint(cloneSource.getEndpoint());
}
if (bean.getEndpointType() == null) {
updatedApi.setEndpointType(cloneSource.getEndpointType());
}
if (bean.getEndpointContentType() == null) {
updatedApi.setEndpointContentType(cloneSource.getEndpointContentType());
}
updatedApi.setEndpointProperties(cloneSource.getEndpointProperties());
updatedApi.setGateways(cloneSource.getGateways());
if (bean.getPlans() == null) {
updatedApi.setPlans(cloneSource.getPlans());
}
if (bean.getPublicAPI() == null) {
updatedApi.setPublicAPI(cloneSource.isPublicAPI());
}
if (bean.getParsePayload() == null) {
updatedApi.setParsePayload(bean.getParsePayload());
}
newVersion = updateApiVersion(organizationId, apiId, bean.getVersion(), updatedApi);
if (bean.getDefinitionUrl() == null) {
// Clone the API definition document
InputStream definition = null;
try {
Response response = getApiDefinition(organizationId, apiId, bean.getCloneVersion());
definition = (InputStream) response.getEntity();
storeApiDefinition(organizationId, apiId, newVersion.getVersion(), cloneSource.getDefinitionType(), definition, cloneSource.getDefinitionUrl());
} catch (ApiDefinitionNotFoundException svnfe) {
// This is ok - it just means the API doesn't have one, so do nothing.
} catch (Exception sdnfe) {
// $NON-NLS-1$
log.error("Unable to create response", sdnfe);
} finally {
IOUtils.closeQuietly(definition);
}
}
// Clone all API policies
List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, bean.getCloneVersion());
for (PolicySummaryBean policySummary : policies) {
PolicyBean policy = getApiPolicy(organizationId, apiId, bean.getCloneVersion(), policySummary.getId());
NewPolicyBean npb = new NewPolicyBean();
npb.setDefinitionId(policy.getDefinition().getId());
npb.setConfiguration(policy.getConfiguration());
createApiPolicy(organizationId, apiId, newVersion.getVersion(), npb);
}
} catch (Exception e) {
// TODO it's ok if the clone fails - we did our best
if (e != null) {
Throwable t = e;
e = (Exception) t;
}
}
}
return newVersion;
}
use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.
the class OrganizationResourceImpl method createClientVersion.
/**
* @see IOrganizationResource#createClientVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.clients.NewClientVersionBean)
*/
@Override
public ClientVersionBean createClientVersion(String organizationId, String clientId, NewClientVersionBean bean) throws ClientNotFoundException, NotAuthorizedException, InvalidVersionException, ClientVersionAlreadyExistsException {
securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
FieldValidator.validateVersion(bean.getVersion());
ClientVersionBean newVersion;
try {
storage.beginTx();
ClientBean client = getClientFromStorage(organizationId, clientId);
if (storage.getClientVersion(organizationId, clientId, bean.getVersion()) != null) {
throw ExceptionFactory.clientVersionAlreadyExistsException(clientId, bean.getVersion());
}
newVersion = createClientVersionInternal(bean, client);
storage.commitTx();
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
if (bean.isClone() && bean.getCloneVersion() != null) {
try {
List<ContractSummaryBean> contracts = getClientVersionContracts(organizationId, clientId, bean.getCloneVersion());
for (ContractSummaryBean contract : contracts) {
NewContractBean ncb = new NewContractBean();
ncb.setPlanId(contract.getPlanId());
ncb.setApiId(contract.getApiId());
ncb.setApiOrgId(contract.getApiOrganizationId());
ncb.setApiVersion(contract.getApiVersion());
createContract(organizationId, clientId, newVersion.getVersion(), ncb);
}
List<PolicySummaryBean> policies = listClientPolicies(organizationId, clientId, bean.getCloneVersion());
for (PolicySummaryBean policySummary : policies) {
PolicyBean policy = getClientPolicy(organizationId, clientId, bean.getCloneVersion(), policySummary.getId());
NewPolicyBean npb = new NewPolicyBean();
npb.setDefinitionId(policy.getDefinition().getId());
npb.setConfiguration(policy.getConfiguration());
createClientPolicy(organizationId, clientId, newVersion.getVersion(), npb);
}
} catch (Exception e) {
// TODO it's ok if the clone fails - we did our best
}
}
return newVersion;
}
use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.
the class ActionResourceImpl method publishApi.
/**
* Publishes an API to the gateway.
* @param action
*/
private void publishApi(ActionBean action) throws ActionException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.apiAdmin, action.getOrganizationId());
ApiVersionBean versionBean;
try {
versionBean = orgs.getApiVersion(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
} catch (ApiVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ApiNotFound"));
}
// Validate that it's ok to perform this action - API must be Ready.
if (!versionBean.isPublicAPI() && versionBean.getStatus() != ApiStatus.Ready) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
}
if (versionBean.isPublicAPI()) {
if (versionBean.getStatus() == ApiStatus.Retired || versionBean.getStatus() == ApiStatus.Created) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
}
if (versionBean.getStatus() == ApiStatus.Published) {
Date modOn = versionBean.getModifiedOn();
Date publishedOn = versionBean.getPublishedOn();
int c = modOn.compareTo(publishedOn);
if (c <= 0) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ApiRePublishNotRequired"));
}
}
}
Api gatewayApi = new Api();
gatewayApi.setEndpoint(versionBean.getEndpoint());
gatewayApi.setEndpointType(versionBean.getEndpointType().toString());
if (versionBean.getEndpointContentType() != null) {
gatewayApi.setEndpointContentType(versionBean.getEndpointContentType().toString());
}
gatewayApi.setEndpointProperties(versionBean.getEndpointProperties());
gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
gatewayApi.setApiId(versionBean.getApi().getId());
gatewayApi.setVersion(versionBean.getVersion());
gatewayApi.setPublicAPI(versionBean.isPublicAPI());
gatewayApi.setParsePayload(versionBean.isParsePayload());
gatewayApi.setKeysStrippingDisabled(versionBean.getDisableKeysStrip());
boolean hasTx = false;
try {
if (versionBean.isPublicAPI()) {
List<Policy> policiesToPublish = new ArrayList<>();
List<PolicySummaryBean> apiPolicies = query.getPolicies(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion(), PolicyType.Api);
storage.beginTx();
hasTx = true;
for (PolicySummaryBean policySummaryBean : apiPolicies) {
PolicyBean apiPolicy = storage.getPolicy(PolicyType.Api, action.getOrganizationId(), action.getEntityId(), action.getEntityVersion(), policySummaryBean.getId());
Policy policyToPublish = new Policy();
policyToPublish.setPolicyJsonConfig(apiPolicy.getConfiguration());
policyToPublish.setPolicyImpl(apiPolicy.getDefinition().getPolicyImpl());
policiesToPublish.add(policyToPublish);
}
gatewayApi.setApiPolicies(policiesToPublish);
}
} catch (StorageException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
} finally {
if (hasTx) {
storage.rollbackTx();
}
}
// Publish the API to all relevant gateways
try {
storage.beginTx();
Set<ApiGatewayBean> gateways = versionBean.getGateways();
if (gateways == null) {
// $NON-NLS-1$
throw new PublishingException("No gateways specified for API!");
}
for (ApiGatewayBean apiGatewayBean : gateways) {
IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
gatewayLink.publishApi(gatewayApi);
gatewayLink.close();
}
versionBean.setStatus(ApiStatus.Published);
versionBean.setPublishedOn(new Date());
ApiBean api = storage.getApi(action.getOrganizationId(), action.getEntityId());
if (api == null) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new PublishingException("Error: could not find API - " + action.getOrganizationId() + "=>" + action.getEntityId());
}
if (api.getNumPublished() == null) {
api.setNumPublished(1);
} else {
api.setNumPublished(api.getNumPublished() + 1);
}
storage.updateApi(api);
storage.updateApiVersion(versionBean);
storage.createAuditEntry(AuditUtils.apiPublished(versionBean, securityContext));
storage.commitTx();
} catch (PublishingException e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
} catch (Exception e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
}
log.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully published API %s on specified gateways: %s", versionBean.getApi().getName(), versionBean.getApi()));
}
Aggregations