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

com.cudoy.framework.websocket.session.JsonWebSocketSession Maven / Gradle / Ivy

package com.cudoy.framework.websocket.session;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.cudoy.framework.core.support.CommonUser;
import com.cudoy.framework.core.utils.JsonUtils;
import com.cudoy.framework.web.utils.RequestUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.*;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.Principal;
import java.util.List;
import java.util.Map;

public class JsonWebSocketSession implements WebSocketSession{

    private long lastUpdateTime;

    public long getLastUpdateTime() {
        return lastUpdateTime;
    }
    public void setLastUpdateTime(long lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    @JsonIgnore
    private WebSocketSession webSocketSession;

    public CommonUser getUser(){
        if(webSocketSession == null || webSocketSession.getAttributes() == null){
            return null;
        }
        CommonUser commonUser = (CommonUser)webSocketSession.getAttributes().get(CommonUser.class.getName());
        return commonUser;
    }

    public String getSessionId(){
        String sessionId = (String)webSocketSession.getAttributes().get(RequestUtils.SESSION_ID);
        return sessionId;
    }

    public JsonWebSocketSession(WebSocketSession webSocketSession){
        this.webSocketSession  = webSocketSession;
    }

    public WebSocketSession getWebSocketSession() {
        return webSocketSession;
    }

    public void setWebSocketSession(WebSocketSession webSocketSession) {
        this.webSocketSession = webSocketSession;
    }

    @Override
    public String getId() {
        return webSocketSession.getId();
    }

    @Override
    public URI getUri() {
        return webSocketSession.getUri();
    }

    @Override
    public HttpHeaders getHandshakeHeaders() {
        return webSocketSession.getHandshakeHeaders();
    }

    @Override
    public Map getAttributes() {
        return webSocketSession.getAttributes();
    }

    @Override
    public Principal getPrincipal() {
        return webSocketSession.getPrincipal();
    }

    @Override
    public InetSocketAddress getLocalAddress() {
        return webSocketSession.getLocalAddress();
    }

    @Override
    public InetSocketAddress getRemoteAddress() {
        return webSocketSession.getRemoteAddress();
    }

    @Override
    public String getAcceptedProtocol() {
        return webSocketSession.getAcceptedProtocol();
    }

    @Override
    public void setTextMessageSizeLimit(int messageSizeLimit) {
        webSocketSession.setTextMessageSizeLimit(messageSizeLimit);
    }

    @Override
    public int getTextMessageSizeLimit() {
        return webSocketSession.getTextMessageSizeLimit();
    }

    @Override
    public void setBinaryMessageSizeLimit(int messageSizeLimit) {
        webSocketSession.setBinaryMessageSizeLimit(messageSizeLimit);
    }

    @Override
    public int getBinaryMessageSizeLimit() {
        return webSocketSession.getBinaryMessageSizeLimit();
    }

    @Override
    public List getExtensions() {
        return webSocketSession.getExtensions();
    }

    @Override
    public void sendMessage(WebSocketMessage message) throws IOException {
        webSocketSession.sendMessage(message);
    }

    public void sendMessage(String message) throws IOException{
        synchronized (webSocketSession) {
            sendMessage(new TextMessage(message));
        }
    }

    public boolean sendJsonMessage(Map message){
        synchronized (webSocketSession) {
            try {
                sendMessage(new TextMessage(JsonUtils.toJson(message)));
            } catch (IOException e) {
                return false;
            }
            return true;
        }
    }

    @Override
    public boolean isOpen() {
        return webSocketSession.isOpen();
    }

    @Override
    public void close() throws IOException {
        webSocketSession.close();
    }

    @Override
    public void close(CloseStatus status) throws IOException {
        webSocketSession.close(status);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        JsonWebSocketSession that = (JsonWebSocketSession) o;

        return !(webSocketSession != null ? !webSocketSession.equals(that.webSocketSession) : that.webSocketSession != null);

    }

    @Override
    public int hashCode() {
        return webSocketSession != null ? webSocketSession.hashCode() : 0;
    }

    @Override
    public String toString() {
        CommonUser user = getUser();
        return "JsonWebSocketSession{" +
                (user == null?"Anonymous":"User[" + user.getCommonName() + "]") +
                "session=" + webSocketSession +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy