Skip to content

Commit

Permalink
TYRUS-367: review, refactoring (naming, error reporting, TyrusSession…
Browse files Browse the repository at this point in the history
… and test modification)
  • Loading branch information
pavelbucek committed Sep 17, 2014
1 parent 1680cc6 commit bf5953f
Show file tree
Hide file tree
Showing 22 changed files with 407 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
import org.glassfish.tyrus.core.RequestContext;
import org.glassfish.tyrus.core.TyrusEndpointWrapper;
import org.glassfish.tyrus.core.TyrusExtension;
import org.glassfish.tyrus.core.TyrusSession;
import org.glassfish.tyrus.core.TyrusWebSocket;
import org.glassfish.tyrus.core.Utils;
import org.glassfish.tyrus.core.Version;
Expand Down Expand Up @@ -522,11 +521,11 @@ public ClientUpgradeStatus getUpgradeStatus() {
}

@Override
public Connection createConnection(Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
public Connection createConnection(Map<Connection.ConnectionProperties, Object> connectionProperties) {

Utils.validateConnectionProperties(connectionProperties);

final TyrusSession sessionForRemoteEndpoint = endpointWrapper.createSessionForRemoteEndpoint(
final Session sessionForRemoteEndpoint = endpointWrapper.createSessionForRemoteEndpoint(
socket,
upgradeResponse.getFirstHeaderValue(HandshakeRequest.SEC_WEBSOCKET_PROTOCOL),
extensions,
Expand Down Expand Up @@ -692,7 +691,7 @@ public ClientUpgradeStatus getUpgradeStatus() {
}

@Override
public Connection createConnection(Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
public Connection createConnection(Map<Connection.ConnectionProperties, Object> connectionProperties) {
return null;
}
};
Expand All @@ -705,7 +704,7 @@ public ClientUpgradeStatus getUpgradeStatus() {
}

@Override
public Connection createConnection(Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
public Connection createConnection(Map<Connection.ConnectionProperties, Object> connectionProperties) {
return null;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class GrizzlyClientFilter extends BaseFilter {
private final Filter sslFilter;
private final HttpCodecFilter httpCodecFilter;
private final ClientEngine engine;
private final URI uri;
private final boolean sharedTransport;
private final Map<String, String> proxyHeaders;
private final Callable<Void> grizzlyConnector;
Expand All @@ -128,15 +127,14 @@ class GrizzlyClientFilter extends BaseFilter {
*/
/* package */ GrizzlyClientFilter(ClientEngine engine, boolean proxy,
Filter sslFilter, HttpCodecFilter httpCodecFilter,
URI uri, boolean sharedTransport,
boolean sharedTransport,
Map<String, String> proxyHeaders,
Callable<Void> grizzlyConnector,
UpgradeRequest upgradeRequest) {
this.engine = engine;
this.proxy = proxy;
this.sslFilter = sslFilter;
this.httpCodecFilter = httpCodecFilter;
this.uri = uri;
this.sharedTransport = sharedTransport;
this.proxyHeaders = proxyHeaders;
this.grizzlyConnector = grizzlyConnector;
Expand Down Expand Up @@ -369,7 +367,7 @@ public void close(CloseReason reason) {
}
return ctx.getInvokeAction();
case SUCCESS:
Map<org.glassfish.tyrus.spi.Connection.ConnectionPropertyKey, Object> connectionProperties = Utils.getConnectionProperties(
Map<org.glassfish.tyrus.spi.Connection.ConnectionProperties, Object> connectionProperties = Utils.getConnectionProperties(
(InetSocketAddress) ctx.getConnection().getLocalAddress(),
(InetSocketAddress) ctx.getAddress());
tyrusConnection = clientUpgradeInfo.createConnection(connectionProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ protected void notifyHandshakeFailed(Connection connection, Throwable t) {


clientFilterChainBuilder.add(new GrizzlyClientFilter(engine, proxy,
sslFilter, httpCodecFilter, uri, sharedTransport, proxyHeaders, grizzlyConnector, upgradeRequest));
sslFilter, httpCodecFilter, sharedTransport, proxyHeaders, grizzlyConnector, upgradeRequest));

return clientFilterChainBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private NextAction handleHandshake(final FilterChainContext ctx, HttpContent con
final Connection grizzlyConnection = ctx.getConnection();
write(ctx, upgradeRequest, upgradeResponse);

Map<org.glassfish.tyrus.spi.Connection.ConnectionPropertyKey, Object> connectionProperties = Utils.getConnectionProperties(
Map<org.glassfish.tyrus.spi.Connection.ConnectionProperties, Object> connectionProperties = Utils.getConnectionProperties(
(InetSocketAddress) ctx.getConnection().getLocalAddress(),
(InetSocketAddress) ctx.getConnection().getPeerAddress());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void close() throws IOException {
}
};

final Map<Connection.ConnectionPropertyKey, Object> connectionProperties = Utils.getConnectionProperties(new InetSocketAddress(1), new InetSocketAddress(1));
final Map<Connection.ConnectionProperties, Object> connectionProperties = Utils.getConnectionProperties(new InetSocketAddress(1), new InetSocketAddress(1));

final Connection serverConnection = upgradeInfo.createConnection(serverWriter, null, connectionProperties);
final ClientEngine.ClientUpgradeInfo clientClientUpgradeInfo = clientEngine.processResponse(upgradeResponse, clientWriter, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ClientFilter extends Filter {
private volatile Connection wsConnection;
private volatile boolean connectedToProxy = false;
private volatile CompletionHandler<Void> connectCompletionHandler;
private volatile Map<Connection.ConnectionPropertyKey, Object> connectionProperties;
private volatile Map<Connection.ConnectionProperties, Object> connectionProperties;

/**
* Constructor.
Expand Down Expand Up @@ -120,7 +120,7 @@ void connect(SocketAddress address, boolean proxy, CompletionHandler<Void> conne
}

@Override
public void processConnect(Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
public void processConnect(Map<Connection.ConnectionProperties, Object> connectionProperties) {
this.connectionProperties = connectionProperties;
final JdkUpgradeRequest handshakeUpgradeRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ final void connect(SocketAddress address, Filter upstreamFilter) {
*
* @param connectionProperties connection related properties like local/remote IP addresses, port numbers and hostnames.
*/
final void onConnect(Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
final void onConnect(Map<Connection.ConnectionProperties, Object> connectionProperties) {
processConnect(connectionProperties);

if (upstreamFilter != null) {
Expand Down Expand Up @@ -203,7 +203,7 @@ void handleConnect(SocketAddress address, Filter upstreamFilter) {
* @param connectionProperties connection related properties like local/remote IP addresses, port numbers and hostnames.
* @see #onConnect(Map)
*/
void processConnect(Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
void processConnect(Map<Connection.ConnectionProperties, Object> connectionProperties) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,14 @@ public void handleConnect(SocketAddress serverAddress, Filter upstreamFilter) {

@Override
public void completed(Void result, Void nothing) {
Map<Connection.ConnectionPropertyKey, Object> connectionProperties;
Map<Connection.ConnectionProperties, Object> connectionProperties;
try {
connectionProperties = Utils.getConnectionProperties(
(InetSocketAddress) socketChannel.getLocalAddress(),
(InetSocketAddress) socketChannel.getRemoteAddress());
} catch (IOException e) {
throw new IllegalStateException(e);
onError(e);
return;
}
onConnect(connectionProperties);
final ByteBuffer inputBuffer = ByteBuffer.allocate(inputBufferSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class TyrusHttpUpgradeHandler implements HttpUpgradeHandler, ReadListener
private Connection connection;
private WebSocketEngine.UpgradeInfo upgradeInfo;
private Writer writer;
private Map<Connection.ConnectionPropertyKey, Object> connectionProperties;
private Map<Connection.ConnectionProperties, Object> connectionProperties;


private boolean authenticated = false;
Expand Down Expand Up @@ -124,7 +124,7 @@ public void close(CloseReason reason) {
connectionLatch.countDown();
}

public void preInit(WebSocketEngine.UpgradeInfo upgradeInfo, Writer writer, boolean authenticated, Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
public void preInit(WebSocketEngine.UpgradeInfo upgradeInfo, Writer writer, boolean authenticated, Map<Connection.ConnectionProperties, Object> connectionProperties) {
this.upgradeInfo = upgradeInfo;
this.writer = writer;
this.authenticated = authenticated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void sessionDestroyed() {
}

@Override
public void preInit(WebSocketEngine.UpgradeInfo upgradeInfo, Writer writer, boolean authenticated, Map<Connection.ConnectionPropertyKey, Object> connectionProperties) {
public void preInit(WebSocketEngine.UpgradeInfo upgradeInfo, Writer writer, boolean authenticated, Map<Connection.ConnectionProperties, Object> connectionProperties) {
handler.preInit(upgradeInfo, writer, authenticated, connectionProperties);
}

Expand Down Expand Up @@ -265,13 +265,13 @@ public boolean isUserInRole(String role) {
handler.setIncomingBufferSize(Integer.parseInt(frameBufferSize));
}

Map<Connection.ConnectionPropertyKey, Object> connectionProperties = new HashMap<>(6);
connectionProperties.put(Connection.ConnectionPropertyKey.REMOTE_ADDR, httpServletRequest.getRemoteAddr());
connectionProperties.put(Connection.ConnectionPropertyKey.REMOTE_HOSTNAME, httpServletRequest.getRemoteHost());
connectionProperties.put(Connection.ConnectionPropertyKey.REMOTE_PORT, httpServletRequest.getRemotePort());
connectionProperties.put(Connection.ConnectionPropertyKey.LOCAL_ADDR, httpServletRequest.getLocalAddr());
connectionProperties.put(Connection.ConnectionPropertyKey.LOCAL_HOSTNAME, httpServletRequest.getLocalName());
connectionProperties.put(Connection.ConnectionPropertyKey.LOCAL_PORT, httpServletRequest.getLocalPort());
Map<Connection.ConnectionProperties, Object> connectionProperties = new HashMap<Connection.ConnectionProperties, Object>(6);
connectionProperties.put(Connection.ConnectionProperties.REMOTE_ADDRESS, httpServletRequest.getRemoteAddr());
connectionProperties.put(Connection.ConnectionProperties.REMOTE_HOSTNAME, httpServletRequest.getRemoteHost());
connectionProperties.put(Connection.ConnectionProperties.REMOTE_PORT, httpServletRequest.getRemotePort());
connectionProperties.put(Connection.ConnectionProperties.LOCAL_ADDRESS, httpServletRequest.getLocalAddr());
connectionProperties.put(Connection.ConnectionProperties.LOCAL_HOSTNAME, httpServletRequest.getLocalName());
connectionProperties.put(Connection.ConnectionProperties.LOCAL_PORT, httpServletRequest.getLocalPort());

handler.preInit(upgradeInfo, webSocketConnection, httpServletRequest.getUserPrincipal() != null, Collections.unmodifiableMap(connectionProperties));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import org.glassfish.tyrus.core.frame.TyrusFrame;
import org.glassfish.tyrus.core.l10n.LocalizationMessages;
import org.glassfish.tyrus.core.monitoring.EndpointEventListener;
import org.glassfish.tyrus.spi.ClientEngine;
import org.glassfish.tyrus.spi.Connection;
import org.glassfish.tyrus.spi.UpgradeRequest;
import org.glassfish.tyrus.spi.UpgradeResponse;
Expand Down Expand Up @@ -604,18 +605,20 @@ Set<RemoteSession> getRemoteSessions() {
* @param connectionProperties connection related properties like remote/local IP addresses, port numbers or hostnames.
* @param debugContext debug context.
* @return {@link Session} representing the connection.
* @see ClientEngine.ClientUpgradeInfo#createConnection(Map)
*/
public TyrusSession createSessionForRemoteEndpoint(TyrusWebSocket socket, String subprotocol, List<Extension> extensions,
Map<Connection.ConnectionPropertyKey, Object> connectionProperties, DebugContext debugContext) {

final InetAddress remoteInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_INET_ADDRESS);
final String remoteAddr = (String) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_ADDR);
final String remoteHostName = (String) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_HOSTNAME);
final int remotePort = (Integer) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_PORT);
final InetAddress localInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_INET_ADDRESS);
final String localAddr = (String) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_ADDR);
final String localHostName = (String) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_HOSTNAME);
final int localPort = (Integer) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_PORT);
Map<Connection.ConnectionProperties, Object> connectionProperties,
DebugContext debugContext) {

final InetAddress remoteInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionProperties.REMOTE_INET_ADDRESS);
final String remoteAddr = (String) connectionProperties.get(Connection.ConnectionProperties.REMOTE_ADDRESS);
final String remoteHostName = (String) connectionProperties.get(Connection.ConnectionProperties.REMOTE_HOSTNAME);
final int remotePort = (Integer) connectionProperties.get(Connection.ConnectionProperties.REMOTE_PORT);
final InetAddress localInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionProperties.LOCAL_INET_ADDRESS);
final String localAddr = (String) connectionProperties.get(Connection.ConnectionProperties.LOCAL_ADDRESS);
final String localHostName = (String) connectionProperties.get(Connection.ConnectionProperties.LOCAL_HOSTNAME);
final int localPort = (Integer) connectionProperties.get(Connection.ConnectionProperties.LOCAL_PORT);

final TyrusSession session = new TyrusSession(container, socket, this, subprotocol, extensions, false,
getURI(contextPath, null), null, Collections.<String, String>emptyMap(), null, Collections.<String, List<String>>emptyMap(), null, null,
Expand All @@ -634,7 +637,7 @@ private TyrusSession getSession(TyrusWebSocket socket) {
*
* @param socket {@link TyrusWebSocket} who has just connected to this web socket endpoint.
* @param upgradeRequest request associated with accepted connection.
* @param subProtocol used subprotocol
* @param subProtocol used subprotocol.
* @param extensions used extensions.
* @param connectionId connection id obtained from request or response header {@value UpgradeRequest#CLUSTER_CONNECTION_ID_HEADER}.
* @param connectionProperties connection related properties like remote/local IP addresses, port numbers or hostnames.
Expand All @@ -643,7 +646,7 @@ private TyrusSession getSession(TyrusWebSocket socket) {
* limit on endpoint or application or issues with endpoint validation).
*/
Session onConnect(TyrusWebSocket socket, UpgradeRequest upgradeRequest, String subProtocol, List<Extension> extensions,
String connectionId, Map<Connection.ConnectionPropertyKey, Object> connectionProperties, DebugContext debugContext) {
String connectionId, Map<Connection.ConnectionProperties, Object> connectionProperties, DebugContext debugContext) {
TyrusSession session = webSocketToSession.get(socket);
// session is null on Server; client always has session instance at this point.
if (session == null) {
Expand All @@ -653,14 +656,14 @@ Session onConnect(TyrusWebSocket socket, UpgradeRequest upgradeRequest, String s
templateValues.put(entry.getKey(), entry.getValue().get(0));
}

final InetAddress remoteInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_INET_ADDRESS);
final String remoteAddr = (String) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_ADDR);
final String remoteHostName = (String) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_HOSTNAME);
final int remotePort = (Integer) connectionProperties.get(Connection.ConnectionPropertyKey.REMOTE_PORT);
final InetAddress localInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_INET_ADDRESS);
final String localAddr = (String) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_ADDR);
final String localHostName = (String) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_HOSTNAME);
final int localPort = (Integer) connectionProperties.get(Connection.ConnectionPropertyKey.LOCAL_PORT);
final InetAddress remoteInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionProperties.REMOTE_INET_ADDRESS);
final String remoteAddr = (String) connectionProperties.get(Connection.ConnectionProperties.REMOTE_ADDRESS);
final String remoteHostName = (String) connectionProperties.get(Connection.ConnectionProperties.REMOTE_HOSTNAME);
final int remotePort = (Integer) connectionProperties.get(Connection.ConnectionProperties.REMOTE_PORT);
final InetAddress localInetAddress = (InetAddress) connectionProperties.get(Connection.ConnectionProperties.LOCAL_INET_ADDRESS);
final String localAddr = (String) connectionProperties.get(Connection.ConnectionProperties.LOCAL_ADDRESS);
final String localHostName = (String) connectionProperties.get(Connection.ConnectionProperties.LOCAL_HOSTNAME);
final int localPort = (Integer) connectionProperties.get(Connection.ConnectionProperties.LOCAL_PORT);

// create a new session
session = new TyrusSession(container, socket, this, subProtocol, extensions, upgradeRequest.isSecure(),
Expand Down
Loading

0 comments on commit bf5953f

Please sign in to comment.