/*
 *  Copyright 2016-2019 Netflix, Inc.
 *
 *     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.netflix.hollow.ui;

import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

public abstract class HollowUIRouter {

    protected final String baseUrlPath;
    protected final VelocityEngine velocityEngine;

    public HollowUIRouter(String baseUrlPath) {
        if(!baseUrlPath.startsWith("/"))
            baseUrlPath = "/" + baseUrlPath;
        if(baseUrlPath.endsWith("/"))
            baseUrlPath = baseUrlPath.substring(0, baseUrlPath.length() - 1);

        this.baseUrlPath = baseUrlPath;
        this.velocityEngine = initVelocity();
    }

    public VelocityEngine getVelocityEngine() {
        return velocityEngine;
    }

    public String getBaseURLPath() {
        return baseUrlPath;
    }

    protected String getTargetRootPath(String target) {
        int baseLength = baseUrlPath.length() + 1;

        if(target.length() < baseLength)
            return "";

        int secondSlashIndex = target.indexOf('/', baseLength);

        if(secondSlashIndex == -1)
            return target.substring(baseLength);

        return target.substring(baseLength, secondSlashIndex);
    }

    protected String getResourceName(String target) {
        int baseLength = baseUrlPath.length() + 1;

        int secondSlashIndex = target.indexOf('/', baseLength);
        if(secondSlashIndex == -1)
            return "";
        return target.substring(secondSlashIndex + 1);
    }

    protected boolean serveResource(HttpServletRequest req, HttpServletResponse resp, String resourceName) {
        try {
            if(req.getParameter("contentType") != null) {
                resp.setContentType(resourceName);
            }

            InputStream is = this.getClass().getResourceAsStream("/" + resourceName);

            IOUtils.copy(is, resp.getOutputStream());
            return true;
        } catch(Exception e){
            return false;
        }
    }

    protected VelocityEngine initVelocity() {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
        ve.setProperty("runtime.log.logsystem.log4j.category", "velocity");
        ve.setProperty("runtime.log.logsystem.log4j.logger", "velocity");
        ve.init();
        return ve;
    }

}

@@ -2464,97 +2464,300 @@
 f(re
-q.getParameter(%22contentType%22) != null) %7B%0A                resp.setContentType(resourceName
+sourceName.endsWith(%22.css%22)) %7B%0A                resp.setContentType(%22text/css%22);%0A            %7D else if(resourceName.endsWith(%22.js%22)) %7B%0A                resp.setContentType(%22text/javascript%22);%0A            %7D else if(resourceName.endsWith(%22.png%22)) %7B%0A                resp.setContentType(%22image/png%22
 );%0A 

Fix Content-Type headers for resources in History UI