Search in sources :

Example 41 with ResourcePath

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;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) XMLStreamException(javax.xml.stream.XMLStreamException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Collection(org.wso2.carbon.registry.core.Collection) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 42 with ResourcePath

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();
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) StringReader(java.io.StringReader) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 43 with ResourcePath

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();
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) StringReader(java.io.StringReader) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 44 with ResourcePath

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/"));
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 45 with ResourcePath

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);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Wsdl(org.wso2.carbon.apimgt.api.model.Wsdl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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