Search in sources :

Example 11 with ApiVersionBean

use of io.apiman.manager.api.beans.apis.ApiVersionBean 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()));
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) Date(java.util.Date) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) Api(io.apiman.gateway.engine.beans.Api) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 12 with ApiVersionBean

use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.

the class DeveloperResourceImpl method getAllPublicApiVersions.

@Override
public List<ApiVersionBean> getAllPublicApiVersions() throws NotAuthorizedException {
    List<ApiVersionBean> apiVersionBeans = new ArrayList<>();
    Iterator<ApiVersionBean> iterator;
    try {
        storage.beginTx();
        iterator = storage.getAllPublicApiVersions();
        storage.commitTx();
        while (iterator.hasNext()) {
            ApiVersionBean apiVersionBean = iterator.next();
            apiVersionBeans.add(apiVersionBean);
        }
    } catch (StorageException e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
    return apiVersionBeans;
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ArrayList(java.util.ArrayList) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 13 with ApiVersionBean

use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.

the class OrganizationResourceImpl method updateApiPolicy.

/**
 * @see IOrganizationResource#updateApiPolicy(java.lang.String,
 *      java.lang.String, java.lang.String, long, io.apiman.manager.api.beans.policies.UpdatePolicyBean)
 */
@Override
public void updateApiPolicy(String organizationId, String apiId, String version, long policyId, UpdatePolicyBean bean) throws OrganizationNotFoundException, ApiVersionNotFoundException, PolicyNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.apiEdit, organizationId);
    // Make sure the API exists
    ApiVersionBean avb = getApiVersion(organizationId, apiId, version);
    try {
        storage.beginTx();
        PolicyBean policy = storage.getPolicy(PolicyType.Api, organizationId, apiId, version, policyId);
        if (policy == null) {
            throw ExceptionFactory.policyNotFoundException(policyId);
        }
        // TODO capture specific change values when auditing policy updates
        if (AuditUtils.valueChanged(policy.getConfiguration(), bean.getConfiguration())) {
            policy.setConfiguration(bean.getConfiguration());
        }
        policy.setModifiedOn(new Date());
        policy.setModifiedBy(securityContext.getCurrentUser());
        storage.updatePolicy(policy);
        storage.createAuditEntry(AuditUtils.policyUpdated(policy, PolicyType.Api, securityContext));
        avb.setModifiedBy(securityContext.getCurrentUser());
        avb.setModifiedOn(new Date());
        storage.updateApiVersion(avb);
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Updated API policy %s", policy));
    } 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) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) UpdatePolicyBean(io.apiman.manager.api.beans.policies.UpdatePolicyBean) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) NewApiVersionBean(io.apiman.manager.api.beans.apis.NewApiVersionBean) Date(java.util.Date) ClientAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) OrganizationAlreadyExistsException(io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) PlanAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) UserNotFoundException(io.apiman.manager.api.rest.exceptions.UserNotFoundException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) RoleNotFoundException(io.apiman.manager.api.rest.exceptions.RoleNotFoundException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) IOException(java.io.IOException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ContractAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException) InvalidClientStatusException(io.apiman.manager.api.rest.exceptions.InvalidClientStatusException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ClientVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException) InvalidPlanStatusException(io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ContractNotFoundException(io.apiman.manager.api.rest.exceptions.ContractNotFoundException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) ClientNotFoundException(io.apiman.manager.api.rest.exceptions.ClientNotFoundException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) InvalidMetricCriteriaException(io.apiman.manager.api.rest.exceptions.InvalidMetricCriteriaException) MalformedURLException(java.net.MalformedURLException) PlanVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanVersionAlreadyExistsException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)

Example 14 with ApiVersionBean

use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.

the class OrganizationResourceImpl method getApiVersionStatus.

/**
 * @see IOrganizationResource#getApiVersionStatus(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public ApiVersionStatusBean getApiVersionStatus(String organizationId, String apiId, String version) throws ApiVersionNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.apiView, organizationId);
    ApiVersionBean versionBean = getApiVersion(organizationId, apiId, version);
    List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, version);
    return apiValidator.getStatus(versionBean, policies);
}
Also used : PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) NewApiVersionBean(io.apiman.manager.api.beans.apis.NewApiVersionBean)

Example 15 with ApiVersionBean

use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.

the class ContractService method probePolicy.

// TODO make properly optimised query for this
public List<IPolicyProbeResponse> probePolicy(Long contractId, long policyId, String rawPayload) throws ClientNotFoundException, ContractNotFoundException {
    ContractBean contract = getContract(contractId);
    ApiVersionBean avb = contract.getApi();
    OrganizationBean apiOrg = avb.getApi().getOrganization();
    String apiKey = contract.getClient().getApikey();
    Set<String> gatewayIds = contract.getApi().getGateways().stream().map(ApiGatewayBean::getGatewayId).collect(Collectors.toSet());
    if (gatewayIds.size() == 0) {
        return List.of();
    }
    List<PolicyBean> policyChain = aggregateContractPolicies(contract);
    int idxFound = -1;
    for (int i = 0, policyChainSize = policyChain.size(); i < policyChainSize; i++) {
        PolicyBean policy = policyChain.get(i);
        if (policy.getId().equals(policyId)) {
            idxFound = i;
        }
    }
    if (idxFound == -1) {
        throw new IllegalArgumentException("Provided policy ID not found in contract " + policyId);
    }
    List<GatewayBean> gateways = tryAction(() -> storage.getGateways(gatewayIds));
    LOGGER.debug("Gateways for contract {0}: {1}", contractId, gateways);
    List<IPolicyProbeResponse> probeResponses = new ArrayList<>(gateways.size());
    for (GatewayBean gateway : gateways) {
        IGatewayLink link = gatewayLinkFactory.create(gateway);
        try {
            probeResponses.add(link.probe(apiOrg.getId(), avb.getApi().getId(), avb.getVersion(), idxFound, apiKey, rawPayload));
        } catch (GatewayAuthenticationException e) {
            throw new SystemErrorException(e);
        }
    }
    LOGGER.debug("Probe responses for contract {0}: {1}", contractId, probeResponses);
    return probeResponses;
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Aggregations

ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)68 StorageException (io.apiman.manager.api.core.exceptions.StorageException)34 Date (java.util.Date)29 NewApiVersionBean (io.apiman.manager.api.beans.apis.NewApiVersionBean)28 UpdateApiVersionBean (io.apiman.manager.api.beans.apis.UpdateApiVersionBean)28 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)25 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)25 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)25 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)23 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)22 PlanVersionNotFoundException (io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException)22 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)20 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)20 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)19 PlanVersionBean (io.apiman.manager.api.beans.plans.PlanVersionBean)18 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)17 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)16 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)16 ApiDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)16 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)16