Skip to content

Commit 6d01d49

Browse files
committed
Downcast to InetSocketAddress for Jetty 10
Closes gh-27120
1 parent a90ed95 commit 6d01d49

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.lang.reflect.Method;
2121
import java.net.InetSocketAddress;
22+
import java.net.SocketAddress;
2223
import java.net.URI;
2324
import java.security.Principal;
2425
import java.util.ArrayList;
@@ -143,13 +144,13 @@ public Principal getPrincipal() {
143144
@Override
144145
public InetSocketAddress getLocalAddress() {
145146
checkNativeSessionInitialized();
146-
return getNativeSession().getLocalAddress();
147+
return this.sessionHelper.getLocalAddress(getNativeSession());
147148
}
148149

149150
@Override
150151
public InetSocketAddress getRemoteAddress() {
151152
checkNativeSessionInitialized();
152-
return getNativeSession().getRemoteAddress();
153+
return this.sessionHelper.getRemoteAddress(getNativeSession());
153154
}
154155

155156
/**
@@ -248,6 +249,11 @@ private interface SessionHelper {
248249
int getTextMessageSizeLimit(Session session);
249250

250251
int getBinaryMessageSizeLimit(Session session);
252+
253+
InetSocketAddress getRemoteAddress(Session session);
254+
255+
InetSocketAddress getLocalAddress(Session session);
256+
251257
}
252258

253259

@@ -275,6 +281,16 @@ public int getTextMessageSizeLimit(Session session) {
275281
public int getBinaryMessageSizeLimit(Session session) {
276282
return session.getPolicy().getMaxBinaryMessageSize();
277283
}
284+
285+
@Override
286+
public InetSocketAddress getRemoteAddress(Session session) {
287+
return session.getRemoteAddress();
288+
}
289+
290+
@Override
291+
public InetSocketAddress getLocalAddress(Session session) {
292+
return session.getLocalAddress();
293+
}
278294
}
279295

280296

@@ -284,11 +300,17 @@ private static class Jetty10SessionHelper implements SessionHelper {
284300

285301
private static final Method getBinaryMessageSizeLimitMethod;
286302

303+
private static final Method getRemoteAddressMethod;
304+
305+
private static final Method getLocalAddressMethod;
306+
287307
static {
288308
try {
289309
Class<?> type = loader.loadClass("org.eclipse.jetty.websocket.api.WebSocketPolicy");
290310
getTextMessageSizeLimitMethod = type.getMethod("getMaxTextMessageSize");
291311
getBinaryMessageSizeLimitMethod = type.getMethod("getMaxBinaryMessageSize");
312+
getRemoteAddressMethod = type.getMethod("getRemoteAddress");
313+
getLocalAddressMethod = type.getMethod("getLocalAddress");
292314
}
293315
catch (ClassNotFoundException | NoSuchMethodException ex) {
294316
throw new IllegalStateException("No compatible Jetty version found", ex);
@@ -321,6 +343,22 @@ public int getBinaryMessageSizeLimit(Session session) {
321343
Assert.state(result <= Integer.MAX_VALUE, "binaryMessageSizeLimit is larger than Integer.MAX_VALUE");
322344
return (int) result;
323345
}
346+
347+
@Override
348+
@SuppressWarnings("ConstantConditions")
349+
public InetSocketAddress getRemoteAddress(Session session) {
350+
SocketAddress address = (SocketAddress) ReflectionUtils.invokeMethod(getRemoteAddressMethod, session);
351+
Assert.isInstanceOf(InetSocketAddress.class, address);
352+
return (InetSocketAddress) address;
353+
}
354+
355+
@Override
356+
@SuppressWarnings("ConstantConditions")
357+
public InetSocketAddress getLocalAddress(Session session) {
358+
SocketAddress address = (SocketAddress) ReflectionUtils.invokeMethod(getLocalAddressMethod, session);
359+
Assert.isInstanceOf(InetSocketAddress.class, address);
360+
return (InetSocketAddress) address;
361+
}
324362
}
325363

326364
}

0 commit comments

Comments
 (0)