Skip to content

Commit

Permalink
Merge pull request #12611 from Tharanidk/counter_improve
Browse files Browse the repository at this point in the history
Add improvements to Transaction Counter
  • Loading branch information
Tharanidk authored Oct 18, 2024
2 parents ee9737d + a537572 commit eb03cef
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3240,4 +3240,10 @@ public static class TokenValidationConstants {
public static final String TOKEN_VALIDATION_CONFIG = "TokenValidation";
public static final String ENFORCE_JWT_TYPE_HEADER_VALIDATION = "EnforceTypeHeaderValidation";
}

public static class TransactionCounter {

public static final String TRANSACTIONCOUNTER = "TransactionCounter";
public static final String COUNTER_ENABLED = "Enabled";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public class APIManagerConfiguration {
private TokenValidationDto tokenValidationDto = new TokenValidationDto();
private boolean enableAiConfiguration;
private String hashingAlgorithm = SHA_256;
private boolean isTransactionCounterEnabled;

public Map<String, List<String>> getRestApiJWTAuthAudiences() {
return restApiJWTAuthAudiences;
Expand Down Expand Up @@ -668,12 +669,21 @@ private void readChildElements(OMElement serverConfig,
setTokenValidation(element);
} else if (APIConstants.HASHING.equals(localName)) {
setHashingAlgorithm(element);
} else if (APIConstants.TransactionCounter.TRANSACTIONCOUNTER.equals(localName)) {
OMElement counterEnabled = element.getFirstChildWithName(new QName(APIConstants.TransactionCounter.COUNTER_ENABLED));
if (counterEnabled != null) {
isTransactionCounterEnabled = Boolean.parseBoolean(counterEnabled.getText());
}
}
readChildElements(element, nameStack);
nameStack.pop();
}
}

public boolean getTransactionCounterProperties() {
return isTransactionCounterEnabled;
}

public JSONObject getSubscriberAttributes() {
return subscriberAttributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10962,4 +10962,14 @@ public static byte[] joinByteArrays(byte[] array1, byte[] array2) {
System.arraycopy(array2, 0, result, array1.length, array2.length);
return result;
}

/**
* This method checks whether the transaction counter feature is enabled.
*
* @return true if the transaction counter is enabled, false otherwise
*/
public static boolean getTransactionCounterEnable() {
return ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService()
.getAPIManagerConfiguration().getTransactionCounterProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,23 @@
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.impl.dao.TransactionCountDAO;
import org.wso2.carbon.apimgt.impl.dto.TransactionCountDTO;
import org.wso2.carbon.apimgt.internal.service.*;
import org.wso2.carbon.apimgt.internal.service.dto.*;
import org.wso2.carbon.apimgt.internal.service.TransactionRecordsApiService;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.MessageContext;

import java.util.List;

import org.wso2.carbon.apimgt.internal.service.dto.TransactionRecordDTO;
import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil;

import java.util.List;

import java.io.InputStream;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

public class TransactionRecordsApiServiceImpl implements TransactionRecordsApiService {

private static final Log log = LogFactory.getLog(TransactionRecordsApiServiceImpl.class);

public Response insertTransactionRecords(List<TransactionRecordDTO> body, MessageContext messageContext) {
TransactionCountDAO transactionCountDAO = TransactionCountDAO.getInstance();

try {
TransactionCountDTO[] transactionCountDTOArray = body.stream().map(recordDTO -> {
TransactionCountDTO transactionCountDTO = new TransactionCountDTO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class SettingsDTO {
private Boolean isJWTEnabledForLoginTokens = false;
private List<SettingsKeyManagerConfigurationDTO> keyManagerConfiguration = new ArrayList<SettingsKeyManagerConfigurationDTO>();
private Boolean analyticsEnabled = null;
private Boolean transactionCounterEnable = null;

/**
**/
Expand Down Expand Up @@ -116,6 +117,24 @@ public void setAnalyticsEnabled(Boolean analyticsEnabled) {
this.analyticsEnabled = analyticsEnabled;
}

/**
* To determine whether the transaction counter is enabled or not
**/
public SettingsDTO transactionCounterEnable(Boolean transactionCounterEnable) {
this.transactionCounterEnable = transactionCounterEnable;
return this;
}


@ApiModelProperty(example = "false", value = "To determine whether the transaction counter is enabled or not")
@JsonProperty("transactionCounterEnable")
public Boolean isTransactionCounterEnable() {
return transactionCounterEnable;
}
public void setTransactionCounterEnable(Boolean transactionCounterEnable) {
this.transactionCounterEnable = transactionCounterEnable;
}


@Override
public boolean equals(java.lang.Object o) {
Expand All @@ -130,12 +149,13 @@ public boolean equals(java.lang.Object o) {
Objects.equals(gatewayTypes, settings.gatewayTypes) &&
Objects.equals(isJWTEnabledForLoginTokens, settings.isJWTEnabledForLoginTokens) &&
Objects.equals(keyManagerConfiguration, settings.keyManagerConfiguration) &&
Objects.equals(analyticsEnabled, settings.analyticsEnabled);
Objects.equals(analyticsEnabled, settings.analyticsEnabled) &&
Objects.equals(transactionCounterEnable, settings.transactionCounterEnable);
}

@Override
public int hashCode() {
return Objects.hash(scopes, gatewayTypes, isJWTEnabledForLoginTokens, keyManagerConfiguration, analyticsEnabled);
return Objects.hash(scopes, gatewayTypes, isJWTEnabledForLoginTokens, keyManagerConfiguration, analyticsEnabled, transactionCounterEnable);
}

@Override
Expand All @@ -148,6 +168,7 @@ public String toString() {
sb.append(" isJWTEnabledForLoginTokens: ").append(toIndentedString(isJWTEnabledForLoginTokens)).append("\n");
sb.append(" keyManagerConfiguration: ").append(toIndentedString(keyManagerConfiguration)).append("\n");
sb.append(" analyticsEnabled: ").append(toIndentedString(analyticsEnabled)).append("\n");
sb.append(" transactionCounterEnable: ").append(toIndentedString(transactionCounterEnable)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.apimgt.rest.api.admin.v1.SettingsApiService;

import org.apache.cxf.jaxrs.ext.MessageContext;
Expand Down Expand Up @@ -51,6 +52,7 @@ public Response settingsGet(MessageContext messageContext) throws APIManagementE
}
SettingsMappingUtil settingsMappingUtil = new SettingsMappingUtil();
SettingsDTO settingsDTO = settingsMappingUtil.fromSettingsToDTO(isUserAvailable);
settingsDTO.setTransactionCounterEnable(APIUtil.getTransactionCounterEnable());
return Response.ok().entity(settingsDTO).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,28 @@
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.impl.dao.TransactionCountDAO;
import org.wso2.carbon.apimgt.impl.dto.TransactionCountDTO;
import org.wso2.carbon.apimgt.rest.api.admin.v1.*;
import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.*;
import org.wso2.carbon.apimgt.rest.api.admin.v1.TransactionCountApiService;
import org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil;
import org.wso2.carbon.apimgt.rest.api.util.exception.ForbiddenException;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.MessageContext;

import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ErrorDTO;
import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.List;

import java.io.InputStream;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

public class TransactionCountApiServiceImpl implements TransactionCountApiService {

private static final Log log = LogFactory.getLog(TransactionCountApiServiceImpl.class);

public Response transactionCountGet(String startTime, String endTime, MessageContext messageContext) {
checkTenantDomain();
try {
ZoneId zoneId = ZoneId.systemDefault();
// Convert start and end times to the start and end of the respective days
Expand All @@ -68,4 +65,13 @@ public Response transactionCountGet(String startTime, String endTime, MessageCon
}
return null;
}
private void checkTenantDomain() throws ForbiddenException {
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
RestApiUtil.handleAuthorizationFailure("You are not allowed to access this resource",
new APIManagementException("Tenant " + tenantDomain + " is not allowed to retrieve transaction " +
"count. Only super tenant is allowed"), log);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5047,6 +5047,10 @@ components:
type: boolean
description: To determine whether analytics is enabled or not
example: false
transactionCounterEnable:
type: boolean
description: To determine whether the transaction counter is enabled or not
example: false
ScopeList:
title: Scope Role Mapping List
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5047,6 +5047,10 @@ components:
type: boolean
description: To determine whether analytics is enabled or not
example: false
transactionCounterEnable:
type: boolean
description: To determine whether the transaction counter is enabled or not
example: false
ScopeList:
title: Scope Role Mapping List
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,18 @@
"apim.sync_runtime_artifacts.gateway.event_waiting_time": "5000",
"apim.sync_runtime_artifacts.gateway.enable": true,
"apim.sync_runtime_artifacts.gateway.enable_on_demand_loading": false,
"apim.correlation_logs.components": ["http","jdbc","ldap","synapse","method-calls"]
"apim.correlation_logs.components": ["http","jdbc","ldap","synapse","method-calls"],
"apim.transaction_counter.enable": false,
"apim.transaction_counter.producer_counting_thread_pool_size":"10",
"apim.transaction_counter.producer_scheduled_interval":"10",
"apim.transaction_counter.max_transaction_count_per_record":"20",
"apim.transaction_counter.min_transaction_count_per_record":"5",
"apim.transaction_counter.record_queue_size":"1000",
"apim.transaction_counter.publisher_scheduled_interval":"5",
"apim.transaction_counter.publisher_max_batch_size":"100",
"apim.transaction_counter.publisher_max_retries":"3",
"apim.transaction_counter.store_impl":"org.wso2.integration.transaction.counter.store.TransactionRecordStoreImpl",
"apim.transaction_counter.service_username": "$ref{super_admin.username}",
"apim.transaction_counter.service_password": "$ref{super_admin.password}",
"apim.transaction_counter.service_url": "https://localhost:${mgt.transport.https.port}/internal/data/v1/transaction-records"
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,47 +195,47 @@
{% endfor %}
</Environments>
<TransactionCounter>
{% if integration.transaction_counter.enable is defined %}
<Enabled>{{integration.transaction_counter.enable}}</Enabled>
{% if apim.transaction_counter.enable is defined %}
<Enabled>{{apim.transaction_counter.enable}}</Enabled>
{% endif %}
{% if integration.transaction_counter.server_id is defined %}
<ServerID>{{integration.transaction_counter.server_id}}</ServerID>
{% if apim.transaction_counter.server_id is defined %}
<ServerID>{{apim.transaction_counter.server_id}}</ServerID>
{% endif %}
{% if integration.transaction_counter.store_impl is defined %}
<StoreClass>{{integration.transaction_counter.store_impl}}</StoreClass>
{% if apim.transaction_counter.store_impl is defined %}
<StoreClass>{{apim.transaction_counter.store_impl}}</StoreClass>
{% endif %}
{% if integration.transaction_counter.record_queue_size is defined %}
<QueueSize>{{integration.transaction_counter.record_queue_size}}</QueueSize>
{% if apim.transaction_counter.record_queue_size is defined %}
<QueueSize>{{apim.transaction_counter.record_queue_size}}</QueueSize>
{% endif %}
{% if integration.transaction_counter.producer_counting_thread_pool_size is defined %}
<ProducerThreadPoolSize>{{integration.transaction_counter.producer_counting_thread_pool_size}}</ProducerThreadPoolSize>
{% if apim.transaction_counter.producer_counting_thread_pool_size is defined %}
<ProducerThreadPoolSize>{{apim.transaction_counter.producer_counting_thread_pool_size}}</ProducerThreadPoolSize>
{% endif %}
{% if integration.transaction_counter.producer_scheduled_interval is defined %}
<ProducerScheduledInterval>{{integration.transaction_counter.producer_scheduled_interval}}</ProducerScheduledInterval>
{% if apim.transaction_counter.producer_scheduled_interval is defined %}
<ProducerScheduledInterval>{{apim.transaction_counter.producer_scheduled_interval}}</ProducerScheduledInterval>
{% endif %}
{% if integration.transaction_counter.max_transaction_count_per_record is defined %}
<MaxTransactionCount>{{integration.transaction_counter.max_transaction_count_per_record}}</MaxTransactionCount>
{% if apim.transaction_counter.max_transaction_count_per_record is defined %}
<MaxTransactionCount>{{apim.transaction_counter.max_transaction_count_per_record}}</MaxTransactionCount>
{% endif %}
{% if integration.transaction_counter.min_transaction_count_per_record is defined %}
<MinTransactionCount>{{integration.transaction_counter.min_transaction_count_per_record}}</MinTransactionCount>
{% if apim.transaction_counter.min_transaction_count_per_record is defined %}
<MinTransactionCount>{{apim.transaction_counter.min_transaction_count_per_record}}</MinTransactionCount>
{% endif %}
{% if integration.transaction_counter.publisher_scheduled_interval is defined %}
<PublisherScheduledInterval>{{integration.transaction_counter.publisher_scheduled_interval}}</PublisherScheduledInterval>
{% if apim.transaction_counter.publisher_scheduled_interval is defined %}
<PublisherScheduledInterval>{{apim.transaction_counter.publisher_scheduled_interval}}</PublisherScheduledInterval>
{% endif %}
{% if integration.transaction_counter.publisher_max_batch_size is defined %}
<MaxBatchSize>{{integration.transaction_counter.publisher_max_batch_size}}</MaxBatchSize>
{% if apim.transaction_counter.publisher_max_batch_size is defined %}
<MaxBatchSize>{{apim.transaction_counter.publisher_max_batch_size}}</MaxBatchSize>
{% endif %}
{% if integration.transaction_counter.publisher_max_retries is defined %}
<MaxRetryCount>{{integration.transaction_counter.publisher_max_retries}}</MaxRetryCount>
{% if apim.transaction_counter.publisher_max_retries is defined %}
<MaxRetryCount>{{apim.transaction_counter.publisher_max_retries}}</MaxRetryCount>
{% endif %}
{% if integration.transaction_counter.service_url is defined %}
<ServiceURL>{{integration.transaction_counter.service_url}}</ServiceURL>
{% if apim.transaction_counter.service_url is defined %}
<ServiceURL>{{apim.transaction_counter.service_url}}</ServiceURL>
{% endif %}
{% if integration.transaction_counter.service_username is defined %}
<ServiceUsername>{{integration.transaction_counter.service_username}}</ServiceUsername>
{% if apim.transaction_counter.service_username is defined %}
<ServiceUsername>{{apim.transaction_counter.service_username}}</ServiceUsername>
{% endif %}
{% if integration.transaction_counter.service_password is defined %}
<ServicePassword>{{integration.transaction_counter.service_password}}</ServicePassword>
{% if apim.transaction_counter.service_password is defined %}
<ServicePassword>{{apim.transaction_counter.service_password}}</ServicePassword>
{% endif %}
</TransactionCounter>
</APIGateway>
Expand Down

0 comments on commit eb03cef

Please sign in to comment.