<?xml version="1.0"?>
<!--

    Copyright 2021 OPS4J.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<!-- Object named "Server" of org.eclipse.jetty.server.Server class is configured by pax-web-jetty -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <!-- =========================================================== -->
    <!-- Set connectors -->
    <!-- =========================================================== -->
    <!-- One of each type! -->
    <!-- =========================================================== -->

    <!-- Use this connector for many frequently idle connections and for
        threadless continuations. -->
    <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
        <Set name="secureScheme">https</Set>
        <Set name="securePort">
            <Property name="jetty.secure.port" default="8443" />
        </Set>
        <Set name="outputBufferSize">32768</Set>
        <Set name="requestHeaderSize">8192</Set>
        <Set name="responseHeaderSize">8192</Set>
        <Set name="sendServerVersion">false</Set>
        <Set name="sendDateHeader">false</Set>
        <Set name="headerCacheSize">512</Set>
    </New>

    <!-- =========================================================== -->
    <!-- Special server connectors -->
    <!-- =========================================================== -->
    <!-- This is a sample for alternative connectors, enable if needed -->
    <!-- =========================================================== -->
    <!--
	<Call name="addConnector">
		<Arg>
			<New class="org.eclipse.jetty.server.ServerConnector">
				<Arg name="server">
					<Ref refid="Server" />
				</Arg>
				<Arg name="factories">
					<Array type="org.eclipse.jetty.server.ConnectionFactory">
						<Item>
							<New class="org.eclipse.jetty.server.HttpConnectionFactory">
								<Arg name="config">
									<Ref refid="httpConfig" />
								</Arg>
							</New>
						</Item>
					</Array>
				</Arg>
				<Set name="host">
					<Property name="jetty.host" default="localhost" />
				</Set>
				<Set name="port">
					<Property name="jetty.port" default="8282" />
				</Set>
				<Set name="idleTimeout">
					<Property name="http.timeout" default="30000" />
				</Set>
				<Set name="name">jettyConn1</Set>
			</New>
		</Arg>
	</Call>
 	-->

    <!-- =========================================================== -->
    <!-- Configure Authentication Realms -->
    <!-- Realms may be configured for the entire server here, or -->
    <!-- they can be configured for a specific web app in a context -->
    <!-- configuration (see $(jetty.home)/contexts/test.xml for an -->
    <!-- example). -->
    <!-- =========================================================== -->
    <Call name="addBean">
        <Arg>
            <New class="org.eclipse.jetty.jaas.JAASLoginService">
                <Set name="name">karaf</Set>
                <Set name="loginModuleName">karaf</Set>
                <Set name="roleClassNames">
                    <Array type="java.lang.String">
                        <Item>org.apache.karaf.jaas.boot.principal.RolePrincipal</Item>
                    </Array>
                </Set>
            </New>
        </Arg>
    </Call>
    <Call name="addBean">
        <Arg>
            <New class="org.eclipse.jetty.jaas.JAASLoginService">
                <Set name="name">default</Set>
                <Set name="loginModuleName">karaf</Set>
                <Set name="roleClassNames">
                    <Array type="java.lang.String">
                        <Item>org.apache.karaf.jaas.boot.principal.RolePrincipal</Item>
                    </Array>
                </Set>
            </New>
        </Arg>
    </Call>

    <!-- Uncomment these two elements to enable GZIP compression -->
    <!-- https://www.eclipse.org/jetty/javadoc/jetty-9/org/eclipse/jetty/server/handler/gzip/GzipHandler.html -->
    <!--
    <Get id="OrigHandler" name="handler" />

    <Set name="handler">
        <New id="GzipHandler" class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
            <Set name="handler">
                <Ref refid="OrigHandler" />
            </Set>
        </New>
    </Set>
    -->

    <!-- ====================== -->
    <!-- Insert the GzipHandler -->
    <!-- ====================== -->
    <Call name="insertHandler">
        <Arg>
            <New id="GzipHandler" class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
                <Set name="minGzipSize"><Property name="jetty.gzip.minGzipSize" default="1024"/></Set>
                <Set name="checkGzExists"><Property name="jetty.gzip.checkGzExists" default="false"/></Set>
                <Set name="compressionLevel"><Property name="jetty.gzip.compressionLevel" default="-1"/></Set>
                <Set name="inflateBufferSize"><Property name="jetty.gzip.inflateBufferSize" default="0"/></Set>
                <Set name="syncFlush"><Property name="jetty.gzip.syncFlush" default="false" /></Set>
                <Set name="excludedAgentPatterns">
                    <Array type="String">
                        <!-- IE 6 has known bugs related to GZIP compression -->
                        <Item><Property name="jetty.gzip.excludedUserAgent" default=".*MSIE.6\.0.*"/></Item>
                    </Array>
                </Set>
                <!-- <Set name="includedMethodList"><Property name="jetty.gzip.includedMethodList" default="GET" /></Set> -->
                <!-- <Set name="excludedMethodList"><Property name="jetty.gzip.excludedMethodList" default="" /></Set> -->
                <Set name="includedMethods">
                    <Array type="String">
                        <Item>GET</Item>
                        <Item>POST</Item>
                    </Array>
                </Set>
                <!--
                    <Set name="includedPaths">
                        <Array type="String">
                            <Item>/*</Item>
                        </Array>
                    </Set>
                    <Set name="excludedPaths">
                        <Array type="String">
                            <Item>*.gz</Item>
                        </Array>
                    </Set>
                    <Call name="addIncludedMimeTypes">
                        <Arg>
                            <Array type="String">
                                <Item>some/type</Item>
                            </Array>
                        </Arg>
                    </Call>
                    <Call name="addExcludedMimeTypes">
                        <Arg>
                            <Array type="String">
                                <Item>some/type</Item>
                            </Array>
                        </Arg>
                    </Call>
                -->
            </New>
        </Arg>
    </Call>

    <!-- ==================== -->
    <!-- Set Response Headers -->
    <!-- ==================== -->
    <Call name="insertHandler">
        <Arg>
            <New id="RewriteHandler" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
                <Set name="rules">
                    <Array type="org.eclipse.jetty.rewrite.handler.Rule">
                        <Item>
                            <New id="header1" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">X-XSS-Protection</Set>
                                <Set name="value">1; mode=block</Set>
                            </New>
                        </Item>
                        <Item>
                            <New id="header2" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">X-DNS-Prefetch-Control</Set>
                                <Set name="value">off</Set>
                            </New>
                        </Item>
                        <Item>
                            <New id="header3" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">X-Content-Type-Options</Set>
                                <Set name="value">nosniff</Set>
                            </New>
                        </Item>
                        <Item>
                            <New id="header4" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">X-Frame-Options</Set>
                                <Set name="value">sameorigin</Set>
                            </New>
                        </Item>
                        <Item>
                            <New id="header5" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">Strict-Transport-Security</Set>
                                <Set name="value">max-age=15768000; includeSubDomains</Set>
                            </New>
                        </Item>
                        <!-- Cache-Control -->
                        <Item>
                            <New id="header6" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">no-store, no-cache, must-revalidate</Set><!-- browser should never ever cache -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header7" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/websocket</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">no-store, no-cache, must-revalidate</Set><!-- browser should never ever cache -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header8" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/cxf/*</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">no-store, no-cache, must-revalidate</Set><!-- browser should never ever cache -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header9" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.js</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">no-store, no-cache, must-revalidate</Set><!-- browser should never ever cache -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header10" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.css</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">no-store, no-cache, must-revalidate</Set><!-- browser should never ever cache -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header11" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.json</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">no-store, no-cache, must-revalidate</Set><!-- browser should never ever cache -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header12" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.jpg</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">public, max-age=604803</Set><!-- browser should cache for 1 week -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header13" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.jpeg</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">public, max-age=604804</Set><!-- browser should cache for 1 week -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header14" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.png</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">public, max-age=604805</Set><!-- browser should cache for 1 week -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header15" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.gif</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">public, max-age=604806</Set><!-- browser should cache for 1 week -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header16" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.svg</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">public, max-age=604807</Set><!-- browser should cache for 1 week -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header17" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.ttf</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">public, max-age=604808</Set><!-- browser should cache for 1 week -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header18" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">*.woff2</Set>
                                <Set name="name">Cache-Control</Set>
                                <Set name="value">public, max-age=604809</Set><!-- browser should cache for 1 week -->
                            </New>
                        </Item>
                        <Item>
                            <New id="header19" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">Referrer-Policy</Set>
                                <Set name="value">strict-origin</Set>
                            </New>
                        </Item>
                        <Item>
                            <New id="header-x-content-type-options" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">X-Content-Type-Options</Set>
                                <Set name="value">nosniff</Set>
                            </New>
                        </Item>
                        <Item>
                            <New id="header-csp" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                                <Set name="pattern">/*</Set>
                                <Set name="name">Content-Security-Policy</Set>
                                <Set name="value">script-src 'self' 'unsafe-eval'; connect-src 'self'; img-src 'self' data: blob:; style-src 'self' 'unsafe-inline'; font-src 'self'; frame-ancestors 'self'; form-action 'self'; worker-src 'self' blob: ;</Set>
                            </New>
                        </Item>
                    </Array>
                </Set>
            </New>
        </Arg>
    </Call>

</Configure>
