• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aspectran / aspectran / #4014

25 Jan 2025 04:30PM UTC coverage: 35.455% (+0.008%) from 35.447%
#4014

push

github

topframe
Update

24 of 52 new or added lines in 13 files covered. (46.15%)

2 existing lines in 2 files now uncovered.

14289 of 40302 relevant lines covered (35.45%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.0
/with-undertow/src/main/java/com/aspectran/undertow/server/servlet/TowWebSocketServerContainerInitializer.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.undertow.server.servlet;
17

18
import com.aspectran.utils.annotation.jsr305.NonNull;
19
import com.aspectran.utils.logging.Logger;
20
import com.aspectran.utils.logging.LoggerFactory;
21
import io.undertow.connector.ByteBufferPool;
22
import io.undertow.server.DefaultByteBufferPool;
23
import io.undertow.server.session.Session;
24
import io.undertow.server.session.SessionListener;
25
import io.undertow.server.session.SessionManager;
26
import io.undertow.servlet.api.Deployment;
27
import io.undertow.websockets.core.CloseMessage;
28
import io.undertow.websockets.core.WebSocketChannel;
29
import io.undertow.websockets.core.WebSockets;
30
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
31

32
import java.util.ArrayList;
33
import java.util.List;
34
import java.util.Set;
35

36
/**
37
 * Initializer for WebSocket Support in Undertow.
38
 */
39
public class TowWebSocketServerContainerInitializer {
1✔
40

41
    private static final Logger logger = LoggerFactory.getLogger(TowWebSocketServerContainerInitializer.class);
1✔
42

43
    private static final String WEBSOCKET_CURRENT_CONNECTIONS_ATTR = "io.undertow.websocket.current-connections";
44

45
    private boolean directBuffers = false;
1✔
46

47
    private int bufferSize = 1024;
1✔
48

49
    private int maximumPoolSize = -1;
1✔
50

51
    private int threadLocalCacheSize = 12;
1✔
52

53
    public void setDirectBuffers(boolean directBuffers) {
54
        this.directBuffers = directBuffers;
1✔
55
    }
1✔
56

57
    public void setBufferSize(int bufferSize) {
58
        this.bufferSize = bufferSize;
1✔
59
    }
1✔
60

61
    public void setMaximumPoolSize(int maximumPoolSize) {
62
        this.maximumPoolSize = maximumPoolSize;
×
63
    }
×
64

65
    public void setThreadLocalCacheSize(int threadLocalCacheSize) {
66
        this.threadLocalCacheSize = threadLocalCacheSize;
×
67
    }
×
68

69
    public void initialize(@NonNull TowServletContext towServletContext) {
70
        if (!towServletContext.getServletContextAttributes().containsKey(WebSocketDeploymentInfo.ATTRIBUTE_NAME)) {
1✔
71
            ByteBufferPool byteBufferPool = new DefaultByteBufferPool(directBuffers, bufferSize, maximumPoolSize, threadLocalCacheSize);
1✔
72
            WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo().setBuffers(byteBufferPool);
1✔
73
            towServletContext.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, webSocketDeploymentInfo);
1✔
74
            towServletContext.addSessionListener(new WebSocketGracefulCloseListener());
1✔
75
        }
76
    }
1✔
77

78
    @SuppressWarnings("unchecked")
79
    public static void destroy(@NonNull Deployment deployment) {
80
        SessionManager sessionManager = deployment.getSessionManager();
1✔
81
        if (sessionManager != null) {
1✔
82
            Set<String> activeSessions = sessionManager.getActiveSessions();
1✔
83
            if (!activeSessions.isEmpty()) {
1✔
84
                activeSessions.forEach(sessionId -> {
1✔
85
                    Session session = sessionManager.getSession(sessionId);
1✔
86
                    Object value = session.getAttribute(WEBSOCKET_CURRENT_CONNECTIONS_ATTR);
1✔
87
                    if (value != null) {
1✔
NEW
88
                        closeWebSockets((List<WebSocketChannel>) value);
×
NEW
89
                        session.removeAttribute(WEBSOCKET_CURRENT_CONNECTIONS_ATTR);
×
90
                    }
91
                });
1✔
92
            }
93
        }
94
    }
1✔
95

96
    private static void closeWebSockets(List<WebSocketChannel> connections) {
97
        if (connections != null && !connections.isEmpty()) {
×
98
            CloseMessage closeMessage = new CloseMessage(CloseMessage.MSG_VIOLATES_POLICY, null);
×
99
            for (WebSocketChannel webSocketChannel : new ArrayList<>(connections)) {
×
NEW
100
                if (webSocketChannel != null && webSocketChannel.isOpen()) {
×
101
                    WebSockets.sendClose(closeMessage, webSocketChannel, null);
×
102
                }
103
            }
×
104
        }
105
    }
×
106

107
    public static class WebSocketGracefulCloseListener implements SessionListener {
1✔
108

109
        @Override
110
        public void attributeUpdated(Session session, String name, Object newValue, Object oldValue) {
111
            if (oldValue != null && oldValue != newValue) {
×
112
                closeWebSockets(name, oldValue);
×
113
            }
114
        }
×
115

116
        @Override
117
        public void attributeRemoved(Session session, String name, Object oldValue) {
118
            if (oldValue != null) {
×
119
                closeWebSockets(name, oldValue);
×
120
            }
121
        }
×
122

123
        @SuppressWarnings("unchecked")
124
        private void closeWebSockets(@NonNull String name, @NonNull Object value) {
125
            if (WEBSOCKET_CURRENT_CONNECTIONS_ATTR.equals(name)) {
×
UNCOV
126
                List<WebSocketChannel> connections = (List<WebSocketChannel>)value;
×
127
                TowWebSocketServerContainerInitializer.closeWebSockets(connections);
×
128
            }
129
        }
×
130

131
    }
132

133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc