Commit 39941c39 authored by Christoph Holter's avatar Christoph Holter
Browse files

add complete WebConfig

parent 3d6c4a4c
package cc.catalysts.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.resource.PathResourceResolver;
import java.io.IOException;
/**
* Created by alexander.navratil@catalysts.cc
* Class responsible for serving static files.
*/
public class FallbackPathResourceResolver extends PathResourceResolver {
private ServerProperties serverProperties;
@Autowired
public FallbackPathResourceResolver(ServerProperties serverProperties) {
this.serverProperties = serverProperties;
}
@Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
Resource res = super.getResource(resourcePath, location);
if(serverProperties.getContextPath() != null && resourcePath.startsWith(serverProperties.getContextPath())) {
resourcePath = resourcePath.substring(serverProperties.getContextPath().length());
}
if(res == null){
if(resourcePath.endsWith(".js")){
String file = resourcePath.substring(resourcePath.lastIndexOf('/')+1);
return super.getResource("/static/"+file, location);
}
return super.getResource("index.html", location);
}
return res;
}
}
package cc.catalysts.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.util.UrlPathHelper;
/**
* Created by alexander.navratil@catalysts.cc
* This class configures: Swagger Documentation, create initial ctep user, allow encoded slashes in PathVariables
*/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
private ServerProperties serverProperties;
private AppConfig appConfig;
public WebConfig(AppConfig appConfig) {
@Autowired
public WebConfig(ServerProperties serverProperties, AppConfig appConfig) {
this.serverProperties = serverProperties;
this.appConfig = appConfig;
}
......@@ -18,6 +28,20 @@ public class WebConfig extends WebMvcConfigurerAdapter {
registry.addResourceHandler("/ext/**")
.addResourceLocations("file://" + appConfig.getExternalStaticLocation())
.resourceChain(true);
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/META-INF/resources/")
.resourceChain(true)
.addResolver(new FallbackPathResourceResolver(serverProperties));
}
/**
* Workaround for allowing encoded slashes in PathVariables
*/
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment