Repository: wicket Updated Branches: refs/heads/master 12305d9f5 -> 3d9ba812d
WICKET-6385 Allow using custom port for web socket connections Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/3d9ba812 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/3d9ba812 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/3d9ba812 Branch: refs/heads/master Commit: 3d9ba812d4311376a88136822b0430319e3f6e16 Parents: 2ec11f1 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Tue May 30 22:46:17 2017 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Tue May 30 22:47:05 2017 +0200 ---------------------------------------------------------------------- .../examples/websocket/JSR356Application.java | 10 +++++ .../wicket/protocol/ws/WebSocketSettings.java | 42 +++++++++++++++++++- .../protocol/ws/api/BaseWebSocketBehavior.java | 15 +++++++ .../ws/api/res/js/wicket-websocket-jquery.js | 12 +++++- .../api/res/js/wicket-websocket-setup.js.tmpl | 2 +- 5 files changed, 77 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/3d9ba812/wicket-examples/src/main/java/org/apache/wicket/examples/websocket/JSR356Application.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/websocket/JSR356Application.java b/wicket-examples/src/main/java/org/apache/wicket/examples/websocket/JSR356Application.java index b0275a4..1d35e42 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/websocket/JSR356Application.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/websocket/JSR356Application.java @@ -20,6 +20,7 @@ import org.apache.wicket.examples.websocket.charts.ChartWebSocketResource; import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.protocol.https.HttpsConfig; import org.apache.wicket.protocol.https.HttpsMapper; +import org.apache.wicket.protocol.ws.WebSocketSettings; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -51,6 +52,15 @@ public class JSR356Application extends WebApplication mountPage("/resource", WebSocketResourceDemoPage.class); getSharedResources().add(ChartWebSocketResource.NAME, new ChartWebSocketResource()); + + if (System.getenv("OPENSHIFT_APP_NAME") != null) + { + // OpenShift uses special proxy for WebSocket connections + // https://blog.openshift.com/paas-websockets/ + final WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(this); + webSocketSettings.setPort(8000); + webSocketSettings.setSecurePort(8443); + } } @Override http://git-wip-us.apache.org/repos/asf/wicket/blob/3d9ba812/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/WebSocketSettings.java ---------------------------------------------------------------------- diff --git a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/WebSocketSettings.java b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/WebSocketSettings.java index dd28e98..d3ae9d1 100644 --- a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/WebSocketSettings.java +++ b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/WebSocketSettings.java @@ -39,7 +39,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; -import java.util.concurrent.Callable; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; /** @@ -79,6 +79,8 @@ public class WebSocketSettings private final AtomicReference<CharSequence> filterPrefix = new AtomicReference<>(); private final AtomicReference<CharSequence> contextPath = new AtomicReference<>(); private final AtomicReference<CharSequence> baseUrl = new AtomicReference<>(); + private final AtomicInteger port = new AtomicInteger(); + private final AtomicInteger securePort = new AtomicInteger(); /** * Holds this WebSocketSettings in the Application's metadata. @@ -311,6 +313,44 @@ public class WebSocketSettings } /** + * Sets the port that should be used for <code>ws:</code> connections. + * If unset then the current HTTP port will be used. + * + * @param wsPort The custom port for WS connections + */ + public void setPort(int wsPort) + { + this.port.set(wsPort); + } + + /** + * @return The custom port for WS connections + */ + public Integer getPort() + { + return port.get(); + } + + /** + * Sets the port that should be used for <code>wss:</code> connections. + * If unset then the current HTTPS port will be used. + * + * @param wssPort The custom port for WSS connections + */ + public void setSecurePort(int wssPort) + { + this.securePort.set(wssPort); + } + + /** + * @return The custom port for WSS connections + */ + public Integer getSecurePort() + { + return securePort.get(); + } + + /** * Simple executor that runs the tasks in the caller thread. */ public static class SameThreadExecutor implements Executor http://git-wip-us.apache.org/repos/asf/wicket/blob/3d9ba812/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/BaseWebSocketBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/BaseWebSocketBehavior.java b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/BaseWebSocketBehavior.java index 37d615e..5711e45 100644 --- a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/BaseWebSocketBehavior.java +++ b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/BaseWebSocketBehavior.java @@ -109,6 +109,11 @@ public class BaseWebSocketBehavior extends Behavior Args.notNull(baseUrl, "baseUrl"); variables.put("baseUrl", baseUrl); + Integer port = getPort(webSocketSettings); + variables.put("port", port); + Integer securePort = getSecurePort(webSocketSettings); + variables.put("securePort", securePort); + CharSequence contextPath = getContextPath(webSocketSettings); Args.notNull(contextPath, "contextPath"); variables.put("contextPath", contextPath); @@ -128,6 +133,16 @@ public class BaseWebSocketBehavior extends Behavior response.render(OnDomReadyHeaderItem.forScript(webSocketSetupScript)); } + protected Integer getPort(WebSocketSettings webSocketSettings) + { + return webSocketSettings.getPort(); + } + + protected Integer getSecurePort(WebSocketSettings webSocketSettings) + { + return webSocketSettings.getSecurePort(); + } + protected CharSequence getFilterPrefix(final WebSocketSettings webSocketSettings) { return webSocketSettings.getFilterPrefix(); } http://git-wip-us.apache.org/repos/asf/wicket/blob/3d9ba812/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-jquery.js ---------------------------------------------------------------------- diff --git a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-jquery.js b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-jquery.js index 3491d61..95c3c6e 100644 --- a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-jquery.js +++ b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-jquery.js @@ -50,13 +50,21 @@ var self = this, url, protocol, - WWS = Wicket.WebSocket; + WWS = Wicket.WebSocket, + port = WWS.port || document.location.port, + securePort = WWS.securePort || document.location.port, + _port; protocol = document.location.protocol .replace('https:', 'wss:') .replace('http:', 'ws:'); - url = protocol + '//' + document.location.host + WWS.contextPath + WWS.filterPrefix + '/wicket/websocket'; + if ('wss:' === protocol) { + _port = securePort ? ":" + securePort : ''; + } else { + _port = port ? ":" + port : ''; + } + url = protocol + '//' + document.location.hostname + _port + WWS.contextPath + WWS.filterPrefix + '/wicket/websocket'; if (WWS.sessionId !== '') { url += ';jsessionid=' + encodeURIComponent(WWS.sessionId); http://git-wip-us.apache.org/repos/asf/wicket/blob/3d9ba812/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-setup.js.tmpl ---------------------------------------------------------------------- diff --git a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-setup.js.tmpl b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-setup.js.tmpl index a0d94d1..4804232 100644 --- a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-setup.js.tmpl +++ b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/res/js/wicket-websocket-setup.js.tmpl @@ -4,7 +4,7 @@ if (typeof(Wicket.WebSocket.appName) === "undefined") { jQuery.extend(Wicket.WebSocket, { pageId: ${pageId}, resourceName: '${resourceName}', baseUrl: '${baseUrl}', contextPath: '${contextPath}', appName: '${applicationName}', - filterPrefix: '${filterPrefix}', sessionId: '${sessionId}' }); + port: ${port}, securePort: ${securePort}, filterPrefix: '${filterPrefix}', sessionId: '${sessionId}' }); Wicket.WebSocket.createDefaultConnection(); } })();
