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

com.ithit.webdav.integration.spring.websocket.WebSocketServer Maven / Gradle / Ivy

There is a newer version: 7.3.10641
Show newest version
package com.ithit.webdav.integration.spring.websocket;

import com.ithit.webdav.integration.servlet.websocket.DavWebSocketNotification;
import com.ithit.webdav.integration.servlet.websocket.MovedDavWebSocketNotification;
import com.ithit.webdav.server.util.StringUtil;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;
import java.util.stream.Collectors;

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

/**
 * WebSocket server, creates web socket endpoint, handles client's sessions
 */
public class WebSocketServer {

    private final List sessions;

    public WebSocketServer(List sessions) {
        this.sessions = sessions;
    }

    /**
     * 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 TextMessage textMessage = new TextMessage(new DavWebSocketNotification(itemPath, operation).toString());
        send(clientId, textMessage);
    }

    /**
     * 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 targetPath 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 TextMessage textMessage = new TextMessage(new MovedDavWebSocketNotification(itemPath, "moved", targetPath).toString());
        send(clientId, textMessage);
    }

    /**
     * Send TextMessage to all sessions but initiator
     * @param clientId      Id of the initiator
     * @param textMessage   Message
     */
    private void send(String clientId, TextMessage textMessage) {
        for (WebSocketSession session: StringUtil.isNullOrEmpty(clientId)
                ? sessions
                : sessions.stream().filter(x -> !x.getAttributes().get(INSTANCE_HEADER_NAME).equals(clientId)).collect(Collectors.toSet())) {
            try {
                session.sendMessage(textMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy