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();
}
}
}
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);
}
}
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);
}
}
}
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;
}
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;
}
Aggregations