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);
}
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;
}
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();
}
}
}
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);
}
}
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;
}
Aggregations