All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.giffing.bucket4j.spring.boot.starter.utils.Bucket4JUtils Maven / Gradle / Ivy

There is a newer version: 0.12.7
Show newest version
package com.giffing.bucket4j.spring.boot.starter.utils;

import java.util.Objects;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Bucket4JUtils {

	public static ResponseEntity validateConfigurationUpdate(Bucket4JConfiguration oldConfig, Bucket4JConfiguration newConfig){
		if (oldConfig == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No filter with id '" + newConfig.getId() + "' could be found.");
		}

		//validate that the version increased
		if (oldConfig.getBucket4JVersionNumber() >= newConfig.getBucket4JVersionNumber()) {
			return ResponseEntity.badRequest().body("The new configuration should have a higher version than the current configuration.");
		}

		//validate that the fields that are not allowed to change remain the same
		ResponseEntity response;
		response = validateFieldEquality("filterMethod", oldConfig.getFilterMethod(), newConfig.getFilterMethod());
		if (response != null) return response;
		response = validateFieldEquality("filterOrder", oldConfig.getFilterOrder(), newConfig.getFilterOrder());
		if (response != null) return response;
		return validateFieldEquality("cacheName", oldConfig.getCacheName(), newConfig.getCacheName());
	}

	private static ResponseEntity validateFieldEquality(String fieldName, Object oldValue, Object newValue) {
		if (!Objects.equals(oldValue, newValue)) {
			String errorMessage = String.format(
					"It is not possible to modify the %s of an existing filter. Expected the field to be '%s' but is '%s'.",
					fieldName, oldValue, newValue);
			return ResponseEntity.badRequest().body(errorMessage);
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy