<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.0.xsd 
	http://www.springframework.org/schema/task
	http://www.springframework.org/schema/task/spring-task-4.0.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- Scan controller -->
		
	<context:component-scan
		base-package="com.goafanti.*.controller;com.goafanti.*.*.controller;com.goafanti.common.task;com.goafanti.core.websocket" />

	<bean name="springContextUtils" class="com.goafanti.common.utils.SpringContextUtils" scope="singleton"></bean>
	
	<bean id="passwordUtil" class="com.goafanti.common.utils.PasswordUtil">
		<property name="algorithmName" value="${pwd.hash_algorithm_name}"/>  
	    <property name="hashIterations" value="${pwd.hash_iterations}"/> 
	</bean>
	
	<!-- spring bean validator -->
	<bean id="validator"
		class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
		<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
		<property name="validationMessageSource" ref="messageSource" />
	</bean>

	<mvc:annotation-driven validator="validator" />

	<bean
		class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
		<property name="securityManager" ref="securityManager" />
	</bean>



	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="defaultEncoding" value="UTF-8"></property>
		<property name="basenames">
			<list>
				<value>classpath:props/error</value>
			</list>
		</property>
	</bean>


	<!-- Handles HTTP GET requests for /assets/** by efficiently serving up 
		static resources in the ${webappRoot}/assets directory -->
	<mvc:resources mapping="/static/images/**" location="/WEB-INF/images/" />
	<mvc:resources mapping="/static/html/**" location="/WEB-INF/html/" />
	<mvc:resources mapping="/static/**" location="/WEB-INF/build/" />
	<mvc:resources mapping="/favicon.ico" location="/WEB-INF/assets/favicon.ico" />
	<mvc:resources mapping="/robots.txt" location="/WEB-INF/assets/robots.txt" />
	
	<bean id="templateResolver" 
        class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML" />
        <property name="characterEncoding" value="UTF-8"/>
        <!-- Template cache is set to false (default is true).        -->
        <property name="cacheable" value="${template.cacheable}" />
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>
    
    <!-- Simple strategy: only path extension is taken into account -->
    <bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="true"/>
        <property name="ignoreAcceptHeader" value="true"/>
        <property name="defaultContentType" value="text/html"/>
        
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html" />
                <entry key="xml" value="application/xml" />
                <entry key="atom" value="application/atom+xml" />
            </map>
        </property>
    </bean>
    
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager" ref="cnManager"/>
        <property name="viewResolvers">
        	<list>
                <!-- Used here for 'xml' and 'atom' views  -->
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
                    <property name="order" value="1"/>
                </bean>
                <!-- thymeleaf -->
                <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
                    <property name="templateEngine" ref="templateEngine" />
                    <property name="characterEncoding" value="UTF-8"/>
                    <property name="order" value="2"/>
                    <property name="excludedViewNames" value="*.xml" />
                </bean>

        	</list>
        </property>
    </bean>

	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="UTF-8" />
		<property name="maxUploadSize" value="10485760" />
	</bean>

	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
	</bean>

	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
			</list>
		</property>
	</bean>

	<bean id="exceptionResolver" class="com.goafanti.common.error.SystemExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="org.apache.shiro.authc.UnknownAccountException">login</prop>
				<prop key="org.apache.shiro.authc.LockedAccountException">login</prop>
				<prop key="org.apache.shiro.authc.ExcessiveAttemptsException">login</prop>
				<prop key="org.apache.shiro.authc.IncorrectCredentialsException">login</prop>
				<prop key="org.apache.shiro.session.ExpiredSessionException">login</prop>
				<prop key="java.lang.Exception">error</prop>
			</props>
		</property>
	</bean>
	<bean id="shiroFilterUtils" class="com.goafanti.core.shiro.filter.ShiroFilterUtils" scope="singleton">
		<property name="appName" value="${app.name}" />
	</bean>
	<bean id="systemWebSocketHandler" class="com.goafanti.core.websocket.SystemWebSocketHandler" scope="singleton"></bean>
	
</beans>