diff --git a/src/main/java/com/dedicatedcode/reitti/config/ConditionalOnPropertyNotEmpty.java b/src/main/java/com/dedicatedcode/reitti/config/ConditionalOnPropertyNotEmpty.java new file mode 100644 index 00000000..78c801a7 --- /dev/null +++ b/src/main/java/com/dedicatedcode/reitti/config/ConditionalOnPropertyNotEmpty.java @@ -0,0 +1,40 @@ +package com.dedicatedcode.reitti.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Conditional annotation that checks if a property has a non-empty value. + * This is a convenience annotation that combines ConditionalOnProperty with + * matchIfMissing=false and havingValue="" (empty string) negated. + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@org.springframework.context.annotation.Conditional(ConditionalOnPropertyNotEmptyCondition.class) +public @interface ConditionalOnPropertyNotEmpty { + + /** + * The name of the property to test. + * @return the property name + */ + String name() default ""; + + /** + * The name of the property to test (alias for name). + * @return the property name + */ + String value() default ""; + + /** + * A prefix that should be applied to each property name. + * @return the prefix + */ + String prefix() default ""; +} diff --git a/src/main/java/com/dedicatedcode/reitti/config/ConditionalOnPropertyNotEmptyCondition.java b/src/main/java/com/dedicatedcode/reitti/config/ConditionalOnPropertyNotEmptyCondition.java new file mode 100644 index 00000000..abc7fd2b --- /dev/null +++ b/src/main/java/com/dedicatedcode/reitti/config/ConditionalOnPropertyNotEmptyCondition.java @@ -0,0 +1,51 @@ +package com.dedicatedcode.reitti.config; + +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.util.StringUtils; + +/** + * Condition that checks if a property has a non-empty value. + */ +public class ConditionalOnPropertyNotEmptyCondition extends SpringBootCondition { + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + AnnotationAttributes attributes = AnnotationAttributes.fromMap( + metadata.getAnnotationAttributes(ConditionalOnPropertyNotEmpty.class.getName())); + + if (attributes == null) { + return ConditionOutcome.noMatch("@ConditionalOnPropertyNotEmpty annotation not found"); + } + + String propertyName = getPropertyName(attributes); + String propertyValue = context.getEnvironment().getProperty(propertyName); + + ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnPropertyNotEmpty.class); + + if (!StringUtils.hasText(propertyValue)) { + return ConditionOutcome.noMatch(message.because("property '" + propertyName + "' is empty or not set")); + } + + return ConditionOutcome.match(message.because("property '" + propertyName + "' has value: " + propertyValue)); + } + + private String getPropertyName(AnnotationAttributes attributes) { + String prefix = attributes.getString("prefix"); + String name = attributes.getString("name"); + String value = attributes.getString("value"); + + // Use 'value' if 'name' is not specified + String propertyName = StringUtils.hasText(name) ? name : value; + + if (StringUtils.hasText(prefix)) { + propertyName = prefix + "." + propertyName; + } + + return propertyName; + } +} diff --git a/src/main/java/com/dedicatedcode/reitti/controller/api/TileProxyController.java b/src/main/java/com/dedicatedcode/reitti/controller/api/TileProxyController.java index 6b6b8a6b..f5d01666 100644 --- a/src/main/java/com/dedicatedcode/reitti/controller/api/TileProxyController.java +++ b/src/main/java/com/dedicatedcode/reitti/controller/api/TileProxyController.java @@ -1,9 +1,9 @@ package com.dedicatedcode.reitti.controller.api; +import com.dedicatedcode.reitti.config.ConditionalOnPropertyNotEmpty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.CacheControl; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -18,14 +18,14 @@ import java.util.concurrent.TimeUnit; @RestController @RequestMapping("/api/v1/tiles") -@ConditionalOnProperty(name = "reitti.ui.tiles.cache.url") +@ConditionalOnPropertyNotEmpty("reitti.ui.tiles.cache.url") public class TileProxyController { private static final Logger log = LoggerFactory.getLogger(TileProxyController.class); private final RestTemplate restTemplate; private final String tileCacheUrl; - public TileProxyController(@Value("${reitti.ui.tiles.cache.url:http://tile-cache}") String tileCacheUrl) { + public TileProxyController(@Value("${reitti.ui.tiles.cache.url}") String tileCacheUrl) { this.tileCacheUrl = tileCacheUrl; this.restTemplate = new RestTemplate(); }