|
22 | 22 | import com.fasterxml.jackson.databind.JsonNode; |
23 | 23 | import com.fasterxml.jackson.databind.ObjectMapper; |
24 | 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | +import com.google.gson.JsonArray; |
| 26 | +import com.google.gson.JsonElement; |
| 27 | +import com.google.gson.JsonObject; |
| 28 | +import com.google.gson.JsonParser; |
25 | 29 | import org.apache.axis2.util.JavaUtils; |
26 | 30 | import org.apache.commons.lang3.BooleanUtils; |
27 | 31 | import org.apache.commons.lang3.StringUtils; |
@@ -3753,40 +3757,60 @@ private Map<String, String> getHostWithSchemeMappingForEnvironment(API api, Stri |
3753 | 3757 |
|
3754 | 3758 | boolean isExternalGateway = false; |
3755 | 3759 | GatewayDeployer gatewayDeployer = null; |
3756 | | - String httpUrl; |
3757 | | - String httpsUrl; |
3758 | 3760 | if (gatewayConfiguration != null && StringUtils.isNotEmpty( |
3759 | 3761 | gatewayConfiguration.getDiscoveryImplementation()) && api.isInitiatedFromGateway()) { |
3760 | | - httpUrl = vhost.getHttpUrl(); |
3761 | | - httpsUrl = vhost.getHttpsUrl(); |
| 3762 | + Map<String, String> extractedURLs = extractEndpointUrlsForDiscoveredApi(api); |
| 3763 | + if (extractedURLs == null) { |
| 3764 | + hostsWithSchemes.put(APIConstants.HTTPS_PROTOCOL, vhost.getHttpsUrl()); |
| 3765 | + hostsWithSchemes.put(APIConstants.HTTP_PROTOCOL, vhost.getHttpUrl()); |
| 3766 | + } else { |
| 3767 | + hostsWithSchemes = extractedURLs; |
| 3768 | + } |
3762 | 3769 | } else { |
3763 | 3770 | if (gatewayConfiguration != null && StringUtils.isNotEmpty( |
3764 | 3771 | gatewayConfiguration.getGatewayDeployerImplementation())) { |
3765 | 3772 | gatewayDeployer = GatewayHolder.getTenantGatewayInstance(tenantDomain, environmentName); |
3766 | 3773 | isExternalGateway = true; |
3767 | 3774 | } |
3768 | | - |
3769 | 3775 | String externalReference = APIUtil.getApiExternalApiMappingReferenceByApiId(api.getUuid(), |
3770 | 3776 | environment.getUuid()); |
3771 | | - httpUrl = isExternalGateway ? |
| 3777 | + String httpUrl = isExternalGateway ? |
3772 | 3778 | gatewayDeployer.getAPIExecutionURL(externalReference) : |
3773 | 3779 | vhost.getHttpUrl(); |
3774 | | - httpsUrl = isExternalGateway ? |
| 3780 | + String httpsUrl = isExternalGateway ? |
3775 | 3781 | gatewayDeployer.getAPIExecutionURL(externalReference) : |
3776 | 3782 | vhost.getHttpsUrl(); |
| 3783 | + if (StringUtils.containsIgnoreCase(api.getTransports(), |
| 3784 | + APIConstants.HTTP_PROTOCOL) && vhost.getHttpPort() != -1) { |
| 3785 | + hostsWithSchemes.put(APIConstants.HTTP_PROTOCOL, httpUrl); |
| 3786 | + } |
| 3787 | + if (StringUtils.containsIgnoreCase(api.getTransports(), |
| 3788 | + APIConstants.HTTPS_PROTOCOL) && vhost.getHttpsPort() != -1) { |
| 3789 | + hostsWithSchemes.put(APIConstants.HTTPS_PROTOCOL, httpsUrl); |
| 3790 | + } |
3777 | 3791 | } |
3778 | | - if (StringUtils.containsIgnoreCase(api.getTransports(), |
3779 | | - APIConstants.HTTP_PROTOCOL) && vhost.getHttpPort() != -1) { |
3780 | | - hostsWithSchemes.put(APIConstants.HTTP_PROTOCOL, httpUrl); |
3781 | | - } |
3782 | | - if (StringUtils.containsIgnoreCase(api.getTransports(), |
3783 | | - APIConstants.HTTPS_PROTOCOL) && vhost.getHttpsPort() != -1) { |
3784 | | - hostsWithSchemes.put(APIConstants.HTTPS_PROTOCOL, httpsUrl); |
3785 | | - } |
3786 | 3792 | } |
3787 | 3793 | return hostsWithSchemes; |
3788 | 3794 | } |
3789 | 3795 |
|
| 3796 | + private static Map<String, String> extractEndpointUrlsForDiscoveredApi(API api) { |
| 3797 | + JsonElement configElement = new JsonParser().parse(api.getSwaggerDefinition()); |
| 3798 | + JsonObject configObject = configElement.getAsJsonObject(); //swaggerDefinition as a json object |
| 3799 | + JsonArray servers = configObject.getAsJsonArray("servers"); |
| 3800 | + JsonObject server = servers.get(0).getAsJsonObject(); |
| 3801 | + String url = server.get("url").getAsString(); |
| 3802 | + JsonObject variables = server.getAsJsonObject("variables"); |
| 3803 | + JsonObject basePath = variables.getAsJsonObject("basePath"); |
| 3804 | + String stageName = basePath.get("default").getAsString(); |
| 3805 | + String serverUrl = url.replace("{basePath}", stageName); |
| 3806 | + if (StringUtils.isEmpty(serverUrl)) { |
| 3807 | + return null; |
| 3808 | + } |
| 3809 | + Map<String, String> hostsWithSchemes = new HashMap<>(); |
| 3810 | + hostsWithSchemes.put(APIConstants.HTTPS_PROTOCOL, serverUrl); |
| 3811 | + return hostsWithSchemes; |
| 3812 | + } |
| 3813 | + |
3790 | 3814 | private String getBasePath(String apiTenantDomain, String basePath) throws APIManagementException { |
3791 | 3815 |
|
3792 | 3816 | Map<String, String> domains = |
|
0 commit comments