/*
 *  Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. 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 org.wso2.sample.is.sso.agent;

import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.sso.agent.SSOAgentConstants;
import org.wso2.carbon.identity.sso.agent.SSOAgentFilter;
import org.wso2.carbon.identity.sso.agent.bean.SSOAgentConfig;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class SSOAgentSampleFilter extends SSOAgentFilter {

    private static Logger LOGGER = Logger.getLogger("org.wso2.sample.is.sso.agent");

    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private static final String CHARACTER_ENCODING = "UTF-8";
    private static Properties properties;
    protected FilterConfig filterConfig = null;

    static{
        properties = SampleContextEventListener.getProperties();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {

        SSOAgentConfig config = (SSOAgentConfig)filterConfig.getServletContext().
                getAttribute(SSOAgentConstants.CONFIG_BEAN_NAME);
        config.getSAML2().setHttpBinding("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect");

        String idpURL = config.getSAML2().getIdPURL();

        // The configurations are shared between all requests.
        // So we need to clean-up the IDP URL for the current request and then set the tenant domain if needed.
        idpURL = idpURL.replaceAll("/t/.*", "");

        String tenantDomain = null;
        Object tenantDomainAttr = ((HttpServletRequest) servletRequest).getSession().getAttribute("tenantDomain");
        if (tenantDomainAttr != null) {
            tenantDomain = (String) tenantDomainAttr;
        }

        if (StringUtils.isBlank(tenantDomain) || tenantDomain.equals("null")) {
            tenantDomain = servletRequest.getParameter("tenantDomain");
            ((HttpServletRequest)servletRequest).getSession().setAttribute("tenantDomain", tenantDomain);
        }
        
        if(tenantDomain != null){
            idpURL = idpURL + "/t/" + tenantDomain;
            config.getSAML2().setIdPURL(idpURL);
        }

        servletRequest.setAttribute(SSOAgentConstants.CONFIG_BEAN_NAME,config);
        super.doFilter(servletRequest, servletResponse, filterChain);
    }

    @Override
    public void destroy() {

    }
}
