diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java index e87a0d182d74..375c320dee07 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java @@ -163,6 +163,8 @@ public enum ExceptionCodes implements ErrorHandler { 400, "Name of the gateway is read only"), GATEWAY_ENVIRONMENT_VHOST_NOT_PROVIDED(900511, "Gateway Environment virtual hosts name not provided", 400, "Gateway Environment VHOST name not provided"), + INVALID_VHOST(900512, "Invalid virtual host name provided", + 400, "Virtual host with provided vhost name does not exist"), // Workflow related codes WORKFLOW_EXCEPTION(900550, "Workflow error", 500, diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/VHost.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/VHost.java index 482cb73dee5e..a726850d3bbd 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/VHost.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/VHost.java @@ -27,12 +27,15 @@ * This class represent an Virtual Host */ public class VHost { + // host name from the http endpoint private String host; private String httpContext = ""; private Integer httpPort = -1; private Integer httpsPort = -1; private Integer wsPort = DEFAULT_WS_PORT; + private String wsHost; private Integer wssPort = DEFAULT_WSS_PORT; + private String wssHost; private Integer websubHttpPort = DEFAULT_WEBSUB_HTTP_PORT; private Integer websubHttpsPort = DEFAULT_WEBSUB_HTTPS_PORT; @@ -95,6 +98,14 @@ public void setWsPort(Integer wsPort) { this.wsPort = wsPort; } + public String getWsHost() { + return wsHost; + } + + public void setWsHost(String wsHost) { + this.wsHost = wsHost; + } + public Integer getWssPort() { return wssPort; } @@ -103,6 +114,14 @@ public void setWssPort(Integer wssPort) { this.wssPort = wssPort; } + public String getWssHost() { + return wssHost; + } + + public void setWssHost(String wssHost) { + this.wssHost = wssHost; + } + public Integer getWebsubHttpPort() { return websubHttpPort; } @@ -120,27 +139,27 @@ public void setWebsubHttpsPort(Integer websubHttpsPort) { } public String getHttpUrl() { - return getUrl("http", httpPort == DEFAULT_HTTP_PORT ? "" : ":" + httpPort, httpContext); + return getUrl("http", host, httpPort == DEFAULT_HTTP_PORT ? "" : ":" + httpPort, httpContext); } public String getHttpsUrl() { - return getUrl("https", httpsPort == DEFAULT_HTTPS_PORT ? "" : ":" + httpsPort, httpContext); + return getUrl("https", host, httpsPort == DEFAULT_HTTPS_PORT ? "" : ":" + httpsPort, httpContext); } public String getWsUrl() { - return getUrl("ws", wsPort == DEFAULT_HTTP_PORT ? "" : ":" + wsPort, ""); + return getUrl("ws", wsHost, wsPort == DEFAULT_HTTP_PORT ? "" : ":" + wsPort, ""); } public String getWssUrl() { - return getUrl("wss", wssPort == DEFAULT_HTTPS_PORT ? "" : ":" + wssPort, ""); + return getUrl("wss", wssHost, wssPort == DEFAULT_HTTPS_PORT ? "" : ":" + wssPort, ""); } - private String getUrl(String protocol, String port, String context) { + private String getUrl(String protocol, String hostName, String port, String context) { // {protocol}://{host}{port}{context} if (StringUtils.isNotEmpty(context) && !context.startsWith("/")) { context = "/" + context; } - return String.format("%s://%s%s%s", protocol, host, port, context); + return String.format("%s://%s%s%s", protocol, hostName, port, context); } public static VHost fromEndpointUrls(String[] endpoints) throws APIManagementException { @@ -178,11 +197,13 @@ public static VHost fromEndpointUrls(String[] endpoints) throws APIManagementExc // URL is not parsing for wss protocols, hence change to https url = new URL(HTTPS_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]); vhost.setWssPort(url.getPort() < 0 ? DEFAULT_WSS_PORT : url.getPort()); + vhost.setWssHost(url.getHost()); break; case WS_PROTOCOL: // URL is not parsing for ws protocols, hence change to http url = new URL(HTTP_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]); vhost.setWsPort(url.getPort() < 0 ? DEFAULT_WS_PORT : url.getPort()); + vhost.setWsHost(url.getHost()); break; case WEBSUB_HTTP_PROTOCOL: url = new URL(HTTP_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]); @@ -203,6 +224,14 @@ public static VHost fromEndpointUrls(String[] endpoints) throws APIManagementExc throw new APIManagementException("Error while building VHost, missing required HTTP or HTTPS endpoint"); } + // If WebSocket host name of Vhost is empty, use HTTP/HTTPS endpoint hostname + if ((vhost.getWsHost() == null) || (StringUtils.isEmpty(vhost.getWsHost()))) { + vhost.setWsHost(vhost.getHost()); + } + if ((vhost.getWssHost() == null) || (StringUtils.isEmpty(vhost.getWssHost()))) { + vhost.setWssHost(vhost.getHost()); + } + return vhost; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 11cac8c85c70..94cfa77f0fa2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -14060,6 +14060,9 @@ private List getVhostGatewayEnvironments(Connection connection, Integer e vhost.setHttpsPort(httpsPort); vhost.setWsPort(wsPort); vhost.setWssPort(wssPort); + // Since DB does not contain columns for wsHost and wssHost, host is used + vhost.setWsHost(host); + vhost.setWssHost(host); vhosts.add(vhost); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/restapi/publisher/ApisApiServiceImplUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/restapi/publisher/ApisApiServiceImplUtils.java index 2dfdd60f7ec1..68ffddf7edc6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/restapi/publisher/ApisApiServiceImplUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/restapi/publisher/ApisApiServiceImplUtils.java @@ -846,14 +846,33 @@ public static APIRevisionDeployment mapAPIRevisionDeploymentWithValidation(Strin final String errorMessage = "Gateway environment not found: " + environment; throw new APIManagementException(errorMessage, ExceptionCodes.from( ExceptionCodes.INVALID_GATEWAY_ENVIRONMENT, String.format("name '%s'", environment))); - } + if (mandatoryVHOST && StringUtils.isEmpty(vhost)) { // vhost is only required when deploying a revision, not required when un-deploying a revision // since the same scheme 'APIRevisionDeployment' is used for deploy and undeploy, handle it here. throw new APIManagementException("Required field 'vhost' not found in deployment", ExceptionCodes.GATEWAY_ENVIRONMENT_VHOST_NOT_PROVIDED); } + + List vhosts = environments.get(environment).getVhosts(); + boolean isVhostValidated = false; + for (VHost vhostItem : vhosts) { + //Checking the vhost is included in the available vhost list + if (vhostItem.getHost().equals(vhost)) { + isVhostValidated = true; + } else if (vhostItem.getWsHost().equals(vhost)) { + // This was added to preserve the functionality in case of Deploying a WebSocket API revision. + // For WebSocket APIs apiRevisionDeploymentDTO.getVhost() returns the wsHost + isVhostValidated = true; + vhost = vhostItem.getHost(); + } + } + + if (mandatoryVHOST && !isVhostValidated) { + throw new APIManagementException("Invalid Vhost: " + vhost, ExceptionCodes.INVALID_VHOST); + } + return mapApiRevisionDeployment(revisionId, vhost, displayOnDevportal, environment); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/VHostUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/VHostUtils.java index fa9cba2be3fa..50c7610f2172 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/VHostUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/VHostUtils.java @@ -41,7 +41,9 @@ public static VHost getVhostFromEnvironment(Environment environment, String host defaultVhost.setHttpsPort(APIConstants.HTTPS_PROTOCOL_PORT); defaultVhost.setHttpPort(APIConstants.HTTP_PROTOCOL_PORT); defaultVhost.setWsPort(APIConstants.WS_PROTOCOL_PORT); + defaultVhost.setWsHost(host); defaultVhost.setWssPort(APIConstants.WSS_PROTOCOL_PORT); + defaultVhost.setWssHost(host); if (host == null && environment.getVhosts().size() > 0) { // VHost is NULL set first Vhost (set in deployment toml) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/dto/VHostDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/dto/VHostDTO.java index cc50419e5792..92dbe48d892b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/dto/VHostDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/dto/VHostDTO.java @@ -25,7 +25,9 @@ public class VHostDTO { private Integer httpPort = null; private Integer httpsPort = null; private Integer wsPort = null; + private String wsHost = null; private Integer wssPort = null; + private String wssHost = null; /** **/ @@ -113,6 +115,23 @@ public void setWsPort(Integer wsPort) { this.wsPort = wsPort; } + /** + **/ + public VHostDTO wsHost(String wsHost) { + this.wsHost = wsHost; + return this; + } + + + @ApiModelProperty(example = "mg.wso2.com", value = "") + @JsonProperty("wsHost") + public String getWsHost() { + return wsHost; + } + public void setWsHost(String wsHost) { + this.wsHost = wsHost; + } + /** **/ public VHostDTO wssPort(Integer wssPort) { @@ -130,6 +149,23 @@ public void setWssPort(Integer wssPort) { this.wssPort = wssPort; } + /** + **/ + public VHostDTO wssHost(String wssHost) { + this.wssHost = wssHost; + return this; + } + + + @ApiModelProperty(example = "mg.wso2.com", value = "") + @JsonProperty("wssHost") + public String getWssHost() { + return wssHost; + } + public void setWssHost(String wssHost) { + this.wssHost = wssHost; + } + @Override public boolean equals(java.lang.Object o) { @@ -145,12 +181,14 @@ public boolean equals(java.lang.Object o) { Objects.equals(httpPort, vhost.httpPort) && Objects.equals(httpsPort, vhost.httpsPort) && Objects.equals(wsPort, vhost.wsPort) && - Objects.equals(wssPort, vhost.wssPort); + Objects.equals(wsHost, vhost.wsHost) && + Objects.equals(wssPort, vhost.wssPort) && + Objects.equals(wssHost, vhost.wssHost); } @Override public int hashCode() { - return Objects.hash(host, httpContext, httpPort, httpsPort, wsPort, wssPort); + return Objects.hash(host, httpContext, httpPort, httpsPort, wsPort, wsHost, wssPort, wssHost); } @Override @@ -163,7 +201,9 @@ public String toString() { sb.append(" httpPort: ").append(toIndentedString(httpPort)).append("\n"); sb.append(" httpsPort: ").append(toIndentedString(httpsPort)).append("\n"); sb.append(" wsPort: ").append(toIndentedString(wsPort)).append("\n"); + sb.append(" wsHost: ").append(toIndentedString(wsHost)).append("\n"); sb.append(" wssPort: ").append(toIndentedString(wssPort)).append("\n"); + sb.append(" wssHost: ").append(toIndentedString(wssHost)).append("\n"); sb.append("}"); return sb.toString(); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/EnvironmentMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/EnvironmentMappingUtil.java index ffb200398bf3..abad3585c062 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/EnvironmentMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/EnvironmentMappingUtil.java @@ -152,6 +152,16 @@ public static VHost fromVHostDtoToVHost(VHostDTO vhostDTO) { vhost.setHttpsPort(vhostDTO.getHttpsPort()); vhost.setWsPort(vhostDTO.getWsPort()); vhost.setWssPort(vhostDTO.getWssPort()); + if (vhostDTO.getWsHost() == null) { + vhost.setWsHost(vhostDTO.getHost()); + } else { + vhost.setWsHost(vhostDTO.getWsHost()); + } + if (vhostDTO.getWssHost() == null) { + vhost.setWssHost(vhostDTO.getHost()); + } else { + vhost.setWssHost(vhostDTO.getWssHost()); + } return vhost; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml index 0bf90b7383ad..a3cffc42f5c1 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml @@ -4084,9 +4084,15 @@ components: wsPort: type: integer example: 9099 + wsHost: + type: string + example: mg.wso2.com wssPort: type: integer example: 8099 + wssHost: + type: string + example: mg.wso2.com AdditionalProperty: title: Additional Gateway Properties type: object diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/VHostDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/VHostDTO.java index 0848b55e549d..a4ab1d9d7750 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/VHostDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/VHostDTO.java @@ -25,7 +25,9 @@ public class VHostDTO { private Integer httpPort = null; private Integer httpsPort = null; private Integer wsPort = null; + private String wsHost = null; private Integer wssPort = null; + private String wssHost = null; private Integer websubHttpPort = null; private Integer websubHttpsPort = null; @@ -114,6 +116,23 @@ public void setWsPort(Integer wsPort) { this.wsPort = wsPort; } + /** + **/ + public VHostDTO wsHost(String wsHost) { + this.wsHost = wsHost; + return this; + } + + + @ApiModelProperty(example = "mg.wso2.com", value = "") + @JsonProperty("wsHost") + public String getWsHost() { + return wsHost; + } + public void setWsHost(String wsHost) { + this.wsHost = wsHost; + } + /** **/ public VHostDTO wssPort(Integer wssPort) { @@ -131,6 +150,23 @@ public void setWssPort(Integer wssPort) { this.wssPort = wssPort; } + /** + **/ + public VHostDTO wssHost(String wssHost) { + this.wssHost = wssHost; + return this; + } + + + @ApiModelProperty(example = "mg.wso2.com", value = "") + @JsonProperty("wssHost") + public String getWssHost() { + return wssHost; + } + public void setWssHost(String wssHost) { + this.wssHost = wssHost; + } + /** **/ public VHostDTO websubHttpPort(Integer websubHttpPort) { @@ -180,14 +216,16 @@ public boolean equals(java.lang.Object o) { Objects.equals(httpPort, vhost.httpPort) && Objects.equals(httpsPort, vhost.httpsPort) && Objects.equals(wsPort, vhost.wsPort) && + Objects.equals(wsHost, vhost.wsHost) && Objects.equals(wssPort, vhost.wssPort) && + Objects.equals(wssHost, vhost.wssHost) && Objects.equals(websubHttpPort, vhost.websubHttpPort) && Objects.equals(websubHttpsPort, vhost.websubHttpsPort); } @Override public int hashCode() { - return Objects.hash(host, httpContext, httpPort, httpsPort, wsPort, wssPort, websubHttpPort, websubHttpsPort); + return Objects.hash(host, httpContext, httpPort, httpsPort, wsPort, wsHost, wssPort, wssHost, websubHttpPort, websubHttpsPort); } @Override @@ -200,7 +238,9 @@ public String toString() { sb.append(" httpPort: ").append(toIndentedString(httpPort)).append("\n"); sb.append(" httpsPort: ").append(toIndentedString(httpsPort)).append("\n"); sb.append(" wsPort: ").append(toIndentedString(wsPort)).append("\n"); + sb.append(" wsHost: ").append(toIndentedString(wsHost)).append("\n"); sb.append(" wssPort: ").append(toIndentedString(wssPort)).append("\n"); + sb.append(" wssHost: ").append(toIndentedString(wssHost)).append("\n"); sb.append(" websubHttpPort: ").append(toIndentedString(websubHttpPort)).append("\n"); sb.append(" websubHttpsPort: ").append(toIndentedString(websubHttpsPort)).append("\n"); sb.append("}"); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/EnvironmentMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/EnvironmentMappingUtil.java index 687e80b3e122..dfad946632e9 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/EnvironmentMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/EnvironmentMappingUtil.java @@ -117,7 +117,9 @@ public static VHostDTO fromVHostToVHostDTO(VHost vHost) { vHostDTO.setHttpPort(vHost.getHttpPort()); vHostDTO.setHttpsPort(vHost.getHttpsPort()); vHostDTO.setWsPort(vHost.getWsPort()); + vHostDTO.setWsHost(vHost.getWsHost()); vHostDTO.setWssPort(vHost.getWssPort()); + vHostDTO.setWssHost(vHost.getWssHost()); vHostDTO.setWebsubHttpPort(vHost.getWebsubHttpPort()); vHostDTO.setWebsubHttpsPort(vHost.getWebsubHttpsPort()); return vHostDTO; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index a9ed88ad1ae2..dfc2bb286d65 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -10367,9 +10367,15 @@ components: wsPort: type: integer example: 9099 + wsHost: + type: string + example: mg.wso2.com wssPort: type: integer example: 8099 + wssHost: + type: string + example: mg.wso2.com websubHttpPort: type: integer example: 9021