use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class AbstractAPIManager method getAllGlobalMediationPolicies.
/**
* Returns list of global mediation policies available
*
* @return List of Mediation objects of global mediation policies
* @throws APIManagementException If failed to get global mediation policies
*/
@Override
public List<Mediation> getAllGlobalMediationPolicies() throws APIManagementException {
List<Mediation> mediationList = new ArrayList<Mediation>();
Mediation mediation;
String resourcePath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION;
try {
// Resource : customsequences
Resource resource = registry.get(resourcePath);
if (resource instanceof Collection) {
Collection typeCollection = (Collection) resource;
String[] typeArray = typeCollection.getChildren();
for (String type : typeArray) {
// Resource : in / out / fault
Resource typeResource = registry.get(type);
if (typeResource instanceof Collection) {
String[] sequenceArray = ((Collection) typeResource).getChildren();
if (sequenceArray.length > 0) {
for (String sequence : sequenceArray) {
// Resource : actual resource eg : log_in_msg.xml
Resource sequenceResource = registry.get(sequence);
String resourceId = sequenceResource.getUUID();
try {
String contentString = IOUtils.toString(sequenceResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
OMElement omElement = AXIOMUtil.stringToOM(contentString);
OMAttribute attribute = omElement.getAttribute(new QName(PolicyConstants.MEDIATION_NAME_ATTRIBUTE));
String mediationPolicyName = attribute.getAttributeValue();
mediation = new Mediation();
mediation.setUuid(resourceId);
mediation.setName(mediationPolicyName);
// Extract sequence type from the registry resource path
String resourceType = type.substring(type.lastIndexOf("/") + 1);
mediation.setType(resourceType);
// Add mediation to the mediation list
mediationList.add(mediation);
} catch (XMLStreamException e) {
// If any exception been caught flow may continue with the next mediation policy
log.error("Error occurred while getting omElement out of " + "mediation content from " + sequence, e);
} catch (IOException e) {
log.error("Error occurred while converting resource " + "contentStream in to string in " + sequence, e);
}
}
}
}
}
}
} catch (RegistryException e) {
String msg = "Failed to get global mediation policies";
throw new APIManagementException(msg, e);
}
return mediationList;
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class APIMConfigServiceImpl method addGAConfig.
@Override
public void addGAConfig(String organization, String gaConfig) throws APIManagementException {
if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (!registry.resourceExists(APIConstants.GA_CONFIGURATION_LOCATION)) {
byte[] data = IOUtils.toByteArray(new StringReader(gaConfig));
Resource resource = registry.newResource();
resource.setContent(data);
resource.setMediaType(APIConstants.GA_CONF_MEDIA_TYPE);
registry.put(APIConstants.GA_CONFIGURATION_LOCATION, resource);
/*set resource permission*/
org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getAuthorizationManager();
String resourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + APIConstants.GA_CONFIGURATION_LOCATION);
authManager.denyRole(APIConstants.EVERYONE_ROLE, resourcePath, ActionConstants.GET);
}
} catch (RegistryException | IOException | UserStoreException e) {
String msg = "Error while add Google Analytics Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class APIMConfigServiceImpl method addExternalStoreConfig.
@Override
public void addExternalStoreConfig(String organization, String externalStoreConfig) throws APIManagementException {
if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getAuthorizationManager();
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (!registry.resourceExists(APIConstants.EXTERNAL_API_STORES_LOCATION)) {
Resource resource = registry.newResource();
resource.setContent(IOUtils.toByteArray(new StringReader(externalStoreConfig)));
registry.put(APIConstants.EXTERNAL_API_STORES_LOCATION, resource);
String resourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + APIConstants.EXTERNAL_API_STORES_LOCATION);
authManager.denyRole(APIConstants.EVERYONE_ROLE, resourcePath, ActionConstants.GET);
}
} catch (RegistryException | IOException | UserStoreException e) {
String msg = "Error while adding External Stores Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testAddResourceFile.
@Test
public void testAddResourceFile() throws APIManagementException, RegistryException, IOException {
Identifier identifier = Mockito.mock(Identifier.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
Resource resource = new ResourceImpl();
Mockito.when(registry.newResource()).thenReturn(resource);
String resourcePath = "/test";
String contentType = "sampleType";
ResourceFile resourceFile = new ResourceFile(null, contentType);
try {
abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while adding the resource to the registry"));
}
InputStream in = IOUtils.toInputStream("sample content", "UTF-8");
resourceFile = new ResourceFile(in, contentType);
String returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
Assert.assertTrue(returnedPath.contains(resourcePath) && returnedPath.contains("/t/"));
abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
Assert.assertTrue(returnedPath.contains(resourcePath) && !returnedPath.contains("/t/"));
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetWsdlById.
@Test
public void testGetWsdlById() throws RegistryException, APIManagementException, IOException {
String resourceId = SAMPLE_RESOURCE_ID;
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
Collection parentCollection = new CollectionImpl();
String wsdlResourcepath = APIConstants.API_WSDL_RESOURCE;
String resourcePath = wsdlResourcepath + "/wsdl1";
parentCollection.setChildren(new String[] { resourcePath });
Mockito.when(registry.get(wsdlResourcepath)).thenReturn(parentCollection);
Resource resource = new ResourceImpl(resourcePath, new ResourceDO());
Mockito.when(registry.get(resourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
Mockito.when(registry.resourceExists(wsdlResourcepath)).thenReturn(false, true);
Assert.assertNull(abstractAPIManager.getWsdlById(resourceId));
resource.setUUID(resourceId);
try {
abstractAPIManager.getWsdlById(resourceId);
Assert.fail("Exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while accessing registry objects"));
}
String wsdlContent = "sample wsdl";
resource.setContent(wsdlContent);
Wsdl wsdl = abstractAPIManager.getWsdlById(resourceId);
Assert.assertNotNull(wsdl);
PowerMockito.mockStatic(IOUtils.class);
PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class);
// covers logged only exception;
abstractAPIManager.getWsdlById(resourceId);
}
Aggregations