Search in sources :

Example 31 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class APIProviderImpl method attachOperationPoliciesToAPI.

private void attachOperationPoliciesToAPI(API newAPI, Map<String, String> clonedOperationPolicyMap, Map<String, List<OperationPolicy>> operationPoliciesMap) throws APIManagementException {
    operationPoliciesMap.forEach((key, operationPolicies) -> operationPolicies.forEach(operationPolicy -> operationPolicy.setPolicyId(clonedOperationPolicyMap.get(operationPolicy.getPolicyId()))));
    Set<URITemplate> uriTemplates = newAPI.getUriTemplates();
    if (uriTemplates != null) {
        for (URITemplate uriTemplate : uriTemplates) {
            List<OperationPolicy> operationPolicies = operationPoliciesMap.get(uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate());
            uriTemplate.setOperationPolicies(operationPolicies);
        }
        apiMgtDAO.addOperationPolicyMapping(uriTemplates);
    }
}
Also used : APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) Enumeration(java.util.Enumeration) DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) StringUtils(org.apache.commons.lang3.StringUtils) ApiKeyGenerator(org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) AuthorizationManager(org.wso2.carbon.user.api.AuthorizationManager) DocumentationType(org.wso2.carbon.apimgt.api.model.DocumentationType) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) WorkflowConstants(org.wso2.carbon.apimgt.impl.workflow.WorkflowConstants) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) MultitenantUtils(org.wso2.carbon.utils.multitenancy.MultitenantUtils) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) Comment(org.wso2.carbon.apimgt.api.model.Comment) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent) ScopeEvent(org.wso2.carbon.apimgt.impl.notifier.events.ScopeEvent) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) Usage(org.wso2.carbon.apimgt.api.model.Usage) DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) StringTokenizer(java.util.StringTokenizer) Resource(org.wso2.carbon.registry.core.Resource) ExportFormat(org.wso2.carbon.apimgt.impl.importexport.ExportFormat) KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader) PublisherAPISearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherAPISearchResult) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) RecommenderDetailsExtractor(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderDetailsExtractor) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) TreeMap(java.util.TreeMap) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) CommonUtil(org.wso2.carbon.governance.lcm.util.CommonUtil) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) SortedSet(java.util.SortedSet) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) LifeCycleEvent(org.wso2.carbon.apimgt.api.model.LifeCycleEvent) OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) ByteArrayInputStream(java.io.ByteArrayInputStream) Gson(com.google.gson.Gson) Event(org.wso2.carbon.databridge.commons.Event) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) NotifierConstants(org.wso2.carbon.apimgt.impl.notification.NotifierConstants) DefaultMonetizationImpl(org.wso2.carbon.apimgt.impl.monetization.DefaultMonetizationImpl) JwtTokenInfoDTO(org.wso2.carbon.apimgt.impl.dto.JwtTokenInfoDTO) AXIOMUtil(org.apache.axiom.om.util.AXIOMUtil) LCManagerFactory(org.wso2.carbon.apimgt.persistence.LCManagerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServiceCatalogDAO(org.wso2.carbon.apimgt.impl.dao.ServiceCatalogDAO) UUID(java.util.UUID) DocumentSourceType(org.wso2.carbon.apimgt.api.model.Documentation.DocumentSourceType) Collectors(java.util.stream.Collectors) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) SubscribedApiDTO(org.wso2.carbon.apimgt.impl.dto.SubscribedApiDTO) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DocumentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult) HashSet(java.util.HashSet) CertificateEvent(org.wso2.carbon.apimgt.impl.notifier.events.CertificateEvent) API(org.wso2.carbon.apimgt.api.model.API) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) SearchContent(org.wso2.carbon.apimgt.persistence.dto.SearchContent) JavaUtils(org.apache.axis2.util.JavaUtils) UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) MapUtils(org.apache.commons.collections.MapUtils) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Caching(javax.cache.Caching) APIVersionComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionComparator) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) GatewayArtifactsMgtDAO(org.wso2.carbon.apimgt.impl.dao.GatewayArtifactsMgtDAO) RecommendationEnvironment(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment) ArtifactSaver(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) CarbonContext(org.wso2.carbon.context.CarbonContext) LifecycleBeanPopulator(org.wso2.carbon.governance.custom.lifecycles.checklist.util.LifecycleBeanPopulator) RegistryAuthorizationManager(org.wso2.carbon.registry.core.jdbc.realm.RegistryAuthorizationManager) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) DocumentMapper(org.wso2.carbon.apimgt.persistence.mapper.DocumentMapper) Arrays(java.util.Arrays) ResponseCode(org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode) OAS3Parser(org.wso2.carbon.apimgt.impl.definitions.OAS3Parser) WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) PaginationContext(org.wso2.carbon.registry.core.pagination.PaginationContext) LifecycleBean(org.wso2.carbon.governance.custom.lifecycles.checklist.beans.LifecycleBean) APIVersionStringComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionStringComparator) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) APIAuthenticationAdminClient(org.wso2.carbon.apimgt.impl.utils.APIAuthenticationAdminClient) Matcher(java.util.regex.Matcher) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) EventPublisherType(org.wso2.carbon.apimgt.eventing.EventPublisherType) Cache(javax.cache.Cache) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) Environment(org.wso2.carbon.apimgt.api.model.Environment) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) Set(java.util.Set) IOUtils(org.apache.commons.io.IOUtils) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) WSO2APIPublisher(org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher) APIMapper(org.wso2.carbon.apimgt.persistence.mapper.APIMapper) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) LogFactory(org.apache.commons.logging.LogFactory) QName(javax.xml.namespace.QName) CarbonConstants(org.wso2.carbon.CarbonConstants) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIStore(org.wso2.carbon.apimgt.api.model.APIStore) WorkflowResponse(org.wso2.carbon.apimgt.api.WorkflowResponse) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) DocumentContent(org.wso2.carbon.apimgt.persistence.dto.DocumentContent) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) SubscriptionEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent) Association(org.wso2.carbon.registry.core.Association) EndpointSecurity(org.wso2.carbon.apimgt.api.model.EndpointSecurity) APIStoreNameComparator(org.wso2.carbon.apimgt.impl.utils.APIStoreNameComparator) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) RegistryConstants(org.wso2.carbon.registry.core.RegistryConstants) EnvironmentPropertiesDTO(org.wso2.carbon.apimgt.api.dto.EnvironmentPropertiesDTO) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties) Monetization(org.wso2.carbon.apimgt.api.model.Monetization) PublisherContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherContentSearchResult) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIStateWorkflowDTO(org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO) LinkedHashSet(java.util.LinkedHashSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) Properties(java.util.Properties) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) StringWriter(java.io.StringWriter) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) EventPublisherEvent(org.wso2.carbon.apimgt.eventing.EventPublisherEvent) RecommenderEventPublisher(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderEventPublisher) File(java.io.File) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) KeyManagerHolder(org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ErrorItem(org.wso2.carbon.apimgt.api.ErrorItem) RegistryContext(org.wso2.carbon.registry.core.config.RegistryContext) MultitenantConstants(org.wso2.carbon.utils.multitenancy.MultitenantConstants) APIProductMapper(org.wso2.carbon.apimgt.persistence.mapper.APIProductMapper) RegistryUtils(org.wso2.carbon.registry.core.utils.RegistryUtils) URL(java.net.URL) Constants(org.apache.axis2.Constants) PolicyConstants(org.wso2.carbon.apimgt.api.model.policy.PolicyConstants) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) PublisherAPIInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIInfo) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) MediationInfo(org.wso2.carbon.apimgt.persistence.dto.MediationInfo) GovernanceUtils(org.wso2.carbon.governance.api.util.GovernanceUtils) NotificationExecutor(org.wso2.carbon.apimgt.impl.notification.NotificationExecutor) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) ActionConstants(org.wso2.carbon.registry.core.ActionConstants) JSONParser(org.json.simple.parser.JSONParser) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) CertificateInformationDTO(org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) PublisherAPIProductSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProductSearchResult) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) List(java.util.List) JSONObject(org.json.simple.JSONObject) DocumentVisibility(org.wso2.carbon.apimgt.api.model.Documentation.DocumentVisibility) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) Pattern(java.util.regex.Pattern) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) PublisherAPIProductInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProductInfo) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) Provider(org.wso2.carbon.apimgt.api.model.Provider) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) HashMap(java.util.HashMap) ResourcePath(org.wso2.carbon.apimgt.api.model.ResourcePath) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) ParseException(org.json.simple.parser.ParseException) InternalAPIKeyGenerator(org.wso2.carbon.apimgt.impl.token.InternalAPIKeyGenerator) Property(org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) NotificationDTO(org.wso2.carbon.apimgt.impl.notification.NotificationDTO) OMElement(org.apache.axiom.om.OMElement) Tier(org.wso2.carbon.apimgt.api.model.Tier) APIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIProductNameComparator) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) CertificateManagerImpl(org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManagerImpl) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) OMException(org.apache.axiom.om.OMException) Registry(org.wso2.carbon.registry.core.Registry) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) CheckListItem(org.wso2.carbon.governance.custom.lifecycles.checklist.util.CheckListItem) SharedScopeUsage(org.wso2.carbon.apimgt.api.model.SharedScopeUsage) WorkflowStatus(org.wso2.carbon.apimgt.impl.workflow.WorkflowStatus) OASParserUtil(org.wso2.carbon.apimgt.impl.definitions.OASParserUtil) Log(org.apache.commons.logging.Log) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) CertificateManager(org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager) Collections(java.util.Collections) StringEscapeUtils(org.apache.commons.lang.StringEscapeUtils) InputStream(java.io.InputStream) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 32 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class APIProviderImpl method validateOperationPolicyParameters.

private void validateOperationPolicyParameters(API api, String tenantDomain) throws APIManagementException {
    boolean isOperationPoliciesAllowedForAPIType = true;
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    if (APIConstants.API_TYPE_WS.equals(api.getType()) || APIConstants.API_TYPE_SSE.equals(api.getType()) || APIConstants.API_TYPE_WEBSUB.equals(api.getType())) {
        if (log.isDebugEnabled()) {
            log.debug("Operation policies are not allowed for " + api.getType() + " APIs");
        }
        isOperationPoliciesAllowedForAPIType = false;
    }
    Map<String, Integer> policyOccurrenceMap = new HashMap<>();
    for (URITemplate uriTemplate : uriTemplates) {
        List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
        List<OperationPolicy> validatedPolicies = new ArrayList<>();
        if (operationPolicies != null && !operationPolicies.isEmpty() && isOperationPoliciesAllowedForAPIType) {
            for (OperationPolicy policy : operationPolicies) {
                String policyId = policy.getPolicyId();
                // First check the API specific operation policy list
                OperationPolicyData policyData = getAPISpecificOperationPolicyByPolicyId(policyId, api.getUuid(), tenantDomain, false);
                if (policyData != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("A policy is found for " + policyId + " as " + policyData.getSpecification().getName() + ". Validating the policy");
                    }
                    if (policyData.isRevision()) {
                        throw new APIManagementException("Invalid policy selected. " + policyId + " policy is not found.", ExceptionCodes.INVALID_OPERATION_POLICY);
                    }
                    OperationPolicySpecification policySpecification = policyData.getSpecification();
                    if (validateAppliedPolicyWithSpecification(policySpecification, policy, api)) {
                        String policyOccurrenceKey = uriTemplate.getUriTemplate() + "_" + policy.getDirection() + "_" + policy.getPolicyName();
                        int previousOccurrenceCount = 0;
                        if (policyOccurrenceMap.get(policyOccurrenceKey) != null) {
                            previousOccurrenceCount = policyOccurrenceMap.get(policyOccurrenceKey);
                            if (previousOccurrenceCount > 0 && !policySpecification.isMultipleAllowed()) {
                                throw new APIManagementException("Policy multiple allowed property violated. " + policySpecification.getDisplayName() + " cannot be applied multiple times.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
                            }
                        }
                        policyOccurrenceMap.put(policyOccurrenceKey, previousOccurrenceCount + 1);
                        validatedPolicies.add(policy);
                    }
                } else {
                    // TODO: get policy based on the name
                    OperationPolicyData commonPolicyData = getCommonOperationPolicyByPolicyId(policyId, tenantDomain, false);
                    if (commonPolicyData != null) {
                        // attributes and added to API policy list
                        if (log.isDebugEnabled()) {
                            log.debug("A common policy is found for " + policyId + ". Validating the policy");
                        }
                        OperationPolicySpecification commonPolicySpec = commonPolicyData.getSpecification();
                        if (validateAppliedPolicyWithSpecification(commonPolicySpec, policy, api)) {
                            String policyOccurrenceKey = uriTemplate.getUriTemplate() + "_" + policy.getDirection() + "_" + policy.getPolicyName();
                            int previousOccurrenceCount = 0;
                            if (policyOccurrenceMap.get(policyOccurrenceKey) != null) {
                                previousOccurrenceCount = policyOccurrenceMap.get(policyOccurrenceKey);
                                if (previousOccurrenceCount > 0 && !commonPolicySpec.isMultipleAllowed()) {
                                    throw new APIManagementException("Policy multiple allowed property violated. " + commonPolicySpec.getDisplayName() + " cannot be applied multiple times.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
                                }
                            }
                            policyOccurrenceMap.put(policyOccurrenceKey, previousOccurrenceCount + 1);
                            validatedPolicies.add(policy);
                        }
                    } else {
                        throw new APIManagementException("Selected policy " + policyId + " is not found.", ExceptionCodes.INVALID_OPERATION_POLICY);
                    }
                }
            }
        }
        uriTemplate.setOperationPolicies(validatedPolicies);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 33 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class APIProviderImpl method extractAndDropOperationPoliciesFromURITemplate.

private static Map<String, List<OperationPolicy>> extractAndDropOperationPoliciesFromURITemplate(Set<URITemplate> uriTemplates) {
    Map<String, List<OperationPolicy>> operationPoliciesMap = new HashMap<>();
    for (URITemplate uriTemplate : uriTemplates) {
        String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
        List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
        if (!operationPolicies.isEmpty()) {
            operationPoliciesMap.put(key, operationPolicies);
        }
        uriTemplate.setOperationPolicies(null);
    }
    return operationPoliciesMap;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)28 ArrayList (java.util.ArrayList)21 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)20 HashMap (java.util.HashMap)16 PreparedStatement (java.sql.PreparedStatement)13 ResultSet (java.sql.ResultSet)12 Connection (java.sql.Connection)11 Gson (com.google.gson.Gson)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 SQLException (java.sql.SQLException)8 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)7 Scope (org.wso2.carbon.apimgt.api.model.Scope)7 HashSet (java.util.HashSet)6 LinkedHashSet (java.util.LinkedHashSet)6 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5