Java Code Examples for org.springframework.web.socket.WebSocketSession#getUri()

The following examples show how to use org.springframework.web.socket.WebSocketSession#getUri() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyWebSocketHandler.java    From spring-cloud-netflix-zuul-websocket with Apache License 2.0 6 votes vote down vote up
private void connectToProxiedTarget(WebSocketSession session) {
    URI sessionUri = session.getUri();
    ZuulWebSocketProperties.WsBrokerage wsBrokerage = getWebSocketBrokarage(
            sessionUri);

    Assert.notNull(wsBrokerage, "wsBrokerage must not be null");

    String path = getWebSocketServerPath(wsBrokerage, sessionUri);
    Assert.notNull(path, "Web socket uri path must be null");

    URI routeTarget = proxyTargetResolver.resolveTarget(wsBrokerage);

    Assert.notNull(routeTarget, "routeTarget must not be null");

    String uri = ServletUriComponentsBuilder
            .fromUri(routeTarget)
            .path(path)
            .replaceQuery(sessionUri.getQuery())
            .toUriString();

    ProxyWebSocketConnectionManager connectionManager = new ProxyWebSocketConnectionManager(
            messagingTemplate, stompClient, session, headersCallback, uri);
    connectionManager.errorHandler(this.errorHandler);
    managers.put(session, connectionManager);
    connectionManager.start();
}
 
Example 2
Source File: WebSocketProxyServerHandler.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private String[] getUriParts(WebSocketSession webSocketSession) {
    URI uri = webSocketSession.getUri();
    String[] uriParts = null;
    if (uri != null && uri.getPath() != null) {
        uriParts = uri.getPath().split(SEPARATOR, 5);
    }
    return uriParts;
}
 
Example 3
Source File: PluginWebSocketHandler.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private PluginWebsocketSessionRef toRef(WebSocketSession session) throws IOException {
    URI sessionUri = session.getUri();
    String path = sessionUri.getPath();
    path = path.substring(WebSocketConfiguration.WS_PLUGIN_PREFIX.length());
    if (path.length() == 0) {
        throw new IllegalArgumentException("URL should contain plugin token!");
    }
    String[] pathElements = path.split("/");
    String pluginToken = pathElements[0];
    // TODO: cache
    PluginMetaData pluginMd = pluginService.findPluginByApiToken(pluginToken);
    if (pluginMd == null) {
        throw new InvalidParameterException("Can't find plugin with specified token!");
    } else {
        SecurityUser currentUser = (SecurityUser) session.getAttributes().get(WebSocketConfiguration.WS_SECURITY_USER_ATTRIBUTE);
        TenantId tenantId = currentUser.getTenantId();
        CustomerId customerId = currentUser.getCustomerId();
        if (PluginApiController.validatePluginAccess(pluginMd, tenantId, customerId)) {
            PluginApiCallSecurityContext securityCtx = new PluginApiCallSecurityContext(pluginMd.getTenantId(), pluginMd.getId(), tenantId,
                    currentUser.getCustomerId());
            return new BasicPluginWebsocketSessionRef(UUID.randomUUID().toString(), securityCtx, session.getUri(), session.getAttributes(),
                    session.getLocalAddress(), session.getRemoteAddress());
        } else {
            throw new SecurityException("Current user is not allowed to use this plugin!");
        }
    }
}
 
Example 4
Source File: WsTtyHandler.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void afterConnectionEstablished(WebSocketSession session) {
    URI uri = session.getUri();
    try {
        UriComponents uc = UriComponentsBuilder.fromUri(uri).build();
        MultiValueMap<String, String> params = uc.getQueryParams();
        String containerId = params.getFirst("container");
        try(TempAuth ta = withAuth(session)) {
            connectToContainer(session, containerId);
        }
    } catch (Exception e) {
        log.error("Can not establish connection for '{}' due to error:", uri, e);
    }
}