|
|
@ -26,13 +26,15 @@ import org.wso2.carbon.apimgt.api.APIProvider;
|
|
|
|
import org.wso2.carbon.apimgt.api.FaultGatewaysException;
|
|
|
|
import org.wso2.carbon.apimgt.api.FaultGatewaysException;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.API;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.API;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
|
|
|
|
|
|
|
import org.wso2.carbon.apimgt.api.model.APIStatus;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.URITemplate;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.URITemplate;
|
|
|
|
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
|
|
|
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
|
|
|
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.exception.APIPublisherException;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
|
|
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.governance.lcm.util.CommonUtil;
|
|
|
|
import org.wso2.carbon.governance.lcm.util.CommonUtil;
|
|
|
|
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
|
|
|
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
|
|
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
|
|
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.xml.stream.XMLStreamException;
|
|
|
|
import javax.xml.stream.XMLStreamException;
|
|
|
@ -41,7 +43,6 @@ import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This class represents the concrete implementation of the APIPublisherService that corresponds to providing all
|
|
|
|
* This class represents the concrete implementation of the APIPublisherService that corresponds to providing all
|
|
|
@ -50,89 +51,134 @@ import java.util.Set;
|
|
|
|
public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class);
|
|
|
|
private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class);
|
|
|
|
|
|
|
|
private static final String PUBLISH_ACTION = "Publish";
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void publishAPI(API api) throws APIManagementException, FaultGatewaysException {
|
|
|
|
public void publishAPI(final API api) throws APIManagementException, FaultGatewaysException {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
// A thread is initialized because we cannot guarantee that the gateway is initialized before publishing.
|
|
|
|
log.debug("Publishing API '" + api.getId() + "'");
|
|
|
|
// A better solution is needs to be implemented - should check in apimanager whether
|
|
|
|
}
|
|
|
|
// the gateway is local and if so then publish it through OSGI
|
|
|
|
try {
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
String tenantDomain = MultitenantUtils.getTenantDomain(api.getApiOwner());
|
|
|
|
public void run() {
|
|
|
|
int tenantId =
|
|
|
|
if (waitForServerStartup()) {
|
|
|
|
APIPublisherDataHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
|
|
|
|
return;
|
|
|
|
// Below code snippet is added load API Lifecycle in tenant mode, where in it does not load when tenant is loaded.
|
|
|
|
}
|
|
|
|
RegistryService registryService = APIPublisherDataHolder.getInstance().getRegistryService();
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
CommonUtil.addDefaultLifecyclesIfNotAvailable(registryService.getConfigSystemRegistry(tenantId),
|
|
|
|
log.debug("Publishing API '" + api.getId() + "");
|
|
|
|
CommonUtil.getRootSystemRegistry(tenantId));
|
|
|
|
}
|
|
|
|
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
|
|
|
|
try {
|
|
|
|
MultitenantUtils.getTenantDomain(api.getApiOwner());
|
|
|
|
Thread.sleep(5000);
|
|
|
|
if (provider != null) {
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
if (provider.isDuplicateContextTemplate(api.getContext())) {
|
|
|
|
Thread.interrupted();
|
|
|
|
throw new APIManagementException("Error occurred while adding the API. A duplicate API" +
|
|
|
|
}
|
|
|
|
" context already exists for " + api.getContext());
|
|
|
|
synchronized (APIPublisherServiceImpl.class) {
|
|
|
|
}
|
|
|
|
String tenantDomain = MultitenantUtils.getTenantDomain(api.getApiOwner());
|
|
|
|
if (!provider.isAPIAvailable(api.getId())) {
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
provider.addAPI(api);
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
|
|
|
|
log.info("Successfully published API '" + api.getId().getApiName() + "' with context '" +
|
|
|
|
try {
|
|
|
|
api.getContext() + "' and version '" + api.getId().getVersion() + "'");
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
} else {
|
|
|
|
// Below code snippet is added to load API Lifecycle in tenant mode.
|
|
|
|
provider.updateAPI(api);
|
|
|
|
RegistryService registryService = APIPublisherDataHolder.getInstance().getRegistryService();
|
|
|
|
log.info("An API already exists with the name '" + api.getId().getApiName() + "', context '" +
|
|
|
|
CommonUtil.addDefaultLifecyclesIfNotAvailable(registryService.getConfigSystemRegistry(tenantId),
|
|
|
|
api.getContext() + "' and version '" + api.getId().getVersion() +
|
|
|
|
CommonUtil.getRootSystemRegistry(tenantId));
|
|
|
|
"'. Thus, the API config is updated");
|
|
|
|
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
|
|
|
|
}
|
|
|
|
MultitenantUtils.getTenantDomain(api.getApiOwner());
|
|
|
|
provider.saveSwagger20Definition(api.getId(), createSwaggerDefinition(api));
|
|
|
|
if (provider != null) {
|
|
|
|
} else {
|
|
|
|
if (provider.isDuplicateContextTemplate(api.getContext())) {
|
|
|
|
throw new APIManagementException("API provider configured for the given API configuration is null. " +
|
|
|
|
throw new APIManagementException(
|
|
|
|
"Thus, the API is not published");
|
|
|
|
"Error occurred while adding the API. A duplicate API" +
|
|
|
|
}
|
|
|
|
" context already exists for " + api.getContext());
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
}
|
|
|
|
throw new APIManagementException("Failed to get the tenant id for the user " + api.getApiOwner(), e);
|
|
|
|
if (!provider.isAPIAvailable(api.getId())) {
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
provider.addAPI(api);
|
|
|
|
throw new APIManagementException("Failed to retrieve life cycle file ", e);
|
|
|
|
provider.changeLifeCycleStatus(api.getId(), PUBLISH_ACTION);
|
|
|
|
} catch (RegistryException e) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
throw new APIManagementException("Failed to access the registry ", e);
|
|
|
|
log.debug("Successfully published API '" + api.getId().getApiName() +
|
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
"' with context '" + api.getContext() + "' and version '"
|
|
|
|
throw new APIManagementException("Failed parsing the lifecycle xml.", e);
|
|
|
|
+ api.getId().getVersion() + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
api.setStatus(APIStatus.PUBLISHED);
|
|
|
|
|
|
|
|
provider.updateAPI(api);
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("An API already exists with the name '" + api.getId().getApiName() +
|
|
|
|
|
|
|
|
"', context '" + api.getContext() + "' and version '"
|
|
|
|
|
|
|
|
+ api.getId().getVersion() + "'. Thus, the API config is updated");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
provider.saveSwagger20Definition(api.getId(), createSwaggerDefinition(api));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new APIManagementException("API provider configured for the given API configuration " +
|
|
|
|
|
|
|
|
"is null. Thus, the API is not published");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
|
|
|
throw new APIPublisherException("Failed to retrieve life cycle file ", e);
|
|
|
|
|
|
|
|
} catch (RegistryException e) {
|
|
|
|
|
|
|
|
throw new APIPublisherException("Failed to access the registry ", e);
|
|
|
|
|
|
|
|
} catch (XMLStreamException e) {
|
|
|
|
|
|
|
|
throw new APIPublisherException("Failed parsing the lifecycle xml.", e);
|
|
|
|
|
|
|
|
} catch (FaultGatewaysException e) {
|
|
|
|
|
|
|
|
throw new APIPublisherException("Failed when publishing to the gateway", e);
|
|
|
|
|
|
|
|
} catch (APIManagementException e) {
|
|
|
|
|
|
|
|
throw new APIPublisherException("Failed publishing the API " + api.getId().getApiName(), e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Thread connectorThread = new Thread(connector);
|
|
|
|
|
|
|
|
connectorThread.setDaemon(true);
|
|
|
|
|
|
|
|
connectorThread.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String createSwaggerDefinition(API api) {
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
Map<String, JsonObject> httpVerbsMap = new HashMap<>();
|
|
|
|
while (!APIPublisherDataHolder.getInstance().getServerStartupListener().isServerReady()) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
Thread.interrupted();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (URITemplate uriTemplate : api.getUriTemplates()) {
|
|
|
|
private String createSwaggerDefinition(API api) {
|
|
|
|
JsonObject response = new JsonObject();
|
|
|
|
Map<String, JsonObject> httpVerbsMap = new HashMap<>();
|
|
|
|
response.addProperty("200", "");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JsonObject responses = new JsonObject();
|
|
|
|
for (URITemplate uriTemplate : api.getUriTemplates()) {
|
|
|
|
responses.add("responses", response);
|
|
|
|
JsonObject response = new JsonObject();
|
|
|
|
JsonObject httpVerbs = httpVerbsMap.get(uriTemplate.getUriTemplate());
|
|
|
|
response.addProperty("200", "");
|
|
|
|
if (httpVerbs == null) {
|
|
|
|
|
|
|
|
httpVerbs = new JsonObject();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
httpVerbs.add(uriTemplate.getHTTPVerb().toLowerCase(), responses);
|
|
|
|
|
|
|
|
httpVerbsMap.put(uriTemplate.getUriTemplate(), httpVerbs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Iterator it = httpVerbsMap.entrySet().iterator();
|
|
|
|
JsonObject responses = new JsonObject();
|
|
|
|
JsonObject paths = new JsonObject();
|
|
|
|
responses.add("responses", response);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
JsonObject httpVerbs = httpVerbsMap.get(uriTemplate.getUriTemplate());
|
|
|
|
Map.Entry<String, JsonObject> pair = (Map.Entry)it.next();
|
|
|
|
if (httpVerbs == null) {
|
|
|
|
paths.add(pair.getKey(), pair.getValue());
|
|
|
|
httpVerbs = new JsonObject();
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpVerbs.add(uriTemplate.getHTTPVerb().toLowerCase(), responses);
|
|
|
|
|
|
|
|
httpVerbsMap.put(uriTemplate.getUriTemplate(), httpVerbs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsonObject info = new JsonObject();
|
|
|
|
Iterator it = httpVerbsMap.entrySet().iterator();
|
|
|
|
info.addProperty("title", api.getId().getApiName());
|
|
|
|
JsonObject paths = new JsonObject();
|
|
|
|
info.addProperty("version", api.getId().getVersion());
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
|
|
|
Map.Entry<String, JsonObject> pair = (Map.Entry) it.next();
|
|
|
|
|
|
|
|
paths.add(pair.getKey(), pair.getValue());
|
|
|
|
|
|
|
|
it.remove();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsonObject swaggerDefinition = new JsonObject();
|
|
|
|
JsonObject info = new JsonObject();
|
|
|
|
swaggerDefinition.add("paths", paths);
|
|
|
|
info.addProperty("title", api.getId().getApiName());
|
|
|
|
swaggerDefinition.addProperty("swagger", "2.0");
|
|
|
|
info.addProperty("version", api.getId().getVersion());
|
|
|
|
swaggerDefinition.add("info", info);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return swaggerDefinition.toString();
|
|
|
|
JsonObject swaggerDefinition = new JsonObject();
|
|
|
|
}
|
|
|
|
swaggerDefinition.add("paths", paths);
|
|
|
|
|
|
|
|
swaggerDefinition.addProperty("swagger", "2.0");
|
|
|
|
|
|
|
|
swaggerDefinition.add("info", info);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return swaggerDefinition.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void removeAPI(APIIdentifier id) throws APIManagementException {
|
|
|
|
public void removeAPI(APIIdentifier id) throws APIManagementException {
|
|
|
|