use of org.apache.knox.gateway.services.registry.ServiceDefEntry in project knox by apache.
the class GatewayWebsocketHandler method getMatchedBackendURL.
/**
* This method looks at the context path and returns the backend websocket
* url. If websocket url is found it is used as is, or we default to
* ws://{host}:{port} which might or might not be right.
*
* @param
* @return Websocket backend url
*/
private synchronized String getMatchedBackendURL(final String path) {
final ServiceRegistry serviceRegistryService = services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
final ServiceDefinitionRegistry serviceDefinitionService = services.getService(GatewayServices.SERVICE_DEFINITION_REGISTRY);
/* Filter out the /cluster/topology to get the context we want */
String[] pathInfo = path.split(REGEX_SPLIT_CONTEXT);
final ServiceDefEntry entry = serviceDefinitionService.getMatchingService(pathInfo[1]);
if (entry == null) {
throw new RuntimeException(String.format("Cannot find service for the given path: %s", path));
}
/* Filter out /cluster/topology/service to get endpoint */
String[] pathService = path.split(REGEX_SPLIT_SERVICE_PATH);
final File servicesDir = new File(config.getGatewayServicesDir());
final Set<ServiceDefinition> serviceDefs = ServiceDefinitionsLoader.getServiceDefinitions(servicesDir);
/* URL used to connect to websocket backend */
String backendURL = urlFromServiceDefinition(serviceDefs, serviceRegistryService, entry, path);
StringBuffer backend = new StringBuffer();
try {
/* if we do not find websocket URL we default to HTTP */
if (!StringUtils.containsAny(backendURL, WEBSOCKET_PROTOCOL_STRING, SECURE_WEBSOCKET_PROTOCOL_STRING)) {
URL serviceUrl = new URL(backendURL);
/* Use http host:port if ws url not configured */
final String protocol = (serviceUrl.getProtocol() == "ws" || serviceUrl.getProtocol() == "wss") ? serviceUrl.getProtocol() : "ws";
backend.append(protocol).append("://");
backend.append(serviceUrl.getHost()).append(":");
backend.append(serviceUrl.getPort()).append("/");
backend.append(serviceUrl.getPath());
} else {
URI serviceUri = new URI(backendURL);
backend.append(serviceUri);
/* Avoid Zeppelin Regression - as this would require ambari changes and break current knox websocket use case*/
if (!StringUtils.endsWith(backend.toString(), "/ws") && pathService.length > 0 && pathService[1] != null) {
backend.append(pathService[1]);
}
}
backendURL = backend.toString();
} catch (MalformedURLException e) {
LOG.badUrlError(e);
throw new RuntimeException(e.toString());
} catch (Exception e1) {
LOG.failedCreatingWebSocket(e1);
throw new RuntimeException(e1.toString());
}
return backendURL;
}
Aggregations