Search in sources :

Example 1 with APIEndpointURLsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO 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;
}
Also used : HashMap(java.util.HashMap) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO) ArrayList(java.util.ArrayList) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 2 with APIEndpointURLsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO 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;
}
Also used : VHost(org.wso2.carbon.apimgt.api.model.VHost) APIURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO) APIDefaultVersionURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDefaultVersionURLsDTO) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO)

Example 3 with APIEndpointURLsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method setEndpointURLsForAwsAPIs.

private static List<APIEndpointURLsDTO> setEndpointURLsForAwsAPIs(ApiTypeWrapper model, String organization) throws APIManagementException {
    APIDTO apidto;
    apidto = fromAPItoDTO(model.getApi(), organization);
    JsonElement configElement = new JsonParser().parse(apidto.getApiDefinition());
    // swaggerDefinition as a json object
    JsonObject configObject = configElement.getAsJsonObject();
    JsonArray servers = configObject.getAsJsonArray("servers");
    JsonObject server = servers.get(0).getAsJsonObject();
    String url = server.get("url").getAsString();
    JsonObject variables = server.getAsJsonObject("variables");
    JsonObject basePath = variables.getAsJsonObject("basePath");
    String stageName = basePath.get("default").getAsString();
    String serverUrl = url.replace("/{basePath}", stageName);
    if (serverUrl == null) {
        serverUrl = "Could not find server URL";
    }
    APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
    List<APIEndpointURLsDTO> endpointUrls = new ArrayList<>();
    APIURLsDTO apiurLsDTO = new APIURLsDTO();
    apiurLsDTO.setHttps(serverUrl);
    apiEndpointURLsDTO.setUrLs(apiurLsDTO);
    endpointUrls.add(apiEndpointURLsDTO);
    return endpointUrls;
}
Also used : JsonArray(com.google.gson.JsonArray) APIURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO) JsonElement(com.google.gson.JsonElement) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 4 with APIEndpointURLsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO in project carbon-apimgt by wso2.

the class APIUtils method extractEndpointURLs.

/**
 * Extracts the API environment details with access url for each endpoint
 *
 * @param apiProduct   API object
 * @param tenantDomain Tenant domain of the API
 * @return the API environment details
 * @throws APIManagementException error while extracting the information
 */
public static List<APIEndpointURLsDTO> extractEndpointURLs(APIProduct apiProduct, String tenantDomain) throws APIManagementException {
    List<APIEndpointURLsDTO> apiEndpointsList = new ArrayList<>();
    String organization = apiProduct.getOrganization();
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    Set<String> environmentsPublishedByAPI = new HashSet<>(apiProduct.getEnvironments());
    environmentsPublishedByAPI.remove("none");
    Set<String> apiTransports = new HashSet<>(Arrays.asList(apiProduct.getTransports().split(",")));
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    for (String environmentName : environmentsPublishedByAPI) {
        Environment environment = environments.get(environmentName);
        if (environment != null) {
            APIURLsDTO apiURLsDTO = new APIURLsDTO();
            String[] gwEndpoints = null;
            gwEndpoints = environment.getApiGatewayEndpoint().split(",");
            Map<String, String> domains = new HashMap<>();
            if (tenantDomain != null) {
                domains = apiConsumer.getTenantDomainMappings(tenantDomain, APIConstants.API_DOMAIN_MAPPINGS_GATEWAY);
            }
            String customGatewayUrl = null;
            if (domains != null) {
                customGatewayUrl = domains.get(APIConstants.CUSTOM_URL);
            }
            for (String gwEndpoint : gwEndpoints) {
                StringBuilder endpointBuilder = new StringBuilder(gwEndpoint);
                if (customGatewayUrl != null) {
                    int index = endpointBuilder.indexOf("//");
                    endpointBuilder.replace(index + 2, endpointBuilder.length(), customGatewayUrl);
                    endpointBuilder.append(apiProduct.getContext().replace("/t/" + tenantDomain, ""));
                } else {
                    endpointBuilder.append(apiProduct.getContext());
                }
                if (gwEndpoint.contains("http:") && apiTransports.contains("http")) {
                    apiURLsDTO.setHttp(endpointBuilder.toString());
                } else if (gwEndpoint.contains("https:") && apiTransports.contains("https")) {
                    apiURLsDTO.setHttps(endpointBuilder.toString());
                }
            }
            APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
            apiEndpointURLsDTO.setUrLs(apiURLsDTO);
            apiEndpointURLsDTO.setEnvironmentName(environment.getName());
            apiEndpointURLsDTO.setEnvironmentType(environment.getType());
            apiEndpointsList.add(apiEndpointURLsDTO);
        }
    }
    return apiEndpointsList;
}
Also used : APIURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO) HashMap(java.util.HashMap) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO) ArrayList(java.util.ArrayList) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) HashSet(java.util.HashSet)

Example 5 with APIEndpointURLsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO in project carbon-apimgt by wso2.

the class APIUtils method extractEndpointURLs.

/**
 * Extracts the API environment details with access url for each endpoint
 *
 * @param api          API object
 * @param tenantDomain Tenant domain of the API
 * @return the API environment details
 * @throws APIManagementException error while extracting the information
 */
public static List<APIEndpointURLsDTO> extractEndpointURLs(API api, String tenantDomain) throws APIManagementException {
    List<APIEndpointURLsDTO> apiEndpointsList = new ArrayList<>();
    String organization = api.getOrganization();
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    Set<String> environmentsPublishedByAPI = new HashSet<>(api.getEnvironments());
    environmentsPublishedByAPI.remove("none");
    Set<String> apiTransports = new HashSet<>(Arrays.asList(api.getTransports().split(",")));
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    for (String environmentName : environmentsPublishedByAPI) {
        Environment environment = environments.get(environmentName);
        if (environment != null) {
            APIURLsDTO apiURLsDTO = new APIURLsDTO();
            APIDefaultVersionURLsDTO apiDefaultVersionURLsDTO = new APIDefaultVersionURLsDTO();
            String[] gwEndpoints = null;
            if ("WS".equalsIgnoreCase(api.getType())) {
                gwEndpoints = environment.getWebsocketGatewayEndpoint().split(",");
            } else {
                gwEndpoints = environment.getApiGatewayEndpoint().split(",");
            }
            Map<String, String> domains = new HashMap<>();
            if (tenantDomain != null) {
                domains = apiConsumer.getTenantDomainMappings(tenantDomain, APIConstants.API_DOMAIN_MAPPINGS_GATEWAY);
            }
            String customGatewayUrl = null;
            if (domains != null) {
                customGatewayUrl = domains.get(APIConstants.CUSTOM_URL);
            }
            for (String gwEndpoint : gwEndpoints) {
                StringBuilder endpointBuilder = new StringBuilder(gwEndpoint);
                if (customGatewayUrl != null) {
                    int index = endpointBuilder.indexOf("//");
                    endpointBuilder.replace(index + 2, endpointBuilder.length(), customGatewayUrl);
                    endpointBuilder.append(api.getContext().replace("/t/" + tenantDomain, ""));
                } else {
                    endpointBuilder.append(api.getContext());
                }
                if (gwEndpoint.contains("http:") && apiTransports.contains("http")) {
                    apiURLsDTO.setHttp(endpointBuilder.toString());
                } else if (gwEndpoint.contains("https:") && apiTransports.contains("https")) {
                    apiURLsDTO.setHttps(endpointBuilder.toString());
                } else if (gwEndpoint.contains("ws:")) {
                    apiURLsDTO.setWs(endpointBuilder.toString());
                } else if (gwEndpoint.contains("wss:")) {
                    apiURLsDTO.setWss(endpointBuilder.toString());
                }
                if (api.isDefaultVersion()) {
                    int index = endpointBuilder.lastIndexOf(api.getId().getVersion());
                    endpointBuilder.replace(index, endpointBuilder.length(), "");
                    if (gwEndpoint.contains("http:") && apiTransports.contains("http")) {
                        apiDefaultVersionURLsDTO.setHttp(endpointBuilder.toString());
                    } else if (gwEndpoint.contains("https:") && apiTransports.contains("https")) {
                        apiDefaultVersionURLsDTO.setHttps(endpointBuilder.toString());
                    } else if (gwEndpoint.contains("ws:")) {
                        apiDefaultVersionURLsDTO.setWs(endpointBuilder.toString());
                    } else if (gwEndpoint.contains("wss:")) {
                        apiDefaultVersionURLsDTO.setWss(endpointBuilder.toString());
                    }
                }
            }
            APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
            apiEndpointURLsDTO.setDefaultVersionURLs(apiDefaultVersionURLsDTO);
            apiEndpointURLsDTO.setUrLs(apiURLsDTO);
            apiEndpointURLsDTO.setEnvironmentName(environment.getName());
            apiEndpointURLsDTO.setEnvironmentType(environment.getType());
            apiEndpointsList.add(apiEndpointURLsDTO);
        }
    }
    return apiEndpointsList;
}
Also used : APIURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO) HashMap(java.util.HashMap) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO) ArrayList(java.util.ArrayList) APIDefaultVersionURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDefaultVersionURLsDTO) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) HashSet(java.util.HashSet)

Aggregations

APIEndpointURLsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO)5 ArrayList (java.util.ArrayList)4 APIURLsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO)4 HashMap (java.util.HashMap)3 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)3 Environment (org.wso2.carbon.apimgt.api.model.Environment)3 HashSet (java.util.HashSet)2 APIDefaultVersionURLsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDefaultVersionURLsDTO)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)1 VHost (org.wso2.carbon.apimgt.api.model.VHost)1 APIDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO)1