PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/server/websocket/src/main/java/org/infinispan/server/websocket/WebSocketServer.java

http://github.com/infinispan/infinispan
Java | 131 lines | 100 code | 24 blank | 7 comment | 4 complexity | 99ed879ab6eedb9a7c5607ee078f97b9 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. package org.infinispan.server.websocket;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.StringWriter;
  6. import java.lang.invoke.MethodHandles;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import org.infinispan.Cache;
  10. import org.infinispan.commons.util.CollectionFactory;
  11. import org.infinispan.manager.CacheContainer;
  12. import org.infinispan.manager.EmbeddedCacheManager;
  13. import org.infinispan.server.core.AbstractProtocolServer;
  14. import org.infinispan.server.core.CacheIgnoreAware;
  15. import org.infinispan.server.websocket.configuration.WebSocketServerConfiguration;
  16. import org.infinispan.server.websocket.handlers.GetHandler;
  17. import org.infinispan.server.websocket.handlers.NotifyHandler;
  18. import org.infinispan.server.websocket.handlers.PutHandler;
  19. import org.infinispan.server.websocket.handlers.RemoveHandler;
  20. import org.infinispan.server.websocket.logging.Log;
  21. import org.infinispan.util.logging.LogFactory;
  22. import io.netty.channel.Channel;
  23. import io.netty.channel.ChannelInboundHandler;
  24. import io.netty.channel.ChannelInitializer;
  25. import io.netty.channel.ChannelOutboundHandler;
  26. import io.netty.channel.ChannelPipeline;
  27. import io.netty.handler.codec.http.HttpObjectAggregator;
  28. import io.netty.handler.codec.http.HttpRequestDecoder;
  29. import io.netty.handler.codec.http.HttpResponseEncoder;
  30. /**
  31. * An HTTP server which serves Web Socket requests on an Infinispan cacheManager.
  32. * <p>
  33. * Websocket specific code lifted from Netty WebSocket Server example.
  34. * </p>
  35. */
  36. public class WebSocketServer extends AbstractProtocolServer<WebSocketServerConfiguration> {
  37. private static final Log logger = LogFactory.getLog(MethodHandles.lookup().lookupClass(), Log.class);
  38. public static final String INFINISPAN_WS_JS_FILENAME = "infinispan-ws.js";
  39. private static String javascript;
  40. public WebSocketServer() {
  41. super("WebSocket");
  42. }
  43. public ChannelOutboundHandler getEncoder() {
  44. return null;
  45. }
  46. public ChannelInboundHandler getDecoder() {
  47. return null;
  48. }
  49. public void startInternal(WebSocketServerConfiguration configuration, EmbeddedCacheManager cacheManager) {
  50. super.startInternal(configuration, cacheManager);
  51. }
  52. @Override
  53. public ChannelInitializer<Channel> getInitializer() {
  54. return new WebSocketServerPipelineFactory(cacheManager, this);
  55. }
  56. private static class WebSocketServerPipelineFactory extends ChannelInitializer<Channel> {
  57. private CacheContainer cacheContainer;
  58. private final CacheIgnoreAware cacheIgnoreAware;
  59. private Map<String, OpHandler> operationHandlers;
  60. private Map<String, Cache<Object, Object>> startedCaches = CollectionFactory.makeConcurrentMap();
  61. public WebSocketServerPipelineFactory(CacheContainer cacheContainer, CacheIgnoreAware cacheIgnoreAware) {
  62. this.cacheContainer = cacheContainer;
  63. this.cacheIgnoreAware = cacheIgnoreAware;
  64. operationHandlers = new HashMap<String, OpHandler>();
  65. operationHandlers.put("put", new PutHandler());
  66. operationHandlers.put("get", new GetHandler());
  67. operationHandlers.put("remove", new RemoveHandler());
  68. NotifyHandler notifyHandler = new NotifyHandler();
  69. operationHandlers.put("notify", notifyHandler);
  70. operationHandlers.put("unnotify", notifyHandler);
  71. }
  72. @Override
  73. public void initChannel(Channel channel) throws Exception {
  74. // Create a default pipeline implementation.
  75. ChannelPipeline pipeline = channel.pipeline();
  76. pipeline.addLast("decoder", new HttpRequestDecoder());
  77. pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
  78. pipeline.addLast("encoder", new HttpResponseEncoder());
  79. pipeline.addLast("handler", new WebSocketServerHandler(cacheContainer, operationHandlers, startedCaches, cacheIgnoreAware));
  80. }
  81. }
  82. public static String getJavascript() {
  83. if (javascript != null) {
  84. return javascript;
  85. }
  86. BufferedReader scriptReader = new BufferedReader(new InputStreamReader(WebSocketServer.class.getResourceAsStream(INFINISPAN_WS_JS_FILENAME)));
  87. try {
  88. StringWriter writer = new StringWriter();
  89. String line = scriptReader.readLine();
  90. while (line != null) {
  91. writer.write(line);
  92. writer.write('\n');
  93. line = scriptReader.readLine();
  94. }
  95. javascript = writer.toString();
  96. return javascript;
  97. } catch (IOException e) {
  98. throw logger.unableToSendWebSocketsScriptToTheClient(e);
  99. } finally {
  100. try {
  101. scriptReader.close();
  102. } catch (IOException e) {
  103. throw logger.unableToCloseWebSocketsStream(e);
  104. }
  105. }
  106. }
  107. }