From 58db3494ae4690eb71b94c7744aa53be2fa60110 Mon Sep 17 00:00:00 2001
From: Artur Signell <artur@vaadin.com>
Date: Mon, 31 Mar 2014 17:20:45 +0300
Subject: Refactor AtmosphereHandler to a field (#13514)

Change-Id: I0956e31acfec605418eff0313fbcf82c3c7d6579
---
 .../vaadin/server/communication/PushHandler.java   | 128 +++++++++++----------
 .../server/communication/PushRequestHandler.java   |   2 +-
 2 files changed, 69 insertions(+), 61 deletions(-)

(limited to 'server/src')

diff --git a/server/src/com/vaadin/server/communication/PushHandler.java b/server/src/com/vaadin/server/communication/PushHandler.java
index 99aff3780f..89e9adb9e6 100644
--- a/server/src/com/vaadin/server/communication/PushHandler.java
+++ b/server/src/com/vaadin/server/communication/PushHandler.java
@@ -52,8 +52,74 @@ import com.vaadin.ui.UI;
  * @author Vaadin Ltd
  * @since 7.1
  */
-public class PushHandler extends AtmosphereResourceEventListenerAdapter
-        implements AtmosphereHandler {
+public class PushHandler extends AtmosphereResourceEventListenerAdapter {
+
+    AtmosphereHandler handler = new AtmosphereHandler() {
+
+        @Override
+        public void onStateChange(AtmosphereResourceEvent event)
+                throws IOException {
+            AtmosphereResource resource = event.getResource();
+
+            String id = resource.uuid();
+            if (event.isCancelled() || event.isResumedOnTimeout()) {
+                getLogger().log(Level.FINER,
+                        "Cancelled connection for resource {0}", id);
+                disconnect(event);
+            } else if (event.isResuming()) {
+                // A connection that was suspended earlier was resumed
+                // (committed to
+                // the client.) Should only happen if the transport is JSONP or
+                // long-polling.
+                getLogger().log(Level.FINER,
+                        "Resuming request for resource {0}", id);
+            } else {
+                // A message was broadcast to this resource and should be sent
+                // to
+                // the client. We don't do any actual broadcasting, in the sense
+                // of
+                // sending to multiple recipients; any UIDL message is specific
+                // to a
+                // single client.
+                getLogger().log(Level.FINER, "Writing message to resource {0}",
+                        id);
+
+                Writer writer = resource.getResponse().getWriter();
+                writer.write(event.getMessage().toString());
+
+                switch (resource.transport()) {
+                case WEBSOCKET:
+                    break;
+                case SSE:
+                case STREAMING:
+                    writer.flush();
+                    break;
+                case JSONP:
+                case LONG_POLLING:
+                    disconnect(event);
+                    break;
+                default:
+                    getLogger().log(Level.SEVERE, "Unknown transport {0}",
+                            resource.transport());
+                }
+            }
+        }
+
+        @Override
+        public void onRequest(AtmosphereResource resource) {
+            AtmosphereRequest req = resource.getRequest();
+
+            if (req.getMethod().equalsIgnoreCase("GET")) {
+                callWithUi(resource, establishCallback);
+            } else if (req.getMethod().equalsIgnoreCase("POST")) {
+                callWithUi(resource, receiveCallback);
+            }
+        }
+
+        @Override
+        public void destroy() {
+        }
+    };
 
     /**
      * Callback interface used internally to process an event with the
@@ -318,17 +384,6 @@ public class PushHandler extends AtmosphereResourceEventListenerAdapter
         }
     }
 
-    @Override
-    public void onRequest(AtmosphereResource resource) {
-        AtmosphereRequest req = resource.getRequest();
-
-        if (req.getMethod().equalsIgnoreCase("GET")) {
-            callWithUi(resource, establishCallback);
-        } else if (req.getMethod().equalsIgnoreCase("POST")) {
-            callWithUi(resource, receiveCallback);
-        }
-    }
-
     private static AtmospherePushConnection getConnectionForUI(UI ui) {
         PushConnection pushConnection = ui.getPushConnection();
         if (pushConnection instanceof AtmospherePushConnection) {
@@ -338,49 +393,6 @@ public class PushHandler extends AtmosphereResourceEventListenerAdapter
         }
     }
 
-    @Override
-    public void onStateChange(AtmosphereResourceEvent event) throws IOException {
-        AtmosphereResource resource = event.getResource();
-
-        String id = resource.uuid();
-        if (event.isCancelled() || event.isResumedOnTimeout()) {
-            getLogger().log(Level.FINER,
-                    "Cancelled connection for resource {0}", id);
-            disconnect(event);
-        } else if (event.isResuming()) {
-            // A connection that was suspended earlier was resumed (committed to
-            // the client.) Should only happen if the transport is JSONP or
-            // long-polling.
-            getLogger().log(Level.FINER, "Resuming request for resource {0}",
-                    id);
-        } else {
-            // A message was broadcast to this resource and should be sent to
-            // the client. We don't do any actual broadcasting, in the sense of
-            // sending to multiple recipients; any UIDL message is specific to a
-            // single client.
-            getLogger().log(Level.FINER, "Writing message to resource {0}", id);
-
-            Writer writer = resource.getResponse().getWriter();
-            writer.write(event.getMessage().toString());
-
-            switch (resource.transport()) {
-            case WEBSOCKET:
-                break;
-            case SSE:
-            case STREAMING:
-                writer.flush();
-                break;
-            case JSONP:
-            case LONG_POLLING:
-                disconnect(event);
-                break;
-            default:
-                getLogger().log(Level.SEVERE, "Unknown transport {0}",
-                        resource.transport());
-            }
-        }
-    }
-
     @Override
     public void onDisconnect(AtmosphereResourceEvent event) {
         // Log event on trace level
@@ -395,10 +407,6 @@ public class PushHandler extends AtmosphereResourceEventListenerAdapter
         disconnect(event);
     }
 
-    @Override
-    public void destroy() {
-    }
-
     private void disconnect(AtmosphereResourceEvent event) {
         callWithUi(event.getResource(), disconnectCallback);
     }
diff --git a/server/src/com/vaadin/server/communication/PushRequestHandler.java b/server/src/com/vaadin/server/communication/PushRequestHandler.java
index aec3aa54c0..c078b328d1 100644
--- a/server/src/com/vaadin/server/communication/PushRequestHandler.java
+++ b/server/src/com/vaadin/server/communication/PushRequestHandler.java
@@ -85,7 +85,7 @@ public class PushRequestHandler implements RequestHandler,
         });
 
         pushHandler = new PushHandler(service);
-        atmosphere.addAtmosphereHandler("/*", pushHandler);
+        atmosphere.addAtmosphereHandler("/*", pushHandler.handler);
         atmosphere.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT,
                 "true");
         atmosphere.addInitParameter(ApplicationConfig.MESSAGE_DELIMITER,
-- 
cgit v1.2.3