|
|
@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.apimgt.webapp.publisher;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.webapp.publisher.dto.ApiScope;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.webapp.publisher.exception.APIManagerPublisherException;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.webapp.publisher.exception.APIManagerPublisherException;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
|
|
|
@ -34,20 +35,22 @@ import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.wso2.carbon.core.ServerStartupObserver;
|
|
|
|
import org.wso2.carbon.core.ServerStartupObserver;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Stack;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
|
|
|
|
public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(APIPublisherStartupHandler.class);
|
|
|
|
private static final Log log = LogFactory.getLog(APIPublisherStartupHandler.class);
|
|
|
|
private static int retryTime = 2000;
|
|
|
|
|
|
|
|
private static final int CONNECTION_RETRY_FACTOR = 2;
|
|
|
|
private static final int CONNECTION_RETRY_FACTOR = 2;
|
|
|
|
private static final int MAX_RETRY_COUNT = 5;
|
|
|
|
private static final int MAX_RETRY_COUNT = 5;
|
|
|
|
private static Stack<APIConfig> failedAPIsStack = new Stack<>();
|
|
|
|
|
|
|
|
private static Stack<APIConfig> currentAPIsStack;
|
|
|
|
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
|
|
|
|
private static final Stack<APIConfig> failedAPIsStack = new Stack<>();
|
|
|
|
|
|
|
|
private static int retryTime = 2000;
|
|
|
|
|
|
|
|
private static Stack<APIConfig> currentAPIsStack;
|
|
|
|
|
|
|
|
private final List<String> publishedAPIs = new ArrayList<>();
|
|
|
|
private APIPublisherService publisher;
|
|
|
|
private APIPublisherService publisher;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -59,64 +62,66 @@ public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
public void completedServerStartup() {
|
|
|
|
public void completedServerStartup() {
|
|
|
|
APIPublisherDataHolder.getInstance().setServerStarted(true);
|
|
|
|
APIPublisherDataHolder.getInstance().setServerStarted(true);
|
|
|
|
currentAPIsStack = APIPublisherDataHolder.getInstance().getUnpublishedApis();
|
|
|
|
currentAPIsStack = APIPublisherDataHolder.getInstance().getUnpublishedApis();
|
|
|
|
Thread t = new Thread(new Runnable() {
|
|
|
|
Thread t = new Thread(() -> {
|
|
|
|
@Override
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
public void run() {
|
|
|
|
log.debug("Server has just started, hence started publishing unpublished APIs");
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Total number of unpublished APIs: "
|
|
|
|
log.debug("Server has just started, hence started publishing unpublished APIs");
|
|
|
|
+ APIPublisherDataHolder.getInstance().getUnpublishedApis().size());
|
|
|
|
log.debug("Total number of unpublished APIs: "
|
|
|
|
}
|
|
|
|
+ APIPublisherDataHolder.getInstance().getUnpublishedApis().size());
|
|
|
|
publisher = APIPublisherDataHolder.getInstance().getApiPublisherService();
|
|
|
|
|
|
|
|
int retryCount = 0;
|
|
|
|
|
|
|
|
while (retryCount < MAX_RETRY_COUNT && (!failedAPIsStack.isEmpty() || !currentAPIsStack.isEmpty())) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
retryTime = retryTime * CONNECTION_RETRY_FACTOR;
|
|
|
|
|
|
|
|
Thread.sleep(retryTime);
|
|
|
|
|
|
|
|
} catch (InterruptedException te) {
|
|
|
|
|
|
|
|
//do nothing.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
publisher = APIPublisherDataHolder.getInstance().getApiPublisherService();
|
|
|
|
Stack<APIConfig> failedApis;
|
|
|
|
int retryCount = 0;
|
|
|
|
if (!APIPublisherDataHolder.getInstance().getUnpublishedApis().isEmpty()) {
|
|
|
|
while (retryCount < MAX_RETRY_COUNT && (!failedAPIsStack.isEmpty() || !currentAPIsStack.isEmpty())) {
|
|
|
|
publishAPIs(currentAPIsStack, failedAPIsStack);
|
|
|
|
try {
|
|
|
|
failedApis = failedAPIsStack;
|
|
|
|
retryTime = retryTime * CONNECTION_RETRY_FACTOR;
|
|
|
|
} else {
|
|
|
|
Thread.sleep(retryTime);
|
|
|
|
publishAPIs(failedAPIsStack, currentAPIsStack);
|
|
|
|
} catch (InterruptedException te) {
|
|
|
|
failedApis = currentAPIsStack;
|
|
|
|
//do nothing.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retryCount++;
|
|
|
|
Stack<APIConfig> failedApis;
|
|
|
|
if (retryCount == MAX_RETRY_COUNT && !failedApis.isEmpty()) {
|
|
|
|
if (!APIPublisherDataHolder.getInstance().getUnpublishedApis().isEmpty()) {
|
|
|
|
StringBuilder error = new StringBuilder();
|
|
|
|
publishAPIs(currentAPIsStack, failedAPIsStack);
|
|
|
|
error.append("Error occurred while publishing API ['");
|
|
|
|
failedApis = failedAPIsStack;
|
|
|
|
while (!failedApis.isEmpty()) {
|
|
|
|
} else {
|
|
|
|
APIConfig api = failedApis.pop();
|
|
|
|
publishAPIs(failedAPIsStack, currentAPIsStack);
|
|
|
|
error.append(api.getName() + ",");
|
|
|
|
failedApis = currentAPIsStack;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
retryCount++;
|
|
|
|
|
|
|
|
if (retryCount == MAX_RETRY_COUNT && !failedApis.isEmpty()) {
|
|
|
|
|
|
|
|
StringBuilder error = new StringBuilder();
|
|
|
|
|
|
|
|
error.append("Error occurred while publishing API ['");
|
|
|
|
|
|
|
|
while (!failedApis.isEmpty()) {
|
|
|
|
|
|
|
|
APIConfig api = failedApis.pop();
|
|
|
|
|
|
|
|
error.append(api.getName() + ",");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
error.append("']");
|
|
|
|
|
|
|
|
log.error(error.toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error.append("']");
|
|
|
|
|
|
|
|
log.info(error.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
publisher.updateScopeRoleMapping();
|
|
|
|
publisher.updateScopeRoleMapping();
|
|
|
|
publisher.addDefaultScopesIfNotExist();
|
|
|
|
publisher.addDefaultScopesIfNotExist();
|
|
|
|
} catch (APIManagerPublisherException e) {
|
|
|
|
} catch (APIManagerPublisherException e) {
|
|
|
|
log.error("failed to update scope role mapping.", e);
|
|
|
|
log.error("failed to update scope role mapping.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updateScopeMetadataEntryWithDefaultScopes();
|
|
|
|
updateScopeMetadataEntryWithDefaultScopes();
|
|
|
|
|
|
|
|
log.info("Successfully published : [" + publishedAPIs + "]. " +
|
|
|
|
|
|
|
|
"and failed : [" + failedAPIsStack + "] " +
|
|
|
|
|
|
|
|
"Total successful count : [" + publishedAPIs.size() + "]. " +
|
|
|
|
|
|
|
|
"Failed count : [" + failedAPIsStack.size() + "]");
|
|
|
|
|
|
|
|
|
|
|
|
// execute after api publishing
|
|
|
|
// execute after api publishing
|
|
|
|
for (PostApiPublishingObsever observer : APIPublisherDataHolder.getInstance().getPostApiPublishingObseverList()) {
|
|
|
|
for (PostApiPublishingObsever observer : APIPublisherDataHolder.getInstance().getPostApiPublishingObseverList()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Executing " + observer.getClass().getName());
|
|
|
|
log.debug("Executing " + observer.getClass().getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
observer.execute();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Finish executing PostApiPublishingObsevers");
|
|
|
|
observer.execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("Finish executing PostApiPublishingObsevers");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
t.start();
|
|
|
|
t.start();
|
|
|
|
|
|
|
|
log.info("Starting API publishing procedure");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void publishAPIs(Stack<APIConfig> apis, Stack<APIConfig> failedStack) {
|
|
|
|
private void publishAPIs(Stack<APIConfig> apis, Stack<APIConfig> failedStack) {
|
|
|
@ -124,6 +129,11 @@ public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
APIConfig api = apis.pop();
|
|
|
|
APIConfig api = apis.pop();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
publisher.publishAPI(api);
|
|
|
|
publisher.publishAPI(api);
|
|
|
|
|
|
|
|
for (ApiScope scope : api.getScopes()) {
|
|
|
|
|
|
|
|
APIPublisherDataHolder.getInstance().getPermScopeMapping().putIfAbsent(scope.getPermissions(), scope.getKey());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
publishedAPIs.add(api.getName());
|
|
|
|
|
|
|
|
log.info("Successfully published API [" + api.getName() + "]");
|
|
|
|
} catch (APIManagerPublisherException e) {
|
|
|
|
} catch (APIManagerPublisherException e) {
|
|
|
|
log.error("failed to publish api.", e);
|
|
|
|
log.error("failed to publish api.", e);
|
|
|
|
failedStack.push(api);
|
|
|
|
failedStack.push(api);
|
|
|
@ -136,34 +146,42 @@ public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
* will create that entry and update the value with default permissions.
|
|
|
|
* will create that entry and update the value with default permissions.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private void updateScopeMetadataEntryWithDefaultScopes() {
|
|
|
|
private void updateScopeMetadataEntryWithDefaultScopes() {
|
|
|
|
|
|
|
|
Map<String, String> permScopeMap = APIPublisherDataHolder.getInstance().getPermScopeMapping();
|
|
|
|
|
|
|
|
Metadata permScopeMapping;
|
|
|
|
|
|
|
|
|
|
|
|
MetadataManagementService metadataManagementService = APIPublisherDataHolder.getInstance().getMetadataManagementService();
|
|
|
|
MetadataManagementService metadataManagementService = APIPublisherDataHolder.getInstance().getMetadataManagementService();
|
|
|
|
|
|
|
|
DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
|
|
|
|
|
|
|
|
DefaultPermissions defaultPermissions = deviceManagementConfig.getDefaultPermissions();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
|
|
|
|
permScopeMapping = metadataManagementService.retrieveMetadata(Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
DefaultPermissions defaultPermissions = deviceManagementConfig.getDefaultPermissions();
|
|
|
|
boolean entryAlreadyExists = permScopeMapping != null;
|
|
|
|
Metadata permScopeMapping = metadataManagementService.retrieveMetadata(Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
if (permScopeMap == null || permScopeMap.isEmpty()) {
|
|
|
|
Map<String, String> permScopeMap = (permScopeMapping != null) ? gson.fromJson(permScopeMapping.getMetaValue(), HashMap.class) :
|
|
|
|
permScopeMap = entryAlreadyExists ? gson.fromJson(permScopeMapping.getMetaValue(), HashMap.class) :
|
|
|
|
new HashMap<>();
|
|
|
|
new HashMap<>();
|
|
|
|
for (DefaultPermission defaultPermission : defaultPermissions.getDefaultPermissions()) {
|
|
|
|
|
|
|
|
permScopeMap.putIfAbsent(defaultPermission.getName(),
|
|
|
|
|
|
|
|
defaultPermission.getScopeMapping().getKey());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
APIPublisherDataHolder.getInstance().setPermScopeMapping(permScopeMap);
|
|
|
|
for (DefaultPermission defaultPermission : defaultPermissions.getDefaultPermissions()) {
|
|
|
|
if (permScopeMapping != null) {
|
|
|
|
permScopeMap.putIfAbsent(defaultPermission.getName(), defaultPermission.getScopeMapping().getKey());
|
|
|
|
permScopeMapping.setMetaValue(gson.toJson(permScopeMap));
|
|
|
|
|
|
|
|
metadataManagementService.updateMetadata(permScopeMapping);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
permScopeMapping = new Metadata();
|
|
|
|
permScopeMapping = new Metadata();
|
|
|
|
permScopeMapping.setMetaKey(Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
permScopeMapping.setMetaKey(Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
permScopeMapping.setMetaValue(gson.toJson(permScopeMap));
|
|
|
|
permScopeMapping.setMetaValue(gson.toJson(permScopeMap));
|
|
|
|
metadataManagementService.createMetadata(permScopeMapping);
|
|
|
|
|
|
|
|
|
|
|
|
if (entryAlreadyExists) {
|
|
|
|
|
|
|
|
metadataManagementService.updateMetadata(permScopeMapping);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
metadataManagementService.createMetadata(permScopeMapping);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
APIPublisherDataHolder.getInstance().setPermScopeMapping(permScopeMap);
|
|
|
|
|
|
|
|
log.info(Constants.PERM_SCOPE_MAPPING_META_KEY + "entry updated successfully");
|
|
|
|
} catch (MetadataManagementException e) {
|
|
|
|
} catch (MetadataManagementException e) {
|
|
|
|
log.error("Error encountered while updating permission scope mapping metadata with default scopes");
|
|
|
|
log.error("Error encountered while updating permission scope mapping metadata with default scopes");
|
|
|
|
} catch (MetadataKeyAlreadyExistsException e) {
|
|
|
|
} catch (MetadataKeyAlreadyExistsException e) {
|
|
|
|
log.error("Metadata entry already exists for " + Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
log.error("Metadata entry already exists for " + Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|