org.eclipse.jetty.websocket.servlet.WebSocketCreator Java Examples

The following examples show how to use org.eclipse.jetty.websocket.servlet.WebSocketCreator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EgressInteractiveHandler.java    From warp10-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
  
  final EgressInteractiveHandler self = this;

  final WebSocketCreator oldcreator = factory.getCreator();
  
  WebSocketCreator creator = new WebSocketCreator() {
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
      InteractiveWebSocket ws = (InteractiveWebSocket) oldcreator.createWebSocket(req, resp);
      ws.setHandler(self);
      try {
        ws.setProcessor(new InteractiveProcessor(self, null, null, null, null));
      } catch (IOException ioe) {
        throw new RuntimeException(ioe);
      }
      return ws;
    }
  };

  factory.setCreator(creator);
  super.configure(factory);
}
 
Example #2
Source File: PHttpServer.java    From jphp with Apache License 2.0 6 votes vote down vote up
@Signature
public void addWebSocket(Environment env, String path, ArrayMemory _handlers) {
    WebSocketParam param = _handlers.toBean(env, WebSocketParam.class);

    ContextHandler contextHandler = new ContextHandler(path);
    contextHandler.setHandler(new WebSocketHandler() {
        @Override
        public void configure(WebSocketServletFactory factory) {
            factory.setCreator(new WebSocketCreator() {
                @Override
                public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
                    return new WebSocket(env, param);
                }
            });
        }
    });

    handlers.addHandler(contextHandler);
}
 
Example #3
Source File: ServerRpcProvider.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("cast")
@Override
public void configure(WebSocketServletFactory factory) {
  if (websocketMaxIdleTime == 0) {
    // Jetty does not allow to set infinite timeout.
    factory.getPolicy().setIdleTimeout(Integer.MAX_VALUE);
  } else {
    factory.getPolicy().setIdleTimeout(websocketMaxIdleTime);
  }
  factory.getPolicy().setMaxTextMessageSize(websocketMaxMessageSize * 1024 * 1024);
  factory.setCreator(new WebSocketCreator() {
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
      ParticipantId loggedInUser =
          provider.sessionManager.getLoggedInUser(req.getSession());

      return new WebSocketConnection(loggedInUser, provider).getWebSocketServerChannel();
    }
  });
}
 
Example #4
Source File: JettyWebSocketClientTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public TestJettyWebSocketServer(final WebSocketHandler webSocketHandler) {

			this.server = new Server();
			ServerConnector connector = new ServerConnector(this.server);
			connector.setPort(0);

			this.server.addConnector(connector);
			this.server.setHandler(new org.eclipse.jetty.websocket.server.WebSocketHandler() {
				@Override
				public void configure(WebSocketServletFactory factory) {
					factory.setCreator(new WebSocketCreator() {
						@Override
						public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
							if (!CollectionUtils.isEmpty(req.getSubProtocols())) {
								resp.setAcceptedSubProtocol(req.getSubProtocols().get(0));
							}
							JettyWebSocketSession session = new JettyWebSocketSession(null, null);
							return new JettyWebSocketHandlerAdapter(webSocketHandler, session);
						}
					});
				}
			});
		}
 
Example #5
Source File: EgressMobiusHandler.java    From warp10-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
  
  final EgressMobiusHandler self = this;

  final WebSocketCreator oldcreator = factory.getCreator();
  
  WebSocketCreator creator = new WebSocketCreator() {
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
      MobiusWebSocket ws = (MobiusWebSocket) oldcreator.createWebSocket(req, resp);
      ws.setMobiusHandler(self);
      return ws;
    }
  };

  factory.setCreator(creator);
  super.configure(factory);
}
 
Example #6
Source File: JettyWebSocketClientTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public TestJettyWebSocketServer(int port, final WebSocketHandler webSocketHandler) {

			this.server = new Server();
			ServerConnector connector = new ServerConnector(this.server);
			connector.setPort(port);

			this.server.addConnector(connector);
			this.server.setHandler(new org.eclipse.jetty.websocket.server.WebSocketHandler() {
				@Override
				public void configure(WebSocketServletFactory factory) {
					factory.setCreator(new WebSocketCreator() {
						@Override
						public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
							if (!CollectionUtils.isEmpty(req.getSubProtocols())) {
								resp.setAcceptedSubProtocol(req.getSubProtocols().get(0));
							}
							JettyWebSocketSession session = new JettyWebSocketSession(null, null);
							return new JettyWebSocketHandlerAdapter(webSocketHandler, session);
						}
					});
				}
			});
		}
 
Example #7
Source File: JettyWebSocketClientTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
public TestJettyWebSocketServer(final WebSocketHandler webSocketHandler) {

			this.server = new Server();
			ServerConnector connector = new ServerConnector(this.server);
			connector.setPort(0);

			this.server.addConnector(connector);
			this.server.setHandler(new org.eclipse.jetty.websocket.server.WebSocketHandler() {
				@Override
				public void configure(WebSocketServletFactory factory) {
					factory.setCreator(new WebSocketCreator() {
						@Override
						public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
							if (!CollectionUtils.isEmpty(req.getSubProtocols())) {
								resp.setAcceptedSubProtocol(req.getSubProtocols().get(0));
							}
							JettyWebSocketSession session = new JettyWebSocketSession(null, null);
							return new JettyWebSocketHandlerAdapter(webSocketHandler, session);
						}
					});
				}
			});
		}
 
Example #8
Source File: StandaloneStreamUpdateHandler.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
  
  final StandaloneStreamUpdateHandler self = this;

  final WebSocketCreator oldcreator = factory.getCreator();
  
  WebSocketCreator creator = new WebSocketCreator() {
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
      StandaloneStreamUpdateWebSocket ws = (StandaloneStreamUpdateWebSocket) oldcreator.createWebSocket(req, resp);
      ws.setHandler(self);
      return ws;
    }
  };

  factory.setCreator(creator);
  
  //
  // Update the maxMessageSize if need be
  //
  if (this.properties.containsKey(Configuration.INGRESS_WEBSOCKET_MAXMESSAGESIZE)) {
    factory.getPolicy().setMaxTextMessageSize((int) Long.parseLong(this.properties.getProperty(Configuration.INGRESS_WEBSOCKET_MAXMESSAGESIZE)));
    factory.getPolicy().setMaxBinaryMessageSize((int) Long.parseLong(this.properties.getProperty(Configuration.INGRESS_WEBSOCKET_MAXMESSAGESIZE)));
  }

  super.configure(factory);
}
 
Example #9
Source File: StandalonePlasmaHandler.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
      
  final StandalonePlasmaHandler self = this;

  final WebSocketCreator oldcreator = factory.getCreator();
  
  WebSocketCreator creator = new WebSocketCreator() {
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
      StandalonePlasmaWebSocket ws = (StandalonePlasmaWebSocket) oldcreator.createWebSocket(req, resp);
      ws.setHandler(self);
      return ws;
    }
  };

  factory.setCreator(creator);
  
  //
  // Update the maxMessageSize if need be
  //
  if (this.properties.containsKey(Configuration.PLASMA_FRONTEND_WEBSOCKET_MAXMESSAGESIZE)) {
    factory.getPolicy().setMaxTextMessageSize((int) Long.parseLong(this.properties.getProperty(Configuration.PLASMA_FRONTEND_WEBSOCKET_MAXMESSAGESIZE)));
    factory.getPolicy().setMaxBinaryMessageSize((int) Long.parseLong(this.properties.getProperty(Configuration.PLASMA_FRONTEND_WEBSOCKET_MAXMESSAGESIZE)));
  }

  super.configure(factory);
}
 
Example #10
Source File: WebSocketTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
    factory.register(EventsWebSocket.class);
    factory.setCreator(new WebSocketCreator() {
        @Override
        public EventsWebSocket createWebSocket(final ServletUpgradeRequest servletUpgradeRequest,
                final ServletUpgradeResponse servletUpgradeResponse) {
            return new EventsWebSocket();
        }
    });
}
 
Example #11
Source File: WebSocketTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
    factory.register(CookiesWebSocket.class);
    factory.setCreator(new WebSocketCreator() {
        @Override
        public Object createWebSocket(final ServletUpgradeRequest servletUpgradeRequest,
                final ServletUpgradeResponse servletUpgradeResponse) {
            return new CookiesWebSocket();
        }
    });
}
 
Example #12
Source File: IngressStreamUpdateHandler.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
  
  final IngressStreamUpdateHandler self = this;

  final WebSocketCreator oldcreator = factory.getCreator();

  WebSocketCreator creator = new WebSocketCreator() {
          
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
      StandaloneStreamUpdateWebSocket ws = (StandaloneStreamUpdateWebSocket) oldcreator.createWebSocket(req, resp);
      ws.setHandler(self);
      return ws;
    }
  };

  factory.setCreator(creator);
  
  //
  // Update the maxMessageSize if need be
  //
  if (this.ingress.properties.containsKey(Configuration.INGRESS_WEBSOCKET_MAXMESSAGESIZE)) {
    factory.getPolicy().setMaxTextMessageSize((int) Long.parseLong(this.ingress.properties.getProperty(Configuration.INGRESS_WEBSOCKET_MAXMESSAGESIZE)));
    factory.getPolicy().setMaxBinaryMessageSize((int) Long.parseLong(this.ingress.properties.getProperty(Configuration.INGRESS_WEBSOCKET_MAXMESSAGESIZE)));
  }
  super.configure(factory);
}
 
Example #13
Source File: OpenfireWebSocketServlet.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory)
{
    if (XmppWebSocket.isCompressionEnabled()) {
        factory.getExtensionFactory().register("permessage-deflate", PerMessageDeflateExtension.class);
    }
    final int messageSize = JiveGlobals.getIntProperty("xmpp.parser.buffer.size", 1048576);
    factory.getPolicy().setMaxTextMessageBufferSize(messageSize * 5);
    factory.getPolicy().setMaxTextMessageSize(messageSize);
    factory.setCreator(new WebSocketCreator() {
        @Override
        public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
        {
            try {
                for (String subprotocol : req.getSubProtocols())
                {
                    if ("xmpp".equals(subprotocol))
                    {
                        resp.setAcceptedSubProtocol(subprotocol);
                        return new XmppWebSocket();
                    }
                }
            } catch (Exception e) {
                Log.warn(MessageFormat.format("Unable to load websocket factory: {0} ({1})", e.getClass().getName(), e.getMessage()));
            }
            Log.warn("Failed to create websocket for {}:{} make a request at {}", req.getRemoteAddress(), req.getRemotePort(), req.getRequestPath() );
            return null;
        }
    });
}
 
Example #14
Source File: JettyWebSocketServlet.java    From sequenceiq-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory webSocketServletFactory) {
    webSocketServletFactory.setCreator(new WebSocketCreator() {
        @Override
        public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
            return new JettyWebSocketListener(channelProcessor);
        }
    });
}
 
Example #15
Source File: ScriptExecutionReportingWebSocketServlet.java    From gp2srv with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {
	factory.setCreator(new WebSocketCreator() {
		public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
			return new ScriptExecutionReportingWebSocket(logger);
		}
	});
}
 
Example #16
Source File: WebSocketTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
    factory.register(ChatWebSocket.class);
    factory.setCreator(new WebSocketCreator() {
        @Override
        public Object createWebSocket(final ServletUpgradeRequest servletUpgradeRequest,
                final ServletUpgradeResponse servletUpgradeResponse) {
            return new ChatWebSocket();
        }
    });
}
 
Example #17
Source File: StreamingWebSocketServlet.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {
  // Most implementations of this method simply invoke factory.register(DispatchSocket.class);
  // however, that requires DispatchSocket to have a no-arg constructor. That does not work for
  // us because we would like all WebSockets created by this factory to have a reference to this
  // parent class. This is why we override the default WebSocketCreator for the factory.
  WebSocketCreator wrapperCreator = (req, resp) -> new MyWebSocket();
  factory.setCreator(wrapperCreator);
}
 
Example #18
Source File: ScriptServlet.java    From purplejs with Apache License 2.0 4 votes vote down vote up
private void acceptWebSocket( final HttpServletRequest req, final HttpServletResponse res, final WebSocketConfig config )
    throws IOException
{
    final WebSocketCreator creator = new WebSocketHandler( this.handler, config );
    this.webSocketServletFactory.acceptWebSocket( creator, req, res );
}
 
Example #19
Source File: ServerRpcProvider.java    From swellrt with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("cast")
@Override
public void configure(WebSocketServletFactory factory) {
  if (websocketMaxIdleTime == 0) {
    // Jetty does not allow to set infinite timeout.
    factory.getPolicy().setIdleTimeout(Integer.MAX_VALUE);
  } else {
    factory.getPolicy().setIdleTimeout(websocketMaxIdleTime);
  }
  factory.getPolicy().setMaxTextMessageSize(websocketMaxMessageSize * 1024 * 1024);
  factory.setCreator(new WebSocketCreator() {
    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {

      ParticipantId loggedInUser = null;
      List<String> tokenParam = req.getParameterMap().get(SESSION_TOKEN);

      // If no logged in user is detected, let the websocket fail when auth
      // message is sent by client.
      String token = null;
      if (tokenParam != null && !tokenParam.isEmpty()) {
        token = tokenParam.get(0);
        loggedInUser = provider.sessionManager.getLoggedInUser(token);
      }

      WebSocketConnection wsConnection = null;

      if (token != null) {

        try {

          final String connectionId = token
              + (loggedInUser != null ? ":" + loggedInUser.getAddress() : "");

          final ParticipantId participantId = loggedInUser;

          wsConnection = provider.wsConnectionRegistry.get(connectionId,
              new Callable<WebSocketConnection>() {

                @Override
                public WebSocketConnection call() throws Exception {
                  return new WebSocketConnection(connectionId, participantId, provider);
                }

              });

        } catch (ExecutionException e) {
          LOG.info("Error creating WebSocket connection ", e);
        }

      } else {
        // Transient WebSocketConnection
        wsConnection = new WebSocketConnection(null, loggedInUser, provider);
      }

      return wsConnection.getWebSocketServerChannel();
    }
  });
}