|
|
@ -25,36 +25,22 @@ import org.apache.catalina.core.StandardContext;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.*;
|
|
|
|
import org.wso2.carbon.apimgt.api.model.*;
|
|
|
|
import org.wso2.carbon.apimgt.impl.APIConstants;
|
|
|
|
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.*;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.*;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResource;
|
|
|
|
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.lifecycle.util.AnnotationUtil;
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.lifecycle.util.AnnotationUtil;
|
|
|
|
import org.wso2.carbon.base.MultitenantConstants;
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public class APIPublisherLifecycleListener implements LifecycleListener {
|
|
|
|
public class APIPublisherLifecycleListener implements LifecycleListener {
|
|
|
|
|
|
|
|
|
|
|
|
private static final String API_CONFIG_DEFAULT_VERSION = "1.0.0";
|
|
|
|
private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class);
|
|
|
|
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_ENABLED = "managed-api-enabled";
|
|
|
|
private static final String PARAM_MANAGED_API_ENABLED = "managed-api-enabled";
|
|
|
|
private static final String PARAM_MANAGED_API_NAME = "managed-api-name";
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_VERSION = "managed-api-version";
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_CONTEXT = "managed-api-context";
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_ENDPOINT = "managed-api-endpoint";
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_OWNER = "managed-api-owner";
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_TRANSPORTS = "managed-api-transports";
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_IS_SECURED = "managed-api-isSecured";
|
|
|
|
|
|
|
|
private static final String PARAM_MANAGED_API_APPLICATION = "managed-api-application";
|
|
|
|
|
|
|
|
private static final String PARAM_SHARED_WITH_ALL_TENANTS = "isSharedWithAllTenants";
|
|
|
|
|
|
|
|
private static final String PARAM_PROVIDER_TENANT_DOMAIN = "providerTenantDomain";
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
|
|
|
|
public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
|
|
|
@ -69,13 +55,20 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
|
|
|
|
AnnotationUtil annotationUtil = new AnnotationUtil(context);
|
|
|
|
AnnotationUtil annotationUtil = new AnnotationUtil(context);
|
|
|
|
Set<String> annotatedAPIClasses = annotationUtil.
|
|
|
|
Set<String> annotatedAPIClasses = annotationUtil.
|
|
|
|
scanStandardContext(org.wso2.carbon.apimgt.annotations.api.API.class.getName());
|
|
|
|
scanStandardContext(org.wso2.carbon.apimgt.annotations.api.API.class.getName());
|
|
|
|
List<APIResourceConfiguration> apiDefinitions = annotationUtil.extractAPIInfo(servletContext, annotatedAPIClasses);
|
|
|
|
List<APIResourceConfiguration> apiDefinitions = annotationUtil.extractAPIInfo(servletContext,
|
|
|
|
|
|
|
|
annotatedAPIClasses);
|
|
|
|
|
|
|
|
|
|
|
|
for (APIResourceConfiguration apiDefinition : apiDefinitions) {
|
|
|
|
for (APIResourceConfiguration apiDefinition : apiDefinitions) {
|
|
|
|
APIConfig apiConfig = this.buildApiConfig(servletContext, apiDefinition);
|
|
|
|
|
|
|
|
|
|
|
|
APIConfig apiConfig = APIPublisherUtil.buildApiConfig(servletContext, apiDefinition);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
int tenantId = APIPublisherDataHolder.getInstance().getTenantManager().getTenantId(apiConfig.getTenantDomain());
|
|
|
|
int tenantId = APIPublisherDataHolder.getInstance().getTenantManager().
|
|
|
|
boolean isTenantActive = APIPublisherDataHolder.getInstance().getTenantManager().isTenantActive(tenantId);
|
|
|
|
getTenantId(apiConfig.getTenantDomain());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean isTenantActive = APIPublisherDataHolder.getInstance().
|
|
|
|
|
|
|
|
getTenantManager().isTenantActive(tenantId);
|
|
|
|
|
|
|
|
|
|
|
|
if (isTenantActive) {
|
|
|
|
if (isTenantActive) {
|
|
|
|
apiConfig.init();
|
|
|
|
apiConfig.init();
|
|
|
|
API api = APIPublisherUtil.getAPI(apiConfig);
|
|
|
|
API api = APIPublisherUtil.getAPI(apiConfig);
|
|
|
@ -87,7 +80,8 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apiPublisherService.publishAPI(api);
|
|
|
|
apiPublisherService.publishAPI(api);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.error("No tenant [" + apiConfig.getTenantDomain() + "] found when publishing the webapp");
|
|
|
|
log.error("No tenant [" + apiConfig.getTenantDomain() + "] " +
|
|
|
|
|
|
|
|
"found when publishing the Web app");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
} catch (Throwable e) {
|
|
|
|
log.error("Error occurred while publishing API '" + apiConfig.getName() +
|
|
|
|
log.error("Error occurred while publishing API '" + apiConfig.getName() +
|
|
|
@ -96,7 +90,7 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Error enconterd while discovering annotated classes", e);
|
|
|
|
log.error("Error encountered while discovering annotated classes", e);
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
log.error("Error while scanning class for annotations", e);
|
|
|
|
log.error("Error while scanning class for annotations", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -104,126 +98,9 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO : Need to implemented, to merge API Definitions in cases where implementation of an API Lies in two classes
|
|
|
|
private List<APIResourceConfiguration> mergeAPIDefinitions(List<APIResourceConfiguration> inputList) {
|
|
|
|
private List<APIResourceConfiguration> mergeAPIDefinitions(List<APIResourceConfiguration> inputList) {
|
|
|
|
//TODO : Need to implemented, to merge API Definitions in cases where implementation of an API Lies in two
|
|
|
|
|
|
|
|
// classes
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Build the API Configuration to be passed to APIM, from a given list of URL templates
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param servletContext
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private APIConfig buildApiConfig(ServletContext servletContext, APIResourceConfiguration apidef) {
|
|
|
|
|
|
|
|
APIConfig apiConfig = new APIConfig();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String name = apidef.getName();
|
|
|
|
|
|
|
|
if (name == null || name.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("API Name not set in @API Annotation");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
name = servletContext.getServletContextName();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setName(name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String version = apidef.getVersion();
|
|
|
|
|
|
|
|
if (version == null || version.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("'API Version not set in @API Annotation'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
version = API_CONFIG_DEFAULT_VERSION;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setVersion(version);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String context = apidef.getContext();
|
|
|
|
|
|
|
|
if (context == null || context.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("'API Context not set in @API Annotation'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
context = servletContext.getContextPath();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setContext(context);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String[] tags = apidef.getTags();
|
|
|
|
|
|
|
|
if (tags == null || tags.length == 0) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("'API tag not set in @API Annotation'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
apiConfig.setTags(tags);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String tenantDomain = servletContext.getInitParameter(PARAM_PROVIDER_TENANT_DOMAIN);
|
|
|
|
|
|
|
|
tenantDomain = (tenantDomain != null && !tenantDomain.isEmpty()) ? tenantDomain :
|
|
|
|
|
|
|
|
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
|
|
|
|
|
|
|
|
apiConfig.setTenantDomain(tenantDomain);
|
|
|
|
|
|
|
|
String contextTemplate = context + "/" + APIConstants.VERSION_PLACEHOLDER;
|
|
|
|
|
|
|
|
if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
|
|
|
|
|
|
|
|
contextTemplate = context + "/t/" + tenantDomain + "/" + APIConstants.VERSION_PLACEHOLDER;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setContextTemplate(contextTemplate);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String endpoint = servletContext.getInitParameter(PARAM_MANAGED_API_ENDPOINT);
|
|
|
|
|
|
|
|
if (endpoint == null || endpoint.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("'managed-api-endpoint' attribute is not configured");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String endpointContext = servletContext.getContextPath();
|
|
|
|
|
|
|
|
endpoint = APIPublisherUtil.getApiEndpointUrl(endpointContext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setEndpoint(endpoint);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String owner = servletContext.getInitParameter(PARAM_MANAGED_API_OWNER);
|
|
|
|
|
|
|
|
if (owner == null || owner.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("'managed-api-owner' attribute is not configured");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setOwner(owner);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String isSecuredParam = servletContext.getInitParameter(PARAM_MANAGED_API_IS_SECURED);
|
|
|
|
|
|
|
|
boolean isSecured;
|
|
|
|
|
|
|
|
if (isSecuredParam == null || isSecuredParam.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("'managed-api-isSecured' attribute is not configured. Therefore, using the default, " +
|
|
|
|
|
|
|
|
"which is 'true'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
isSecured = false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
isSecured = Boolean.parseBoolean(isSecuredParam);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setSecured(isSecured);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String transports = servletContext.getInitParameter(PARAM_MANAGED_API_TRANSPORTS);
|
|
|
|
|
|
|
|
if (transports == null || transports.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("'managed-api-transports' attribute is not configured. Therefore using the default, " +
|
|
|
|
|
|
|
|
"which is 'https'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
transports = "https";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setTransports(transports);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String sharingValueParam = servletContext.getInitParameter(PARAM_SHARED_WITH_ALL_TENANTS);
|
|
|
|
|
|
|
|
boolean isSharedWithAllTenants = (sharingValueParam == null || (!sharingValueParam.isEmpty()) && Boolean.parseBoolean(
|
|
|
|
|
|
|
|
sharingValueParam));
|
|
|
|
|
|
|
|
apiConfig.setSharedWithAllTenants(isSharedWithAllTenants);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
|
|
|
|
|
|
|
|
for (APIResource apiResource : apidef.getResources()) {
|
|
|
|
|
|
|
|
URITemplate template = new URITemplate();
|
|
|
|
|
|
|
|
template.setAuthType(apiResource.getAuthType());
|
|
|
|
|
|
|
|
template.setHTTPVerb(apiResource.getHttpVerb());
|
|
|
|
|
|
|
|
template.setResourceURI(apiResource.getUri());
|
|
|
|
|
|
|
|
template.setUriTemplate(apiResource.getUriTemplate());
|
|
|
|
|
|
|
|
uriTemplates.add(template);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apiConfig.setUriTemplates(uriTemplates);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return apiConfig;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|