All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ithit.webdav.integration.servlet.websocket.DavWebSocketEndpoint Maven / Gradle / Ivy

package com.ithit.webdav.integration.servlet.websocket;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import jakarta.websocket.CloseReason;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.Session;

import com.ithit.webdav.server.util.StringUtil;

import static com.ithit.webdav.integration.utils.IntegrationUtil.INSTANCE_HEADER_NAME;

/**
 * Represents WebSocket server. Encapsulates main WebSocket logic. Made abstract to be not instantiated by default.
 */
public abstract class DavWebSocketEndpoint extends Endpoint {

    private static final Map SESSIONS = new HashMap<>();
    private static DavWebSocketEndpoint instance;

    public static DavWebSocketEndpoint getInstance() {
        return instance;
    }

    private static void setInstance(DavWebSocketEndpoint instance) {
        DavWebSocketEndpoint.instance = instance;
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        SESSIONS.remove(session.getId());
        setInstance(this);
    }

    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
        SESSIONS.put(session.getId(), new WebSocketClient((String) endpointConfig.getUserProperties().get(INSTANCE_HEADER_NAME), session));
        setInstance(this);
    }

    /**
     * Send notification to the client
     *
     * @param itemPath   File/Folder path.
     * @param operation  Operation name: created/updated/deleted/moved
     * @param clientId Current clientId.
     */
    private void send(String itemPath, String operation, String clientId) {
        itemPath = StringUtil.trimEnd(StringUtil.trimStart(itemPath, "/"), "/");
        final DavWebSocketNotification notification = new DavWebSocketNotification(itemPath, operation);
        for (WebSocketClient s :
                StringUtil.isNullOrEmpty(clientId)
                        ? SESSIONS.values()
                        : SESSIONS.values().stream().filter(webSocketClient -> !webSocketClient.instanceId.equals(clientId)).collect(Collectors.toSet())) {
            if (s.session.isOpen()) {
                s.session.getAsyncRemote().sendObject(notification);
            }
        }
    }

    /**
     * Notifies client that file/folder was created.
     *
     * @param itemPath file/folder.
     * @param clientId Current clientId.
     */
    public void notifyCreated(String itemPath, String clientId) {
        send(itemPath, "created", clientId);
    }

    /**
     * Notifies client that file/folder was updated.
     *
     * @param itemPath file/folder.
     * @param clientId Current clientId.
     */
    public void notifyUpdated(String itemPath, String clientId) {
        send(itemPath, "updated", clientId);
    }

    /**
     * Notifies client that file/folder was deleted.
     *
     * @param itemPath file/folder.
     * @param clientId Current clientId.
     */
    public void notifyDeleted(String itemPath, String clientId) {
        send(itemPath, "deleted", clientId);
    }

    /**
     * Notifies client that file/folder was locked.
     *
     * @param itemPath file/folder.
     * @param clientId Current clientId.
     */
    public void notifyLocked(String itemPath, String clientId) {
        send(itemPath, "locked", clientId);
    }

    /**
     * Notifies client that file/folder was unlocked.
     *
     * @param itemPath file/folder.
     * @param clientId Current clientId.
     */
    public void notifyUnlocked(String itemPath, String clientId) {
        send(itemPath, "unlocked", clientId);
    }

    /**
     * Notifies client that file/folder was moved.
     *
     * @param itemPath file/folder.
     * @param clientId Current clientId.
     */
    public void notifyMoved(String itemPath, String targetPath, String clientId) {
        itemPath = StringUtil.trimEnd(StringUtil.trimStart(itemPath, "/"), "/");
        targetPath = StringUtil.trimEnd(StringUtil.trimStart(targetPath, "/"), "/");
        final MovedDavWebSocketNotification movedNotification = new MovedDavWebSocketNotification(itemPath, "moved", targetPath);
        for (WebSocketClient s :
                clientId != null ?
                        SESSIONS.values().stream().filter(webSocketClient -> !webSocketClient.instanceId.equals(clientId)).collect(Collectors.toSet()) :
                        SESSIONS.values()) {
            if (s.session.isOpen()) {
                s.session.getAsyncRemote().sendObject(movedNotification);
            }
        }
    }

    private static class WebSocketClient {
        private final String instanceId;
        private final Session session;

        public WebSocketClient(String instanceId, Session session) {
            this.instanceId = instanceId;
            this.session = session;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy