use of org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher in project carbon-apimgt by wso2.
the class WSO2APIPublisherTestCase method init.
@Before
public void init() throws Exception {
store = new APIStore();
store.setDisplayName(storeName);
store.setUsername(storeUserName);
store.setPassword(storePassword);
store.setEndpoint(storeEndpoint);
identifier = new APIIdentifier(apiIdentifier);
api = new API(identifier);
defaultHttpClient = Mockito.mock(CloseableHttpClient.class);
wso2APIPublisher = new WSO2APIPublisherWrapper(defaultHttpClient, username, Mockito.mock(APIProvider.class));
CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class);
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
HttpEntity entity = Mockito.mock(HttpEntity.class);
statusLine = Mockito.mock(StatusLine.class);
Mockito.doReturn(statusLine).when(httpResponse).getStatusLine();
Mockito.doReturn(entity).when(httpResponse).getEntity();
PowerMockito.mockStatic(EntityUtils.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.EXTERNAL_API_STORES + "." + APIConstants.EXTERNAL_API_STORES_STORE_URL)).thenReturn(storeRedirectURL);
HttpGet httpGet = Mockito.mock(HttpGet.class);
HttpPost httpPost = Mockito.mock(HttpPost.class);
HttpDelete httpDelete = Mockito.mock(HttpDelete.class);
PowerMockito.whenNew(HttpGet.class).withAnyArguments().thenReturn(httpGet);
PowerMockito.whenNew(HttpPost.class).withAnyArguments().thenReturn(httpPost);
PowerMockito.whenNew(HttpDelete.class).withAnyArguments().thenReturn(httpDelete);
Mockito.doReturn(httpResponse).when(defaultHttpClient).execute(httpPost);
Mockito.doReturn(httpResponse).when(defaultHttpClient).execute(httpGet);
Mockito.doReturn(httpResponse).when(defaultHttpClient).execute(httpDelete);
MultipartEntityBuilder multipartEntityBuilder = Mockito.mock(MultipartEntityBuilder.class);
PowerMockito.mockStatic(MultipartEntityBuilder.class);
Mockito.when(MultipartEntityBuilder.create()).thenReturn(multipartEntityBuilder);
Mockito.when(multipartEntityBuilder.build()).thenReturn(Mockito.mock(HttpEntity.class));
Mockito.doNothing().when(httpPost).setEntity(Matchers.any());
importExportAPI = Mockito.mock(ImportExportAPI.class);
}
use of org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher in project carbon-apimgt by wso2.
the class APIProviderImpl method publishToExternalAPIStores.
/**
* When enabled publishing to external APIStores support,publish the API to external APIStores
*
* @param api The API which need to published
* @param apiStoreSet The APIStores set to which need to publish API
* @throws org.wso2.carbon.apimgt.api.APIManagementException If failed to update subscription status
*/
@Override
public void publishToExternalAPIStores(API api, Set<APIStore> apiStoreSet, boolean apiOlderVersionExist) throws APIManagementException {
Set<APIStore> publishedStores = new HashSet<APIStore>();
StringBuilder errorStatus = new StringBuilder("Failure to publish to External Stores : ");
boolean failure = false;
for (APIStore store : apiStoreSet) {
org.wso2.carbon.apimgt.api.model.APIPublisher publisher = store.getPublisher();
try {
// First trying to publish the API to external APIStore
boolean published;
String version = ApiMgtDAO.getInstance().getLastPublishedAPIVersionFromAPIStore(api.getId(), store.getName());
if (apiOlderVersionExist && version != null && !(publisher instanceof WSO2APIPublisher)) {
published = publisher.createVersionedAPIToStore(api, store, version);
publisher.updateToStore(api, store);
} else {
published = publisher.publishToStore(api, store);
}
if (published) {
// If published,then save to database.
publishedStores.add(store);
}
} catch (APIManagementException e) {
failure = true;
log.error(e);
errorStatus.append(store.getDisplayName()).append(',');
}
}
if (!publishedStores.isEmpty()) {
addExternalAPIStoresDetails(api.getUuid(), publishedStores);
}
if (failure) {
throw new APIManagementException(errorStatus.substring(0, errorStatus.length() - 2));
}
}
use of org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPI.
public void deleteAPI(String apiUuid, String organization) throws APIManagementException {
boolean isError = false;
int apiId = -1;
API api = null;
// get api object by uuid
try {
api = getAPIbyUUID(apiUuid, organization);
} catch (APIManagementException e) {
log.error("Error while getting API by uuid for deleting API " + apiUuid + " on organization " + organization);
log.debug("Following steps will be skipped while deleting API " + apiUuid + "on organization " + organization + " due to api being null. " + "deleting Resource Registration from key managers, deleting on external API stores, " + "event publishing to gateways, logging audit message, extracting API details for " + "the recommendation system. ");
isError = true;
}
// get api id from db
try {
apiId = apiMgtDAO.getAPIID(apiUuid);
} catch (APIManagementException e) {
log.error("Error while getting API ID from DB for deleting API " + apiUuid + " on organization " + organization, e);
log.debug("Following steps will be skipped while deleting the API " + apiUuid + " on organization " + organization + "due to api id being null. cleanup workflow tasks of the API, " + "delete event publishing to gateways");
isError = true;
}
// DB delete operations
if (!isError && api != null) {
try {
deleteAPIRevisions(apiUuid, organization);
deleteAPIFromDB(api);
if (log.isDebugEnabled()) {
String logMessage = "API Name: " + api.getId().getApiName() + ", API Version " + api.getId().getVersion() + " successfully removed from the database.";
log.debug(logMessage);
}
} catch (APIManagementException e) {
log.error("Error while executing API delete operations on DB for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
// Deleting Resource Registration from key managers
if (api != null && api.getId() != null && api.getId().toString() != null) {
Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
if (keyManager != null) {
try {
keyManager.deleteRegisteredResourceByAPIId(api.getId().toString());
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the Key Manager " + keyManagerDtoEntry.getKey());
} catch (APIManagementException e) {
log.error("Error while deleting Resource Registration for API " + apiUuid + " on organization " + organization + " in Key Manager " + keyManagerDtoEntry.getKey(), e);
}
}
}
}
try {
GatewayArtifactsMgtDAO.getInstance().deleteGatewayArtifacts(apiUuid);
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the gateway artifacts.");
} catch (APIManagementException e) {
log.error("Error while executing API delete operation on gateway artifacts for API " + apiUuid, e);
isError = true;
}
try {
apiPersistenceInstance.deleteAPI(new Organization(organization), apiUuid);
log.debug("API " + apiUuid + " on organization " + organization + " has successfully removed from the persistence instance.");
} catch (APIPersistenceException e) {
log.error("Error while executing API delete operation on persistence instance for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
// Deleting on external API stores
if (api != null) {
// gatewayType check is required when API Management is deployed on
// other servers to avoid synapse
// Check if there are already published external APIStores.If yes,removing APIs from them.
Set<APIStore> apiStoreSet;
try {
apiStoreSet = getPublishedExternalAPIStores(apiUuid);
WSO2APIPublisher wso2APIPublisher = new WSO2APIPublisher();
if (apiStoreSet != null && !apiStoreSet.isEmpty()) {
for (APIStore store : apiStoreSet) {
wso2APIPublisher.deleteFromStore(api.getId(), APIUtil.getExternalAPIStore(store.getName(), tenantId));
}
}
} catch (APIManagementException e) {
log.error("Error while executing API delete operation on external API stores for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
if (apiId != -1) {
try {
cleanUpPendingAPIStateChangeTask(apiId, false);
} catch (WorkflowException | APIManagementException e) {
log.error("Error while executing API delete operation on cleanup workflow tasks for API " + apiUuid + " on organization " + organization, e);
isError = true;
}
}
// Delete event publishing to gateways
if (api != null && apiId != -1) {
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_DELETE.name(), tenantId, tenantDomain, api.getId().getApiName(), apiId, api.getUuid(), api.getId().getVersion(), api.getType(), api.getContext(), APIUtil.replaceEmailDomainBack(api.getId().getProviderName()), api.getStatus());
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
} else {
log.debug("Event has not published to gateways due to API id has failed to retrieve from DB for API " + apiUuid + " on organization " + organization);
}
// Logging audit message for API delete
if (api != null) {
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.NAME, api.getId().getApiName());
apiLogObject.put(APIConstants.AuditLogConstants.VERSION, api.getId().getVersion());
apiLogObject.put(APIConstants.AuditLogConstants.PROVIDER, api.getId().getProviderName());
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API, apiLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
}
// Extracting API details for the recommendation system
if (api != null && recommendationEnvironment != null) {
RecommenderEventPublisher extractor = new RecommenderDetailsExtractor(api, tenantDomain, APIConstants.DELETE_API);
Thread recommendationThread = new Thread(extractor);
recommendationThread.start();
}
// if one of the above has failed throw an error
if (isError) {
throw new APIManagementException("Error while deleting the API " + apiUuid + " on organization " + organization);
}
}
Aggregations