Skip to content

Commit ed3c425

Browse files
committed
Fix devportal url issue for discovered APIs
1 parent 860bbd6 commit ed3c425

File tree

2 files changed

+73
-27
lines changed
  • components/apimgt
    • org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl
    • org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings

2 files changed

+73
-27
lines changed

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
import com.fasterxml.jackson.databind.JsonNode;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
2424
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;
2529
import org.apache.axis2.util.JavaUtils;
2630
import org.apache.commons.lang3.BooleanUtils;
2731
import org.apache.commons.lang3.StringUtils;
@@ -3753,40 +3757,60 @@ private Map<String, String> getHostWithSchemeMappingForEnvironment(API api, Stri
37533757

37543758
boolean isExternalGateway = false;
37553759
GatewayDeployer gatewayDeployer = null;
3756-
String httpUrl;
3757-
String httpsUrl;
37583760
if (gatewayConfiguration != null && StringUtils.isNotEmpty(
37593761
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+
}
37623769
} else {
37633770
if (gatewayConfiguration != null && StringUtils.isNotEmpty(
37643771
gatewayConfiguration.getGatewayDeployerImplementation())) {
37653772
gatewayDeployer = GatewayHolder.getTenantGatewayInstance(tenantDomain, environmentName);
37663773
isExternalGateway = true;
37673774
}
3768-
37693775
String externalReference = APIUtil.getApiExternalApiMappingReferenceByApiId(api.getUuid(),
37703776
environment.getUuid());
3771-
httpUrl = isExternalGateway ?
3777+
String httpUrl = isExternalGateway ?
37723778
gatewayDeployer.getAPIExecutionURL(externalReference) :
37733779
vhost.getHttpUrl();
3774-
httpsUrl = isExternalGateway ?
3780+
String httpsUrl = isExternalGateway ?
37753781
gatewayDeployer.getAPIExecutionURL(externalReference) :
37763782
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+
}
37773791
}
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-
}
37863792
}
37873793
return hostsWithSchemes;
37883794
}
37893795

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+
37903814
private String getBasePath(String apiTenantDomain, String basePath) throws APIManagementException {
37913815

37923816
Map<String, String> domains =

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/APIMappingUtil.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,24 @@ public static List<APIEndpointURLsDTO> fromAPIRevisionListToEndpointsList(APIDTO
551551
return endpointUrls;
552552
}
553553

554+
private static APIURLsDTO extractEndpointUrlsForDiscoveredApi(APIDTO apidto) {
555+
JsonElement configElement = new JsonParser().parse(apidto.getApiDefinition());
556+
JsonObject configObject = configElement.getAsJsonObject(); //swaggerDefinition as a json object
557+
JsonArray servers = configObject.getAsJsonArray("servers");
558+
JsonObject server = servers.get(0).getAsJsonObject();
559+
String url = server.get("url").getAsString();
560+
JsonObject variables = server.getAsJsonObject("variables");
561+
JsonObject basePath = variables.getAsJsonObject("basePath");
562+
String stageName = basePath.get("default").getAsString();
563+
String serverUrl = url.replace("{basePath}", stageName);
564+
if (StringUtils.isEmpty(serverUrl)) {
565+
return null;
566+
}
567+
APIURLsDTO apiurLsDTO = new APIURLsDTO();
568+
apiurLsDTO.setHttps(serverUrl);
569+
return apiurLsDTO;
570+
}
571+
554572
private static APIEndpointURLsDTO fromAPIRevisionToEndpoints(APIDTO apidto, Environment environment,
555573
String host, String customGatewayUrl,
556574
String tenantDomain) throws APIManagementException {
@@ -600,26 +618,30 @@ private static APIEndpointURLsDTO fromAPIRevisionToEndpoints(APIDTO apidto, Envi
600618
GatewayDeployer gatewayDeployer = GatewayHolder.getTenantGatewayInstance(tenantDomain,
601619
environment.getName());
602620
context = gatewayDeployer != null ? "" : context;
603-
String httpUrl;
604-
String httpsUrl;
605621
if (apidto.isInitiatedFromGateway()) {
606-
httpUrl = vHost.getHttpUrl();
607-
httpsUrl = vHost.getHttpsUrl();
622+
APIURLsDTO extractedURLs;
623+
extractedURLs = extractEndpointUrlsForDiscoveredApi(apidto);
624+
if (extractedURLs == null) {
625+
apiurLsDTO.setHttps(vHost.getHttpsUrl());
626+
apiurLsDTO.setHttp(vHost.getHttpUrl());
627+
} else {
628+
apiurLsDTO = extractedURLs;
629+
}
608630
} else {
609631
String externalReference = APIUtil.getApiExternalApiMappingReferenceByApiId(apidto.getId(),
610632
environment.getUuid());
611-
httpUrl = gatewayDeployer != null ?
633+
String httpUrl = gatewayDeployer != null ?
612634
gatewayDeployer.getAPIExecutionURL(externalReference) :
613635
vHost.getHttpUrl();
614-
httpsUrl = gatewayDeployer != null ?
636+
String httpsUrl = gatewayDeployer != null ?
615637
gatewayDeployer.getAPIExecutionURL(externalReference) :
616638
vHost.getHttpsUrl();
617-
}
618-
if (apidto.getTransport().contains(APIConstants.HTTP_PROTOCOL)) {
619-
apiurLsDTO.setHttp(httpUrl + context);
620-
}
621-
if (apidto.getTransport().contains(APIConstants.HTTPS_PROTOCOL)) {
622-
apiurLsDTO.setHttps(httpsUrl + context);
639+
if (apidto.getTransport().contains(APIConstants.HTTP_PROTOCOL)) {
640+
apiurLsDTO.setHttp(httpUrl + context);
641+
}
642+
if (apidto.getTransport().contains(APIConstants.HTTPS_PROTOCOL)) {
643+
apiurLsDTO.setHttps(httpsUrl + context);
644+
}
623645
}
624646
}
625647
if (isWs || isGQLSubscription) {

0 commit comments

Comments
 (0)