Search in sources :

Example 1 with APIDefaultVersionURLsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDefaultVersionURLsDTO 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 2 with APIDefaultVersionURLsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDefaultVersionURLsDTO 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

APIDefaultVersionURLsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDefaultVersionURLsDTO)2 APIEndpointURLsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO)2 APIURLsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 Environment (org.wso2.carbon.apimgt.api.model.Environment)1 VHost (org.wso2.carbon.apimgt.api.model.VHost)1