/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* 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 org.wso2.carbon.is.migration.service.v550.migrator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.wso2.carbon.core.util.CryptoUtil;
import org.wso2.carbon.identity.core.migrate.MigrationClientException;
import org.wso2.carbon.is.migration.service.Migrator;
import org.wso2.carbon.is.migration.util.Constant;

import java.io.File;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

/**
 * Migrator class to migrate event publishers
 */
public class EventPublisherMigrator extends Migrator {

    private static final Log log = LogFactory.getLog(EventPublisherMigrator.class);

    @Override
    public void migrate() throws MigrationClientException {

        migrateEventPublishers();
    }

    private void migrateEventPublishers() throws MigrationClientException {

        log.info(Constant.MIGRATION_LOG + "Migration starting on event publisher files. ");
        File publisherPath = readFiles(System.getProperty(Constant.CARBON_HOME) + Constant.EVENT_PUBLISHER_PATH);
        migrateData(publisherPath);
        log.info(Constant.MIGRATION_LOG + "Migrating event publishers was successful. ");
    }

    private void migrateData(File folder) throws MigrationClientException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc;
        try {
            for (File fileEntry : Objects.requireNonNull(folder.listFiles())) {
                builder = documentBuilderFactory.newDocumentBuilder();
                doc = builder.parse(fileEntry);
                XPathFactory xpathFactory = XPathFactory.newInstance();
                XPath xpath = xpathFactory.newXPath();
                NodeList data = getEncryptedPayload(doc, xpath);
                if (data.getLength() > 0) {
                    for (int i = 0; i < data.getLength(); i++) {
                        if (!CryptoUtil.getDefaultCryptoUtil()
                                .base64DecodeAndIsSelfContainedCipherText(data.item(i).getNodeValue())) {
                            byte[] decryptedPassword = CryptoUtil.getDefaultCryptoUtil()
                                    .base64DecodeAndDecrypt(data.item(i).getNodeValue(), "RSA");
                            String newEncryptedPassword = CryptoUtil.getDefaultCryptoUtil()
                                    .encryptAndBase64Encode(decryptedPassword);
                            data.item(i).setNodeValue(newEncryptedPassword);
                        }
                    }

                    Transformer xformer = TransformerFactory.newInstance().newTransformer();
                    xformer.transform(new DOMSource(doc),
                            new StreamResult(new File(fileEntry.getAbsolutePath()).getPath()));
                }
            }

        } catch (Exception e) {
            throw new MigrationClientException(
                    "Error occurred while migrating data in folder : " + folder.getAbsolutePath() + " . ", e);
        }

    }

    private File readFiles(String path) {
        return new File(path);
    }

    private NodeList getEncryptedPayload(Document doc, XPath xpath) throws Exception {

        try {
            XPathExpression expr = xpath
                    .compile("//*[local-name()='property'][@*[local-name()='encrypted'='true']]/text()");
            return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            throw new MigrationClientException(
                    "Error has occurred while retriving the payload from file : " + doc.getDocumentURI(), e);
        }

    }
}



@@ -677,16 +677,60 @@
 rator;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -1034,16 +1034,86 @@
 grator;%0A
+import org.wso2.carbon.is.migration.service.v550.util.EncryptionUtil;%0A
 import o
@@ -3217,190 +3217,20 @@
     
-if (!CryptoUtil.getDefaultCryptoUtil()%0A                                .base64DecodeAndIsSelfContainedCipherText(data.item(i).getNodeValue())) %7B%0A                            byte%5B%5D de
+String newEn
 cryp
@@ -3239,38 +3239,42 @@
 dPassword = 
-C
+Enc
 rypt
-o
+ion
 Util.get
 DefaultCrypt
@@ -3265,87 +3265,25 @@
 .get
-DefaultCryptoUtil()%0A                                    .base64DecodeAndDecrypt
+NewEncryptedValue
 (dat
@@ -3310,15 +3310,8 @@
 ue()
-, %22RSA%22
 );%0A 
@@ -3337,138 +3337,40 @@
     
-  
-  
+if (
 String
- newEncryptedPassword = CryptoUtil.getDefaultCryptoUtil()%0A                                    .encryptAndBase64Encode(de
+Utils.isNotEmpty(newEn
 cryp
@@ -3373,33 +3373,35 @@
 cryptedPassword)
-;
+) %7B
 %0A               
@@ -3480,32 +3480,32 @@
               %7D%0A
+
                 
@@ -3502,33 +3502,32 @@
               %7D%0A
-%0A
                 

Fix PR comment to use utility class