Search in sources :

Example 26 with ResourceType

use of org.wso2.carbon.identity.configuration.mgt.core.model.ResourceType 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 27 with ResourceType

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

the class AbstractAPIManager method getGlobalMediationPolicy.

/**
 * Return mediation policy corresponds to the given identifier
 *
 * @param mediationPolicyId uuid of the registry resource
 * @return Mediation object related to the given identifier or null
 * @throws APIManagementException If failed to get specified mediation policy
 */
@Override
public Mediation getGlobalMediationPolicy(String mediationPolicyId) throws APIManagementException {
    Mediation mediation = null;
    // Get registry resource correspond to identifier
    Resource mediationResource = this.getCustomMediationResourceFromUuid(mediationPolicyId);
    if (mediationResource != null) {
        // Get mediation config details
        try {
            // extracting content stream of mediation policy in to  string
            String contentString = IOUtils.toString(mediationResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
            // Get policy name from the mediation config
            OMElement omElement = AXIOMUtil.stringToOM(contentString);
            OMAttribute attribute = omElement.getAttribute(new QName(PolicyConstants.MEDIATION_NAME_ATTRIBUTE));
            String mediationPolicyName = attribute.getAttributeValue();
            mediation = new Mediation();
            mediation.setUuid(mediationResource.getUUID());
            mediation.setName(mediationPolicyName);
            String resourcePath = mediationResource.getPath();
            // Extracting mediation type from the registry resource path
            String[] path = resourcePath.split(RegistryConstants.PATH_SEPARATOR);
            String resourceType = path[(path.length - 2)];
            mediation.setType(resourceType);
            mediation.setConfig(contentString);
        } catch (RegistryException e) {
            log.error("Error occurred while getting content stream of the ,mediation policy ", e);
        } catch (IOException e) {
            log.error("Error occurred while converting content stream of mediation policy " + "into string ", e);
        } catch (XMLStreamException e) {
            log.error("Error occurred while getting omElement out of mediation content ", e);
        }
    }
    return mediation;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) 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 28 with ResourceType

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

the class AbstractAPIManager method getAllApiSpecificMediationPolicies.

/**
 * Returns a list of API specific mediation policies from registry
 *
 * @param apiIdentifier API identifier
 * @return List of api specific mediation objects available
 * @throws APIManagementException If unable to get mediation policies of specified API Id
 */
@Override
public List<Mediation> getAllApiSpecificMediationPolicies(APIIdentifier apiIdentifier) throws APIManagementException {
    List<Mediation> mediationList = new ArrayList<Mediation>();
    Mediation mediation;
    String apiResourcePath = APIUtil.getAPIPath(apiIdentifier);
    apiResourcePath = apiResourcePath.substring(0, apiResourcePath.lastIndexOf("/"));
    try {
        // 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 Mediation();
                                    mediation.setUuid(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 e) {
        String msg = "Error occurred  while getting Api Specific 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 29 with ResourceType

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

the class CodeGenerator method createResourceInfoEntry.

private void createResourceInfoEntry(BLangResource resourceNode, ServiceInfo serviceInfo) {
    BInvokableType resourceType = (BInvokableType) resourceNode.symbol.type;
    // Add resource name as an UTFCPEntry to the constant pool
    int serviceNameCPIndex = addUTF8CPEntry(currentPkgInfo, resourceNode.name.value);
    ResourceInfo resourceInfo = new ResourceInfo(currentPackageRefCPIndex, serviceNameCPIndex);
    resourceInfo.paramTypes = resourceType.paramTypes.toArray(new BType[0]);
    setParameterNames(resourceNode, resourceInfo);
    resourceInfo.retParamTypes = new BType[0];
    resourceInfo.signatureCPIndex = addUTF8CPEntry(currentPkgInfo, generateFunctionSig(resourceInfo.paramTypes, resourceInfo.retParamTypes));
    // Add worker info
    int workerNameCPIndex = addUTF8CPEntry(currentPkgInfo, "default");
    resourceInfo.defaultWorkerInfo = new WorkerInfo(workerNameCPIndex, "default");
    resourceNode.workers.forEach(worker -> addWorkerInfoEntry(worker, resourceInfo));
    // Add resource info to the service info
    serviceInfo.resourceInfoMap.put(resourceNode.name.getValue(), resourceInfo);
}
Also used : ResourceInfo(org.wso2.ballerinalang.programfile.ResourceInfo) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) WorkerInfo(org.wso2.ballerinalang.programfile.WorkerInfo) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 30 with ResourceType

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

the class ScimAttributeAware method replaceLocation.

/**
 * sets or overrides the location attribute of the given {@link #getResource()} object
 *
 * @param resourceType the location attribute to write
 */
public void replaceLocation(String resourceType) {
    SCIMAttributeSchema metaDefinition = SCIMSchemaDefinitions.META;
    SCIMAttributeSchema locationDefinition = SCIMSchemaDefinitions.LOCATION;
    ComplexAttribute meta = getOrCrateComplexAttribute(metaDefinition);
    getSetSubAttributeConsumer(meta).accept(locationDefinition, () -> resourceType);
}
Also used : SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute)

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