diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResource.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResource.java index 800592d5c4f..4d4c8575177 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResource.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResource.java @@ -19,13 +19,8 @@ package org.apache.fineract.infrastructure.cache.api; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.media.Content; -import io.swagger.v3.oas.annotations.media.Schema; -import io.swagger.v3.oas.annotations.parameters.RequestBody; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.PUT; @@ -33,16 +28,16 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import java.util.Collection; +import java.util.UUID; +import java.util.function.Supplier; import lombok.RequiredArgsConstructor; -import org.apache.fineract.commands.domain.CommandWrapper; -import org.apache.fineract.commands.service.CommandWrapperBuilder; -import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService; +import org.apache.fineract.command.core.CommandPipeline; +import org.apache.fineract.infrastructure.cache.command.CacheSwitchCommand; import org.apache.fineract.infrastructure.cache.data.CacheData; -import org.apache.fineract.infrastructure.cache.data.request.CacheRequest; +import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest; +import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse; import org.apache.fineract.infrastructure.cache.service.RuntimeDelegatingCacheManager; -import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; -import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer; -import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; +import org.apache.fineract.infrastructure.core.service.DateUtils; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @@ -50,37 +45,45 @@ @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @Component -@Tag(name = "Cache", description = "The following settings are possible for cache:\n" + "\n" + "No Caching: caching turned off\n" - + "Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat)\n" - + "By default caching is set to No Caching. Switching between caches results in the cache been clear e.g. from Single node to No cache and back again would clear down the single node cache.") +@Tag(name = "Cache", description = """ + The following settings are possible for cache: + + No Caching: caching turned off + + Single node: caching on for single instance deployments of platorm (works for multiple tenants but only one tomcat). + By default caching is set to No Caching. Switching between caches results in the cache been clear e.g. from single + node to no cache and back again would clear down the single node cache. + """) @RequiredArgsConstructor public class CacheApiResource { - private static final String RESOURCE_NAME_FOR_PERMISSIONS = "CACHE"; - - private final PlatformSecurityContext context; - private final DefaultToApiJsonSerializer toApiJsonSerializer; - private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; @Qualifier("runtimeDelegatingCacheManager") private final RuntimeDelegatingCacheManager cacheService; + private final CommandPipeline commandPipeline; @GET - @Operation(summary = "Retrieve Cache Types", description = "Returns the list of caches.\n" + "\n" + "Example Requests:\n" + "\n" - + "caches") + @Operation(summary = "Retrieve Cache Types", description = """ + Returns the list of caches. + + Example Requests: + + caches + """) public Collection retrieveAll() { - this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS); return cacheService.retrieveAll(); } @PUT @Operation(summary = "Switch Cache", description = "Switches the cache to chosen one.") - @RequestBody(required = true, content = @Content(schema = @Schema(implementation = CacheRequest.class))) - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CacheApiResourceSwagger.PutCachesResponse.class))) }) - public CommandProcessingResult switchCache(@Parameter(hidden = true) CacheRequest cacheRequest) { - final CommandWrapper commandRequest = new CommandWrapperBuilder().updateCache() - .withJson(toApiJsonSerializer.serialize(cacheRequest)).build(); + public CacheSwitchResponse switchCache(@Valid CacheSwitchRequest request) { + final var command = new CacheSwitchCommand(); + + command.setId(UUID.randomUUID()); + command.setCreatedAt(DateUtils.getAuditOffsetDateTime()); + command.setPayload(request); + + final Supplier response = commandPipeline.send(command); - return this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + return response.get(); } } diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/command/CacheSwitchCommand.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/command/CacheSwitchCommand.java new file mode 100644 index 00000000000..a8a95889449 --- /dev/null +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/command/CacheSwitchCommand.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.apache.fineract.infrastructure.cache.command; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.apache.fineract.command.core.Command; +import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest; + +@Data +@EqualsAndHashCode(callSuper = true) +public class CacheSwitchCommand extends Command {} diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/command/UpdateCacheCommandHandler.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/command/UpdateCacheCommandHandler.java deleted file mode 100644 index 99d90afd2a3..00000000000 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/command/UpdateCacheCommandHandler.java +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF 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.apache.fineract.infrastructure.cache.command; - -import com.google.gson.reflect.TypeToken; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.apache.commons.lang3.StringUtils; -import org.apache.fineract.commands.annotation.CommandType; -import org.apache.fineract.commands.handler.NewCommandSourceHandler; -import org.apache.fineract.infrastructure.cache.CacheApiConstants; -import org.apache.fineract.infrastructure.cache.domain.CacheType; -import org.apache.fineract.infrastructure.cache.service.CacheWritePlatformService; -import org.apache.fineract.infrastructure.core.api.JsonCommand; -import org.apache.fineract.infrastructure.core.data.ApiParameterError; -import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; -import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder; -import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder; -import org.apache.fineract.infrastructure.core.exception.InvalidJsonException; -import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@CommandType(entity = "CACHE", action = "UPDATE") -public class UpdateCacheCommandHandler implements NewCommandSourceHandler { - - private final CacheWritePlatformService cacheService; - private static final Set REQUEST_DATA_PARAMETERS = new HashSet<>(Arrays.asList(CacheApiConstants.CACHE_TYPE_PARAMETER)); - - @Autowired - public UpdateCacheCommandHandler(final CacheWritePlatformService cacheService) { - this.cacheService = cacheService; - } - - @Transactional - @Override - public CommandProcessingResult processCommand(final JsonCommand command) { - - final String json = command.json(); - - if (StringUtils.isBlank(json)) { - throw new InvalidJsonException(); - } - - final Type typeOfMap = new TypeToken>() {}.getType(); - command.checkForUnsupportedParameters(typeOfMap, json, REQUEST_DATA_PARAMETERS); - - final List dataValidationErrors = new ArrayList<>(); - final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) - .resource(CacheApiConstants.RESOURCE_NAME.toLowerCase()); - - final int cacheTypeEnum = command.integerValueSansLocaleOfParameterNamed(CacheApiConstants.CACHE_TYPE_PARAMETER); - baseDataValidator.reset().parameter(CacheApiConstants.CACHE_TYPE_PARAMETER).value(Integer.valueOf(cacheTypeEnum)).notNull() - .isOneOfTheseValues(Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)); - - if (!dataValidationErrors.isEmpty()) { - throw new PlatformApiDataValidationException(dataValidationErrors); - } - - final CacheType cacheType = CacheType.fromInt(cacheTypeEnum); - - final Map changes = this.cacheService.switchToCache(cacheType); - - return new CommandProcessingResultBuilder().withCommandId(command.commandId()).with(changes).build(); - } -} diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheData.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheData.java index f487b5544fb..771b6d553b3 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheData.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheData.java @@ -18,23 +18,20 @@ */ package org.apache.fineract.infrastructure.cache.data; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import lombok.experimental.Accessors; import org.apache.fineract.infrastructure.core.data.EnumOptionData; +@Builder @Data @NoArgsConstructor -@Accessors(chain = true) +@AllArgsConstructor public final class CacheData { @SuppressWarnings("unused") private EnumOptionData cacheType; @SuppressWarnings("unused") private boolean enabled; - - public static CacheData instance(final EnumOptionData cacheType, final boolean enabled) { - return new CacheData().setCacheType(cacheType).setEnabled(enabled); - } - } diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResourceSwagger.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchRequest.java similarity index 55% rename from fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResourceSwagger.java rename to fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchRequest.java index 0c200e93f65..7aa06c41e71 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/api/CacheApiResourceSwagger.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchRequest.java @@ -16,38 +16,25 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.fineract.infrastructure.cache.api; - -import io.swagger.v3.oas.annotations.media.Schema; - -/** - * Created by sanyam on 28/7/17. - */ -final class CacheApiResourceSwagger { - - private CacheApiResourceSwagger() { - - } - - @Schema(description = "PutCachesResponse") - public static final class PutCachesResponse { - - private PutCachesResponse() { - - } - - public static final class PutCachechangesSwagger { - - private PutCachechangesSwagger() { - - } - - @Schema(example = "2") - public Long cacheType; - - } - - public PutCachechangesSwagger cacheType; - - } +package org.apache.fineract.infrastructure.cache.data; + +import jakarta.validation.constraints.NotNull; +import java.io.Serial; +import java.io.Serializable; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Builder +@Data +@NoArgsConstructor +@AllArgsConstructor +public class CacheSwitchRequest implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + @NotNull(message = "{org.apache.fineract.cache.cache-type.not-null}") + private Integer cacheType; } diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/request/CacheRequest.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchResponse.java similarity index 71% rename from fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/request/CacheRequest.java rename to fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchResponse.java index 6adf680e029..392412c2ddb 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/request/CacheRequest.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/data/CacheSwitchResponse.java @@ -16,13 +16,25 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.fineract.infrastructure.cache.data.request; +package org.apache.fineract.infrastructure.cache.data; import java.io.Serial; import java.io.Serializable; +import java.util.Map; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; -public record CacheRequest(Long cacheType) implements Serializable { +@Builder +@Data +@NoArgsConstructor +@AllArgsConstructor +public class CacheSwitchResponse implements Serializable { @Serial private static final long serialVersionUID = 1L; + + private Integer cacheType; + private Map changes; } diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/handler/CacheSwitchCommandHandler.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/handler/CacheSwitchCommandHandler.java new file mode 100644 index 00000000000..f609863fa10 --- /dev/null +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/handler/CacheSwitchCommandHandler.java @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.apache.fineract.infrastructure.cache.handler; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.fineract.command.core.Command; +import org.apache.fineract.command.core.CommandHandler; +import org.apache.fineract.infrastructure.cache.data.CacheSwitchRequest; +import org.apache.fineract.infrastructure.cache.data.CacheSwitchResponse; +import org.apache.fineract.infrastructure.cache.domain.CacheType; +import org.apache.fineract.infrastructure.cache.service.CacheWritePlatformService; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@Component +@RequiredArgsConstructor +public class CacheSwitchCommandHandler implements CommandHandler { + + private final CacheWritePlatformService cacheService; + + @Transactional + @Override + public CacheSwitchResponse handle(final Command command) { + var request = command.getPayload(); + var cacheType = CacheType.fromInt(request.getCacheType()); + var changes = cacheService.switchToCache(cacheType); + + return CacheSwitchResponse.builder().changes(changes).cacheType(request.getCacheType()).build(); + } +} diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/service/RuntimeDelegatingCacheManager.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/service/RuntimeDelegatingCacheManager.java index 1886701293d..6e8bcb0dc1c 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/service/RuntimeDelegatingCacheManager.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/cache/service/RuntimeDelegatingCacheManager.java @@ -78,8 +78,8 @@ public Collection retrieveAll() { final EnumOptionData noCacheType = CacheEnumerations.cacheType(CacheType.NO_CACHE); final EnumOptionData singleNodeCacheType = CacheEnumerations.cacheType(CacheType.SINGLE_NODE); - final CacheData noCache = CacheData.instance(noCacheType, noCacheEnabled); - final CacheData singleNodeCache = CacheData.instance(singleNodeCacheType, ehCacheEnabled); + final CacheData noCache = CacheData.builder().cacheType(noCacheType).enabled(noCacheEnabled).build(); + final CacheData singleNodeCache = CacheData.builder().cacheType(singleNodeCacheType).enabled(ehCacheEnabled).build(); return Arrays.asList(noCache, singleNodeCache); } diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/external/handler/ExternalEventConfigurationUpdateHandler.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/external/handler/ExternalEventConfigurationUpdateHandler.java index 43e2dcc077a..2c7ca64b30e 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/external/handler/ExternalEventConfigurationUpdateHandler.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/external/handler/ExternalEventConfigurationUpdateHandler.java @@ -19,18 +19,18 @@ package org.apache.fineract.infrastructure.event.external.handler; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.fineract.command.core.Command; import org.apache.fineract.command.core.CommandHandler; -import org.apache.fineract.commands.annotation.CommandType; import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationUpdateRequest; import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationUpdateResponse; import org.apache.fineract.infrastructure.event.external.service.ExternalEventConfigurationWritePlatformService; -import org.springframework.stereotype.Service; +import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; +@Slf4j +@Component @RequiredArgsConstructor -@Service -@CommandType(entity = "EXTERNAL_EVENT_CONFIGURATION", action = "UPDATE") public class ExternalEventConfigurationUpdateHandler implements CommandHandler { diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/SecurityConfig.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/SecurityConfig.java index 30f98464e21..9ae39f235bc 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/SecurityConfig.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/SecurityConfig.java @@ -142,6 +142,11 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_READ", "READ_EXTERNAL_EVENT_CONFIGURATION") .requestMatchers(antMatcher(HttpMethod.PUT, "/api/*/externalevents/configuration")) .hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_WRITE", "UPDATE_EXTERNAL_EVENT_CONFIGURATION") + // cache + .requestMatchers(antMatcher(HttpMethod.GET, "/api/*/caches")) + .hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_READ", "READ_CACHE") + .requestMatchers(antMatcher(HttpMethod.PUT, "/api/*/caches")) + .hasAnyAuthority("ALL_FUNCTIONS", "ALL_FUNCTIONS_WRITE", "UPDATE_CACHE") // ... .requestMatchers(antMatcher(HttpMethod.POST, "/api/*/twofactor/validate")).fullyAuthenticated() // .requestMatchers(antMatcher("/api/*/twofactor")).fullyAuthenticated() // diff --git a/fineract-validation/src/main/resources/fineract/validation/messages.properties b/fineract-validation/src/main/resources/fineract/validation/messages.properties index 8508fdd06d9..743eabc4e07 100644 --- a/fineract-validation/src/main/resources/fineract/validation/messages.properties +++ b/fineract-validation/src/main/resources/fineract/validation/messages.properties @@ -32,3 +32,7 @@ org.apache.fineract.businessdate.locale.not-blank=The parameter 'locale' is mand # External Events org.apache.fineract.externalevent.configurations.not-null=The parameter 'externalEventConfigurations' is mandatory: '${validatedValue}'. + +# Cache + +org.apache.fineract.cache.cache-type.not-null=The parameter 'cacheType' is mandatory. diff --git a/fineract-validation/src/main/resources/fineract/validation/messages_en.properties b/fineract-validation/src/main/resources/fineract/validation/messages_en.properties index 8508fdd06d9..743eabc4e07 100644 --- a/fineract-validation/src/main/resources/fineract/validation/messages_en.properties +++ b/fineract-validation/src/main/resources/fineract/validation/messages_en.properties @@ -32,3 +32,7 @@ org.apache.fineract.businessdate.locale.not-blank=The parameter 'locale' is mand # External Events org.apache.fineract.externalevent.configurations.not-null=The parameter 'externalEventConfigurations' is mandatory: '${validatedValue}'. + +# Cache + +org.apache.fineract.cache.cache-type.not-null=The parameter 'cacheType' is mandatory.