// Path: src/main/java/com/adeptj/runtime/config/Configs.java
// public enum Configs {
// 
//     INSTANCE;
// 
//     /**
//      * This will include system properties as well.
//      */
//     private final Config root;
// 
//     Configs() {
//         this.root = this.loadConf();
//     }
// 
//     public Config root() {
//         return this.root;
//     }
// 
//     public Config main() {
//         return this.root.getConfig(MAIN_CONF_SECTION);
//     }
// 
//     public Config undertow() {
//         return this.main().getConfig(UNDERTOW_CONF_SECTION);
//     }
// 
//     public Config felix() {
//         return this.main().getConfig(FELIX_CONF_SECTION);
//     }
// 
//     public Config common() {
//         return this.main().getConfig(COMMON_CONF_SECTION);
//     }
// 
//     public Config logging() {
//         return this.main().getConfig(LOGGING_CONF_SECTION);
//     }
// 
//     public Config trimou() {
//         return this.main().getConfig(TRIMOU_CONF_SECTION);
//     }
// 
//     public Config resteasy() {
//         return this.main().getConfig(RESTEASY_CONF_SECTION);
//     }
// 
//     private Config loadConf() {
//         Config config;
//         File configFile = Environment.getServerConfPath().toFile();
//         if (configFile.exists()) {
//             // if overwrite.server.conf.file system property is provided, then load the configs from classpath.
//             if (Boolean.getBoolean(SYS_PROP_OVERWRITE_SERVER_CONF)) {
//                 config = ConfigFactory.load(SERVER_CONF_FILE);
//             } else {
//                 config = ConfigFactory.parseFile(configFile)
//                         .withFallback(ConfigFactory.systemProperties())
//                         .resolve();
//             }
//         } else {
//             config = ConfigFactory.load(SERVER_CONF_FILE);
//         }
//         return config;
//     }
// 
//     public static Configs of() {
//         return INSTANCE;
//     }
// 
// }


import com.adeptj.runtime.config.Configs;
import com.typesafe.config.Config;
import io.undertow.Handlers;
import io.undertow.predicate.Predicate;
import io.undertow.predicate.Predicates;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.servlet.handlers.ServletInitialHandler;
/*
###############################################################################
#                                                                             # 
#    Copyright 2016, AdeptJ (http://www.adeptj.com)                           #
#                                                                             #
#    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.                                           #
#                                                                             #
###############################################################################
*/

package com.adeptj.runtime.handler;


/**
 * Handler returned by this HandlerWrapper is invoked before any Servlet handlers are invoked.
 * <p>
 * Here it registers Undertow's ClassPath based ResourceHandler for serving static content.
 * If the Predicate grouping is true then it invokes the non blocking ResourceHandler
 * completely bypassing security handler chain, which is desirable as we don't need security
 * and blocking I/O to kick in while serving static content.
 * <p>
 * Undertow order of precedence to invoke HandlerChainWrapper(s).
 * <p>
 * 1. OuterHandlerChainWrapper - Run after the servlet initial handler, but before any other handlers.
 * These are only run on REQUEST invocations, they are not invoked on a FORWARD or INCLUDE.
 * <p>
 * 2. InnerHandlerChainWrapper - This handler will run after the security handler, but before any other
 * servlet handlers, and will run for every request.
 * <p>
 * 3. InitialHandlerChainWrapper - Run before security and servlet initial handler.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
public final class ServletInitialHandlerWrapper implements HandlerWrapper {

    private static final String RESOURCE_PREFIX = "common.static-resource-prefix";

    private static final String RESOURCE_EXTNS = "common.static-resource-extns";

    private static final String RESOURCE_MGR_PREFIX = "common.resource-mgr-prefix";

    /**
     * Wraps the passed {@link ServletInitialHandler} with a PredicateHandler for serving static content.
     *
     * @param servletInitialHandler the ServletInitialHandler
     * @return PredicateHandler which decides whether to invoke ResourceHandler or pass on the request to
     * next handler in the chain which is ServletInitialHandler.
     * @see ServletInitialHandlerWrapper class header for detailed information.
     */
    @Override
    public HttpHandler wrap(HttpHandler servletInitialHandler) {
// Path: src/main/java/com/adeptj/runtime/config/Configs.java
// public enum Configs {
// 
//     INSTANCE;
// 
//     /**
//      * This will include system properties as well.
//      */
//     private final Config root;
// 
//     Configs() {
//         this.root = this.loadConf();
//     }
// 
//     public Config root() {
//         return this.root;
//     }
// 
//     public Config main() {
//         return this.root.getConfig(MAIN_CONF_SECTION);
//     }
// 
//     public Config undertow() {
//         return this.main().getConfig(UNDERTOW_CONF_SECTION);
//     }
// 
//     public Config felix() {
//         return this.main().getConfig(FELIX_CONF_SECTION);
//     }
// 
//     public Config common() {
//         return this.main().getConfig(COMMON_CONF_SECTION);
//     }
// 
//     public Config logging() {
//         return this.main().getConfig(LOGGING_CONF_SECTION);
//     }
// 
//     public Config trimou() {
//         return this.main().getConfig(TRIMOU_CONF_SECTION);
//     }
// 
//     public Config resteasy() {
//         return this.main().getConfig(RESTEASY_CONF_SECTION);
//     }
// 
//     private Config loadConf() {
//         Config config;
//         File configFile = Environment.getServerConfPath().toFile();
//         if (configFile.exists()) {
//             // if overwrite.server.conf.file system property is provided, then load the configs from classpath.
//             if (Boolean.getBoolean(SYS_PROP_OVERWRITE_SERVER_CONF)) {
//                 config = ConfigFactory.load(SERVER_CONF_FILE);
//             } else {
//                 config = ConfigFactory.parseFile(configFile)
//                         .withFallback(ConfigFactory.systemProperties())
//                         .resolve();
//             }
//         } else {
//             config = ConfigFactory.load(SERVER_CONF_FILE);
//         }
//         return config;
//     }
// 
//     public static Configs of() {
//         return INSTANCE;
//     }
// 
// }


// Path: src/main/java/com/adeptj/runtime/handler/ServletInitialHandlerWrapper.java
import com.adeptj.runtime.config.Configs;
import com.typesafe.config.Config;
import io.undertow.Handlers;
import io.undertow.predicate.Predicate;
import io.undertow.predicate.Predicates;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.servlet.handlers.ServletInitialHandler;

/*
###############################################################################
#                                                                             # 
#    Copyright 2016, AdeptJ (http://www.adeptj.com)                           #
#                                                                             #
#    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.                                           #
#                                                                             #
###############################################################################
*/

package com.adeptj.runtime.handler;


/**
 * Handler returned by this HandlerWrapper is invoked before any Servlet handlers are invoked.
 * <p>
 * Here it registers Undertow's ClassPath based ResourceHandler for serving static content.
 * If the Predicate grouping is true then it invokes the non blocking ResourceHandler
 * completely bypassing security handler chain, which is desirable as we don't need security
 * and blocking I/O to kick in while serving static content.
 * <p>
 * Undertow order of precedence to invoke HandlerChainWrapper(s).
 * <p>
 * 1. OuterHandlerChainWrapper - Run after the servlet initial handler, but before any other handlers.
 * These are only run on REQUEST invocations, they are not invoked on a FORWARD or INCLUDE.
 * <p>
 * 2. InnerHandlerChainWrapper - This handler will run after the security handler, but before any other
 * servlet handlers, and will run for every request.
 * <p>
 * 3. InitialHandlerChainWrapper - Run before security and servlet initial handler.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
public final class ServletInitialHandlerWrapper implements HandlerWrapper {

    private static final String RESOURCE_PREFIX = "common.static-resource-prefix";

    private static final String RESOURCE_EXTNS = "common.static-resource-extns";

    private static final String RESOURCE_MGR_PREFIX = "common.resource-mgr-prefix";

    /**
     * Wraps the passed {@link ServletInitialHandler} with a PredicateHandler for serving static content.
     *
     * @param servletInitialHandler the ServletInitialHandler
     * @return PredicateHandler which decides whether to invoke ResourceHandler or pass on the request to
     * next handler in the chain which is ServletInitialHandler.
     * @see ServletInitialHandlerWrapper class header for detailed information.
     */
    @Override
    public HttpHandler wrap(HttpHandler servletInitialHandler) {
        Config cfg = Configs.of().undertow();