/*
 * Copyright (c) 2005-2006, 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.carbon.identity.provider;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.rahas.RahasData;
import org.w3c.dom.Element;
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.utils.ServerConstants;
import org.wso2.carbon.utils.WSO2Constants;

import javax.xml.namespace.QName;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Hashtable;
import java.util.Map;

public class IdentityProviderUtil {

    private static boolean intial;

    private IdentityProviderUtil() {
    }

    public static boolean isIntial() {
        return intial;
    }

    public static void setIntial(boolean intial) {
        IdentityProviderUtil.intial = intial;
    }

    public static OMElement createRequestedDisplayToken(OMElement parent, GenericIdentityProviderData data) {

        return createOMElement(parent, IdentityConstants.NS,
                               IdentityConstants.LocalNames.REQUESTED_DISPLAY_TOKEN, IdentityConstants.PREFIX);
    }

    public static OMElement createDisplayToken(OMElement parent, GenericIdentityProviderData data) {

        return createOMElement(parent, IdentityConstants.NS,
                               IdentityConstants.LocalNames.DISPLAY_TOKEN, IdentityConstants.PREFIX);
    }

    public static OMElement createDisplayClaim(OMElement parent, String displayTag,
                                               String displayValue, String uri) {
        OMElement claimElem = createOMElement(parent, IdentityConstants.NS,
                                              IdentityConstants.LocalNames.DISPLAY_CLAIM, IdentityConstants.PREFIX);

        claimElem.addAttribute("Uri", uri, null);

        OMElement tagElem = createOMElement(claimElem, IdentityConstants.NS,
                                            IdentityConstants.LocalNames.DISPLAY_TAG, IdentityConstants.PREFIX);
        tagElem.setText(displayTag);

        OMElement valElem = createOMElement(claimElem, IdentityConstants.NS,
                                            IdentityConstants.LocalNames.DISPLAY_VALUE, IdentityConstants.PREFIX);
        valElem.setText(displayValue);

        return claimElem;
    }

    public static OMElement createOpenIdToken(OMElement parent, GenericIdentityProviderData data) {

        return createOMElement(parent, IdentityConstants.OpenId.OPENID_URL,
                               IdentityConstants.LocalNames.OPEN_ID_TOKEN, IdentityConstants.OpenId.PREFIX);
    }

    private static OMElement createOMElement(OMElement parent, String ns, String ln, String prefix) {

        return parent.getOMFactory().createOMElement(new QName(ns, ln, prefix), parent);

    }

    public static String dumpInfoCard(ConfigurationContext configurationContext, Element content)
            throws IdentityProviderException {
        Map fileResourcesMap = null;
        String workdir = null;
        String uuid = null;
        String filePath = null;

        workdir = (String) configurationContext.getProperty(ServerConstants.WORK_DIR);
        uuid = String.valueOf(System.currentTimeMillis() + Math.random()) + ".crd";

        filePath = workdir + File.separator + "dump_cards" + File.separator + uuid;

        try (OutputStream outStream = new FileOutputStream(filePath);) {
            try {
                TransformerFactory.newInstance().newTransformer().transform(new DOMSource(content),
                                                                            new StreamResult(outStream));
            } catch (TransformerException e) {
                throw new IdentityProviderException("Failed to transform XML content.", e);
            }
        } catch (FileNotFoundException e) {
            throw new IdentityProviderException("Invalid file path.", e);
        } catch (IOException e) {
            throw new IdentityProviderException("Failed to write to the file path.", e);
        }

        fileResourcesMap = (Map) configurationContext.getProperty(WSO2Constants.FILE_RESOURCE_MAP);
        if (fileResourcesMap == null) {
            fileResourcesMap = new Hashtable();
            configurationContext.setProperty(WSO2Constants.FILE_RESOURCE_MAP, fileResourcesMap);
        }
        fileResourcesMap.put(uuid, filePath);
        return WSO2Constants.ContextPaths.DOWNLOAD_PATH + "?id=" + uuid;
    }

    /**
     * Obtain the applies to host name from the WS-Trust request.
     *
     * @param data Data from WS-Trust request.
     * @return applies to host name if found.
     * @throws IdentityProviderException
     */
    public static String getAppliesToHostName(RahasData data) throws IdentityProviderException {
        // If there's no applies to then we don't have to encrypt
        if (data.getAppliesToEpr() == null) {
            return null;
        }

        String relyingPartyURI = data.getAppliesToAddress();

        if (relyingPartyURI == null) {
            // Addressing policy not used in the policy
            relyingPartyURI = data.getAppliesToEpr().getText();
            if (relyingPartyURI == null) {
                throw new IdentityProviderException("cannotFindRelyingParty");
            }
        }

        URI uri = null;
        try {
            // To get the host name extracted
            uri = new URI(relyingPartyURI);
        } catch (URISyntaxException e) {
            throw new IdentityProviderException("Invalid Uril", e);
        }
        return uri.getHost();
    }
}

@@ -3753,1662 +3753,8 @@
  %7D%0A%0A
-    public static String dumpInfoCard(ConfigurationContext configurationContext, Element content)%0A            throws IdentityProviderException %7B%0A        Map fileResourcesMap = null;%0A        String workdir = null;%0A        String uuid = null;%0A        String filePath = null;%0A%0A        workdir = (String) configurationContext.getProperty(ServerConstants.WORK_DIR);%0A        uuid = String.valueOf(System.currentTimeMillis() + Math.random()) + %22.crd%22;%0A%0A        filePath = workdir + File.separator + %22dump_cards%22 + File.separator + uuid;%0A%0A        try (OutputStream outStream = new FileOutputStream(filePath);) %7B%0A            try %7B%0A                TransformerFactory.newInstance().newTransformer().transform(new DOMSource(content),%0A                                                                            new StreamResult(outStream));%0A            %7D catch (TransformerException e) %7B%0A                throw new IdentityProviderException(%22Failed to transform XML content.%22, e);%0A            %7D%0A        %7D catch (FileNotFoundException e) %7B%0A            throw new IdentityProviderException(%22Invalid file path.%22, e);%0A        %7D catch (IOException e) %7B%0A            throw new IdentityProviderException(%22Failed to write to the file path.%22, e);%0A        %7D%0A%0A        fileResourcesMap = (Map) configurationContext.getProperty(WSO2Constants.FILE_RESOURCE_MAP);%0A        if (fileResourcesMap == null) %7B%0A            fileResourcesMap = new Hashtable();%0A            configurationContext.setProperty(WSO2Constants.FILE_RESOURCE_MAP, fileResourcesMap);%0A        %7D%0A        fileResourcesMap.put(uuid, filePath);%0A        return WSO2Constants.ContextPaths.DOWNLOAD_PATH + %22?id=%22 + uuid;%0A    %7D%0A%0A
     

remove unnecessary dumpInfoCard method