Search in sources :

Example 1 with UpdateApiVersionBean

use of io.apiman.manager.api.beans.apis.UpdateApiVersionBean 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;
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) UpdatePolicyBean(io.apiman.manager.api.beans.policies.UpdatePolicyBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) GatewaySummaryBean(io.apiman.manager.api.beans.summary.GatewaySummaryBean) 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) UpdateApiBean(io.apiman.manager.api.beans.apis.UpdateApiBean) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) NewApiBean(io.apiman.manager.api.beans.apis.NewApiBean) ClientUsagePerApiBean(io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean) Response(javax.ws.rs.core.Response) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) 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)

Example 2 with UpdateApiVersionBean

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

the class ApiService method createApiVersion.

public ApiVersionBeanDto createApiVersion(String organizationId, String apiId, NewApiVersionBean newApiVersion) throws ApiNotFoundException, NotAuthorizedException, InvalidVersionException, ApiVersionAlreadyExistsException {
    FieldValidator.validateVersion(newApiVersion.getVersion());
    ApiVersionBean newVersion = tryAction(() -> {
        GatewaySummaryBean gateway = getSingularGateway();
        ApiBean api = getApiFromStorage(organizationId, apiId);
        if (storage.getApiVersion(organizationId, apiId, newApiVersion.getVersion()) != null) {
            throw ExceptionFactory.apiVersionAlreadyExistsException(apiId, newApiVersion.getVersion());
        }
        return createApiVersionInternal(newApiVersion, api, gateway);
    });
    if (newApiVersion.isClone() && newApiVersion.getCloneVersion() != null) {
        try {
            ApiVersionBean cloneSource = storage.getApiVersion(organizationId, apiId, newApiVersion.getCloneVersion());
            // storage.flush();
            // 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 (newApiVersion.getEndpoint() == null) {
                updatedApi.setEndpoint(cloneSource.getEndpoint());
            }
            if (newApiVersion.getEndpointType() == null) {
                updatedApi.setEndpointType(cloneSource.getEndpointType());
            }
            if (newApiVersion.getEndpointContentType() == null) {
                updatedApi.setEndpointContentType(cloneSource.getEndpointContentType());
            }
            updatedApi.setEndpointProperties(cloneSource.getEndpointProperties());
            updatedApi.setGateways(cloneSource.getGateways());
            if (newApiVersion.getPlans() == null) {
                Set<UpdateApiPlanDto> plans = ApiVersionMapper.INSTANCE.toDto2(cloneSource.getPlans());
                updatedApi.setPlans(plans);
            }
            if (newApiVersion.getPublicAPI() == null) {
                updatedApi.setPublicAPI(cloneSource.isPublicAPI());
            }
            if (newApiVersion.getParsePayload() == null) {
                updatedApi.setParsePayload(cloneSource.isParsePayload());
            }
            if (newApiVersion.getExtendedDescription() == null) {
                updatedApi.setExtendedDescription(cloneSource.getExtendedDescription());
            }
            ApiVersionBean updated = updateApiVersion(newVersion, updatedApi);
            if (newApiVersion.getDefinitionUrl() == null) {
                // Clone the API definition document
                InputStream definition = null;
                try {
                    definition = getApiDefinition(organizationId, apiId, newApiVersion.getCloneVersion()).getDefinition();
                    setApiDefinition(organizationId, apiId, updated.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$
                    LOGGER.error("Unable to create response", sdnfe);
                } finally {
                    IOUtils.closeQuietly(definition);
                }
            }
            // Clone all API policies
            List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, newApiVersion.getCloneVersion());
            for (PolicySummaryBean policySummary : policies) {
                PolicyBean policy = getApiPolicy(organizationId, apiId, newApiVersion.getCloneVersion(), policySummary.getId());
                NewPolicyBean npb = new NewPolicyBean();
                npb.setDefinitionId(policy.getDefinition().getId());
                npb.setConfiguration(policy.getConfiguration());
                createApiPolicy(organizationId, apiId, updated.getVersion(), npb);
            }
        } catch (Exception e) {
            e.printStackTrace();
            // TODO it's ok if the clone fails - we did our best
            if (e != null) {
                Throwable t = e;
                e = (Exception) t;
            }
        }
    }
    return apiVersionMapper.toDto(newVersion);
}
Also used : PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) InputStream(java.io.InputStream) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) UpdatePolicyBean(io.apiman.manager.api.beans.policies.UpdatePolicyBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) GatewaySummaryBean(io.apiman.manager.api.beans.summary.GatewaySummaryBean) UpdateApiPlanDto(io.apiman.manager.api.beans.apis.dto.UpdateApiPlanDto) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) MalformedURLException(java.net.MalformedURLException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) UpdateApiBean(io.apiman.manager.api.beans.apis.UpdateApiBean) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) NewApiBean(io.apiman.manager.api.beans.apis.NewApiBean) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) 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 3 with UpdateApiVersionBean

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

the class ApiService method updateApiVersion.

private ApiVersionBean updateApiVersion(ApiVersionBean avb, UpdateApiVersionBean update) throws ApiVersionNotFoundException {
    if (avb.getStatus() == ApiStatus.Published) {
        // If published and public API, then allow full modification (caveat emptor).
        if (avb.isPublicAPI()) {
            return updateApiVersionInternal(avb, update);
        } else {
            Set<UpdateApiPlanDto> updatePlans = Optional.ofNullable(update.getPlans()).orElse(Collections.emptySet());
            // TODO refactor with HTTP PATCH?
            if (updatePlans.size() != avb.getPlans().size()) {
                throw new InvalidParameterException("Can not attach or remove plans from an API Version after it has been published");
            }
            // Collect just the ID and Versions (as these must be the same). Other fields can change freely.
            Set<PlanIdVersion> existingPIV = avb.getPlans().stream().map(PlanIdVersion::new).collect(Collectors.toSet());
            Set<PlanIdVersion> updatedPIV = updatePlans.stream().map(PlanIdVersion::new).collect(Collectors.toSet());
            if (!existingPIV.equals(updatedPIV)) {
                throw new InvalidParameterException("Plan IDs and versions must not change after publication");
            }
            // If published, then mapper copies across only fields that are safe for change after publication.
            UpdateApiVersionBean afterPublishUpdate = ApiVersionMapper.INSTANCE.toPublishedUpdateBean(update);
            return updateApiVersionInternal(avb, afterPublishUpdate);
        }
    } else {
        return updateApiVersionInternal(avb, update);
    }
}
Also used : InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) UpdateApiPlanDto(io.apiman.manager.api.beans.apis.dto.UpdateApiPlanDto)

Example 4 with UpdateApiVersionBean

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

the class ApiService method updateApiVersionInternal.

private ApiVersionBean updateApiVersionInternal(ApiVersionBean avb, UpdateApiVersionBean update) throws ApiVersionNotFoundException {
    if (avb.getStatus() == ApiStatus.Retired) {
        throw ExceptionFactory.invalidApiStatusException();
    }
    avb.setModifiedBy(securityContext.getCurrentUser());
    avb.setModifiedOn(new Date());
    EntityUpdatedData data = new EntityUpdatedData();
    if (AuditUtils.valueChanged(avb.getPlans(), update.getPlans())) {
        Set<ApiPlanBean> updateEntities = update.getPlans().stream().map(dto -> ApiPlanMapper.INSTANCE.fromDto(dto, avb)).collect(Collectors.toSet());
        // $NON-NLS-1$
        data.addChange("plans", AuditUtils.asString_ApiPlanBeans(avb.getPlans()), AuditUtils.asString_ApiPlanBeans(updateEntities));
        if (update.getPlans() != null) {
            // Work around: https://hibernate.atlassian.net/browse/HHH-3799
            // Step 1: Set intersection
            Set<UpdateApiPlanDto> existingAsDto = ApiPlanMapper.INSTANCE.toDto(avb.getPlans());
            existingAsDto.retainAll(update.getPlans());
            // Step 2: Upsert
            Set<ApiPlanBean> mergedPlans = new HashSet<>();
            for (ApiPlanBean updateApb : updateEntities) {
                existingAsDto.stream().map(e -> ApiPlanMapper.INSTANCE.fromDto(e, avb)).filter(e -> e.equals(updateApb)).findAny().ifPresentOrElse(// Merge (existing element to be updated)
                ep -> {
                    ApiPlanMapper.INSTANCE.merge(updateApb, ep);
                    mergedPlans.add(ep);
                }, // Insert (new element)
                () -> {
                    mergedPlans.add(updateApb);
                });
            }
            avb.setPlans(mergedPlans);
            tryAction(() -> storage.merge(avb));
        }
    }
    if (AuditUtils.valueChanged(avb.getGateways(), update.getGateways())) {
        // $NON-NLS-1$
        data.addChange("gateways", AuditUtils.asString_ApiGatewayBeans(avb.getGateways()), AuditUtils.asString_ApiGatewayBeans(update.getGateways()));
        if (avb.getGateways() == null) {
            avb.setGateways(new HashSet<>());
        }
        avb.getGateways().clear();
        avb.getGateways().addAll(update.getGateways());
    }
    if (AuditUtils.valueChanged(avb.getEndpoint(), update.getEndpoint())) {
        // validate the endpoint is a URL
        validateEndpoint(update.getEndpoint());
        // $NON-NLS-1$
        data.addChange("endpoint", avb.getEndpoint(), update.getEndpoint());
        avb.setEndpoint(update.getEndpoint());
    }
    if (AuditUtils.valueChanged(avb.getEndpointType(), update.getEndpointType())) {
        // $NON-NLS-1$
        data.addChange("endpointType", avb.getEndpointType(), update.getEndpointType());
        avb.setEndpointType(update.getEndpointType());
    }
    if (AuditUtils.valueChanged(avb.getEndpointContentType(), update.getEndpointContentType())) {
        // $NON-NLS-1$
        data.addChange("endpointContentType", avb.getEndpointContentType(), update.getEndpointContentType());
        avb.setEndpointContentType(update.getEndpointContentType());
    }
    if (AuditUtils.valueChanged(avb.getEndpointProperties(), update.getEndpointProperties())) {
        if (avb.getEndpointProperties() == null) {
            avb.setEndpointProperties(new HashMap<>());
        } else {
            avb.getEndpointProperties().clear();
        }
        if (update.getEndpointProperties() != null) {
            avb.getEndpointProperties().putAll(update.getEndpointProperties());
        }
    }
    if (AuditUtils.valueChanged(avb.isPublicAPI(), update.getPublicAPI())) {
        // $NON-NLS-1$
        data.addChange("publicAPI", String.valueOf(avb.isPublicAPI()), String.valueOf(update.getPublicAPI()));
        avb.setPublicAPI(update.getPublicAPI());
    }
    if (AuditUtils.valueChanged(avb.isParsePayload(), update.getParsePayload())) {
        // $NON-NLS-1$
        data.addChange("parsePayload", String.valueOf(avb.isParsePayload()), String.valueOf(update.getParsePayload()));
        avb.setParsePayload(update.getParsePayload());
    }
    if (AuditUtils.valueChanged(avb.getDisableKeysStrip(), update.getDisableKeysStrip())) {
        // $NON-NLS-1$
        data.addChange("disableKeysStrip", String.valueOf(avb.getDisableKeysStrip()), String.valueOf(update.getDisableKeysStrip()));
        avb.setDisableKeysStrip(update.getDisableKeysStrip());
    }
    if (AuditUtils.valueChanged(avb.getExtendedDescription(), update.getExtendedDescription())) {
        // $NON-NLS-1$
        data.addChange("extendedDescription", String.valueOf(avb.getExtendedDescription()), String.valueOf(update.getExtendedDescription()));
        avb.setExtendedDescription(update.getExtendedDescription());
    }
    if (AuditUtils.valueChanged(avb.getDiscoverability(), update.getPublicDiscoverability())) {
        // $NON-NLS-1$
        data.addChange("discoverability", String.valueOf(avb.getDiscoverability()), String.valueOf(update.getPublicDiscoverability()));
        avb.setDiscoverability(update.getPublicDiscoverability());
    }
    return tryAction(() -> {
        if (avb.getGateways() == null || avb.getGateways().isEmpty()) {
            GatewaySummaryBean gateway = getSingularGateway();
            if (gateway != null && avb.getGateways() == null) {
                avb.setGateways(new HashSet<>());
                ApiGatewayBean sgb = new ApiGatewayBean();
                sgb.setGatewayId(gateway.getId());
                avb.getGateways().add(sgb);
            }
        }
        if (avb.getStatus() != ApiStatus.Published) {
            if (apiValidator.isReady(avb)) {
                avb.setStatus(ApiStatus.Ready);
            } else {
                avb.setStatus(ApiStatus.Created);
            }
        } else {
            if (!apiValidator.isReady(avb)) {
                throw ExceptionFactory.invalidApiStatusException();
            }
        }
        encryptEndpointProperties(avb);
        // Ensure all the plans are in the right status (locked)
        Set<ApiPlanBean> plans = avb.getPlans();
        if (plans != null) {
            for (ApiPlanBean splanBean : plans) {
                String orgId = avb.getApi().getOrganization().getId();
                PlanVersionBean pvb = storage.getPlanVersion(orgId, splanBean.getPlanId(), splanBean.getVersion());
                if (pvb == null) {
                    // $NON-NLS-1$
                    throw new StorageException(Messages.i18n.format("PlanVersionDoesNotExist", splanBean.getPlanId(), splanBean.getVersion()));
                }
                if (pvb.getStatus() != PlanStatus.Locked) {
                    // $NON-NLS-1$
                    throw new StorageException(Messages.i18n.format("PlanNotLocked", splanBean.getPlanId(), splanBean.getVersion()));
                }
            }
        }
        storage.updateApiVersion(avb);
        storage.createAuditEntry(AuditUtils.apiVersionUpdated(avb, data, securityContext));
        // $NON-NLS-1$
        LOGGER.debug(String.format("Successfully updated API Version: %s", avb));
        decryptEndpointProperties(avb);
        return avb;
    });
}
Also used : PolicyChainBean(io.apiman.manager.api.beans.policies.PolicyChainBean) ApiPlanSummaryBean(io.apiman.manager.api.beans.summary.ApiPlanSummaryBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) UpdateApiBean(io.apiman.manager.api.beans.apis.UpdateApiBean) ISecurityContext(io.apiman.manager.api.security.ISecurityContext) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) IDataEncrypter(io.apiman.common.util.crypt.IDataEncrypter) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) ApiVersionEndpointSummaryBean(io.apiman.manager.api.beans.summary.ApiVersionEndpointSummaryBean) ApiPlanMapper(io.apiman.manager.api.beans.apis.dto.ApiPlanMapper) PagingBean(io.apiman.manager.api.beans.search.PagingBean) Map(java.util.Map) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) NewApiDefinitionBean(io.apiman.manager.api.beans.apis.NewApiDefinitionBean) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) Transactional(javax.transaction.Transactional) IApiValidator(io.apiman.manager.api.core.IApiValidator) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) ApiVersionStatusBean(io.apiman.manager.api.beans.apis.ApiVersionStatusBean) Set(java.util.Set) KeyValueTagMapper(io.apiman.manager.api.beans.apis.dto.KeyValueTagMapper) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) IOUtils(org.apache.commons.io.IOUtils) AuditEntryBean(io.apiman.manager.api.beans.audit.AuditEntryBean) SchemaRewriterService(io.apiman.manager.api.schema.SchemaRewriterService) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) ApiStatus(io.apiman.manager.api.beans.apis.ApiStatus) ArrayList(java.util.ArrayList) DataEncryptionContext(io.apiman.common.util.crypt.DataEncryptionContext) IStorageQuery(io.apiman.manager.api.core.IStorageQuery) StreamSupport(java.util.stream.StreamSupport) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) DataAccessUtilMixin(io.apiman.manager.api.rest.impl.util.DataAccessUtilMixin) KeyValueTag(io.apiman.manager.api.beans.apis.KeyValueTag) ApiVersionBeanDto(io.apiman.manager.api.beans.apis.dto.ApiVersionBeanDto) EntityUpdatedData(io.apiman.manager.api.beans.audit.data.EntityUpdatedData) ApiMapper(io.apiman.manager.api.beans.summary.mappers.ApiMapper) BeanUtils(io.apiman.manager.api.beans.BeanUtils) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) PolicyType(io.apiman.manager.api.beans.policies.PolicyType) Messages(io.apiman.manager.api.rest.exceptions.i18n.Messages) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ExceptionFactory(io.apiman.manager.api.rest.exceptions.util.ExceptionFactory) ApiBeanDto(io.apiman.manager.api.beans.apis.dto.ApiBeanDto) NewApiBean(io.apiman.manager.api.beans.apis.NewApiBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) URL(java.net.URL) Date(java.util.Date) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) IGatewayLinkFactory(io.apiman.manager.api.gateway.IGatewayLinkFactory) ApiDefinitionType(io.apiman.manager.api.beans.apis.ApiDefinitionType) ApimanLoggerFactory(io.apiman.common.logging.ApimanLoggerFactory) UpdateApiPlanDto(io.apiman.manager.api.beans.apis.dto.UpdateApiPlanDto) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) KeyValueTagDto(io.apiman.manager.api.beans.apis.dto.KeyValueTagDto) GatewaySummaryBean(io.apiman.manager.api.beans.summary.GatewaySummaryBean) Collectors(java.util.stream.Collectors) ApiVersionSummaryBean(io.apiman.manager.api.beans.summary.ApiVersionSummaryBean) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) Objects(java.util.Objects) ApiSummaryBean(io.apiman.manager.api.beans.summary.ApiSummaryBean) List(java.util.List) IApimanLogger(io.apiman.common.logging.IApimanLogger) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) Entry(java.util.Map.Entry) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) NotNull(org.jetbrains.annotations.NotNull) PermissionType(io.apiman.manager.api.beans.idm.PermissionType) IBlobStore(io.apiman.manager.api.core.IBlobStore) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) HashMap(java.util.HashMap) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) AuditUtils(io.apiman.manager.api.rest.impl.audit.AuditUtils) HashSet(java.util.HashSet) Inject(javax.inject.Inject) FieldValidator(io.apiman.manager.api.rest.impl.util.FieldValidator) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) Iterator(java.util.Iterator) EntityType(io.apiman.common.util.crypt.DataEncryptionContext.EntityType) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) MalformedURLException(java.net.MalformedURLException) SearchResultsBean(io.apiman.manager.api.beans.search.SearchResultsBean) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) ApiVersionMapper(io.apiman.manager.api.beans.apis.dto.ApiVersionMapper) IStorage(io.apiman.manager.api.core.IStorage) NewApiVersionBean(io.apiman.manager.api.beans.apis.NewApiVersionBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) Collectors.toList(java.util.stream.Collectors.toList) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) UpdatePolicyBean(io.apiman.manager.api.beans.policies.UpdatePolicyBean) PlanStatus(io.apiman.manager.api.beans.plans.PlanStatus) Collections(java.util.Collections) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) RestHelper(io.apiman.manager.api.rest.impl.util.RestHelper) InputStream(java.io.InputStream) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) GatewaySummaryBean(io.apiman.manager.api.beans.summary.GatewaySummaryBean) UpdateApiPlanDto(io.apiman.manager.api.beans.apis.dto.UpdateApiPlanDto) Date(java.util.Date) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException) EntityUpdatedData(io.apiman.manager.api.beans.audit.data.EntityUpdatedData) HashSet(java.util.HashSet)

Example 5 with UpdateApiVersionBean

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

the class OrganizationResourceImpl method updateApiVersion.

/**
 * @see IOrganizationResource#updateApiVersion(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.apis.UpdateApiVersionBean)
 */
@Override
public ApiVersionBean updateApiVersion(String organizationId, String apiId, String version, UpdateApiVersionBean bean) throws ApiVersionNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.apiEdit, organizationId);
    ApiVersionBean avb = getApiVersion(organizationId, apiId, version);
    if (avb.isPublicAPI()) {
        if (avb.getStatus() == ApiStatus.Retired) {
            throw ExceptionFactory.invalidApiStatusException();
        }
    } else {
        if (avb.getStatus() == ApiStatus.Published || avb.getStatus() == ApiStatus.Retired) {
            throw ExceptionFactory.invalidApiStatusException();
        }
    }
    avb.setModifiedBy(securityContext.getCurrentUser());
    avb.setModifiedOn(new Date());
    EntityUpdatedData data = new EntityUpdatedData();
    if (AuditUtils.valueChanged(avb.getPlans(), bean.getPlans())) {
        // $NON-NLS-1$
        data.addChange("plans", AuditUtils.asString_ApiPlanBeans(avb.getPlans()), AuditUtils.asString_ApiPlanBeans(bean.getPlans()));
        if (avb.getPlans() == null) {
            avb.setPlans(new HashSet<>());
        }
        avb.getPlans().clear();
        if (bean.getPlans() != null) {
            avb.getPlans().addAll(bean.getPlans());
        }
    }
    if (AuditUtils.valueChanged(avb.getGateways(), bean.getGateways())) {
        // $NON-NLS-1$
        data.addChange("gateways", AuditUtils.asString_ApiGatewayBeans(avb.getGateways()), AuditUtils.asString_ApiGatewayBeans(bean.getGateways()));
        if (avb.getGateways() == null) {
            avb.setGateways(new HashSet<>());
        }
        avb.getGateways().clear();
        avb.getGateways().addAll(bean.getGateways());
    }
    if (AuditUtils.valueChanged(avb.getEndpoint(), bean.getEndpoint())) {
        // validate the endpoint is a URL
        validateEndpoint(bean.getEndpoint());
        // $NON-NLS-1$
        data.addChange("endpoint", avb.getEndpoint(), bean.getEndpoint());
        avb.setEndpoint(bean.getEndpoint());
    }
    if (AuditUtils.valueChanged(avb.getEndpointType(), bean.getEndpointType())) {
        // $NON-NLS-1$
        data.addChange("endpointType", avb.getEndpointType(), bean.getEndpointType());
        avb.setEndpointType(bean.getEndpointType());
    }
    if (AuditUtils.valueChanged(avb.getEndpointContentType(), bean.getEndpointContentType())) {
        // $NON-NLS-1$
        data.addChange("endpointContentType", avb.getEndpointContentType(), bean.getEndpointContentType());
        avb.setEndpointContentType(bean.getEndpointContentType());
    }
    if (AuditUtils.valueChanged(avb.getEndpointProperties(), bean.getEndpointProperties())) {
        if (avb.getEndpointProperties() == null) {
            avb.setEndpointProperties(new HashMap<>());
        } else {
            avb.getEndpointProperties().clear();
        }
        if (bean.getEndpointProperties() != null) {
            avb.getEndpointProperties().putAll(bean.getEndpointProperties());
        }
    }
    if (AuditUtils.valueChanged(avb.isPublicAPI(), bean.getPublicAPI())) {
        // $NON-NLS-1$
        data.addChange("publicAPI", String.valueOf(avb.isPublicAPI()), String.valueOf(bean.getPublicAPI()));
        avb.setPublicAPI(bean.getPublicAPI());
    }
    if (AuditUtils.valueChanged(avb.isParsePayload(), bean.getParsePayload())) {
        // $NON-NLS-1$
        data.addChange("parsePayload", String.valueOf(avb.isParsePayload()), String.valueOf(bean.getParsePayload()));
        avb.setParsePayload(bean.getParsePayload());
    }
    if (AuditUtils.valueChanged(avb.getDisableKeysStrip(), bean.getDisableKeysStrip())) {
        // $NON-NLS-1$
        data.addChange("disableKeysStrip", String.valueOf(avb.getDisableKeysStrip()), String.valueOf(bean.getDisableKeysStrip()));
        avb.setDisableKeysStrip(bean.getDisableKeysStrip());
    }
    try {
        if (avb.getGateways() == null || avb.getGateways().isEmpty()) {
            GatewaySummaryBean gateway = getSingularGateway();
            if (gateway != null && avb.getGateways() == null) {
                avb.setGateways(new HashSet<>());
                ApiGatewayBean sgb = new ApiGatewayBean();
                sgb.setGatewayId(gateway.getId());
                avb.getGateways().add(sgb);
            }
        }
        if (avb.getStatus() != ApiStatus.Published) {
            if (apiValidator.isReady(avb)) {
                avb.setStatus(ApiStatus.Ready);
            } else {
                avb.setStatus(ApiStatus.Created);
            }
        } else {
            if (!apiValidator.isReady(avb)) {
                throw ExceptionFactory.invalidApiStatusException();
            }
        }
    } catch (Exception e) {
        throw new SystemErrorException(e);
    }
    try {
        encryptEndpointProperties(avb);
        storage.beginTx();
        // Ensure all of the plans are in the right status (locked)
        Set<ApiPlanBean> plans = avb.getPlans();
        if (plans != null) {
            for (ApiPlanBean splanBean : plans) {
                String orgId = avb.getApi().getOrganization().getId();
                PlanVersionBean pvb = storage.getPlanVersion(orgId, splanBean.getPlanId(), splanBean.getVersion());
                if (pvb == null) {
                    // $NON-NLS-1$
                    throw new StorageException(Messages.i18n.format("PlanVersionDoesNotExist", splanBean.getPlanId(), splanBean.getVersion()));
                }
                if (pvb.getStatus() != PlanStatus.Locked) {
                    // $NON-NLS-1$
                    throw new StorageException(Messages.i18n.format("PlanNotLocked", splanBean.getPlanId(), splanBean.getVersion()));
                }
            }
        }
        storage.updateApiVersion(avb);
        storage.createAuditEntry(AuditUtils.apiVersionUpdated(avb, data, securityContext));
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Successfully updated API Version: %s", avb));
        decryptEndpointProperties(avb);
        return avb;
    } 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) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) GatewaySummaryBean(io.apiman.manager.api.beans.summary.GatewaySummaryBean) 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) NewPlanVersionBean(io.apiman.manager.api.beans.plans.NewPlanVersionBean) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) 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) StorageException(io.apiman.manager.api.core.exceptions.StorageException) EntityUpdatedData(io.apiman.manager.api.beans.audit.data.EntityUpdatedData)

Aggregations

UpdateApiVersionBean (io.apiman.manager.api.beans.apis.UpdateApiVersionBean)5 InvalidParameterException (io.apiman.manager.api.rest.exceptions.InvalidParameterException)5 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)4 NewApiVersionBean (io.apiman.manager.api.beans.apis.NewApiVersionBean)4 GatewaySummaryBean (io.apiman.manager.api.beans.summary.GatewaySummaryBean)4 StorageException (io.apiman.manager.api.core.exceptions.StorageException)4 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)4 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)4 ApiDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)4 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)4 ApiVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException)4 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)4 EntityStillActiveException (io.apiman.manager.api.rest.exceptions.EntityStillActiveException)4 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)4 InvalidApiStatusException (io.apiman.manager.api.rest.exceptions.InvalidApiStatusException)4 InvalidNameException (io.apiman.manager.api.rest.exceptions.InvalidNameException)4 InvalidVersionException (io.apiman.manager.api.rest.exceptions.InvalidVersionException)4 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)4 OrganizationNotFoundException (io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException)4 PlanNotFoundException (io.apiman.manager.api.rest.exceptions.PlanNotFoundException)4