Repository: wicket
Updated Branches:
  refs/heads/wicket-7.x 41a3b04a5 -> 1236f50ad


WICKET-6254 Wicket WebSockets problem behind HTTP proxy without servlet context

Extract the logic to calculate the url in WebSocketSettings. This way it could 
be configured globally and per behavior


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/1236f50a
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/1236f50a
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/1236f50a

Branch: refs/heads/wicket-7.x
Commit: 1236f50ad06a842c0efb68492038d51042f1b59d
Parents: 41a3b04
Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org>
Authored: Sat Oct 15 11:19:22 2016 +0200
Committer: Martin Tzvetanov Grigorov <mgrigo...@apache.org>
Committed: Sat Oct 15 11:19:22 2016 +0200

----------------------------------------------------------------------
 .../wicket/protocol/ws/WebSocketSettings.java   | 75 ++++++++++++++++++++
 .../protocol/ws/api/BaseWebSocketBehavior.java  | 63 +++++-----------
 2 files changed, 91 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/1236f50a/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 c3fd8ad..fb18d5a 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
@@ -29,12 +29,18 @@ import org.apache.wicket.protocol.ws.api.WebSocketResponse;
 import org.apache.wicket.protocol.ws.api.registry.IWebSocketConnectionRegistry;
 import 
org.apache.wicket.protocol.ws.api.registry.SimpleWebSocketConnectionRegistry;
 import org.apache.wicket.protocol.ws.concurrent.Executor;
+import org.apache.wicket.request.Url;
+import org.apache.wicket.request.cycle.RequestCycle;
 import org.apache.wicket.request.http.WebRequest;
 import org.apache.wicket.request.http.WebResponse;
 import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.string.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * Web Socket related settings.
@@ -43,11 +49,38 @@ import java.util.concurrent.Callable;
  */
 public class WebSocketSettings
 {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(WebSocketSettings.class);
+
        private static final MetaDataKey<WebSocketSettings> KEY = new 
MetaDataKey<WebSocketSettings>()
        {
        };
 
        /**
+        * A flag indicating whether JavaxWebSocketFilter is in use.
+        * When using JSR356 based implementations the ws:// url should not
+        * use the WicketFilter's filterPath because JSR356 Upgrade connections
+        * are never passed to the Servlet Filters.
+        */
+       private static boolean USING_JAVAX_WEB_SOCKET = false;
+       static
+       {
+               try
+               {
+                       
Class.forName("org.apache.wicket.protocol.ws.javax.JavaxWebSocketFilter");
+                       USING_JAVAX_WEB_SOCKET = true;
+                       LOG.debug("Using JSR356 Native WebSocket 
implementation!");
+               } catch (ClassNotFoundException e)
+               {
+                       LOG.debug("Using non-JSR356 Native WebSocket 
implementation!");
+               }
+       }
+
+       private final AtomicReference<CharSequence> filterPrefix = new 
AtomicReference<>();
+       private final AtomicReference<CharSequence> contextPath = new 
AtomicReference<>();
+       private final AtomicReference<CharSequence> baseUrl = new 
AtomicReference<>();
+
+       /**
         * Holds this WebSocketSettings in the Application's metadata.
         * This way wicket-core module doesn't have reference to 
wicket-native-websocket.
         */
@@ -235,6 +268,48 @@ public class WebSocketSettings
                return new WebSocketRequest(new ServletRequestCopy(request), 
filterPath);
        }
 
+       public void setFilterPrefix(final CharSequence filterPrefix) {
+               this.filterPrefix.set(filterPrefix);
+       }
+
+       public CharSequence getFilterPrefix() {
+               if (filterPrefix.get() == null)
+               {
+                       if (USING_JAVAX_WEB_SOCKET)
+                       {
+                               filterPrefix.compareAndSet(null, "");
+                       }
+                       else
+                       {
+                               filterPrefix.compareAndSet(null, 
RequestCycle.get().getRequest().getFilterPath());
+                       }
+               }
+               return filterPrefix.get();
+       }
+
+       public void setContextPath(final CharSequence contextPath) {
+               this.contextPath.set(contextPath);
+       }
+
+       public CharSequence getContextPath() {
+               contextPath.compareAndSet(null, 
RequestCycle.get().getRequest().getContextPath());
+               return contextPath.get();
+       }
+
+       public void setBaseUrl(final CharSequence baseUrl)
+       {
+               this.baseUrl.set(baseUrl);
+       }
+
+       public CharSequence getBaseUrl() {
+               if (baseUrl.get() == null)
+               {
+                       Url _baseUrl = 
RequestCycle.get().getUrlRenderer().getBaseUrl();
+                       baseUrl.compareAndSet(null, 
Strings.escapeMarkup(_baseUrl.toString()));
+               }
+               return baseUrl.get();
+       }
+
        /**
         * Simple executor that runs the tasks in the caller thread.
         */

http://git-wip-us.apache.org/repos/asf/wicket/blob/1236f50a/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 6290aeb..2442cef 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
@@ -16,48 +16,24 @@
  */
 package org.apache.wicket.protocol.ws.api;
 
-import java.util.Map;
-
 import org.apache.wicket.Component;
 import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.markup.head.IHeaderResponse;
 import org.apache.wicket.markup.head.JavaScriptHeaderItem;
 import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
-import org.apache.wicket.request.Url;
+import org.apache.wicket.protocol.ws.WebSocketSettings;
 import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Generics;
 import org.apache.wicket.util.string.Strings;
 import org.apache.wicket.util.template.PackageTextTemplate;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+
+import java.util.Map;
 
 /**
  * A behavior that contributes {@link WicketWebSocketJQueryResourceReference}
  */
 public class BaseWebSocketBehavior extends Behavior
 {
-       private static final Logger LOG = 
LoggerFactory.getLogger(BaseWebSocketBehavior.class);
-
-       /**
-        * A flag indicating whether JavaxWebSocketFilter is in use.
-        * When using JSR356 based implementations the ws:// url should not
-        * use the WicketFilter's filterPath because JSR356 Upgrade connections
-        * are never passed to the Servlet Filters.
-        */
-       private static boolean USING_JAVAX_WEB_SOCKET = false;
-       static
-       {
-               try
-               {
-                       
Class.forName("org.apache.wicket.protocol.ws.javax.JavaxWebSocketFilter");
-                       USING_JAVAX_WEB_SOCKET = true;
-                       LOG.debug("Using JSR356 Native WebSocket 
implementation!");
-               } catch (ClassNotFoundException e)
-               {
-                       LOG.debug("Using non-JSR356 Native WebSocket 
implementation!");
-               }
-       }
-
        private final String resourceName;
 
        /**
@@ -118,17 +94,21 @@ public class BaseWebSocketBehavior extends Behavior
                        variables.put("pageId", false);
                }
 
-               CharSequence baseUrl = getBaseUrl(component);
+               WebSocketSettings webSocketSettings = 
WebSocketSettings.Holder.get(component.getApplication());
+
+               CharSequence baseUrl = getBaseUrl(webSocketSettings);
                Args.notEmpty(baseUrl, "baseUrl");
                variables.put("baseUrl", baseUrl);
 
-               String contextPath = getContextPath(component);
+               CharSequence contextPath = getContextPath(webSocketSettings);
+               Args.notEmpty(contextPath, "contextPath");
                variables.put("contextPath", contextPath);
 
                // preserve the application name for JSR356 based impl
                variables.put("applicationName", 
component.getApplication().getName());
 
-               CharSequence filterPrefix = getFilterPrefix(component);
+               CharSequence filterPrefix = getFilterPrefix(webSocketSettings);
+               Args.notEmpty(filterPrefix, "filterPrefix");
                variables.put("filterPrefix", filterPrefix);
 
                String webSocketSetupScript = 
webSocketSetupTemplate.asString(variables);
@@ -136,27 +116,16 @@ public class BaseWebSocketBehavior extends Behavior
                
response.render(OnDomReadyHeaderItem.forScript(webSocketSetupScript));
        }
 
-       protected CharSequence getFilterPrefix(final Component component) {
-               CharSequence filterPrefix;
-               if (USING_JAVAX_WEB_SOCKET)
-               {
-                       filterPrefix = "";
-               }
-               else
-               {
-                       filterPrefix = component.getRequest().getFilterPath();
-               }
-               return filterPrefix;
+       protected CharSequence getFilterPrefix(final WebSocketSettings 
webSocketSettings) {
+               return webSocketSettings.getFilterPrefix();
        }
 
-       protected String getContextPath(final Component component) {
-               return component.getRequest().getContextPath();
+       protected CharSequence getContextPath(final WebSocketSettings 
webSocketSettings) {
+               return webSocketSettings.getContextPath();
        }
 
-       protected CharSequence getBaseUrl(Component component) {
-               Url baseUrl = 
component.getRequestCycle().getUrlRenderer().getBaseUrl();
-               CharSequence ajaxBaseUrl = 
Strings.escapeMarkup(baseUrl.toString());
-               return ajaxBaseUrl;
+       protected CharSequence getBaseUrl(final WebSocketSettings 
webSocketSettings) {
+               return webSocketSettings.getBaseUrl();
        }
 
        @Override

Reply via email to