<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE urlrewrite
	PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
	"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite>
	<!--
    Configuration file for UrlRewriteFilter
    http://www.tuckey.org/urlrewrite/
    https://github.com/paultuckey/urlrewritefilter

    Rules are used to rewrite request urls that are sent from the client to the server.
    Outbound rules are used to rewrite responses that are sent back to the client. I.e., they can replace HTML code inside response.encodeUrl().
    -->

	<outbound-rule>
		<note>
			Adding a hash to JavaScript files under assets since they do not contain one by default.
			A hash is necessary in order to enable browser cache busting in case of new or updated assets.
		</note>
		<from>^(assets/(.*)\.js$)</from>
		<run class="com.airlock.iam.login.app.infrastructure.web.SpaCacheBuster"
			method="addCacheBusterHash(java.lang.String,jakarta.servlet.http.HttpServletRequest)"/>
		<to>%{attribute:cacheBust}</to>
	</outbound-rule>

	<rule>
		<note>
			Removing the hash from the JavaScript files under assets since internally they do not contain one.
		</note>
		<from>^/ui/(.*assets/.*)\.(?!min)([^.]+)\.(js)$</from>
		<set type="response-header" name="Cache-Control">public, max-age=31536000</set>
		<to>/ui/$1.js</to>
	</rule>

	<outbound-rule>
		<note>Replacing the styles with the customized CSS if present</note>
		<from>^styles-([^.]+)\.css$</from>
		<run class="com.airlock.iam.login.app.infrastructure.web.SpaCustomStyles" method="replaceStyles(java.lang.String,jakarta.servlet.http.HttpServletRequest)"/>
		<to>%{attribute:customStyles}</to>
	</outbound-rule>

	<rule>
		<note>Login SPA root redirect</note>
		<from>^/ui/$</from>
		<to type="redirect">./app</to>
	</rule>

	<rule>
		<note>
			Cache JavaScript, CSS, fonts and images that have at least a 16 character hash for 1 year.
			Hash detection is a heuristic: files ending with a dot followed by at least 16 characters and one of the given
			file endings will be assumed to be hashed and therefore be cached for one year.
		</note>
		<from>^/ui/.+\.[a-z0-9]{16,}\.(js|css|woff|eot|woff|ttf|png|svg|jpg|jpeg|tiff|gif)$</from>
		<set type="response-header" name="Cache-Control">public, max-age=31536000</set>
	</rule>

	<rule>
		<note>Never Cache anything for MSOFBA-related requests.</note>
		<from>^/public/msofba/.*$</from>
		<set type="response-header" name="Cache-Control">no-cache, no-store</set>
	</rule>

</urlrewrite>
