19
19
import java .io .IOException ;
20
20
import java .lang .reflect .Method ;
21
21
import java .net .InetSocketAddress ;
22
+ import java .net .SocketAddress ;
22
23
import java .net .URI ;
23
24
import java .security .Principal ;
24
25
import java .util .ArrayList ;
@@ -143,13 +144,13 @@ public Principal getPrincipal() {
143
144
@ Override
144
145
public InetSocketAddress getLocalAddress () {
145
146
checkNativeSessionInitialized ();
146
- return getNativeSession (). getLocalAddress ();
147
+ return this . sessionHelper . getLocalAddress (getNativeSession () );
147
148
}
148
149
149
150
@ Override
150
151
public InetSocketAddress getRemoteAddress () {
151
152
checkNativeSessionInitialized ();
152
- return getNativeSession (). getRemoteAddress ();
153
+ return this . sessionHelper . getRemoteAddress (getNativeSession () );
153
154
}
154
155
155
156
/**
@@ -248,6 +249,11 @@ private interface SessionHelper {
248
249
int getTextMessageSizeLimit (Session session );
249
250
250
251
int getBinaryMessageSizeLimit (Session session );
252
+
253
+ InetSocketAddress getRemoteAddress (Session session );
254
+
255
+ InetSocketAddress getLocalAddress (Session session );
256
+
251
257
}
252
258
253
259
@@ -275,6 +281,16 @@ public int getTextMessageSizeLimit(Session session) {
275
281
public int getBinaryMessageSizeLimit (Session session ) {
276
282
return session .getPolicy ().getMaxBinaryMessageSize ();
277
283
}
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
+ }
278
294
}
279
295
280
296
@@ -284,11 +300,17 @@ private static class Jetty10SessionHelper implements SessionHelper {
284
300
285
301
private static final Method getBinaryMessageSizeLimitMethod ;
286
302
303
+ private static final Method getRemoteAddressMethod ;
304
+
305
+ private static final Method getLocalAddressMethod ;
306
+
287
307
static {
288
308
try {
289
309
Class <?> type = loader .loadClass ("org.eclipse.jetty.websocket.api.WebSocketPolicy" );
290
310
getTextMessageSizeLimitMethod = type .getMethod ("getMaxTextMessageSize" );
291
311
getBinaryMessageSizeLimitMethod = type .getMethod ("getMaxBinaryMessageSize" );
312
+ getRemoteAddressMethod = type .getMethod ("getRemoteAddress" );
313
+ getLocalAddressMethod = type .getMethod ("getLocalAddress" );
292
314
}
293
315
catch (ClassNotFoundException | NoSuchMethodException ex ) {
294
316
throw new IllegalStateException ("No compatible Jetty version found" , ex );
@@ -321,6 +343,22 @@ public int getBinaryMessageSizeLimit(Session session) {
321
343
Assert .state (result <= Integer .MAX_VALUE , "binaryMessageSizeLimit is larger than Integer.MAX_VALUE" );
322
344
return (int ) result ;
323
345
}
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
+ }
324
362
}
325
363
326
364
}
0 commit comments