Skip to content
Snippets Groups Projects
WebMvcConfig.java 2.95 KiB
/*
 * CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
 * https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
 */
package fr.unistra.sil.erp.back;

import fr.unistra.sil.erp.back.interceptor.api.ApiAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Main configuration file for the application.
 * @author BEAUVAIS ANTOINE
 */
@Configuration
@EnableWebMvc
@PropertySources(
        @PropertySource("/apikey.properties")
)
public class WebMvcConfig implements WebMvcConfigurer {
    
    /**
     * Autowired environment for retrieving properties.
     */
    @Autowired
    private Environment env;
    
    /**
     * API version.
     */
    public static final String API_VERSION = "v1";
    
    /**
     * API prefix.
     */
    public static final String API_PREFIX = "/api/";
    
    /**
     * Prefix for API calls.
     */
    public static final String API_FULL_PREFIX = API_PREFIX + API_VERSION;
    
    /**
     * API Mapping for retrieving all items.
     */
    public static final String MAPPING_RETRIEVEALL = API_FULL_PREFIX +
            "/retrieveItems";
    
    /**
     * API Mapping for retrieving all categories.
     */
    public static final String MAPPING_GETCATEGORIES = API_FULL_PREFIX +
            "/retrieveCategories";
    
    /**
     * API Mapping for submitting transactions.
     */
    public static final String MAPPING_SUBTRANSAC = API_FULL_PREFIX +
            "/submitTransaction";
    
    /**
     * API Mapping for retrieving stocks.
     */
    public static final String MAPPING_GETSTOCKS = API_FULL_PREFIX +
            "/retrieveStocks";
    
    
    /*
    // TODO: Define default servlet.
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }*/
    
    /**
     * Adds interceptors to the application.
     * 
     * Interceptors process HTTP requests before they reach their 
     * attributed methods.
     * 
     * Currently, interceptors are only used for authentication in this 
     * application.
     * 
     * @param registry the Interceptor Registry to use for additions.
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        
        registry.addInterceptor(new ApiAuthenticationInterceptor(
                this.env.getProperty("api.key")))
                .addPathPatterns(API_PREFIX + "**");
        
    }
}