Search in sources :

Example 1 with ResourceType

use of org.wso2.carbon.identity.configuration.mgt.core.model.ResourceType in project carbon-apimgt by wso2.

the class AbstractAPIManager method getApiSpecificMediationPolicy.

/**
 * Returns Mediation policy specify by given identifier
 *
 * @param identifier        API or Product identifier
 * @param apiResourcePath   registry path to the API resource
 * @param mediationPolicyId mediation policy identifier
 * @return Mediation object contains details of the mediation policy or null
 */
@Override
public Mediation getApiSpecificMediationPolicy(Identifier identifier, String apiResourcePath, String mediationPolicyId) throws APIManagementException {
    // Get registry resource correspond to given policy identifier
    Resource mediationResource = getApiSpecificMediationResourceFromUuid(identifier, mediationPolicyId, apiResourcePath);
    Mediation mediation = null;
    if (mediationResource != null) {
        try {
            // Get mediation policy config content
            String contentString = IOUtils.toString(mediationResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
            // Extracting name specified in the mediation config
            OMElement omElement = AXIOMUtil.stringToOM(contentString);
            OMAttribute attribute = omElement.getAttribute(new QName("name"));
            String mediationPolicyName = attribute.getAttributeValue();
            mediation = new Mediation();
            mediation.setUuid(mediationResource.getUUID());
            mediation.setName(mediationPolicyName);
            // Extracting mediation policy type from registry path
            String resourcePath = mediationResource.getPath();
            String[] path = resourcePath.split(RegistryConstants.PATH_SEPARATOR);
            String resourceType = path[(path.length - 2)];
            mediation.setType(resourceType);
            mediation.setConfig(contentString);
        } catch (XMLStreamException e) {
            String errorMsg = "Error occurred while getting omElement out of mediation content";
            throw new APIManagementException(errorMsg, e);
        } catch (IOException e) {
            String errorMsg = "Error occurred while converting content stream into string ";
            throw new APIManagementException(errorMsg, e);
        } catch (RegistryException e) {
            String errorMsg = "Error occurred while accessing content stream of mediation" + " policy";
            throw new APIManagementException(errorMsg, e);
        }
    }
    return mediation;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) QName(javax.xml.namespace.QName) 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) OMAttribute(org.apache.axiom.om.OMAttribute) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 2 with ResourceType

use of org.wso2.carbon.identity.configuration.mgt.core.model.ResourceType in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getAllMediationPolicies.

@Override
public List<MediationInfo> getAllMediationPolicies(Organization org, String apiId) throws MediationPolicyPersistenceException {
    boolean isTenantFlowStarted = false;
    List<MediationInfo> mediationList = new ArrayList<MediationInfo>();
    MediationInfo mediation;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        BasicAPI api = getbasicAPIInfo(apiId, registry);
        if (api == null) {
            throw new MediationPolicyPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
        }
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiResourcePath = apiPath.substring(0, prependIndex);
        // apiResourcePath = apiResourcePath.substring(0, apiResourcePath.lastIndexOf("/"));
        // Getting API registry resource
        Resource resource = registry.get(apiResourcePath);
        // resource eg: /_system/governance/apimgt/applicationdata/provider/admin/calculatorAPI/2.0
        if (resource instanceof Collection) {
            Collection typeCollection = (Collection) resource;
            String[] typeArray = typeCollection.getChildren();
            for (String type : typeArray) {
                // Check for mediation policy sequences
                if ((type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN)) || (type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT)) || (type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT))) {
                    Resource typeResource = registry.get(type);
                    // typeResource : in / out / fault
                    if (typeResource instanceof Collection) {
                        String[] mediationPolicyArr = ((Collection) typeResource).getChildren();
                        if (mediationPolicyArr.length > 0) {
                            for (String mediationPolicy : mediationPolicyArr) {
                                Resource policyResource = registry.get(mediationPolicy);
                                // policyResource eg: custom_in_message
                                // Get uuid of the registry resource
                                String resourceId = policyResource.getUUID();
                                // Get mediation policy config
                                try {
                                    String contentString = IOUtils.toString(policyResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
                                    // Extract name from the policy config
                                    OMElement omElement = AXIOMUtil.stringToOM(contentString);
                                    OMAttribute attribute = omElement.getAttribute(new QName("name"));
                                    String mediationPolicyName = attribute.getAttributeValue();
                                    mediation = new MediationInfo();
                                    mediation.setId(resourceId);
                                    mediation.setName(mediationPolicyName);
                                    // Extracting mediation policy type from the registry resource path
                                    String resourceType = type.substring(type.lastIndexOf("/") + 1);
                                    mediation.setType(resourceType);
                                    mediationList.add(mediation);
                                } catch (XMLStreamException e) {
                                    // If exception been caught flow will continue with next mediation policy
                                    log.error("Error occurred while getting omElement out of" + " mediation content", e);
                                } catch (IOException e) {
                                    log.error("Error occurred while converting the content " + "stream of mediation " + mediationPolicy + " to string", e);
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error occurred  while getting Api Specific mediation policies ";
        throw new MediationPolicyPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return mediationList;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) OMElement(org.apache.axiom.om.OMElement) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) XMLStreamException(javax.xml.stream.XMLStreamException) MediationInfo(org.wso2.carbon.apimgt.persistence.dto.MediationInfo) Collection(org.wso2.carbon.registry.core.Collection) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 3 with ResourceType

use of org.wso2.carbon.identity.configuration.mgt.core.model.ResourceType in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getMediationPolicy.

@Override
public Mediation getMediationPolicy(Organization org, String apiId, String mediationPolicyId) throws MediationPolicyPersistenceException {
    boolean isTenantFlowStarted = false;
    Mediation mediation = null;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        BasicAPI api = getbasicAPIInfo(apiId, registry);
        if (api == null) {
            throw new MediationPolicyPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
        }
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiResourcePath = apiPath.substring(0, prependIndex);
        String policyPath = GovernanceUtils.getArtifactPath(registry, mediationPolicyId);
        if (!policyPath.startsWith(apiResourcePath)) {
            throw new MediationPolicyPersistenceException("Policy not foud ", ExceptionCodes.POLICY_NOT_FOUND);
        }
        Resource mediationResource = registry.get(policyPath);
        if (mediationResource != null) {
            String contentString = IOUtils.toString(mediationResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
            // Extracting name specified in the mediation config
            OMElement omElement = AXIOMUtil.stringToOM(contentString);
            OMAttribute attribute = omElement.getAttribute(new QName("name"));
            String mediationPolicyName = attribute.getAttributeValue();
            String[] path = policyPath.split(RegistryConstants.PATH_SEPARATOR);
            String resourceType = path[(path.length - 2)];
            mediation = new Mediation();
            mediation.setConfig(contentString);
            mediation.setType(resourceType);
            mediation.setId(mediationResource.getUUID());
            mediation.setName(mediationPolicyName);
        }
    } catch (RegistryException | APIPersistenceException | IOException | XMLStreamException e) {
        String msg = "Error occurred  while getting Api Specific mediation policies ";
        throw new MediationPolicyPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return mediation;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) OMElement(org.apache.axiom.om.OMElement) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) Mediation(org.wso2.carbon.apimgt.persistence.dto.Mediation) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) XMLStreamException(javax.xml.stream.XMLStreamException) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 4 with ResourceType

use of org.wso2.carbon.identity.configuration.mgt.core.model.ResourceType in project charon by wso2.

the class ServerSideValidator method validateCreatedSCIMObject.

/*
     * Validate created SCIMObject according to the spec
     *
     * @param scimObject
     * @param resourceSchema
     * @throw CharonException
     * @throw BadRequestException
     * @throw NotFoundException
     */
public static void validateCreatedSCIMObject(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException, NotFoundException {
    if (scimObject instanceof User) {
        // set display names for complex multivalued attributes
        setDisplayNameInComplexMultiValuedAttributes(scimObject, resourceSchema);
    }
    // remove any read only attributes
    removeAnyReadOnlyAttributes(scimObject, resourceSchema);
    if (!(scimObject instanceof Role)) {
        String id = UUID.randomUUID().toString();
        scimObject.setId(id);
        Instant now = Instant.now();
        // Set the created date and time.
        scimObject.setCreatedInstant(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(now)));
        // Creates date and the last modified are the same if not updated.
        scimObject.setLastModifiedInstant(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(now)));
    }
    // set location and resourceType
    if (resourceSchema.isSchemaAvailable(SCIMConstants.USER_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.USER);
    } else if (resourceSchema.isSchemaAvailable(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.GROUP);
    } else if (resourceSchema.isSchemaAvailable(SCIMConstants.ROLE_SCHEMA_URI)) {
        scimObject.setResourceType(SCIMConstants.ROLE);
    }
    // check for required attributes
    validateSCIMObjectForRequiredAttributes(scimObject, resourceSchema);
    validateSchemaList(scimObject, resourceSchema);
}
Also used : Role(org.wso2.charon3.core.objects.Role) User(org.wso2.charon3.core.objects.User) Instant(java.time.Instant)

Example 5 with ResourceType

use of org.wso2.carbon.identity.configuration.mgt.core.model.ResourceType in project charon by wso2.

the class ServerSideValidatorTest method dataToValidateResourceTypeSCIMObject.

@DataProvider(name = "dataForValidateResourceTypeSCIMObject")
public Object[][] dataToValidateResourceTypeSCIMObject() throws CharonException, InstantiationException, IllegalAccessException {
    AbstractSCIMObject userResourceTypeObject = AbstractSCIMObject.class.newInstance();
    userResourceTypeObject.setSchema("urn:ietf:params:scim:api:messages:2.0:ListResponse");
    SimpleAttribute simpleAttribute1 = new SimpleAttribute("id", "User");
    userResourceTypeObject.setAttribute(simpleAttribute1);
    SimpleAttribute simpleAttribute2 = new SimpleAttribute("name", "User");
    userResourceTypeObject.setAttribute(simpleAttribute2);
    SimpleAttribute simpleAttribute3 = new SimpleAttribute("endpoint", "/Users");
    userResourceTypeObject.setAttribute(simpleAttribute3);
    AbstractSCIMObject groupResourceTypeObject = AbstractSCIMObject.class.newInstance();
    groupResourceTypeObject.setSchema("urn:ietf:params:scim:api:messages:2.0:ListResponse");
    SimpleAttribute simpleAttribute4 = new SimpleAttribute("id", "Group");
    groupResourceTypeObject.setAttribute(simpleAttribute4);
    SimpleAttribute simpleAttribute5 = new SimpleAttribute("name", "Group");
    groupResourceTypeObject.setAttribute(simpleAttribute5);
    SimpleAttribute simpleAttribute6 = new SimpleAttribute("endpoint", "/Groups");
    groupResourceTypeObject.setAttribute(simpleAttribute6);
    return new Object[][] { { userResourceTypeObject, "https://localhost:9443/scim2/ResourceTypes/User", "ResourceType" }, { groupResourceTypeObject, "https://localhost:9443/scim2/ResourceTypes/Group", "ResourceType" } };
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) DataProvider(org.testng.annotations.DataProvider)

Aggregations

ResourceType (org.wso2.carbon.identity.configuration.mgt.core.model.ResourceType)36 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)27 Test (org.testng.annotations.Test)27 Resource (org.wso2.carbon.identity.configuration.mgt.core.model.Resource)22 Attribute (org.wso2.carbon.identity.configuration.mgt.core.model.Attribute)9 InputStream (java.io.InputStream)7 ResourceFile (org.wso2.carbon.identity.configuration.mgt.core.model.ResourceFile)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 OMAttribute (org.apache.axiom.om.OMAttribute)6 OMElement (org.apache.axiom.om.OMElement)6 Resources (org.wso2.carbon.identity.configuration.mgt.core.model.Resources)6 Resource (org.wso2.carbon.registry.core.Resource)6 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)6 File (java.io.File)5 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)4 Mediation (org.wso2.carbon.apimgt.api.model.Mediation)4 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)4