Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 "";
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,14 +18,14 @@

@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();
}
Expand Down