Skip to content

Commit

Permalink
Add: Support to operation and API Policies dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Jan 4, 2024
1 parent d3305b7 commit d9138d3
Show file tree
Hide file tree
Showing 17 changed files with 329 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public boolean equals(Object o) {

@Override
public int hashCode() {

return Objects.hash(policyName, policyVersion, direction, parameters, policyId);
}

Expand All @@ -124,6 +123,7 @@ public String toString() {
", direction ='" + direction + '\'' +
", order ='" + order + '\'' +
", policyId ='" + policyId + '\'' +
", parameters ='" + parameters + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

package org.wso2.carbon.apimgt.api.model.subscription;

import org.wso2.carbon.apimgt.api.model.OperationPolicy;
import org.wso2.carbon.apimgt.api.model.policy.APIPolicy;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Entity for keeping API related information.
Expand All @@ -39,6 +44,7 @@ public class API implements CacheableEntity<String> {
private String status;
private String revision;
private String organization;
private Set<OperationPolicy> apiPolicies = new HashSet<>();

public String getRevision() {

Expand Down Expand Up @@ -199,4 +205,12 @@ public String getOrganization() {
public void setOrganization(String organization) {
this.organization = organization;
}

public void setApiPolicy(OperationPolicy apiPolicy) {
this.apiPolicies.add(apiPolicy);
}

public Set<OperationPolicy> getApiPolicies() {
return apiPolicies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class API {
private String apiCreator;
private String apiCreatorTenantDomain;
private List<URITemplate> uriTemplates = new ArrayList<>();
private List<OperationPolicy> apiPolicies = new ArrayList<>();

public String getApiId() {
return apiId;
Expand Down Expand Up @@ -87,4 +88,12 @@ public List<URITemplate> getUriTemplates() {
public void setUriTemplates(List<URITemplate> uriTemplates) {
this.uriTemplates = uriTemplates;
}

public void setApiPolicies(List<OperationPolicy> apiPolicies) {
this.apiPolicies = apiPolicies;
}

public List<OperationPolicy> getApiPolicies() {
return apiPolicies;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.common.analytics.publishers.dto;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS;
import static org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants.UNKNOWN_VALUE;
Expand Down Expand Up @@ -172,16 +170,20 @@ public API getApi() throws DataNotFoundException {
api.setApiCreatorTenantDomain(MultitenantUtils.getTenantDomain(api.getApiCreator()));
List<URITemplate> uriTemplates = new ArrayList<>();
for (URLMapping uriTemplate : apiObj.getUrlMappings()) {
org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate uriTemplateObj = new org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate();
org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate uriTemplateObj
= new org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate();
if (uriTemplate.getHttpMethod() != null && uriTemplate.getHttpMethod()
.equals(messageContext.getProperty("api.ut.HTTP_METHOD")) && uriTemplate.getUrlPattern() != null
&& uriTemplate.getUrlPattern().equals(messageContext.getProperty("api.ut.resource"))) {
.equals(messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD))
&& uriTemplate.getUrlPattern() != null && uriTemplate.getUrlPattern()
.equals(messageContext.getProperty("API_ELECTED_RESOURCE"))) {
uriTemplateObj.setResourceURI(uriTemplate.getUrlPattern());
uriTemplateObj.setHttpVerb(uriTemplate.getHttpMethod());
uriTemplateObj.setAuthScheme(uriTemplate.getAuthScheme());
List<org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy> operationPolicies = new ArrayList<>();
List<org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy> operationPolicies
= new ArrayList<>();
for (OperationPolicy operationPolicy : uriTemplate.getOperationPolicies()) {
org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy operationPolicyObj = new org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy();
org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy operationPolicyObj
= new org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy();
operationPolicyObj.setPolicyVersion(operationPolicy.getPolicyVersion());
operationPolicyObj.setPolicyName(operationPolicy.getPolicyName());
operationPolicyObj.setPolicyId(operationPolicy.getPolicyId());
Expand All @@ -191,9 +193,23 @@ public API getApi() throws DataNotFoundException {
}
uriTemplateObj.setOperationPolicies(operationPolicies);
uriTemplates.add(uriTemplateObj);
break;
}
}
List<org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy> apiPolicyList =
new ArrayList<>();
for (OperationPolicy apiPolicy : apiObj.getApiPolicies()) {
org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy operationPolicyObj
= new org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy();
operationPolicyObj.setPolicyVersion(apiPolicy.getPolicyVersion());
operationPolicyObj.setPolicyName(apiPolicy.getPolicyName());
operationPolicyObj.setPolicyId(apiPolicy.getPolicyId());
operationPolicyObj.setDirection(apiPolicy.getDirection());
operationPolicyObj.setOrder(apiPolicy.getOrder());
apiPolicyList.add(operationPolicyObj);
}
api.setUriTemplates(uriTemplates);
api.setApiPolicies(apiPolicyList);
}
return api;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class APIConstants {
//governance registry apimgt root location
public static final String APIMGT_REGISTRY_LOCATION = "/apimgt";

public static final String POLICY_ENABLED_FOR_ANALYTICS = "policyEnabled";

public static final String API_CONTEXT_ID = "api.context.id";
//This is the resource name of API
public static final String API_RESOURCE_NAME = "/api";
Expand Down Expand Up @@ -973,6 +975,7 @@ private Permissions() {

public static final String API_ANALYTICS = "Analytics.";
public static final String API_USAGE_ENABLED = API_ANALYTICS + "Enabled";
public static final String API_ANALYTICS_POLICY_ENABLED = API_ANALYTICS + "PolicyEnabled";
public static final String API_ANALYTICS_REPORTER_CLASS = API_ANALYTICS + "ReporterClass";
public static final String API_ANALYTICS_PROPERTIES = API_ANALYTICS + "Properties";
public static final String API_ANALYTICS_RESPONSE_SCHEMA_NAME = API_ANALYTICS + "ResponseSchemaName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ private void readChildElements(OMElement serverConfig,

OMElement analyticsType = element.getFirstChildWithName(new QName("Type"));
analyticsProps.put("type", analyticsType.getText());

OMElement enablePolicy = element.getFirstChildWithName(new QName("PolicyEnabled"));
analyticsProps.put("policyEnabled", enablePolicy.getText());

analyticsProperties = analyticsProps;
} else if ("PersistenceConfigs".equals(localName)) {
OMElement properties = element.getFirstChildWithName(new QName("Properties"));
Expand Down
Loading

0 comments on commit d9138d3

Please sign in to comment.