Exemplo n.º 1
0
  public final ServerStarter start(Stage stage, Consumer<ServerBuilder> consumer) {
    notNull(consumer, "Null ServerBuilder consumer");

    /* Create a new injector and set up the module */
    final Injector injector =
        Guice.createInjector(stage, (binder) -> consumer.accept(new ServerBuilder(binder)));

    /* Get a hold on our HttpServer instance */
    final HttpServer server = injector.getInstance(HttpServer.class);
    final String serverName = server.getServerConfiguration().getName();

    /* Attempt to start our server */
    try {
      log.info("Starting server %s", serverName);
      server.start();
    } catch (IOException exception) {
      log.error(exception, "Exception starting server %s", serverName);
      System.exit(1);
    }

    /* Add a shutdown hook terminating the server on exit */
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                log.info("Shutting down server %s", serverName);
                server.shutdown();
              }
            });

    /* Return self for chaining */
    this.server = server;
    return this;
  }
Exemplo n.º 2
0
  public void start() {
    // set up server handler and handlers strategy
    handler = new HTTPHandler(strategy);
    server.getServerConfiguration().addHttpHandler(handler, "/getwsurl");

    try {
      server.start();
      logger.info("HTTP Server started");
    } catch (IOException e) {
      logger.error("HTTP Server couldnt start : " + e);
    }
  }
  public static void main(String[] args) throws Exception {

    HttpServer server = HttpServer.createSimpleServer("/", 8080);
    WebappContext ctx = new WebappContext("api");
    ServletRegistration jerseyServlet =
        ctx.addServlet("jersey", org.glassfish.jersey.servlet.ServletContainer.class);
    jerseyServlet.addMapping("/api/*");
    jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "com.resource");
    jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    ctx.deploy(server);
    try {
      server.start();
      System.out.println("Press any key to stop the server....");
      System.in.read();
    } finally {
      server.shutdownNow();
    }
  }
Exemplo n.º 4
0
  @Override
  public void startServer() throws Exception {
    server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));

    AtmosphereServlet atmoServlet = new AtmosphereServlet();

    WebappContext context = new WebappContext("Chatty", "/chatty");

    ServletRegistration atmosphereRegistration = context.addServlet("Atmosphere", atmoServlet);
    atmosphereRegistration.addMapping("/atmos/*");

    ServletContainer jerseyContainer = new ServletContainer(resourceConfig);
    ServletRegistration jerseyRegistration = context.addServlet("Jersey", jerseyContainer);
    jerseyRegistration.addMapping("/api/*");

    context.deploy(server);

    server.start();
  }
Exemplo n.º 5
0
  public static void main(String[] args) throws Exception {
    final Server server = new DefaultServer();
    server.socketAction(
        new Action<ServerSocket>() {
          @Override
          public void on(final ServerSocket socket) {
            System.out.println("on socket: " + socket.uri());
            socket.on(
                "echo",
                new Action<Object>() {
                  @Override
                  public void on(Object data) {
                    System.out.println("on echo event: " + data);
                    socket.send("echo", data);
                  }
                });
            socket.on(
                "chat",
                new Action<Object>() {
                  @Override
                  public void on(Object data) {
                    System.out.println("on chat event: " + data);
                    server.all().send("chat", data);
                  }
                });
          }
        });

    HttpTransportServer httpTransportServer = new HttpTransportServer().transportAction(server);
    WebSocketTransportServer wsTransportServer =
        new WebSocketTransportServer().transportAction(server);

    HttpServer httpServer = HttpServer.createSimpleServer();
    ServerConfiguration config = httpServer.getServerConfiguration();
    config.addHttpHandler(new VibeHttpHandler().httpAction(httpTransportServer), "/vibe");
    NetworkListener listener = httpServer.getListener("grizzly");
    listener.registerAddOn(new WebSocketAddOn());
    WebSocketEngine.getEngine()
        .register("", "/vibe", new VibeWebSocketApplication().wsAction(wsTransportServer));
    httpServer.start();
    System.in.read();
  }
  public static void main(String[] args) {

    // create a basic server that listens on port 8080.
    final HttpServer server = HttpServer.createSimpleServer();

    final ServerConfiguration config = server.getServerConfiguration();

    // Map the path, /echo, to the NonBlockingEchoHandler
    config.addHttpHandler(new NonBlockingEchoHandler(), "/echo");

    try {
      server.start();
      Client client = new Client();
      client.run();
    } catch (IOException ioe) {
      LOGGER.log(Level.SEVERE, ioe.toString(), ioe);
    } finally {
      server.shutdownNow();
    }
  }
Exemplo n.º 7
0
  public static void main(String[] args) {
    try {
      System.out.println("Jersey Entity Data Filtering Example.");

      final HttpServer server =
          GrizzlyHttpServerFactory.createHttpServer(
              BASE_URI, new EntityFilteringApplication(), false);
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread(
                  new Runnable() {
                    @Override
                    public void run() {
                      server.shutdownNow();
                    }
                  }));
      server.start();

      System.out.println("Application started.\nTry out one of these URIs:");
      for (final String path :
          new String[] {
            "projects",
            "projects/detailed",
            "users",
            "users?detailed=true",
            "tasks",
            "tasks/detailed"
          }) {
        System.out.println(BASE_URI + path);
      }
      System.out.println("Stop the application using CTRL+C");

      Thread.currentThread().join();
    } catch (IOException | InterruptedException ex) {
      Logger.getLogger(App.class.getName())
          .log(Level.SEVERE, "I/O error occurred during reading from an system input stream.", ex);
    }
  }
Exemplo n.º 8
0
  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();

    clientUtil = new ClientUtil(target());

    wiser = new Wiser();
    wiser.setPort(2500);
    wiser.start();

    // Force shutdown
    DBIF.reset();

    String httpRoot =
        URLDecoder.decode(
            new File(getClass().getResource("/").getFile()).getAbsolutePath(), "utf-8");
    httpServer = HttpServer.createSimpleServer(httpRoot, "localhost", getPort());
    WebappContext context = new WebappContext("GrizzlyContext", "/music");
    context
        .addFilter("requestContextFilter", RequestContextFilter.class)
        .addMappingForUrlPatterns(null, "/*");
    context
        .addFilter("tokenBasedSecurityFilter", TokenBasedSecurityFilter.class)
        .addMappingForUrlPatterns(null, "/*");
    ServletRegistration reg = context.addServlet("jerseyServlet", ServletContainer.class);
    reg.setInitParameter(
        "jersey.config.server.provider.packages", "com.sismics.music.rest.resource");
    reg.setInitParameter(
        "jersey.config.server.provider.classnames",
        "org.glassfish.jersey.media.multipart.MultiPartFeature");
    reg.setInitParameter("jersey.config.server.response.setStatusOverSendError", "true");
    reg.setLoadOnStartup(1);
    reg.addMapping("/*");
    reg.setAsyncSupported(true);
    context.deploy(httpServer);
    httpServer.start();
  }
Exemplo n.º 9
0
  public static void main(String[] args) throws IOException {
    try {
      System.out.println("JSON with MOXy Jersey Example App");

      final HttpServer server =
          GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), createAppConfig());
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread(
                  new Runnable() {
                    @Override
                    public void run() {
                      server.shutdownNow();
                    }
                  }));
      server.start();

      System.out.println(String.format("Application started.%nStop the application using CTRL+C"));

      Thread.currentThread().join();
    } catch (IOException | InterruptedException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
Exemplo n.º 10
0
  public static void main(String[] args) throws Exception {

    BufferedReader br = null;
    String line = null;

    SocketServer socketServer = new SocketServer();

    HttpServer server = HttpServer.createSimpleServer();
    server.getListener("grizzly").registerAddOn(new WebSocketAddOn());
    WebSocketEngine.getEngine().register("/", socketServer);
    server.start();

    List<WebSocket> sockets = socketServer.getSockets();
    br = new BufferedReader(new InputStreamReader(System.in));

    while ((line = br.readLine()) != null) {
      // desde el servidor al los browsers
      for (WebSocket webSocket : sockets) {
        webSocket.send(line);
      }
    }

    server.stop();
  }
 public void start() throws IOException {
   server.start();
 }