Search in sources :

Example 1 with ClientVersionNotFoundException

use of io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException in project apiman by apiman.

the class OrganizationResourceImpl method updateClientPolicy.

/**
 * @see IOrganizationResource#updateClientPolicy(java.lang.String, java.lang.String, java.lang.String, long, io.apiman.manager.api.beans.policies.UpdatePolicyBean)
 */
@Override
public void updateClientPolicy(String organizationId, String clientId, String version, long policyId, UpdatePolicyBean bean) throws OrganizationNotFoundException, ClientVersionNotFoundException, PolicyNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
    // Make sure the client version exists.
    ClientVersionBean cvb = getClientVersionInternal(organizationId, clientId, version);
    try {
        storage.beginTx();
        PolicyBean policy = this.storage.getPolicy(PolicyType.Client, organizationId, clientId, version, policyId);
        if (policy == null) {
            throw ExceptionFactory.policyNotFoundException(policyId);
        }
        if (AuditUtils.valueChanged(policy.getConfiguration(), bean.getConfiguration())) {
            policy.setConfiguration(bean.getConfiguration());
        // TODO figure out what changed and include that in the audit entry
        }
        policy.setModifiedOn(new Date());
        policy.setModifiedBy(this.securityContext.getCurrentUser());
        storage.updatePolicy(policy);
        storage.createAuditEntry(AuditUtils.policyUpdated(policy, PolicyType.Client, securityContext));
        cvb.setModifiedOn(new Date());
        cvb.setModifiedBy(securityContext.getCurrentUser());
        storage.updateClientVersion(cvb);
        storage.commitTx();
    } 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) 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) 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 2 with ClientVersionNotFoundException

use of io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException in project apiman by apiman.

the class ActionResourceImpl method registerClient.

/**
 * Registers an client (along with all of its contracts) to the gateway.
 * @param action
 */
private void registerClient(ActionBean action) throws ActionException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.clientAdmin, action.getOrganizationId());
    ClientVersionBean versionBean;
    List<ContractSummaryBean> contractBeans;
    try {
        versionBean = orgs.getClientVersion(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
    } catch (ClientVersionNotFoundException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("clientVersionDoesNotExist", action.getEntityId(), action.getEntityVersion()));
    }
    try {
        contractBeans = query.getClientContracts(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
    } catch (StorageException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
    }
    boolean isReregister = false;
    // Validate that it's ok to perform this action
    if (versionBean.getStatus() == ClientStatus.Registered) {
        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("ClientReRegisterNotRequired"));
        }
        isReregister = true;
    }
    Client client = new Client();
    client.setOrganizationId(versionBean.getClient().getOrganization().getId());
    client.setClientId(versionBean.getClient().getId());
    client.setVersion(versionBean.getVersion());
    client.setApiKey(versionBean.getApikey());
    Set<Contract> contracts = new HashSet<>();
    for (ContractSummaryBean contractBean : contractBeans) {
        Contract contract = new Contract();
        contract.setPlan(contractBean.getPlanId());
        contract.setApiId(contractBean.getApiId());
        contract.setApiOrgId(contractBean.getApiOrganizationId());
        contract.setApiVersion(contractBean.getApiVersion());
        contract.getPolicies().addAll(aggregateContractPolicies(contractBean));
        contracts.add(contract);
    }
    client.setContracts(contracts);
    // Each of those gateways must be told about the client.
    try {
        storage.beginTx();
        Map<String, IGatewayLink> links = new HashMap<>();
        for (Contract contract : client.getContracts()) {
            ApiVersionBean svb = storage.getApiVersion(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
            Set<ApiGatewayBean> gateways = svb.getGateways();
            if (gateways == null) {
                // $NON-NLS-1$
                throw new PublishingException("No gateways specified for API: " + svb.getApi().getName());
            }
            for (ApiGatewayBean apiGatewayBean : gateways) {
                if (!links.containsKey(apiGatewayBean.getGatewayId())) {
                    IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
                    links.put(apiGatewayBean.getGatewayId(), gatewayLink);
                }
            }
        }
        if (isReregister) {
            // Once we figure out which gateways to register with, make sure we also "unregister"
            // the client app from all other gateways.  This is necessary because we may have broken
            // contracts we previously had on APIs that are published to other gateways.  And thus
            // it's possible we need to remove a contract from a Gateway that is not otherwise/currently
            // referenced.
            // 
            // This is a fix for:  https://issues.jboss.org/browse/APIMAN-895
            Iterator<GatewayBean> gateways = storage.getAllGateways();
            while (gateways.hasNext()) {
                GatewayBean gbean = gateways.next();
                if (!links.containsKey(gbean.getId())) {
                    IGatewayLink gatewayLink = createGatewayLink(gbean.getId());
                    try {
                        gatewayLink.unregisterClient(client);
                    } catch (Exception e) {
                    // We need to catch the error, but ignore it,
                    // in the event that the gateway is invalid.
                    }
                    gatewayLink.close();
                }
            }
        }
        for (IGatewayLink gatewayLink : links.values()) {
            gatewayLink.registerClient(client);
            gatewayLink.close();
        }
        storage.commitTx();
    } catch (Exception e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
    }
    versionBean.setStatus(ClientStatus.Registered);
    versionBean.setPublishedOn(new Date());
    try {
        storage.beginTx();
        storage.updateClientVersion(versionBean);
        storage.createAuditEntry(AuditUtils.clientRegistered(versionBean, securityContext));
        storage.commitTx();
    } catch (Exception e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
    }
    log.debug(// $NON-NLS-1$
    String.format(// $NON-NLS-1$
    "Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) HashMap(java.util.HashMap) 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) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) Client(io.apiman.gateway.engine.beans.Client) StorageException(io.apiman.manager.api.core.exceptions.StorageException) Contract(io.apiman.gateway.engine.beans.Contract) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) HashSet(java.util.HashSet)

Example 3 with ClientVersionNotFoundException

use of io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException in project apiman by apiman.

the class ActionService method unregisterClient.

/**
 * De-registers an client that is currently registered with the gateway.
 */
public void unregisterClient(String orgId, String clientId, String clientVersion) throws ActionException, NotAuthorizedException {
    ClientVersionBean versionBean;
    List<ContractSummaryBean> contractBeans;
    try {
        versionBean = clientAppService.getClientVersion(orgId, clientId, clientVersion);
    } catch (ClientVersionNotFoundException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"));
    }
    try {
        contractBeans = query.getClientContracts(orgId, clientId, clientVersion).stream().peek(c -> {
            if (c.getStatus() != ContractStatus.Created) {
                LOGGER.debug("Will not try to delete contract {0} from gateway(s) as it is not in 'Created' state", c);
            }
        }).filter(c -> c.getStatus() == ContractStatus.Created).collect(Collectors.toList());
    } catch (StorageException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
    }
    // Validate that it's ok to perform this action - client must be either registered or awaiting approval (or there's nothing to unregister)
    if (versionBean.getStatus() != ClientStatus.Registered && versionBean.getStatus() != ClientStatus.AwaitingApproval) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("InvalidClientStatus"));
    }
    Client client = new Client();
    client.setOrganizationId(versionBean.getClient().getOrganization().getId());
    client.setClientId(versionBean.getClient().getId());
    client.setVersion(versionBean.getVersion());
    // Each of those gateways must be told about the client.
    try {
        Map<String, IGatewayLink> links = new HashMap<>();
        for (ContractSummaryBean contractBean : contractBeans) {
            ApiVersionBean svb = storage.getApiVersion(contractBean.getApiOrganizationId(), contractBean.getApiId(), contractBean.getApiVersion());
            Set<ApiGatewayBean> gateways = svb.getGateways();
            if (gateways == null) {
                // $NON-NLS-1$
                throw new PublishingException("No gateways specified for API: " + svb.getApi().getName());
            }
            for (ApiGatewayBean apiGatewayBean : gateways) {
                if (!links.containsKey(apiGatewayBean.getGatewayId())) {
                    IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
                    links.put(apiGatewayBean.getGatewayId(), gatewayLink);
                }
            }
        }
        for (IGatewayLink gatewayLink : links.values()) {
            gatewayLink.unregisterClient(client);
            gatewayLink.close();
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
    }
    ClientStatus oldStatus = versionBean.getStatus();
    versionBean.setStatus(ClientStatus.Retired);
    versionBean.setRetiredOn(new Date());
    clientAppService.fireClientStatusChangeEvent(versionBean, oldStatus);
    try {
        storage.updateClientVersion(versionBean);
        storage.createAuditEntry(AuditUtils.clientUnregistered(versionBean, securityContext));
    } catch (Exception e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
    }
    LOGGER.debug(// $NON-NLS-1$
    String.format(// $NON-NLS-1$
    "Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ContractApprovalEvent(io.apiman.manager.api.beans.events.ContractApprovalEvent) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) Date(java.util.Date) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) ISecurityContext(io.apiman.manager.api.security.ISecurityContext) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) IGatewayLinkFactory(io.apiman.manager.api.gateway.IGatewayLinkFactory) ApimanEventHeaders(io.apiman.manager.api.beans.events.ApimanEventHeaders) UserBean(io.apiman.manager.api.beans.idm.UserBean) Client(io.apiman.gateway.engine.beans.Client) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) UserMapper(io.apiman.manager.api.beans.idm.UserMapper) IClientValidator(io.apiman.manager.api.core.IClientValidator) Map(java.util.Map) URI(java.net.URI) ApimanLoggerFactory(io.apiman.common.logging.ApimanLoggerFactory) Contract(io.apiman.gateway.engine.beans.Contract) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) Policy(io.apiman.gateway.engine.beans.Policy) Transactional(javax.transaction.Transactional) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) EventService(io.apiman.manager.api.events.EventService) Set(java.util.Set) UUID(java.util.UUID) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) ContractStatus(io.apiman.manager.api.beans.contracts.ContractStatus) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) List(java.util.List) IApimanLogger(io.apiman.common.logging.IApimanLogger) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ContractActionDto(io.apiman.manager.api.beans.actions.ContractActionDto) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) HashMap(java.util.HashMap) ApiStatus(io.apiman.manager.api.beans.apis.ApiStatus) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) AuditUtils(io.apiman.manager.api.rest.impl.audit.AuditUtils) ArrayList(java.util.ArrayList) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) IStorageQuery(io.apiman.manager.api.core.IStorageQuery) DataAccessUtilMixin(io.apiman.manager.api.rest.impl.util.DataAccessUtilMixin) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) Iterator(java.util.Iterator) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) ClientStatus(io.apiman.manager.api.beans.clients.ClientStatus) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) IStorage(io.apiman.manager.api.core.IStorage) ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) Api(io.apiman.gateway.engine.beans.Api) PolicyType(io.apiman.manager.api.beans.policies.PolicyType) Messages(io.apiman.manager.api.rest.exceptions.i18n.Messages) PlanStatus(io.apiman.manager.api.beans.plans.PlanStatus) ExceptionFactory(io.apiman.manager.api.rest.exceptions.util.ExceptionFactory) ClientStatus(io.apiman.manager.api.beans.clients.ClientStatus) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) HashMap(java.util.HashMap) 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) Date(java.util.Date) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) Client(io.apiman.gateway.engine.beans.Client) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 4 with ClientVersionNotFoundException

use of io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException in project apiman by apiman.

the class ActionService method registerClient.

/**
 * Registers a client (along with all of its contracts) to the gateway.
 */
public void registerClient(String orgId, String clientId, String clientVersion) throws ActionException, NotAuthorizedException {
    ClientVersionBean versionBean;
    List<ContractSummaryBean> contractBeans;
    try {
        versionBean = clientAppService.getClientVersion(orgId, clientId, clientVersion);
    } catch (ClientVersionNotFoundException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("clientVersionDoesNotExist", clientId, clientVersion));
    }
    try {
        contractBeans = query.getClientContracts(orgId, clientId, clientVersion);
        // Any awaiting approval then don't let them republish.
        List<ContractSummaryBean> awaitingApproval = contractBeans.stream().filter(f -> f.getStatus() == ContractStatus.AwaitingApproval).collect(Collectors.toList());
        if (!awaitingApproval.isEmpty()) {
            throw ExceptionFactory.contractNotYetApprovedException(awaitingApproval);
        }
    } catch (StorageException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
    }
    boolean isReregister = false;
    // Validate that it's ok to perform this action
    if (versionBean.getStatus() == ClientStatus.Registered) {
        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("ClientReRegisterNotRequired"));
        }
        isReregister = true;
    }
    Client client = new Client();
    client.setOrganizationId(versionBean.getClient().getOrganization().getId());
    client.setClientId(versionBean.getClient().getId());
    client.setVersion(versionBean.getVersion());
    client.setApiKey(versionBean.getApikey());
    Set<Contract> contracts = new HashSet<>();
    for (ContractSummaryBean contractBean : contractBeans) {
        Contract contract = new Contract();
        contract.setPlan(contractBean.getPlanId());
        contract.setApiId(contractBean.getApiId());
        contract.setApiOrgId(contractBean.getApiOrganizationId());
        contract.setApiVersion(contractBean.getApiVersion());
        contract.getPolicies().addAll(contractService.aggregateContractPolicies(contractBean));
        contracts.add(contract);
    }
    client.setContracts(contracts);
    // Each of those gateways must be told about the client.
    try {
        Map<String, IGatewayLink> links = new HashMap<>();
        for (Contract contract : client.getContracts()) {
            ApiVersionBean svb = storage.getApiVersion(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
            Set<ApiGatewayBean> gateways = svb.getGateways();
            if (gateways == null) {
                // $NON-NLS-1$
                throw new PublishingException("No gateways specified for API: " + svb.getApi().getName());
            }
            for (ApiGatewayBean apiGatewayBean : gateways) {
                if (!links.containsKey(apiGatewayBean.getGatewayId())) {
                    IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
                    links.put(apiGatewayBean.getGatewayId(), gatewayLink);
                }
            }
        }
        if (isReregister) {
            // Once we figure out which gateways to register with, make sure we also "unregister"
            // the client app from all other gateways.  This is necessary because we may have broken
            // contracts we previously had on APIs that are published to other gateways.  And thus
            // it's possible we need to remove a contract from a Gateway that is not otherwise/currently
            // referenced.
            // 
            // This is a fix for:  https://issues.jboss.org/browse/APIMAN-895
            Iterator<GatewayBean> gateways = storage.getAllGateways();
            while (gateways.hasNext()) {
                GatewayBean gbean = gateways.next();
                if (!links.containsKey(gbean.getId())) {
                    IGatewayLink gatewayLink = createGatewayLink(gbean.getId());
                    try {
                        gatewayLink.unregisterClient(client);
                    } catch (Exception e) {
                    // We need to catch the error, but ignore it,
                    // in the event that the gateway is invalid.
                    }
                    gatewayLink.close();
                }
            }
        }
        for (IGatewayLink gatewayLink : links.values()) {
            gatewayLink.registerClient(client);
            gatewayLink.close();
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
    }
    ClientStatus oldStatus = versionBean.getStatus();
    versionBean.setStatus(ClientStatus.Registered);
    versionBean.setPublishedOn(new Date());
    try {
        storage.updateClientVersion(versionBean);
        storage.createAuditEntry(AuditUtils.clientRegistered(versionBean, securityContext));
        clientAppService.fireClientStatusChangeEvent(versionBean, oldStatus);
    } catch (Exception e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
    }
    LOGGER.debug(// $NON-NLS-1$
    String.format(// $NON-NLS-1$
    "Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ContractApprovalEvent(io.apiman.manager.api.beans.events.ContractApprovalEvent) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) Date(java.util.Date) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) ISecurityContext(io.apiman.manager.api.security.ISecurityContext) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) IGatewayLinkFactory(io.apiman.manager.api.gateway.IGatewayLinkFactory) ApimanEventHeaders(io.apiman.manager.api.beans.events.ApimanEventHeaders) UserBean(io.apiman.manager.api.beans.idm.UserBean) Client(io.apiman.gateway.engine.beans.Client) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) UserMapper(io.apiman.manager.api.beans.idm.UserMapper) IClientValidator(io.apiman.manager.api.core.IClientValidator) Map(java.util.Map) URI(java.net.URI) ApimanLoggerFactory(io.apiman.common.logging.ApimanLoggerFactory) Contract(io.apiman.gateway.engine.beans.Contract) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) Policy(io.apiman.gateway.engine.beans.Policy) Transactional(javax.transaction.Transactional) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) EventService(io.apiman.manager.api.events.EventService) Set(java.util.Set) UUID(java.util.UUID) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) ContractStatus(io.apiman.manager.api.beans.contracts.ContractStatus) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) List(java.util.List) IApimanLogger(io.apiman.common.logging.IApimanLogger) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ContractActionDto(io.apiman.manager.api.beans.actions.ContractActionDto) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) HashMap(java.util.HashMap) ApiStatus(io.apiman.manager.api.beans.apis.ApiStatus) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) AuditUtils(io.apiman.manager.api.rest.impl.audit.AuditUtils) ArrayList(java.util.ArrayList) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) IStorageQuery(io.apiman.manager.api.core.IStorageQuery) DataAccessUtilMixin(io.apiman.manager.api.rest.impl.util.DataAccessUtilMixin) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) Iterator(java.util.Iterator) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) ClientStatus(io.apiman.manager.api.beans.clients.ClientStatus) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) IStorage(io.apiman.manager.api.core.IStorage) ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) Api(io.apiman.gateway.engine.beans.Api) PolicyType(io.apiman.manager.api.beans.policies.PolicyType) Messages(io.apiman.manager.api.rest.exceptions.i18n.Messages) PlanStatus(io.apiman.manager.api.beans.plans.PlanStatus) ExceptionFactory(io.apiman.manager.api.rest.exceptions.util.ExceptionFactory) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) HashMap(java.util.HashMap) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) Client(io.apiman.gateway.engine.beans.Client) HashSet(java.util.HashSet) ClientStatus(io.apiman.manager.api.beans.clients.ClientStatus) Date(java.util.Date) 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) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException) Contract(io.apiman.gateway.engine.beans.Contract) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 5 with ClientVersionNotFoundException

use of io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException in project apiman by apiman.

the class ActionResourceImpl method unregisterClient.

/**
 * De-registers an client that is currently registered with the gateway.
 * @param action
 */
private void unregisterClient(ActionBean action) throws ActionException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.clientAdmin, action.getOrganizationId());
    ClientVersionBean versionBean;
    List<ContractSummaryBean> contractBeans;
    try {
        versionBean = orgs.getClientVersion(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
    } catch (ClientVersionNotFoundException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"));
    }
    try {
        contractBeans = query.getClientContracts(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
    } catch (StorageException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
    }
    // Validate that it's ok to perform this action - client must be Ready.
    if (versionBean.getStatus() != ClientStatus.Registered) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("InvalidClientStatus"));
    }
    Client client = new Client();
    client.setOrganizationId(versionBean.getClient().getOrganization().getId());
    client.setClientId(versionBean.getClient().getId());
    client.setVersion(versionBean.getVersion());
    // Each of those gateways must be told about the client.
    try {
        storage.beginTx();
        Map<String, IGatewayLink> links = new HashMap<>();
        for (ContractSummaryBean contractBean : contractBeans) {
            ApiVersionBean svb = storage.getApiVersion(contractBean.getApiOrganizationId(), contractBean.getApiId(), contractBean.getApiVersion());
            Set<ApiGatewayBean> gateways = svb.getGateways();
            if (gateways == null) {
                // $NON-NLS-1$
                throw new PublishingException("No gateways specified for API: " + svb.getApi().getName());
            }
            for (ApiGatewayBean apiGatewayBean : gateways) {
                if (!links.containsKey(apiGatewayBean.getGatewayId())) {
                    IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
                    links.put(apiGatewayBean.getGatewayId(), gatewayLink);
                }
            }
        }
        storage.commitTx();
        for (IGatewayLink gatewayLink : links.values()) {
            gatewayLink.unregisterClient(client);
            gatewayLink.close();
        }
    } catch (Exception e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
    }
    versionBean.setStatus(ClientStatus.Retired);
    versionBean.setRetiredOn(new Date());
    try {
        storage.beginTx();
        storage.updateClientVersion(versionBean);
        storage.createAuditEntry(AuditUtils.clientUnregistered(versionBean, securityContext));
        storage.commitTx();
    } catch (Exception e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
    }
    log.debug(// $NON-NLS-1$
    String.format(// $NON-NLS-1$
    "Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) HashMap(java.util.HashMap) 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) Date(java.util.Date) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) Client(io.apiman.gateway.engine.beans.Client) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Aggregations

StorageException (io.apiman.manager.api.core.exceptions.StorageException)10 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)10 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)10 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)10 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)10 PlanVersionNotFoundException (io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException)10 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)9 Date (java.util.Date)8 NewClientVersionBean (io.apiman.manager.api.beans.clients.NewClientVersionBean)5 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)5 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)5 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)5 ApiDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)5 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)5 ApiVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException)5 ClientAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException)5 ClientNotFoundException (io.apiman.manager.api.rest.exceptions.ClientNotFoundException)5 ClientVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException)5 ContractAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException)5 ContractNotFoundException (io.apiman.manager.api.rest.exceptions.ContractNotFoundException)5