Search in sources :

Example 71 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIProviderImplTest method prepareForLCStateChangeOfAPIProduct.

private void prepareForLCStateChangeOfAPIProduct(APIProviderImplWrapper apiProvider, APIProduct apiProduct) throws Exception {
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PrivilegedCarbonContext prContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prContext);
    PowerMockito.doNothing().when(prContext).setUsername(Mockito.anyString());
    PowerMockito.doNothing().when(prContext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
    Mockito.when(artifactManager.getGenericArtifact(any(String.class))).thenReturn(artifact);
    PowerMockito.when(RegistryPersistenceUtil.createAPIProductArtifactContent(artifact, apiProduct)).thenReturn(artifact);
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, artifact.getId())).thenReturn(artifactPath);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
    Mockito.when(apiProvider.registry.resourceExists(artifactPath)).thenReturn(false);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) RealmService(org.wso2.carbon.user.core.service.RealmService) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager)

Example 72 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class SOAPOperationBindingUtils method isSOAPToRESTApi.

/**
 * Checks the api is a soap to rest converted one or a soap pass through
 * <p>
 * Note: This method directly called from the jaggery layer
 *
 * @param name     api name
 * @param version  api version
 * @param provider api provider
 * @return true if the api is soap to rest converted one. false if the user have a pass through
 * @throws APIManagementException if an error occurs when accessing the registry
 */
public static boolean isSOAPToRESTApi(String name, String version, String provider) throws APIManagementException {
    provider = (provider != null ? provider.trim() : null);
    name = (name != null ? name.trim() : null);
    version = (version != null ? version.trim() : null);
    boolean isTenantFlowStarted = false;
    try {
        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);
            String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + APIUtil.replaceEmailDomain(provider) + RegistryConstants.PATH_SEPARATOR + name + RegistryConstants.PATH_SEPARATOR + version + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SOAP_TO_REST_RESOURCE;
            if (log.isDebugEnabled()) {
                log.debug("Resource path to the soap to rest converted sequence: " + resourcePath);
            }
            return registry.resourceExists(resourcePath);
        } catch (RegistryException e) {
            handleException("Error when create registry instance", e);
        } catch (UserStoreException e) {
            handleException("Error while reading tenant information", e);
        }
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return false;
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 73 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class SequenceUtils method updateRestToSoapConvertedSequences.

/**
 * Updates the api sequences where user will be able to edits the generated sequences
 * <p>
 * Note: this method is directly invoked from the jaggery layer
 *
 * @param name     api name
 * @param version  api version
 * @param provider api provider
 * @param seqType  to identify the sequence is whether in/out sequence
 * @param content  sequence content
 * @throws APIManagementException throws exceptions on unsuccessful persistence in registry or json parsing of the content
 */
public static void updateRestToSoapConvertedSequences(String name, String version, String provider, String seqType, String content) throws APIManagementException {
    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);
    boolean isTenantFlowStarted = false;
    JSONParser jsonParser = new JSONParser();
    try {
        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;
        tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        APIUtil.loadTenantRegistry(tenantId);
        registry = registryService.getGovernanceSystemRegistry(tenantId);
        JSONObject sequences = (JSONObject) jsonParser.parse(content);
        for (Object sequence : sequences.keySet()) {
            String sequenceContent = (String) ((JSONObject) sequences.get(sequence)).get("content");
            String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + APIUtil.replaceEmailDomain(provider) + RegistryConstants.PATH_SEPARATOR + name + RegistryConstants.PATH_SEPARATOR + version + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SOAP_TO_REST_RESOURCE + RegistryConstants.PATH_SEPARATOR + seqType + RegistryConstants.PATH_SEPARATOR + sequence + ".xml";
            Resource regResource;
            if (registry.resourceExists(resourcePath)) {
                regResource = registry.get(resourcePath);
                regResource.setContent(sequenceContent);
                if (log.isDebugEnabled()) {
                    log.debug("Api sequence content for " + resourcePath + " is: " + sequenceContent);
                }
                regResource.setMediaType("text/xml");
                registry.put(resourcePath, regResource);
            }
        }
    } catch (ParseException e) {
        handleException("Error occurred while parsing the sequence json", e);
    } catch (UserStoreException e) {
        handleException("Error while reading tenant information", e);
    } catch (RegistryException e) {
        handleException("Error when create registry instance", e);
    } catch (org.wso2.carbon.registry.api.RegistryException e) {
        handleException("Error while creating registry resource", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : Resource(org.wso2.carbon.registry.api.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) JSONObject(org.json.simple.JSONObject) UserStoreException(org.wso2.carbon.user.api.UserStoreException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) RegistryService(org.wso2.carbon.registry.core.service.RegistryService)

Example 74 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class SequenceUtils method saveRestToSoapConvertedSequence.

/**
 * Saves the converted api sequence in the registry for the given resource path
 *
 * @param registry     user registry reference
 * @param sequence     sequence to be saved
 * @param method       http method of the operation
 * @param resourcePath registry resource path
 * @throws APIManagementException throws errors on if the resource persistence gets unsuccessful
 */
public static void saveRestToSoapConvertedSequence(UserRegistry registry, String sequence, String method, String resourcePath, String apiResourceName) throws APIManagementException {
    try {
        Resource regResource;
        if (apiResourceName.startsWith(SOAPToRESTConstants.SequenceGen.PATH_SEPARATOR)) {
            apiResourceName = apiResourceName.substring(1);
        }
        if (!registry.resourceExists(resourcePath)) {
            regResource = registry.newResource();
        } else {
            regResource = registry.get(resourcePath);
        }
        regResource.setContent(sequence);
        regResource.addProperty(SOAPToRESTConstants.METHOD, method);
        if (regResource.getProperty(SOAPToRESTConstants.Template.RESOURCE_PATH) != null) {
            regResource.removeProperty(SOAPToRESTConstants.Template.RESOURCE_PATH);
        }
        regResource.addProperty(SOAPToRESTConstants.Template.RESOURCE_PATH, apiResourceName);
        regResource.setMediaType("text/xml");
        registry.put(resourcePath, regResource);
    } catch (RegistryException e) {
        handleException("Error occurred while accessing the registry to save api sequence", e);
    } catch (org.wso2.carbon.registry.api.RegistryException e) {
        handleException("Error occurred while saving api sequence", e);
    }
}
Also used : Resource(org.wso2.carbon.registry.api.Resource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 75 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIProviderImpl method getCustomFaultSequences.

/**
 * Get stored custom fault sequences from governanceSystem registry
 *
 * @throws APIManagementException
 */
public List<String> getCustomFaultSequences(APIIdentifier apiIdentifier) throws APIManagementException {
    List<String> sequenceList = new ArrayList<String>();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = null;
        if (apiIdentifier.getProviderName().contains("-AT-")) {
            String provider = apiIdentifier.getProviderName().replace("-AT-", "@");
            tenantDomain = MultitenantUtils.getTenantDomain(provider);
        }
        PrivilegedCarbonContext.startTenantFlow();
        isTenantFlowStarted = true;
        if (!StringUtils.isEmpty(tenantDomain)) {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        } else {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        if (registry.resourceExists(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION)) {
            org.wso2.carbon.registry.api.Collection faultSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION);
            if (faultSeqCollection != null) {
                String[] faultSeqChildPaths = faultSeqCollection.getChildren();
                Arrays.sort(faultSeqChildPaths);
                for (String faultSeqChildPath : faultSeqChildPaths) {
                    Resource outSequence = registry.get(faultSeqChildPath);
                    try {
                        OMElement seqElment = APIUtil.buildOMElement(outSequence.getContentStream());
                        sequenceList.add(seqElment.getAttributeValue(new QName("name")));
                    } catch (OMException e) {
                        log.info("Error occurred when reading the sequence '" + faultSeqChildPath + "' from the registry.", e);
                    }
                }
            }
        }
        String customOutSeqFileLocation = APIUtil.getSequencePath(apiIdentifier, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
        if (registry.resourceExists(customOutSeqFileLocation)) {
            org.wso2.carbon.registry.api.Collection faultSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(customOutSeqFileLocation);
            if (faultSeqCollection != null) {
                String[] faultSeqChildPaths = faultSeqCollection.getChildren();
                Arrays.sort(faultSeqChildPaths);
                for (String faultSeqChildPath : faultSeqChildPaths) {
                    Resource faultSequence = registry.get(faultSeqChildPath);
                    try {
                        OMElement seqElment = APIUtil.buildOMElement(faultSequence.getContentStream());
                        sequenceList.add(seqElment.getAttributeValue(new QName("name")));
                    } catch (OMException e) {
                        log.info("Error occurred when reading the sequence '" + faultSeqChildPath + "' from the registry.", e);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Error while retrieving registry for tenant " + tenantId;
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.registry.api.RegistryException e) {
        String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT + " sequences of " + apiIdentifier + " in the registry";
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new APIManagementException(e.getMessage(), e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return sequenceList;
}
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) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) OMException(org.apache.axiom.om.OMException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OMException(org.apache.axiom.om.OMException)

Aggregations

UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)147 Resource (org.wso2.carbon.registry.core.Resource)96 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)86 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)84 Test (org.junit.Test)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)66 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)65 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)53 API (org.wso2.carbon.apimgt.api.model.API)47 UserStoreException (org.wso2.carbon.user.api.UserStoreException)44 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)43 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)41 Registry (org.wso2.carbon.registry.core.Registry)39 IOException (java.io.IOException)38 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)37 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)34 ArrayList (java.util.ArrayList)33 QName (javax.xml.namespace.QName)32 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)32