/*
 * 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.
 */

package com.ibm.odm.ondocker;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class RedirectServlet extends HttpServlet {

    public static final String PROPERTIES_FILE="app.properties";
    public static final String DASHBOARD_KEY="dashboard";
    public static final String REDIRECT_URL_KEY="redirect_url";

    private boolean use_dashboard = false;
    private String redirect_url = null;

    @Override
    public void init() throws ServletException {
          InputStream is = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
        if (null == is)
            throw new ServletException("Could not read the " + PROPERTIES_FILE + " file.");

        try {
            Properties prop = new Properties();
            prop.load(is);
            use_dashboard = Boolean.parseBoolean(prop.getProperty(DASHBOARD_KEY));
            redirect_url = prop.getProperty(REDIRECT_URL_KEY);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServletException("Could not read the " + PROPERTIES_FILE + " file.");
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (use_dashboard) {
              this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        }
        else if (redirect_url != null) {
            String url = generateUrl(request, redirect_url);
            response.sendRedirect(url);
        }
    }

    /**
     * Generate an URL with the right hostname and port.
     * @param request The HttpServletRequest request.
     * @param context the url after the hostname and port (e.g.: /some/url).
     * @return An URL.
     */
    private String generateUrl(HttpServletRequest request, String context) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + context;
    }
}

@@ -2410,27 +2410,203 @@
 ) %7B%0A
+%09
     
-        String 
+String contextRoot = System.getenv(%22ODM_CONTEXT_ROOT%22);%0A%09    String url;%0A            if ( contextRoot != null)%7B%0A%09    %09url = generateUrl(request, contextRoot + redirect_url);%0A%09    %7D else %7B%0A%09%09
 url 
@@ -2639,24 +2639,31 @@
 irect_url);%0A
+%09    %7D%0A
             

add context-root to welcome page redirect