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

The following examples show how to use org.eclipse.jetty.websocket.servlet.WebSocketServletFactory. 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: 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 #2
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 #3
Source File: Jetty9WebSocketDestination.java    From cxf with Apache License 2.0 6 votes vote down vote up
public synchronized WebSocketServletFactory getWebSocketFactory(Server server) {
    if (webSocketFactory == null) {
        webSockethandler = new WebSocketHandler() {
            @Override
            public void configure(WebSocketServletFactory factory) {
            }
        };
        try {
            webSockethandler.setServer(server);
            webSockethandler.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        webSocketFactory = webSockethandler.getWebSocketFactory();
        webSocketFactory.setCreator(new Creator());
    }
    return webSocketFactory;
}
 
Example #4
Source File: GatewayWebsocketHandler.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final WebSocketServletFactory factory) {
  factory.setCreator(this);
  factory.getPolicy()
      .setMaxTextMessageSize(config.getWebsocketMaxTextMessageSize());
  factory.getPolicy()
      .setMaxBinaryMessageSize(config.getWebsocketMaxBinaryMessageSize());

  factory.getPolicy().setMaxBinaryMessageBufferSize(
      config.getWebsocketMaxBinaryMessageBufferSize());
  factory.getPolicy().setMaxTextMessageBufferSize(
      config.getWebsocketMaxTextMessageBufferSize());

  factory.getPolicy()
      .setInputBufferSize(config.getWebsocketInputBufferSize());

  factory.getPolicy()
      .setAsyncWriteTimeout(config.getWebsocketAsyncWriteTimeout());
  factory.getPolicy().setIdleTimeout(config.getWebsocketIdleTimeout());

}
 
Example #5
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 #6
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 #7
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 #8
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 #9
Source File: MinicapJettyServer.java    From android-uiconductor with Apache License 2.0 6 votes vote down vote up
public void runServer() {
  if (server.isStarted()) {
    return;
  }

  System.out.println("server port: " + port);
  WebSocketHandler wsHandler =
      new WebSocketHandler() {
        @Override
        public void configure(WebSocketServletFactory factory) {
          factory.register(MinicapWebSocketHandler.class);
        }
      };
  server.setHandler(wsHandler);

  try {
    server.start();
    server.join();
  } catch (Exception e) {
    logger.warning(e.getMessage());
  }
}
 
Example #10
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 #11
Source File: ArriaddnaWebSocketServlet.java    From ariADDna with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {

    //TODO run when server has connection
    factory.getPolicy().setIdleTimeout(idleTimeout);
    applicationContext =
            new AnnotationConfigApplicationContext(ConfigServer.class);
    serverMessageSystem = applicationContext.getBean(ServerMessageSystem.class);
    serverMessageSystem.runMessagesSystem();
    AriaddnaWebSocketCreator socket = applicationContext
            .getBean(AriaddnaWebSocketCreator.class);
    factory.setCreator(socket);
}
 
Example #12
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 #13
Source File: Jetty9WebSocketDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebSocketServletFactory getWebSocketFactory(ServletConfig config, ServletContext context) {
    if (webSocketFactory == null) {
        Server server = getServer(config, context);
        return getWebSocketFactory(server);
    }
    return webSocketFactory;
}
 
Example #14
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 #15
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 #16
Source File: StartUpInit.java    From android-uiconductor with Apache License 2.0 5 votes vote down vote up
private void initLogServer() {
  if (logServerThread != null && logServerThread.isAlive()) {
    logServerThread.interrupt();
  }

  if (logServer != null && logServer.isStarted()) {
    try {
      logServer.stop();
    } catch (Exception e) {
      logger.warning(e.getMessage());
    }
  }

  logServerThread =
      new Thread(
          () -> {
            logServer = new Server(8888);

            WebSocketHandler wsHandler =
                new WebSocketHandler() {
                  @Override
                  public void configure(WebSocketServletFactory factory) {
                    factory.register(LogWebSocketHandler.class);
                  }
                };
            logServer.setHandler(wsHandler);

            try {
              logServer.start();
              logServer.join();
            } catch (Exception e) {
              logger.warning(e.getMessage());
            }
          });
  logServerThread.start();
}
 
Example #17
Source File: WebSocketServlet.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {
	factory.getPolicy().setMaxTextMessageSize(MAX_MESSAGE_SIZE);
	factory.getPolicy().setMaxTextMessageBufferSize(MAX_MESSAGE_SIZE);
	factory.getPolicy().setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
	factory.getPolicy().setMaxBinaryMessageBufferSize(MAX_MESSAGE_SIZE);

	Map<String,WebSocketInvocation> methods = new HashMap<>();
	scanBeanMethods(this.kmehrWsFacade, methods);
	factory.setCreator((req, resp) ->
			new WebSocket(sessionLogic.getCurrentSessionContext(), prefix, gsonMapper, sessionLogic, wsExecutor, methods));
}
 
Example #18
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 #19
Source File: EventServlet.java    From barge with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {
  factory.setCreator((req, resp) -> {

    // remove all compressions extensions that might be requested by the browser...
    resp.setExtensions(Collections.<ExtensionConfig>emptyList());

    return new EventSocket(events);
  });
}
 
Example #20
Source File: Jetty9WebSocketDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void invoke(final ServletConfig config,
                   final ServletContext context,
                   final HttpServletRequest request,
                   final HttpServletResponse response) throws IOException {

    WebSocketServletFactory wsf = getWebSocketFactory(config, context);

    if (wsf.isUpgradeRequest(request, response)
        && wsf.acceptWebSocket(request, response)) {
        ((Request)request).setHandled(true);
        return;
    }
    super.invoke(config, context, request, response);
}
 
Example #21
Source File: WebSocketProducerServlet.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {
    factory.getPolicy().setMaxTextMessageSize(service.getConfig().getWebSocketMaxTextFrameSize());
    if (service.getConfig().getWebSocketSessionIdleTimeoutMillis() > 0) {
        factory.getPolicy().setIdleTimeout(service.getConfig().getWebSocketSessionIdleTimeoutMillis());
    }
    factory.setCreator((request, response) -> new ProducerHandler(service, request.getHttpServletRequest(), response));
}
 
Example #22
Source File: WebSocketConsumerServlet.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {
    factory.getPolicy().setMaxTextMessageSize(service.getConfig().getWebSocketMaxTextFrameSize());
    if (service.getConfig().getWebSocketSessionIdleTimeoutMillis() > 0) {
        factory.getPolicy().setIdleTimeout(service.getConfig().getWebSocketSessionIdleTimeoutMillis());
    }
    factory.setCreator(
            (request, response) -> new ConsumerHandler(service, request.getHttpServletRequest(), response));
}
 
Example #23
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 #24
Source File: RoboconfWebSocketServletTest.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testFactoryConfiguration() {

	RoboconfWebSocketServlet servlet = new RoboconfWebSocketServlet();
	WebSocketServletFactory factory = Mockito.mock( WebSocketServletFactory.class );

	servlet.configure( factory );
	Mockito.verify( factory ).register( RoboconfWebSocket.class );
}
 
Example #25
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 #26
Source File: ControlWebSocketServlet.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory webSocketServletFactory) {
    PlayerSocketCreator creator =
            new PlayerSocketCreator(transport,
                    authentication,
                    PlayerSocket.SERVER_SEND_FIRST);

    webSocketServletFactory.setCreator(creator);

    timer.resume();
}
 
Example #27
Source File: ScreenWebSocketServlet.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory webSocketServletFactory) {
    PlayerSocketCreator creator =
            new PlayerSocketCreator(transport,
                    authentication,
                    PlayerSocket.CLIENT_SEND_FIRST);

    webSocketServletFactory.setCreator(creator);
}
 
Example #28
Source File: ClientProxy.java    From OmniOcular with Apache License 2.0 5 votes vote down vote up
@Override
public void startHttpServer() {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            Server server = new Server(23333);
            try {
                ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
                context.setContextPath("/");
                ServletHolder wsHolder = new ServletHolder("echo", new WebSocketServlet() {
                    @Override
                    public void configure(WebSocketServletFactory factory) {
                        factory.register(WebSocketHandler.class);
                    }
                });
                context.addServlet(wsHolder, "/w");

                URI uri = OmniOcular.class.getResource("/assets/omniocular/static/").toURI();
                context.setBaseResource(Resource.newResource(uri));
                context.setWelcomeFiles(new String[]{"index.html"});
                ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
                holderPwd.setInitParameter("cacheControl", "max-age=0,public");
                holderPwd.setInitParameter("useFileMappedBuffer", "false");
                context.addServlet(holderPwd, "/");

                server.setHandler(context);
                server.start();
                server.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    t.start();

}
 
Example #29
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 #30
Source File: WebSocketReaderServlet.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(WebSocketServletFactory factory) {
    factory.getPolicy().setMaxTextMessageSize(service.getConfig().getWebSocketMaxTextFrameSize());
    if (service.getConfig().getWebSocketSessionIdleTimeoutMillis() > 0) {
        factory.getPolicy().setIdleTimeout(service.getConfig().getWebSocketSessionIdleTimeoutMillis());
    }
    factory.setCreator(
            (request, response) -> new ReaderHandler(service, request.getHttpServletRequest(), response));
}