Skip to content

Commit

Permalink
Downcast to InetSocketAddress for Jetty 10
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev authored and lxbzmy committed Mar 26, 2022
1 parent a0970e7 commit 43b5b2d
Showing 1 changed file with 40 additions and 2 deletions.
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
Expand Down Expand Up @@ -143,13 +144,13 @@ public Principal getPrincipal() {
@Override
public InetSocketAddress getLocalAddress() {
checkNativeSessionInitialized();
return getNativeSession().getLocalAddress();
return this.sessionHelper.getLocalAddress(getNativeSession());
}

@Override
public InetSocketAddress getRemoteAddress() {
checkNativeSessionInitialized();
return getNativeSession().getRemoteAddress();
return this.sessionHelper.getRemoteAddress(getNativeSession());
}

/**
Expand Down Expand Up @@ -248,6 +249,11 @@ private interface SessionHelper {
int getTextMessageSizeLimit(Session session);

int getBinaryMessageSizeLimit(Session session);

InetSocketAddress getRemoteAddress(Session session);

InetSocketAddress getLocalAddress(Session session);

}


Expand Down Expand Up @@ -275,6 +281,16 @@ public int getTextMessageSizeLimit(Session session) {
public int getBinaryMessageSizeLimit(Session session) {
return session.getPolicy().getMaxBinaryMessageSize();
}

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

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


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

private static final Method getBinaryMessageSizeLimitMethod;

private static final Method getRemoteAddressMethod;

private static final Method getLocalAddressMethod;

static {
try {
Class<?> type = loader.loadClass("org.eclipse.jetty.websocket.api.WebSocketPolicy");
getTextMessageSizeLimitMethod = type.getMethod("getMaxTextMessageSize");
getBinaryMessageSizeLimitMethod = type.getMethod("getMaxBinaryMessageSize");
getRemoteAddressMethod = type.getMethod("getRemoteAddress");
getLocalAddressMethod = type.getMethod("getLocalAddress");
}
catch (ClassNotFoundException | NoSuchMethodException ex) {
throw new IllegalStateException("No compatible Jetty version found", ex);
Expand Down Expand Up @@ -321,6 +343,22 @@ public int getBinaryMessageSizeLimit(Session session) {
Assert.state(result <= Integer.MAX_VALUE, "binaryMessageSizeLimit is larger than Integer.MAX_VALUE");
return (int) result;
}

@Override
@SuppressWarnings("ConstantConditions")
public InetSocketAddress getRemoteAddress(Session session) {
SocketAddress address = (SocketAddress) ReflectionUtils.invokeMethod(getRemoteAddressMethod, session);
Assert.isInstanceOf(InetSocketAddress.class, address);
return (InetSocketAddress) address;
}

@Override
@SuppressWarnings("ConstantConditions")
public InetSocketAddress getLocalAddress(Session session) {
SocketAddress address = (SocketAddress) ReflectionUtils.invokeMethod(getLocalAddressMethod, session);
Assert.isInstanceOf(InetSocketAddress.class, address);
return (InetSocketAddress) address;
}
}

}

0 comments on commit 43b5b2d

Please sign in to comment.