use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method deleteAPI.
@Override
public void deleteAPI(Organization org, String apiId) throws APIPersistenceException {
boolean transactionCommitted = false;
boolean tenantFlowStarted = false;
Registry registry = null;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
registry = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
registry.beginTransaction();
GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Failed to retrieve artifact manager when deleting API " + apiId;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
APIIdentifier identifier = new APIIdentifier(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
// Delete the dependencies associated with the api artifact
GovernanceArtifact[] dependenciesArray = apiArtifact.getDependencies();
if (dependenciesArray.length > 0) {
for (GovernanceArtifact artifact : dependenciesArray) {
registry.delete(artifact.getPath());
}
}
artifactManager.removeGenericArtifact(apiArtifact);
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
Resource apiResource = registry.get(path);
String artifactId = apiResource.getUUID();
artifactManager.removeGenericArtifact(artifactId);
String thumbPath = RegistryPersistenceUtil.getIconPath(identifier);
if (registry.resourceExists(thumbPath)) {
registry.delete(thumbPath);
}
String wsdlArchivePath = RegistryPersistenceUtil.getWsdlArchivePath(identifier);
if (registry.resourceExists(wsdlArchivePath)) {
registry.delete(wsdlArchivePath);
}
/*Remove API Definition Resource - swagger*/
String apiDefinitionFilePath = APIConstants.API_DOC_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + '-' + identifier.getVersion() + '-' + identifier.getProviderName();
if (registry.resourceExists(apiDefinitionFilePath)) {
registry.delete(apiDefinitionFilePath);
}
/*remove empty directories*/
String apiCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName();
if (registry.resourceExists(apiCollectionPath)) {
Resource apiCollection = registry.get(apiCollectionPath);
CollectionImpl collection = (CollectionImpl) apiCollection;
// if there is no other versions of apis delete the directory of the api
if (collection.getChildCount() == 0) {
if (log.isDebugEnabled()) {
log.debug("No more versions of the API found, removing API collection from registry");
}
registry.delete(apiCollectionPath);
}
}
String apiProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName();
if (registry.resourceExists(apiProviderPath)) {
Resource providerCollection = registry.get(apiProviderPath);
CollectionImpl collection = (CollectionImpl) providerCollection;
// if there is no api for given provider delete the provider directory
if (collection.getChildCount() == 0) {
if (log.isDebugEnabled()) {
log.debug("No more APIs from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
}
registry.delete(apiProviderPath);
}
}
registry.commitTransaction();
transactionCommitted = true;
} catch (RegistryException e) {
throw new APIPersistenceException("Failed to remove the API : " + apiId, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
try {
if (!transactionCommitted) {
registry.rollbackTransaction();
}
} catch (RegistryException ex) {
throw new APIPersistenceException("Error occurred while rolling back the transaction. ", ex);
}
}
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetSecurityAuditAttributesFromTenantConfig.
/**
* This method tests the retrieval of API Security Audit Properties from the Tenant Config
* (i.e. tenant-conf.json)
* @throws RegistryException
* @throws APIManagementException
*/
@Test
public void testGetSecurityAuditAttributesFromTenantConfig() throws RegistryException, APIManagementException {
// Instantiating required variables
final int tenantId = -1234;
String apiToken = "1234f0ca-9879-112f-0e8f-a098e0do12456";
String collectionId = "467f8ca-40f8-4baf-8b0f-c6854ed04653";
boolean overrideGlobal = true;
// Sample JSONObject for mocking
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiToken", apiToken);
jsonObject.put("collectionId", collectionId);
jsonObject.put("overrideGlobal", overrideGlobal);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
Resource resource = Mockito.mock(Resource.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(tenantId)).thenReturn(userRegistry);
PowerMockito.mockStatic(APIUtil.class);
Mockito.when(APIUtil.getSecurityAuditAttributesFromRegistry(superTenantDomain)).thenReturn(jsonObject);
// Pass the mock values to the method call
JSONObject jsonObject1 = apiProvider.getSecurityAuditAttributesFromConfig("admin");
// Compare the API Token and Collection ID returned from the method call
Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_API_TOKEN), apiToken);
Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_COLLECTION_ID), collectionId);
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class AbstractWSDLProcessor method getWSDLArchive.
/**
* Creates a zip for from the files of the provided path and returns an InputStream for the created zip file.
*
* @param wsdlArchiveFilePath file path where the WSDL archive is extracted
* @return ByteArrayInputStream object of the WSDL archive zip file
* @throws APIMgtWSDLException when error occurred when creating the zip and returning the InputStream
*/
ByteArrayInputStream getWSDLArchive(String wsdlArchiveFilePath) throws APIMgtWSDLException {
try {
Collection<File> allFiles = FileUtils.listFiles(new File(wsdlArchiveFilePath), null, true);
String outputZipFilePath = wsdlArchiveFilePath + File.separator + APIConstants.WSDL_ARCHIVE_UPDATED_ZIP_FILE;
ZIPUtils.zipFiles(outputZipFilePath, allFiles);
File outputZipFile = new File(outputZipFilePath);
return new ByteArrayInputStream(FileUtils.readFileToByteArray(outputZipFile));
} catch (APIManagementException | IOException e) {
throw new APIMgtWSDLException("General error occurred while creating WSDL archive", e, ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
}
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class WSDL11ProcessorImpl method initPath.
@Override
public boolean initPath(String path) throws APIMgtWSDLException {
setMode(Mode.ARCHIVE);
pathToDefinitionMap = new HashMap<>();
wsdlArchiveExtractedPath = path;
WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
try {
// switch off the verbose mode
wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
File folderToImport = new File(path);
Collection<File> foundWSDLFiles = APIFileUtil.searchFilesWithMatchingExtension(folderToImport, "wsdl");
if (log.isDebugEnabled()) {
log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
}
for (File file : foundWSDLFiles) {
String absWSDLPath = file.getAbsolutePath();
if (log.isDebugEnabled()) {
log.debug("Processing WSDL file: " + absWSDLPath);
}
Definition definition = wsdlReader.readWSDL(path, getSecuredParsedDocumentFromPath(absWSDLPath));
pathToDefinitionMap.put(absWSDLPath, definition);
// set the first found WSDL as wsdlDefinition variable assuming that it is the root WSDL
if (wsdlDefinition == null) {
wsdlDefinition = definition;
}
}
if (foundWSDLFiles.isEmpty()) {
setError(ExceptionCodes.NO_WSDL_FOUND_IN_WSDL_ARCHIVE);
}
if (log.isDebugEnabled()) {
log.debug("Successfully processed all WSDL files in path " + path);
}
} catch (WSDLException | APIManagementException e) {
// This implementation class cannot process the WSDL. Continuing after setting canProcess = false
log.debug(this.getClass().getName() + " was unable to process the WSDL Files for the path: " + path, e);
setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
}
return !hasError;
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class WSDL20ProcessorImpl method initPath.
@Override
public boolean initPath(String path) throws APIMgtWSDLException {
setMode(Mode.ARCHIVE);
pathToDescriptionMap = new HashMap<>();
wsdlArchiveExtractedPath = path;
WSDLReader reader;
try {
reader = getWsdlFactoryInstance().newWSDLReader();
} catch (WSDLException e) {
throw new APIMgtWSDLException("Error while initializing the WSDL reader", e);
}
reader.setFeature(WSDLReader.FEATURE_VALIDATION, false);
File folderToImport = new File(path);
Collection<File> foundWSDLFiles = APIFileUtil.searchFilesWithMatchingExtension(folderToImport, "wsdl");
if (log.isDebugEnabled()) {
log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
}
try {
for (File file : foundWSDLFiles) {
if (log.isDebugEnabled()) {
log.debug("Processing WSDL file: " + file.getAbsolutePath());
}
Document document = getSecuredParsedDocumentFromPath(file.getAbsolutePath());
WSDLSource wsdlSource = getWSDLSourceFromDocument(document, reader);
Description description = reader.readWSDL(wsdlSource);
pathToDescriptionMap.put(file.getAbsolutePath(), description);
}
if (foundWSDLFiles.isEmpty()) {
setError(ExceptionCodes.NO_WSDL_FOUND_IN_WSDL_ARCHIVE);
}
if (log.isDebugEnabled()) {
log.debug("Successfully processed all WSDL files in path " + path);
}
} catch (WSDLException e) {
// This implementation class cannot process the WSDL. Continuing after setting canProcess = false
log.debug(this.getClass().getName() + " was unable to process the WSDL Files for the path: " + path, e);
setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
}
return !hasError;
}
Aggregations