<?xml version="1.0"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	 version="3.0">

  <display-name>Apache Jena Fuseki Server</display-name>

  <listener>
    <!-- Basic server initialization, including logging -->
    <listener-class>org.apache.jena.fuseki.webapp.FusekiServerEnvironmentInit</listener-class>
  </listener>

  <!-- Apache Shiro setup -->
  <listener>
    <!-- This replaces org.apache.shiro.web.env.EnvironmentLoaderListener
         Support multiple locations and looks in Fuseki-relevant places.
     -->
    <listener-class>org.apache.jena.fuseki.webapp.ShiroEnvironmentLoader</listener-class>
  </listener>
  
  <!-- 
  	Disable Shiro automatic initialization. This allows us to order the listeners. 
  -->
  <context-param>
    <param-name>org.apache.shiro.ee.disabled</param-name>
    <param-value>true</param-value>
  </context-param>
  
  <!-- Apache Jena Fuseki setup -->
  <listener>
    <listener-class>org.apache.jena.fuseki.webapp.FusekiServerListener</listener-class>
  </listener>

  <!-- CORS -->
  <filter>
    <filter-name>cross-origin</filter-name>
    <!-- Ported and standalone version of org.eclipse.jetty.servlets.CrossOriginFilter -->
    <filter-class>org.apache.jena.fuseki.servlets.CrossOriginFilter</filter-class>
    <!-- Defaults may be fine --> 
    <init-param>
      <param-name>allowedOrigins</param-name>
      <param-value>*</param-value>
    </init-param>
    <init-param>
      <param-name>allowedMethods</param-name>
      <param-value>GET,POST,DELETE,PUT,HEAD,OPTIONS,PATCH</param-value>
    </init-param>
    <init-param>
      <param-name>allowedHeaders</param-name>
      <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified, Authorization</param-value>
    </init-param>
    <init-param>
      <param-name>exposedHeaders</param-name>
      <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param>
    <init-param>
      <param-name>chainPreflight</param-name>
      <param-value>false</param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- Before FusekiFilter -->
  <filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>

  <context-param>
    <param-name>shiroConfigLocations</param-name>
    <!-- Try a path name in: FUSEKI_BASE, FUSEKI_HOME, war resource
	 If a "file:" then look there and there only.
    -->
    <param-value>shiro.ini</param-value>
  </context-param>

  <context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>log4j2.properties</param-value>
  </context-param>
  
  <!-- Fuseki dataset serviced calls -->
  <filter>
    <filter-name>Fuseki</filter-name>
    <filter-class>org.apache.jena.fuseki.servlets.FusekiFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>Fuseki</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>

  <!-- Server controls-->
  
  <servlet>
    <servlet-name>PingServlet</servlet-name>
    <servlet-class>org.apache.jena.fuseki.ctl.ActionPing</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>PingServlet</servlet-name>
    <url-pattern>/$/ping</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>ActionStats</servlet-name>
    <servlet-class>org.apache.jena.fuseki.ctl.ActionStats</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ActionStats</servlet-name>
    <url-pattern>/$/stats/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>ActionMetrics</servlet-name>
    <servlet-class>org.apache.jena.fuseki.ctl.ActionMetrics</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ActionMetrics</servlet-name>
    <url-pattern>/$/metrics</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>ActionTasks</servlet-name>
    <servlet-class>org.apache.jena.fuseki.ctl.ActionTasks</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ActionTasks</servlet-name>
    <url-pattern>/$/tasks/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>ActionBackup</servlet-name>
    <servlet-class>org.apache.jena.fuseki.mgt.ActionBackup</servlet-class>
  </servlet>

  <servlet>
    <servlet-name>BackupListServlet</servlet-name>
    <servlet-class>org.apache.jena.fuseki.mgt.ActionBackupList</servlet-class>
  </servlet>
  
  <servlet>
    <servlet-name>ActionCompact</servlet-name>
    <servlet-class>org.apache.jena.fuseki.ctl.ActionCompact</servlet-class>
  </servlet>

  <!-- An action that only creates a background task that sleeps. -->
  <servlet>
    <servlet-name>ActionSleep</servlet-name>
    <servlet-class>org.apache.jena.fuseki.ctl.ActionSleep</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ActionSleep</servlet-name>
    <url-pattern>/$/sleep/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>ActionBackup</servlet-name>
    <url-pattern>/$/backup/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>ActionBackup</servlet-name>
    <url-pattern>/$/backups/*</url-pattern>         <!-- Alt spelling -->
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>BackupListServlet</servlet-name>
    <url-pattern>/$/backups-list</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>ActionCompact</servlet-name>
    <url-pattern>/$/compact/*</url-pattern>
  </servlet-mapping>

  <!-- Admin controls-->

  <servlet>
    <servlet-name>ServerStatusServlet</servlet-name>
    <servlet-class>org.apache.jena.fuseki.mgt.ActionServerStatus</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ServerStatusServlet</servlet-name>
    <url-pattern>/$/server</url-pattern>
  </servlet-mapping>

  <!-- Alternative name mentioned in default shiro.ini -->
  <servlet-mapping>
    <servlet-name>ServerStatusServlet</servlet-name>
    <url-pattern>/$/status</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>ActionDatasets</servlet-name>
    <servlet-class>org.apache.jena.fuseki.mgt.ActionDatasets</servlet-class>
  </servlet>
  
  <servlet>
    <servlet-name>ActionLogs</servlet-name>
    <servlet-class>org.apache.jena.fuseki.mgt.ActionLogs</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ActionDatasets</servlet-name>
    <url-pattern>/$/datasets/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>ActionLogs</servlet-name>
    <url-pattern>/$/logs</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <!-- For serving static files -->

  <mime-mapping>
    <extension>rdf</extension>
    <mime-type>application/rdf+xml</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>ttl</extension>
    <mime-type>text/turtle;charset=utf-8</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>nt</extension>
    <mime-type>application/n-triples</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>nq</extension>
    <mime-type>application/n-quads</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>trig</extension>
    <mime-type>application/trig</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>jsonld</extension>
    <mime-type>application/ld+json</mime-type>
  </mime-mapping>

  <mime-mapping>
    <extension>tr</extension>
    <mime-type>application/rdf+thrift</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>trdf</extension>
    <mime-type>application/rdf+thrift</mime-type>
  </mime-mapping>
  
  <mime-mapping>
    <extension>rq</extension>
    <mime-type>application/sparql-query</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>ru</extension>
    <mime-type>application/sparql-update</mime-type>
  </mime-mapping>

  <mime-mapping>
    <extension>srx</extension>
    <mime-type>application/sparql-results+xml</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>srj</extension>
    <mime-type>application/sparql-results+json</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>srt</extension>
    <mime-type>application/sparql-results+thrift</mime-type>
  </mime-mapping>

</web-app>
