|
|
|
@ -18,6 +18,9 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.wso2.carbon.apimgt.webapp.publisher;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
|
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiScope;
|
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.dto.ApiUriTemplate;
|
|
|
|
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
|
|
|
@ -36,10 +39,15 @@ import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.config.WebappPublisherConfig;
|
|
|
|
|
import org.wso2.carbon.apimgt.webapp.publisher.exception.APIManagerPublisherException;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
import org.wso2.carbon.user.core.service.RealmService;
|
|
|
|
|
import org.wso2.carbon.user.core.tenant.Tenant;
|
|
|
|
|
import org.wso2.carbon.user.core.tenant.TenantSearchResult;
|
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -55,17 +63,54 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
private static final String CREATED_STATUS = "CREATED";
|
|
|
|
|
private static final String PUBLISH_ACTION = "Publish";
|
|
|
|
|
public static final APIManagerFactory API_MANAGER_FACTORY = APIManagerFactory.getInstance();
|
|
|
|
|
private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void publishAPI(APIConfig apiConfig) throws APIManagerPublisherException {
|
|
|
|
|
String tenantDomain = MultitenantUtils.getTenantDomain(apiConfig.getOwner());
|
|
|
|
|
WebappPublisherConfig config = WebappPublisherConfig.getInstance();
|
|
|
|
|
List<String> tenants = new ArrayList<>(Collections.singletonList(APIConstants.SUPER_TENANT_DOMAIN));
|
|
|
|
|
tenants.addAll(config.getTenants().getTenant());
|
|
|
|
|
RealmService realmService = (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
|
|
|
|
.getOSGiService(RealmService.class, null);
|
|
|
|
|
try {
|
|
|
|
|
boolean tenantFound = false;
|
|
|
|
|
boolean tenantsLoaded = false;
|
|
|
|
|
TenantSearchResult tenantSearchResult = null;
|
|
|
|
|
for (String tenantDomain : tenants) {
|
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
|
|
|
|
|
if (!tenantsLoaded) {
|
|
|
|
|
tenantSearchResult = realmService.getTenantManager()
|
|
|
|
|
.listTenants(Integer.MAX_VALUE, 0, "asc", "UM_ID", null);
|
|
|
|
|
tenantsLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
if (tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
|
|
|
|
|
tenantFound = true;
|
|
|
|
|
realmService.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID)
|
|
|
|
|
.getRealmConfiguration().getAdminUserName();
|
|
|
|
|
} else {
|
|
|
|
|
List<Tenant> allTenants = tenantSearchResult.getTenantList();
|
|
|
|
|
for (Tenant tenant : allTenants) {
|
|
|
|
|
if (tenant.getDomain().equals(tenantDomain)) {
|
|
|
|
|
tenantFound = true;
|
|
|
|
|
tenant.getAdminName();
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
tenantFound = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tenantFound) {
|
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(apiConfig.getOwner());
|
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
apiConfig.setOwner(APIUtil.getTenantAdminUserName(tenantDomain));
|
|
|
|
|
apiConfig.setTenantDomain(tenantDomain);
|
|
|
|
|
APIProvider apiProvider = API_MANAGER_FACTORY.getAPIProvider(apiConfig.getOwner());
|
|
|
|
|
APIIdentifier apiIdentifier = new APIIdentifier(apiConfig.getOwner(), apiConfig.getName(), apiConfig.getVersion());
|
|
|
|
|
APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(apiConfig.getOwner()),
|
|
|
|
|
apiConfig.getName(), apiConfig.getVersion());
|
|
|
|
|
|
|
|
|
|
if (!apiProvider.isAPIAvailable(apiIdentifier)) {
|
|
|
|
|
|
|
|
|
@ -82,6 +127,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
API api = getAPI(apiConfig, true);
|
|
|
|
|
api.setId(apiIdentifier);
|
|
|
|
|
API createdAPI = apiProvider.addAPI(api);
|
|
|
|
|
if (CREATED_STATUS.equals(createdAPI.getStatus())) {
|
|
|
|
|
apiProvider.changeLifeCycleStatus(tenantDomain, createdAPI.getUuid(), PUBLISH_ACTION, null);
|
|
|
|
@ -98,7 +144,6 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>();
|
|
|
|
|
apiRevisionDeploymentList.add(apiRevisionDeployment);
|
|
|
|
|
apiProvider.deployAPIRevision(createdAPI.getUuid(), apiRevisionId, apiRevisionDeploymentList);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (WebappPublisherConfig.getInstance().isEnabledUpdateApi()) {
|
|
|
|
@ -189,14 +234,21 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (FaultGatewaysException | APIManagementException e) {
|
|
|
|
|
String msg = "Error occurred while publishing api";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIManagerPublisherException(e);
|
|
|
|
|
} finally {
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving admin user from tenant user realm";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIManagerPublisherException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private API getAPI(APIConfig config, boolean includeScopes) {
|
|
|
|
|
|
|
|
|
@ -209,7 +261,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
api.setStatus(CREATED_STATUS);
|
|
|
|
|
api.setWsdlUrl(null);
|
|
|
|
|
api.setResponseCache("Disabled");
|
|
|
|
|
api.setContextTemplate(context + "/{version}" );
|
|
|
|
|
api.setContextTemplate(context + "/{version}");
|
|
|
|
|
api.setSwaggerDefinition(APIPublisherUtil.getSwaggerDefinition(config));
|
|
|
|
|
api.setType("HTTP");
|
|
|
|
|
|
|
|
|
@ -262,7 +314,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
api.setVisibility(APIConstants.API_PRIVATE_VISIBILITY);
|
|
|
|
|
}
|
|
|
|
|
String endpointConfig = "{ \"endpoint_type\": \"http\", \"sandbox_endpoints\": { \"url\": \" " +
|
|
|
|
|
config.getEndpoint() + "\" }, \"production_endpoints\": { \"url\": \" "+ config.getEndpoint()+"\" } }";
|
|
|
|
|
config.getEndpoint() + "\" }, \"production_endpoints\": { \"url\": \" " + config.getEndpoint() + "\" } }";
|
|
|
|
|
|
|
|
|
|
api.setEndpointConfig(endpointConfig);
|
|
|
|
|
List<String> accessControlAllowOrigins = new ArrayList<>();
|
|
|
|
|