Search in sources :

Example 1 with ContractBean

use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.

the class EsMarshalling method unmarshallContract.

/**
 * Unmarshals the given map source into a bean.
 * @param source the source
 * @return the contract
 */
public static ContractBean unmarshallContract(Map<String, Object> source) {
    if (source == null) {
        return null;
    }
    ContractBean bean = new ContractBean();
    bean.setId(asLong(source.get("id")));
    bean.setCreatedBy(asString(source.get("createdBy")));
    bean.setCreatedOn(asDate(source.get("createdOn")));
    postMarshall(bean);
    return bean;
}
Also used : ContractBean(io.apiman.manager.api.beans.contracts.ContractBean)

Example 2 with ContractBean

use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.

the class OrganizationResourceImpl method createContract.

/**
 * @see IOrganizationResource#createContract(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.contracts.NewContractBean)
 */
@Override
public ContractBean createContract(String organizationId, String clientId, String version, NewContractBean bean) throws OrganizationNotFoundException, ClientNotFoundException, ApiNotFoundException, PlanNotFoundException, ContractAlreadyExistsException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
    try {
        storage.beginTx();
        ContractBean contract = createContractInternal(organizationId, clientId, version, bean);
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Created new contract %s: %s", contract.getId(), contract));
        return contract;
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        // reduce overhead on the typical happy path.
        if (contractAlreadyExists(organizationId, clientId, version, bean)) {
            throw ExceptionFactory.contractAlreadyExistsException();
        } else {
            throw new SystemErrorException(e);
        }
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) 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) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean)

Example 3 with ContractBean

use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.

the class OrganizationResourceImpl method deleteContract.

/**
 * @see IOrganizationResource#deleteContract(java.lang.String, java.lang.String, java.lang.String, java.lang.Long)
 */
@Override
public void deleteContract(String organizationId, String clientId, String version, Long contractId) throws ClientNotFoundException, ContractNotFoundException, NotAuthorizedException, InvalidClientStatusException {
    securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
    try {
        storage.beginTx();
        ContractBean contract = storage.getContract(contractId);
        if (contract == null) {
            throw ExceptionFactory.contractNotFoundException(contractId);
        }
        if (!contract.getClient().getClient().getOrganization().getId().equals(organizationId)) {
            throw ExceptionFactory.contractNotFoundException(contractId);
        }
        if (!contract.getClient().getClient().getId().equals(clientId)) {
            throw ExceptionFactory.contractNotFoundException(contractId);
        }
        if (!contract.getClient().getVersion().equals(version)) {
            throw ExceptionFactory.contractNotFoundException(contractId);
        }
        if (contract.getClient().getStatus() == ClientStatus.Retired) {
            throw ExceptionFactory.invalidClientStatusException();
        }
        storage.deleteContract(contract);
        storage.createAuditEntry(AuditUtils.contractBrokenFromClient(contract, securityContext));
        storage.createAuditEntry(AuditUtils.contractBrokenToApi(contract, securityContext));
        // Update the version with new meta-data (e.g. modified-by)
        ClientVersionBean clientV = getClientVersionFromStorage(organizationId, clientId, version);
        clientV.setModifiedBy(securityContext.getCurrentUser());
        clientV.setModifiedOn(new Date());
        storage.updateClientVersion(clientV);
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Deleted contract: %s", contract));
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
}
Also used : NewClientVersionBean(io.apiman.manager.api.beans.clients.NewClientVersionBean) ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) 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) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean)

Example 4 with ContractBean

use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.

the class OrganizationResourceImpl method getContract.

/**
 * @see IOrganizationResource#getContract(java.lang.String, java.lang.String, java.lang.String, java.lang.Long)
 */
@Override
public ContractBean getContract(String organizationId, String clientId, String version, Long contractId) throws ClientNotFoundException, ContractNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.clientView, organizationId);
    try {
        storage.beginTx();
        ContractBean contract = storage.getContract(contractId);
        if (contract == null)
            throw ExceptionFactory.contractNotFoundException(contractId);
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Got contract %s: %s", contract.getId(), contract));
        return contract;
    } 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) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) 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) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean)

Example 5 with ContractBean

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

ContractBean (io.apiman.manager.api.beans.contracts.ContractBean)27 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)13 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)12 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)12 NewContractBean (io.apiman.manager.api.beans.contracts.NewContractBean)11 StorageException (io.apiman.manager.api.core.exceptions.StorageException)11 PlanVersionBean (io.apiman.manager.api.beans.plans.PlanVersionBean)9 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)8 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)7 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)7 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)7 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)7 Date (java.util.Date)7 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)6 ApiPlanBean (io.apiman.manager.api.beans.apis.ApiPlanBean)6 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)6 ClientStatus (io.apiman.manager.api.beans.clients.ClientStatus)5 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)5 ContractSummaryBean (io.apiman.manager.api.beans.summary.ContractSummaryBean)5 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)5