Skip to content

Commit

Permalink
chore(core): allow missing WebMvcConfigurer class in classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed May 31, 2024
1 parent 21defb7 commit 0203152
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.configuration;

import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@RequiredArgsConstructor
// Highest so that all others will replace this configuration
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class SpringwolfUiResourceConfiguration implements WebMvcConfigurer {

private final SpringwolfConfigProperties springwolfConfigProperties;
private final WebProperties webProperties;
private final WebMvcProperties webMvcProperties;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(springwolfConfigProperties.getPath().getBase() + "/**", webMvcProperties.getStaticPathPattern())
.addResourceLocations(buildStaticLocation());
}

private String[] buildStaticLocation() {
List<String> staticLocations = new ArrayList<>(Arrays.asList(webProperties.getResources().getStaticLocations()));
staticLocations.add("classpath:/META-INF/resources/springwolf/");

return staticLocations.toArray(new String[0]);
}

}
@ConditionalOnClass(WebMvcConfigurer.class)
@Import(SpringwolfUiResourceConfigurer.class)
public class SpringwolfUiResourceConfiguration {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.configuration;

import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@RequiredArgsConstructor
@Order(value = Ordered.HIGHEST_PRECEDENCE) // Highest so that all others will replace this configuration
public class SpringwolfUiResourceConfigurer implements WebMvcConfigurer {

private final SpringwolfConfigProperties springwolfConfigProperties;
private final WebProperties webProperties;
private final WebMvcProperties webMvcProperties;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
springwolfConfigProperties.getPath().getBase() + "/**", webMvcProperties.getStaticPathPattern())
.addResourceLocations(buildStaticLocation());
}

private String[] buildStaticLocation() {
List<String> staticLocations =
new ArrayList<>(Arrays.asList(webProperties.getResources().getStaticLocations()));
staticLocations.add("classpath:/META-INF/resources/springwolf/");

return staticLocations.toArray(new String[0]);
}
}

0 comments on commit 0203152

Please sign in to comment.