diff options
author | Artur Signell <artur@vaadin.com> | 2014-03-31 17:20:45 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-04-01 06:47:09 +0000 |
commit | 58db3494ae4690eb71b94c7744aa53be2fa60110 (patch) | |
tree | e6d865d93f7620134ec346ba615cba6e9fee7433 /server/src/com | |
parent | 9422946a4009f19ebb04c83c94bae023551a8db3 (diff) | |
download | vaadin-framework-58db3494ae4690eb71b94c7744aa53be2fa60110.tar.gz vaadin-framework-58db3494ae4690eb71b94c7744aa53be2fa60110.zip |
Refactor AtmosphereHandler to a field (#13514)
Change-Id: I0956e31acfec605418eff0313fbcf82c3c7d6579
Diffstat (limited to 'server/src/com')
-rw-r--r-- | server/src/com/vaadin/server/communication/PushHandler.java | 128 | ||||
-rw-r--r-- | server/src/com/vaadin/server/communication/PushRequestHandler.java | 2 |
2 files changed, 69 insertions, 61 deletions
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) { @@ -339,49 +394,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 super.onDisconnect(event); @@ -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, |