Configs Generator

open class ConfigsGenerator

The ConfigsGenerator class is useful to create automatically some configuration files

Author

N7ghtm4r3 - Tecknobit

Since

1.0.1

Constructors

Link copied to clipboard
constructor(context: Class<out Any>)
Constructor to init the ConfigsGenerator class

Properties

Link copied to clipboard
private val context: Class<out Any>
context the Class where this method has been invoked
Link copied to clipboard
private val CORS_ADVICE_FILE_NAME: String = "CORSAdvice"
CORS_ADVICE_FILE_NAME the name for the CORS configuration file
Link copied to clipboard
private val isKotlinClass: Boolean
isKotlinClass whether the caller class (context) is a Kotlin class
Link copied to clipboard
private val JAVA_CONFIGURATION_FILE_CONTENT: String = " import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.resource.PathResourceResolver; /** * The {@code ResourceConfigs} class is useful to set the configuration of the resources to correctly serve the * images by the server * * @author N7ghtm4r3 - Tecknobit * @see WebMvcConfigurer */ @Configuration public class ResourcesConfig implements WebMvcConfigurer { /** * Add handlers to serve static resources such as images, js, and, css * files from specific locations under web application root, the classpath, * and others. * * @see ResourceHandlerRegistry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("file:<list>") .setCachePeriod(0) .resourceChain(true) .addResolver(new PathResourceResolver()); } }"
JAVA_CONFIGURATION_FILE_CONTENT the content of the WebMvcConfigurer, as Java class, to allow the serve of the static resources by the backend
Link copied to clipboard
private val JAVA_CORS_ADVICE_FILE_CONTENT: String = " import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; /** * The {@code CORSAdvice} class is useful to set the CORS policy * * @author N7ghtm4r3 - Tecknobit */ @Configuration public class CORSAdvice { /** * Method to set the CORS filter <br> * No any-params required */ @Bean public FilterRegistrationBean corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(false); config.addAllowedOrigin("*"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); bean.setOrder(0); return bean; } }"
JAVA_CORS_ADVICE_FILE_CONTENT the content of the CORSAdvice file, as Java class, to set the CORS origin policy for the backend
Link copied to clipboard
private val KOTLIN_CONFIGURATION_FILE_CONTENT: String = " import org.springframework.context.annotation.Configuration import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurer import org.springframework.web.servlet.resource.PathResourceResolver /** * The `ResourceConfigs` class is useful to set the configuration of the resources to correctly serve the * images by the server * * @author N7ghtm4r3 - Tecknobit * @see WebMvcConfigurer */ @Configuration class ResourcesConfig : WebMvcConfigurer { /** * Add handlers to serve static resources such as images, js, and, css * files from specific locations under web application root, the classpath, * and others. * * @see ResourceHandlerRegistry */ override fun addResourceHandlers(registry: ResourceHandlerRegistry) { registry.addResourceHandler("/**") .addResourceLocations("file:<list>") .setCachePeriod(0) .resourceChain(true) .addResolver(PathResourceResolver()) } }"
KOTLIN_CONFIGURATION_FILE_CONTENT the content of the WebMvcConfigurer, as Kotlin class, to allow the serve of the static resources by the backend
Link copied to clipboard
private val KOTLIN_CORS_ADVICE_FILE_CONTENT: String = " import org.springframework.boot.web.servlet.FilterRegistrationBean import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.web.cors.CorsConfiguration import org.springframework.web.cors.UrlBasedCorsConfigurationSource import org.springframework.web.filter.CorsFilter /** * The `CORSAdvice` class is useful to set the CORS policy * * @author N7ghtm4r3 - Tecknobit */ @Configuration class CORSAdvice { /** * Method to set the CORS filter * * No any-params required */ @Bean fun corsFilter(): FilterRegistrationBean<*> { val source = UrlBasedCorsConfigurationSource() val config = CorsConfiguration() config.allowCredentials = false config.addAllowedOrigin("*") config.addAllowedHeader("*") config.addAllowedMethod("*") source.registerCorsConfiguration("/**", config) val bean: FilterRegistrationBean<*> = FilterRegistrationBean(CorsFilter(source)) bean.order = 0 return bean } }"
KOTLIN_CORS_ADVICE_FILE_CONTENT the content of the CORSAdvice file, as Kotlin class, to set the CORS origin policy for the backend
Link copied to clipboard
private val preferences: Preferences
preferences helper to check with the configFileStillNotCreated method if a config file has been already created or not
Link copied to clipboard
private val RESOURCES_CONFIG_FILE_NAME: String = "ResourcesConfig"
RESOURCES_CONFIG_FILE_NAME the name for the resources configuration file

Functions

Link copied to clipboard
private open fun configFileStillNotCreated(configsPath: String): Boolean
Method to check if a config file has been already created for the current project, so to skip its creation
Link copied to clipboard
Method to create the CORSAdvice to correctly set the CORS origin policy for the backend No-any params required
Link copied to clipboard
private open fun createConfigFile(configsPath: String, content: String)
Method to create the config file and then save it
Link copied to clipboard
Method to create the WebMvcConfigurer to correctly serve the static resources by backend
Link copied to clipboard
private open fun getConfigFileContent(kotlinContent: String, javaContent: String): String
Method to get the content of the configuration file to create
Link copied to clipboard
private open fun getConfigsPath(configFileName: String): String
Method to get the path where place the configuration file class
Link copied to clipboard
private open fun isKotlinClass(): Boolean
Method to get whether the caller class is a Kotlin class No-any params required