Skip to content

Commit

Permalink
Merge pull request #12022 from SavinduDimal/ws-hostname
Browse files Browse the repository at this point in the history
Fix Host Name in Gateway Access URL in Portals for WebSocket APIs Showing Wrong Host
  • Loading branch information
chamilaadhi authored Jun 15, 2023
2 parents c6fe017 + de9ea6b commit 00df05e
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14060,6 +14060,9 @@ private List<VHost> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VHost> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
**/
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 00df05e

Please sign in to comment.