/*
 *  Copyright WSO2 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 org.wso2.carbon.apimgt.impl;

import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.nio.charset.Charset;

public class APIMRegistryServiceImpl implements APIMRegistryService {


    @Override
    public String getConfigRegistryResourceContent(String tenantDomain, final String registryLocation)
                                        throws UserStoreException, RegistryException {
        String content = null;
        if (tenantDomain == null) {
            tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
        }

        try {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);

            int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
            Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getConfigSystemRegistry(tenantId);
            APIUtil.loadTenantRegistry(tenantId);

            if (registry.resourceExists(registryLocation)) {
                Resource resource = registry.get(registryLocation);
                content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
            }
        } finally {
            PrivilegedCarbonContext.endTenantFlow();
        }

        return content;
    }

    @Override
    public String getGovernanceRegistryResourceContent(String tenantDomain, String registryLocation)
                                        throws UserStoreException, RegistryException {
        String content = null;
        if (tenantDomain == null) {
            tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
        }

        try {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);

            int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
            Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);

            if (registry.resourceExists(registryLocation)) {
                Resource resource = registry.get(registryLocation);
                content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
            }
        }
        finally {
            PrivilegedCarbonContext.endTenantFlow();
        }

        return content;
    }
}

@@ -630,16 +630,159 @@
 .impl;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0Aimport org.wso2.carbon.apimgt.api.APIManagementException;%0A
 import o
@@ -841,16 +841,16 @@
 Holder;%0A
-
 import o
@@ -1330,16 +1330,101 @@
 vice %7B%0A%0A
+    private static final Log log = LogFactory.getLog(APIMRegistryServiceImpl.class);%0A
 %0A    @Ov
@@ -2239,32 +2239,183 @@
 istry(tenantId);
+%0A            if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) %7B%0A                APIUtil.loadTenantConf(tenantId);%0A            %7D
 %0A%0A            if
@@ -2617,32 +2617,32 @@
 aultCharset());%0A
-
             %7D%0A  
@@ -2631,32 +2631,181 @@
 ;%0A            %7D%0A
+        %7D catch (APIManagementException e) %7B%0A            log.error(%22Error occurred while loading tenant configuration for '%22 + tenantDomain + %22'%22);%0A%0A
         %7D finall

fix NPE in generating token to Invoke Rest API (APIMANAGER-5666)