Code cleanup

revert-70aa11f8
prabathabey 10 years ago
parent 103a83d425
commit 8ac5cb47a7

@ -67,10 +67,14 @@ public class APIPublisherServiceImpl implements APIPublisherService {
log.debug("Publishing a batch of APIs");
}
for (API api : apis) {
try {
this.publishAPI(api);
} catch (APIManagementException e) {
log.error("Error occurred while publishing API '" + api.getId().getApiName() + "'", e);
}
}
if (log.isDebugEnabled()) {
log.debug("Successfully published the batch of APIs");
log.debug("End of publishing the batch of APIs");
}
}

@ -22,10 +22,12 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.api.model.API;
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.core.ServerStartupObserver;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.api.mgt.config.APIPublisherConfig;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.util.List;
@ -51,43 +53,26 @@ public class APIRegistrationStartupObserver implements ServerStartupObserver {
@Override
public void completedServerStartup() {
try {
this.initAPIConfigs();
/* Publish all mobile device management related JAX-RS services as APIs */
this.publishAPIs();
} catch (DeviceManagementException e) {
log.error("Error occurred while publishing Mobile Device Management related APIs", e);
}
}
private void initAPIConfigs() throws DeviceManagementException {
if (log.isDebugEnabled()) {
log.debug("Initializing Mobile Device Management related APIs");
log.debug("Publishing all mobile device management related JAX-RS services as APIs");
}
List<APIConfig> apiConfigs = APIPublisherConfig.getInstance().getApiConfigs();
for (APIConfig apiConfig : apiConfigs) {
try {
APIProvider provider =
APIManagerFactory.getInstance().getAPIProvider(apiConfig.getOwner());
apiConfig.init(provider);
} catch (APIManagementException e) {
throw new DeviceManagementException("Error occurred while initializing API Config '" +
apiConfig.getName() + "'", e);
}
}
}
private void publishAPIs() throws DeviceManagementException {
if (log.isDebugEnabled()) {
log.debug("Publishing Mobile Device Management related APIs");
}
List<APIConfig> apiConfigs = APIPublisherConfig.getInstance().getApiConfigs();
for (APIConfig apiConfig : apiConfigs) {
DeviceManagerUtil.publishAPI(apiConfig);
API api = DeviceManagerUtil.getAPI(apiConfig);
DeviceManagementDataHolder.getInstance().getApiPublisherService().publishAPI(api);
if (log.isDebugEnabled()) {
log.debug("Successfully published API '" + apiConfig.getName() + "' with the context '" +
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'");
}
} catch (APIManagementException e) {
log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" +
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'");
}
}
if (log.isDebugEnabled()) {
log.debug("End of publishing all mobile device management related JAX-RS services as APIs");
}
}

@ -167,37 +167,22 @@ public final class DeviceManagerUtil {
return ctx.getTenantId();
}
public static void publishAPI(APIConfig config) throws DeviceManagementException {
public static API getAPI(APIConfig config) throws APIManagementException {
APIProvider provider = config.getProvider();
APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion());
API api = new API(id);
try {
if(!provider.isAPIAvailable(id)) {
if (!provider.isAPIAvailable(id)) {
api.setContext(config.getContext());
api.setUrl(config.getEndpoint());
api.setUriTemplates(getURITemplates(config.getEndpoint(),
APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN));
api.setUriTemplates(
getURITemplates(config.getEndpoint(), APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN));
api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);
api.addAvailableTiers(provider.getTiers());
api.setEndpointSecured(false);
api.setStatus(APIStatus.PUBLISHED);
api.setTransports(config.getTransports());
provider.addAPI(api);
}
} catch (APIManagementException e) {
throw new DeviceManagementException("Error occurred while registering the API", e);
}
}
public static void removeAPI(APIConfig config) throws DeviceManagementException {
try {
APIProvider provider = config.getProvider();
APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion());
provider.deleteAPI(id);
} catch (APIManagementException e) {
throw new DeviceManagementException("Error occurred while removing API", e);
}
return api;
}
private static Set<URITemplate> getURITemplates(String endpoint, String authType) {

Loading…
Cancel
Save