Search in sources :

Example 76 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class SequenceGenerator method saveApiSequences.

private static void saveApiSequences(APIIdentifier apiIdentifier, String inSequence, String outSequence, String method, String resourcePath) throws APIManagementException {
    boolean isTenantFlowStarted = false;
    try {
        String provider = apiIdentifier.getProviderName();
        String name = apiIdentifier.getName();
        String version = apiIdentifier.getVersion();
        if (provider != null) {
            provider = APIUtil.replaceEmailDomain(provider);
        }
        provider = (provider != null ? provider.trim() : null);
        name = (name != null ? name.trim() : null);
        version = (version != null ? version.trim() : null);
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(provider));
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        }
        RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
        int tenantId;
        UserRegistry registry;
        try {
            tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
            APIUtil.loadTenantRegistry(tenantId);
            registry = registryService.getGovernanceSystemRegistry(tenantId);
            Pattern pattern = Pattern.compile("[{}]");
            Matcher hasSpecialCharacters = pattern.matcher(resourcePath);
            String resourcePathName = resourcePath;
            if (hasSpecialCharacters.find()) {
                resourcePathName = resourcePath.split("[{]")[0];
                if (resourcePathName.endsWith("/")) {
                    resourcePathName = StringUtils.removeEnd(resourcePathName, "/");
                }
            }
            String resourceInPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + provider + RegistryConstants.PATH_SEPARATOR + name + RegistryConstants.PATH_SEPARATOR + version + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SequenceGen.SOAP_TO_REST_IN_RESOURCE + resourcePathName + SOAPToRESTConstants.SequenceGen.RESOURCE_METHOD_SEPERATOR + method + SOAPToRESTConstants.SequenceGen.XML_FILE_EXTENSION;
            String resourceOutPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + provider + RegistryConstants.PATH_SEPARATOR + name + RegistryConstants.PATH_SEPARATOR + version + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SequenceGen.SOAP_TO_REST_OUT_RESOURCE + resourcePathName + SOAPToRESTConstants.SequenceGen.RESOURCE_METHOD_SEPERATOR + method + SOAPToRESTConstants.SequenceGen.XML_FILE_EXTENSION;
            SequenceUtils.saveRestToSoapConvertedSequence(registry, inSequence, method, resourceInPath, resourcePath);
            SequenceUtils.saveRestToSoapConvertedSequence(registry, outSequence, method, resourceOutPath, resourcePath);
        } catch (UserStoreException e) {
            handleException("Error while reading tenant information", e);
        } catch (RegistryException e) {
            handleException("Error while creating registry resource", e);
        } catch (APIManagementException e) {
            handleException("Error while saving the soap to rest converted sequence for resource path: " + resourcePath, e);
        }
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Matcher(java.util.regex.Matcher) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 77 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method addResourceFile.

protected String addResourceFile(String resourcePath, ResourceFile resourceFile, Registry registry, String tenantDomain) throws PersistenceException {
    try {
        Resource thumb = registry.newResource();
        thumb.setContentStream(resourceFile.getContent());
        thumb.setMediaType(resourceFile.getContentType());
        registry.put(resourcePath, thumb);
        if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
            return RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
        } else {
            return "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
        }
    } catch (RegistryException e) {
        String msg = "Error while adding the resource to the registry";
        throw new PersistenceException(msg, e);
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 78 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class APIUtil method loadloadTenantAPIRXT.

/**
 * Load the  API RXT to the registry for tenants
 *
 * @param tenant
 * @param tenantID
 * @throws APIManagementException
 */
public static void loadloadTenantAPIRXT(String tenant, int tenantID) throws APIManagementException {
    RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
    UserRegistry registry = null;
    try {
        registry = registryService.getGovernanceSystemRegistry(tenantID);
    } catch (RegistryException e) {
        throw new APIManagementException("Error when create registry instance ", e);
    }
    String rxtDir = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + "rxts";
    File file = new File(rxtDir);
    FilenameFilter filenameFilter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            // if the file extension is .rxt return true, else false
            return name.endsWith(".rxt");
        }
    };
    String[] rxtFilePaths = file.list(filenameFilter);
    if (rxtFilePaths == null) {
        throw new APIManagementException("rxt files not found in directory " + rxtDir);
    }
    for (String rxtPath : rxtFilePaths) {
        String resourcePath = GovernanceConstants.RXT_CONFIGS_PATH + RegistryConstants.PATH_SEPARATOR + rxtPath;
        // This is  "registry" is a governance registry instance, therefore calculate the relative path to governance.
        String govRelativePath = RegistryUtils.getRelativePathToOriginal(resourcePath, APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH));
        try {
            // calculate resource path
            RegistryAuthorizationManager authorizationManager = new RegistryAuthorizationManager(ServiceReferenceHolder.getUserRealm());
            resourcePath = authorizationManager.computePathOnMount(resourcePath);
            org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
            if (registry.resourceExists(govRelativePath)) {
                // set anonymous user permission to RXTs
                authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
                continue;
            }
            String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
            Resource resource = registry.newResource();
            resource.setContent(rxt.getBytes(Charset.defaultCharset()));
            resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
            registry.put(govRelativePath, resource);
            authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
        } catch (UserStoreException e) {
            throw new APIManagementException("Error while adding role permissions to API", e);
        } catch (IOException e) {
            String msg = "Failed to read rxt files";
            throw new APIManagementException(msg, e);
        } catch (RegistryException e) {
            String msg = "Failed to add rxt to registry ";
            throw new APIManagementException(msg, e);
        }
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) FilenameFilter(java.io.FilenameFilter) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryAuthorizationManager(org.wso2.carbon.registry.core.jdbc.realm.RegistryAuthorizationManager) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File)

Example 79 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class APIUtil method getTenantBasedPublisherDomainMapping.

public static Map getTenantBasedPublisherDomainMapping(String tenantDomain) throws APIManagementException {
    try {
        Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry();
        String resourcePath = APIConstants.API_DOMAIN_MAPPINGS.replace(APIConstants.API_DOMAIN_MAPPING_TENANT_ID_IDENTIFIER, tenantDomain);
        if (registry.resourceExists(resourcePath)) {
            Resource resource = registry.get(resourcePath);
            String content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
            JSONParser parser = new JSONParser();
            JSONObject mappings = (JSONObject) parser.parse(content);
            if (mappings.containsKey(APIConstants.API_DOMAIN_MAPPINGS_PUBLISHER)) {
                return (Map) mappings.get(APIConstants.API_DOMAIN_MAPPINGS_PUBLISHER);
            }
        }
    } catch (RegistryException e) {
        String msg = "Error while retrieving publisher domain mappings from registry";
        throw new APIManagementException(msg, e);
    } catch (ParseException e) {
        String msg = "Malformed JSON found in the publisher tenant domain mappings";
        throw new APIManagementException(msg, e);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) JSONParser(org.json.simple.parser.JSONParser) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) EnumMap(java.util.EnumMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 80 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class APIUtil method getDomainMappings.

/**
 * Returns a map of gateway / store domains for the tenant
 *
 * @return a Map of domain names for tenant
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if an error occurs when loading tiers from the registry
 */
public static Map<String, String> getDomainMappings(String tenantDomain, String appType) throws APIManagementException {
    Map<String, String> domains = new HashMap<String, String>();
    String resourcePath;
    try {
        Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry();
        resourcePath = APIConstants.API_DOMAIN_MAPPINGS.replace(APIConstants.API_DOMAIN_MAPPING_TENANT_ID_IDENTIFIER, tenantDomain);
        if (registry.resourceExists(resourcePath)) {
            Resource resource = registry.get(resourcePath);
            String content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
            JSONParser parser = new JSONParser();
            JSONObject mappings = (JSONObject) parser.parse(content);
            if (mappings.get(appType) != null) {
                mappings = (JSONObject) mappings.get(appType);
                for (Object o : mappings.entrySet()) {
                    Entry thisEntry = (Entry) o;
                    String key = (String) thisEntry.getKey();
                    // to allow users to add multiple URLs if needed
                    if (!StringUtils.isEmpty(key) && key.startsWith(APIConstants.CUSTOM_URL)) {
                        String value = (String) thisEntry.getValue();
                        domains.put(key, value);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Error while retrieving gateway domain mappings from registry";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    } catch (ClassCastException e) {
        String msg = "Invalid JSON found in the gateway tenant domain mappings";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    } catch (ParseException e) {
        String msg = "Malformed JSON found in the gateway tenant domain mappings";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
    return domains;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Entry(java.util.Map.Entry) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONParser(org.json.simple.parser.JSONParser) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)51 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)48 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)44 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)28 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)25 IOException (java.io.IOException)18 Registry (org.wso2.carbon.registry.core.Registry)16 Collection (org.wso2.carbon.registry.core.Collection)15 UserStoreException (org.wso2.carbon.user.api.UserStoreException)14 Test (org.junit.Test)13 Resource (org.wso2.carbon.registry.api.Resource)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 ArrayList (java.util.ArrayList)11 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)11 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)11 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)11 JSONParser (org.json.simple.parser.JSONParser)10 ParseException (org.json.simple.parser.ParseException)10 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)10