use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ImportUtils method importDependentAPIs.
/**
* This method imports dependent APIs of the API Product.
*
* @param path Location of the extracted folder of the API Product
* @param currentUser The current logged in user
* @param isDefaultProviderAllowed Decision to keep or replace the provider
* @param apiProvider API provider
* @param overwriteAPIs Whether to overwrite the APIs or not
* @param apiProductDto API Product DTO
* @param tokenScopes Scopes of the token
* @param organization Organization Identifier
* @return Modified API Product DTO with the correct API UUIDs
* @throws IOException If there is an error while reading an API file
* @throws APIImportExportException If there is an error in importing an API
* @throws APIManagementException If failed to get the API Provider of an API, or failed when
* checking the existence of an API
*/
private static APIProductDTO importDependentAPIs(String path, String currentUser, boolean isDefaultProviderAllowed, APIProvider apiProvider, boolean overwriteAPIs, Boolean rotateRevision, APIProductDTO apiProductDto, String[] tokenScopes, String organization) throws IOException, APIManagementException {
JsonObject dependentAPIParamsConfigObject = null;
// Retrieve the dependent APIs param configurations from the params file of the API Product
JsonObject dependentAPIsParams = APIControllerUtil.getDependentAPIsParams(path);
String apisDirectoryPath = path + File.separator + ImportExportConstants.APIS_DIRECTORY;
File apisDirectory = new File(apisDirectoryPath);
File[] apisDirectoryListing = apisDirectory.listFiles();
if (apisDirectoryListing != null) {
for (File apiDirectory : apisDirectoryListing) {
String apiDirectoryPath = path + File.separator + ImportExportConstants.APIS_DIRECTORY + File.separator + apiDirectory.getName();
// API in the API directory will be retrieved if available
if (dependentAPIsParams != null) {
dependentAPIParamsConfigObject = APIControllerUtil.getDependentAPIParams(dependentAPIsParams, apiDirectory.getName());
// If the "certificates" directory is specified, copy it inside Deployment directory of the
// dependent API since there may be certificates required for APIs
String deploymentCertificatesDirectoryPath = path + ImportExportConstants.DEPLOYMENT_DIRECTORY + ImportExportConstants.CERTIFICATE_DIRECTORY;
if (CommonUtil.checkFileExistence(deploymentCertificatesDirectoryPath)) {
try {
CommonUtil.copyDirectory(deploymentCertificatesDirectoryPath, apiDirectoryPath + ImportExportConstants.DEPLOYMENT_DIRECTORY + ImportExportConstants.CERTIFICATE_DIRECTORY);
} catch (APIImportExportException e) {
throw new APIManagementException("Error while copying the directory " + deploymentCertificatesDirectoryPath, e);
}
}
}
JsonElement jsonObject = retrieveValidatedDTOObject(apiDirectoryPath, isDefaultProviderAllowed, currentUser, ImportExportConstants.TYPE_API);
APIDTO apiDtoToImport = new Gson().fromJson(jsonObject, APIDTO.class);
API importedApi = null;
String apiName = apiDtoToImport.getName();
String apiVersion = apiDtoToImport.getVersion();
if (isDefaultProviderAllowed) {
APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(apiDtoToImport.getProvider()), apiName, apiVersion);
// Checking whether the API exists
if (apiProvider.isAPIAvailable(apiIdentifier, organization)) {
// otherwise do not update the API. (Just skip it)
if (Boolean.TRUE.equals(overwriteAPIs)) {
importedApi = importApi(apiDirectoryPath, apiDtoToImport, isDefaultProviderAllowed, rotateRevision, Boolean.TRUE, Boolean.TRUE, tokenScopes, dependentAPIParamsConfigObject, organization);
}
} else {
// If the API is not already imported, import it
importedApi = importApi(apiDirectoryPath, apiDtoToImport, isDefaultProviderAllowed, rotateRevision, Boolean.FALSE, Boolean.TRUE, tokenScopes, dependentAPIParamsConfigObject, organization);
}
} else {
// Retrieve the current tenant domain of the logged in user
String currentTenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(currentUser));
// Get the provider of the API if the API is in current user's tenant domain.
String apiProviderInCurrentTenantDomain = APIUtil.getAPIProviderFromAPINameVersionTenant(apiName, apiVersion, currentTenantDomain);
if (StringUtils.isBlank(apiProviderInCurrentTenantDomain)) {
// If there is no API in the current tenant domain (which means the provider name is blank)
// then the API should be imported freshly
importedApi = importApi(apiDirectoryPath, apiDtoToImport, isDefaultProviderAllowed, rotateRevision, Boolean.FALSE, Boolean.TRUE, tokenScopes, dependentAPIParamsConfigObject, organization);
} else {
// otherwise do not import/update the API. (Just skip it)
if (Boolean.TRUE.equals(overwriteAPIs)) {
importedApi = importApi(apiDirectoryPath, apiDtoToImport, isDefaultProviderAllowed, rotateRevision, Boolean.TRUE, Boolean.TRUE, tokenScopes, dependentAPIParamsConfigObject, organization);
}
}
}
if (importedApi == null) {
// Retrieve the API from the environment (This happens when you have not specified
// the overwrite flag, so that we should retrieve the API from inside)
importedApi = retrieveApiToOverwrite(apiDtoToImport.getName(), apiDtoToImport.getVersion(), MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(currentUser)), apiProvider, Boolean.FALSE, organization);
}
updateApiUuidInApiProduct(apiProductDto, importedApi);
}
} else {
String msg = "No dependent APIs supplied. Continuing ...";
log.info(msg);
}
return apiProductDto;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method undeployAPIRevision.
@Override
public Response undeployAPIRevision(String apiId, String revisionId, String revisionNum, Boolean allEnvironments, List<APIRevisionDeploymentDTO> apIRevisionDeploymentDTOList, MessageContext messageContext) throws APIManagementException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().toString());
String organization = RestApiUtil.getValidatedOrganization(messageContext);
if (revisionId == null && revisionNum != null) {
revisionId = apiProvider.getAPIRevisionUUID(revisionNum, apiId);
if (revisionId == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(null).build();
}
}
Map<String, Environment> environments = APIUtil.getEnvironments(organization);
List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
if (allEnvironments) {
apiRevisionDeployments = apiProvider.getAPIRevisionDeploymentList(revisionId);
} else {
for (APIRevisionDeploymentDTO apiRevisionDeploymentDTO : apIRevisionDeploymentDTOList) {
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setRevisionUUID(revisionId);
String environment = apiRevisionDeploymentDTO.getName();
if (environments.get(environment) == null) {
RestApiUtil.handleBadRequest("Gateway environment not found: " + environment, log);
}
apiRevisionDeployment.setDeployment(environment);
apiRevisionDeployment.setVhost(apiRevisionDeploymentDTO.getVhost());
apiRevisionDeployment.setDisplayOnDevportal(apiRevisionDeploymentDTO.isDisplayOnDevportal());
apiRevisionDeployments.add(apiRevisionDeployment);
}
}
apiProvider.undeployAPIRevisionDeployment(apiId, revisionId, apiRevisionDeployments, organization);
List<APIRevisionDeployment> apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionDeploymentList(revisionId);
List<APIRevisionDeploymentDTO> apiRevisionDeploymentDTOS = new ArrayList<>();
for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeploymentsResponse) {
apiRevisionDeploymentDTOS.add(APIMappingUtil.fromAPIRevisionDeploymenttoDTO(apiRevisionDeployment));
}
Response.Status status = Response.Status.CREATED;
return Response.status(status).entity(apiRevisionDeploymentDTOS).build();
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class APIMappingUtil method fromAPIRevisionListToEndpointsList.
public static List<APIEndpointURLsDTO> fromAPIRevisionListToEndpointsList(APIDTO apidto, String organization) throws APIManagementException {
Map<String, Environment> environments = APIUtil.getEnvironments(organization);
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
List<APIRevisionDeployment> revisionDeployments = apiConsumer.getAPIRevisionDeploymentListOfAPI(apidto.getId());
// custom gateway URL of tenant
Map<String, String> domains = new HashMap<>();
if (organization != null) {
domains = apiConsumer.getTenantDomainMappings(organization, APIConstants.API_DOMAIN_MAPPINGS_GATEWAY);
}
String customGatewayUrl = domains.get(APIConstants.CUSTOM_URL);
List<APIEndpointURLsDTO> endpointUrls = new ArrayList<>();
for (APIRevisionDeployment revisionDeployment : revisionDeployments) {
if (revisionDeployment.isDisplayOnDevportal()) {
// Deployed environment
Environment environment = environments.get(revisionDeployment.getDeployment());
if (environment != null) {
APIEndpointURLsDTO apiEndpointURLsDTO = fromAPIRevisionToEndpoints(apidto, environment, revisionDeployment.getVhost(), customGatewayUrl, organization);
endpointUrls.add(apiEndpointURLsDTO);
}
}
}
return endpointUrls;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class APIMappingUtil method fromAPIRevisionToEndpoints.
private static APIEndpointURLsDTO fromAPIRevisionToEndpoints(APIDTO apidto, Environment environment, String host, String customGatewayUrl, String tenantDomain) throws APIManagementException {
// Deployed VHost
VHost vHost;
String context = apidto.getContext();
if (StringUtils.isEmpty(customGatewayUrl)) {
vHost = VHostUtils.getVhostFromEnvironment(environment, host);
} else {
if (!StringUtils.contains(customGatewayUrl, "://")) {
customGatewayUrl = APIConstants.HTTPS_PROTOCOL_URL_PREFIX + customGatewayUrl;
}
vHost = VHost.fromEndpointUrls(new String[] { customGatewayUrl });
context = context.replace("/t/" + tenantDomain, "");
}
APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
apiEndpointURLsDTO.setEnvironmentName(environment.getName());
apiEndpointURLsDTO.setEnvironmentDisplayName(environment.getDisplayName());
apiEndpointURLsDTO.setEnvironmentType(environment.getType());
APIURLsDTO apiurLsDTO = new APIURLsDTO();
boolean isWs = StringUtils.equalsIgnoreCase("WS", apidto.getType());
boolean isGQLSubscription = StringUtils.equalsIgnoreCase(APIConstants.GRAPHQL_API, apidto.getType()) && isGraphQLSubscriptionsAvailable(apidto);
if (!isWs) {
if (apidto.getTransport().contains(APIConstants.HTTP_PROTOCOL)) {
apiurLsDTO.setHttp(vHost.getHttpUrl() + context);
}
if (apidto.getTransport().contains(APIConstants.HTTPS_PROTOCOL)) {
apiurLsDTO.setHttps(vHost.getHttpsUrl() + context);
}
}
if (isWs || isGQLSubscription) {
apiurLsDTO.setWs(vHost.getWsUrl() + context);
apiurLsDTO.setWss(vHost.getWssUrl() + context);
}
apiEndpointURLsDTO.setUrLs(apiurLsDTO);
APIDefaultVersionURLsDTO apiDefaultVersionURLsDTO = new APIDefaultVersionURLsDTO();
if (apidto.isIsDefaultVersion() != null && apidto.isIsDefaultVersion()) {
String defaultContext = context.replaceAll("/" + apidto.getVersion() + "$", "");
if (!isWs) {
if (apidto.getTransport().contains(APIConstants.HTTP_PROTOCOL)) {
apiDefaultVersionURLsDTO.setHttp(vHost.getHttpUrl() + defaultContext);
}
if (apidto.getTransport().contains(APIConstants.HTTPS_PROTOCOL)) {
apiDefaultVersionURLsDTO.setHttps(vHost.getHttpsUrl() + defaultContext);
}
}
if (isWs || isGQLSubscription) {
apiDefaultVersionURLsDTO.setWs(vHost.getWsUrl() + defaultContext);
apiDefaultVersionURLsDTO.setWss(vHost.getWssUrl() + defaultContext);
}
}
apiEndpointURLsDTO.setDefaultVersionURLs(apiDefaultVersionURLsDTO);
return apiEndpointURLsDTO;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getWSDLOfAPI.
@Override
public Response getWSDLOfAPI(String apiId, String environmentName, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
API api = apiConsumer.getLightweightAPIByUUID(apiId, organization);
APIIdentifier apiIdentifier = api.getId();
List<Environment> environments = APIUtil.getEnvironmentsOfAPI(api);
if (environments != null && environments.size() > 0) {
if (StringUtils.isEmpty(environmentName)) {
environmentName = api.getEnvironments().iterator().next();
}
Environment selectedEnvironment = null;
for (Environment environment : environments) {
if (environment.getName().equals(environmentName)) {
selectedEnvironment = environment;
break;
}
}
if (selectedEnvironment == null) {
throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.INVALID_GATEWAY_ENVIRONMENT, environmentName));
}
ResourceFile wsdl = apiConsumer.getWSDL(api, selectedEnvironment.getName(), selectedEnvironment.getType(), organization);
return RestApiUtil.getResponseFromResourceFile(apiIdentifier.toString(), wsdl);
} else {
throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.NO_GATEWAY_ENVIRONMENTS_ADDED, apiIdentifier.toString()));
}
}
Aggregations