Exemple #1
0
  public static void main(final String[] args) {
    try {

      DeploymentInfo servletBuilder =
          deployment()
              .setClassLoader(ServletServer.class.getClassLoader())
              .setContextPath(MYAPP)
              .setDeploymentName("test.war")
              .addServlets(
                  servlet("MessageServlet", MessageServlet.class)
                      .addInitParam("message", "Hello World")
                      .addMapping("/*"),
                  servlet("MyServlet", MessageServlet.class)
                      .addInitParam("message", "MyServlet")
                      .addMapping("/myservlet"));

      DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
      manager.deploy();

      HttpHandler servletHandler = manager.start();
      PathHandler path =
          Handlers.path(Handlers.redirect(MYAPP)).addPrefixPath(MYAPP, servletHandler);
      Undertow server = Undertow.builder().addListener(8080, "localhost").setHandler(path).build();
      server.start();
    } catch (ServletException e) {
      throw new RuntimeException(e);
    }
  }
  @BeforeClass
  public static void setup() throws Exception {

    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder =
        new DeploymentInfo()
            .setClassLoader(ClientEndpointReconnectTestCase.class.getClassLoader())
            .setContextPath("/ws")
            .setResourceManager(new TestResourceLoader(ClientEndpointReconnectTestCase.class))
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .addServletContextAttribute(
                WebSocketDeploymentInfo.ATTRIBUTE_NAME,
                new WebSocketDeploymentInfo()
                    .setBuffers(new ByteBufferSlicePool(100, 1000))
                    .setWorker(DefaultServer.getWorker())
                    .addEndpoint(DisconnectServerEndpoint.class)
                    .addEndpoint(AnnotatedClientReconnectEndpoint.class)
                    .addListener(
                        new WebSocketDeploymentInfo.ContainerReadyListener() {
                          @Override
                          public void ready(ServerWebSocketContainer container) {
                            deployment = container;
                          }
                        })
                    .setReconnectHandler(
                        new WebSocketReconnectHandler() {
                          @Override
                          public long disconnected(
                              CloseReason closeReason,
                              URI connectionUri,
                              Session session,
                              int disconnectCount) {
                            if (disconnectCount < 3) {
                              return 1;
                            } else {
                              return -1;
                            }
                          }

                          @Override
                          public long reconnectFailed(
                              IOException exception,
                              URI connectionUri,
                              Session session,
                              int failedCount) {
                            failed = true;
                            return -1;
                          }
                        }))
            .setDeploymentName("servletContext.war");
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();

    DefaultServer.setRootHandler(Handlers.path().addPrefixPath("/ws", manager.start()));
  }
 @Deprecated
 public PathHandler mount(ScannerResult scanResult) {
   PathHandler pathHandler = Handlers.path();
   /*    try {
     for (Class<?> handlerClass : scanResult.getHandlers()) {
         Constructor<?> constructor = handlerClass.getConstructor();
         constructor.setAccessible(true);
         mountMethods(pathHandler, handlerClass);
     }
     pathHandler = mountServerEndpoints(pathHandler , scanResult.getServerEndpoints() );
   } catch (Exception e) {
     LOGGER.error(e.getMessage());
   }*/
   return pathHandler;
 }
Exemple #4
0
  @Override
  public void run() {
    Undertow server =
        Undertow.builder()
            .addHttpListener(port, host)
            .setHandler(
                Handlers.path()
                    //                        .addPrefixPath("/", new ResourceHandler(new
                    // ClassPathResourceManager(Server.class.getClassLoader(), "templates")))
                    .addPrefixPath("/", new IndexPageHandler(indexTemplate, this))
                    .addPrefixPath(
                        "/form",
                        new EagerFormParsingHandler().setNext(new FormHandler(indexTemplate, this)))
                    .addPrefixPath("/api/message", new MessagesRequestHandler(serializer, this))
                    .addPrefixPath("/api/update", new UpdateHandler(serializer, this)))
            .build();

    server.start();
  }
  @BeforeClass
  public static void setup() throws ServletException {

    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder =
        new DeploymentInfo()
            .setClassLoader(TestMessagesReceivedInOrder.class.getClassLoader())
            .setContextPath("/")
            .setResourceManager(new TestResourceLoader(TestMessagesReceivedInOrder.class))
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .addServletContextAttribute(
                WebSocketDeploymentInfo.ATTRIBUTE_NAME,
                new WebSocketDeploymentInfo()
                    .setBuffers(new ByteBufferSlicePool(100, 1000))
                    .setWorker(DefaultServer.getWorker())
                    .addEndpoint(EchoSocket.class))
            .setDeploymentName("servletContext.war");

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();

    DefaultServer.setRootHandler(Handlers.path().addPrefixPath("/", manager.start()));
  }
  public void start() {
    try {
      // start store.
      ZWaveProductsStoreModel storeModel = new ZWaveProductsStoreModel();
      storeModel.setContentDeliveryDriver(
          new WebSocketWrapper(new LevelDbContentDeliveryDriver("ZWaveProductsDb"), 23666));
      storeModel
          .connect()
          .then(
              new Callback<Throwable>() {
                @Override
                public void on(Throwable throwable) {
                  final ZWaveProductsStoreUniverse universe = storeModel.universe(0);
                  ZWaveProductsStoreView baseView = universe.time(0);
                  baseView
                      .select("/")
                      .then(
                          new Callback<KObject[]>() {
                            public void on(KObject[] kObjects) {
                              if (kObjects == null || kObjects.length == 0) {
                                ProductStore store = baseView.createProductStore();
                                baseView
                                    .setRoot(store)
                                    .then(
                                        new Callback<Throwable>() {
                                          public void on(Throwable throwable) {
                                            System.err.println("ProductStore Loading");
                                            load(store);
                                            storeModel
                                                .save()
                                                .then(
                                                    new Callback<Throwable>() {
                                                      @Override
                                                      public void on(Throwable throwable) {
                                                        if (throwable != null) {
                                                          throwable.printStackTrace();
                                                        }
                                                      }
                                                    });
                                          }
                                        });
                              } else {
                                System.err.println("ProductStore root found");
                              }
                            }
                          });
                }
              });

      // serves pictures
      server =
          Undertow.builder()
              .addHttpListener(currentPort, currentIp)
              .setHandler(
                  Handlers.path()
                      .addPrefixPath(
                          "/",
                          Handlers.resource(
                              new ClassPathResourceManager(
                                  this.getClass().getClassLoader(), "static"))))
              .build();
      server.start();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
public class JsonChannelTest {

  public static final int MOCK_PORT = 16000;
  public static final String MOCK_SERVER = "localhost";

  public static final String ROUTE_BOUNCE = "/bounce";

  public static final class BounceAPI extends JsonChannel<ParserTest.Car, ParserTest.Car> {
    public BounceAPI() {
      super(ParserTest.Car.class, ParserTest.Car.class);
    }

    protected ParserTest.Car handle(final HttpMethod method, final ParserTest.Car input) {
      return input;
    }
  }

  public static final class BounceDuplicateAPI extends JsonChannel<ParserTest.Car, ParserTest.Car> {
    public BounceDuplicateAPI() {
      super(ParserTest.Car.class, ParserTest.Car.class);
    }

    protected ParserTest.Car handle(final HttpMethod method, final ParserTest.Car input) {
      return input;
    }
  }

  public static final String ROUTE_BOUNCE_POST = "/bounce_post";

  public static final class BouncePostAPI extends JsonChannel.Post<ParserTest.Car, ParserTest.Car> {
    public BouncePostAPI() {
      super(ParserTest.Car.class, ParserTest.Car.class);
    }

    protected ParserTest.Car post(final ParserTest.Car input) {
      return input;
    }
  }

  public static final String ROUTE_BOUNCE_PUT = "/bounce_put";

  public static final class BouncePutAPI extends JsonChannel.Put<ParserTest.Car, ParserTest.Car> {
    public BouncePutAPI() {
      super(ParserTest.Car.class, ParserTest.Car.class);
    }

    protected ParserTest.Car put(final ParserTest.Car input) {
      return input;
    }
  }

  public static final String ROUTE_BOUNCE_DELETE = "/bounce_delete";

  public static final class BounceDeleteAPI
      extends JsonChannel.Delete<ParserTest.Car, ParserTest.Car> {
    public BounceDeleteAPI() {
      super(ParserTest.Car.class, ParserTest.Car.class);
    }

    protected ParserTest.Car delete(final ParserTest.Car input) {
      return input;
    }
  }

  public static final Undertow undertow =
      Undertow.builder()
          .addHttpListener(MOCK_PORT, MOCK_SERVER)
          .setHandler(
              Handlers.path()
                  .addExactPath(ROUTE_BOUNCE, new BounceAPI())
                  .addExactPath(ROUTE_BOUNCE, new BounceAPI()) // duplicate - OK!
                  .addExactPath(
                      ROUTE_BOUNCE,
                      new BounceDuplicateAPI()) // duplicate but different handler - will override
                                                // previous prefix OK!
                  .addExactPath(ROUTE_BOUNCE_POST, new BouncePostAPI())
                  .addExactPath(ROUTE_BOUNCE_PUT, new BouncePutAPI())
                  .addExactPath(ROUTE_BOUNCE_DELETE, new BounceDeleteAPI()))
          .build();

  @Before
  public final void before() {
    undertow.start();
  }

  @After
  public final void stop() {
    undertow.stop();
  }

  @Test
  public final void bounce_Get() {
    given().port(MOCK_PORT).get(ROUTE_BOUNCE).then().body(equalTo("")).statusCode(405);
  }

  @Test
  public final void bounce_Post() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .post(ROUTE_BOUNCE)
        .then()
        .body(equalTo(json(ParserTest.MOCK_CAR).toString()))
        .statusCode(200);
  }

  @Test
  public final void bounce_Put() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .put(ROUTE_BOUNCE)
        .then()
        .body(equalTo(json(ParserTest.MOCK_CAR).toString()))
        .statusCode(200);
  }

  @Test
  public final void bounce_Delete() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .delete(ROUTE_BOUNCE)
        .then()
        .body(equalTo(json(ParserTest.MOCK_CAR).toString()))
        .statusCode(200);
  }

  @Test
  public final void bouncePost_Get() {
    given().port(MOCK_PORT).get(ROUTE_BOUNCE_POST).then().body(equalTo("")).statusCode(405);
  }

  @Test
  public final void bouncePost_Post() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .post(ROUTE_BOUNCE_POST)
        .then()
        .body(equalTo(json(ParserTest.MOCK_CAR).toString()))
        .statusCode(200);
  }

  @Test
  public final void bouncePost_Put() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .put(ROUTE_BOUNCE_POST)
        .then()
        .body(equalTo(""))
        .statusCode(405);
  }

  @Test
  public final void bouncePost_Delete() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .delete(ROUTE_BOUNCE_POST)
        .then()
        .body(equalTo(""))
        .statusCode(405);
  }

  @Test
  public final void bouncePut_Get() {
    given().port(MOCK_PORT).get(ROUTE_BOUNCE_PUT).then().body(equalTo("")).statusCode(405);
  }

  @Test
  public final void bouncePut_Post() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .post(ROUTE_BOUNCE_PUT)
        .then()
        .body(equalTo(""))
        .statusCode(405);
  }

  @Test
  public final void bouncePut_Put() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .put(ROUTE_BOUNCE_PUT)
        .then()
        .body(equalTo(json(ParserTest.MOCK_CAR).toString()))
        .statusCode(200);
  }

  @Test
  public final void bouncePut_Delete() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .delete(ROUTE_BOUNCE_PUT)
        .then()
        .body(equalTo(""))
        .statusCode(405);
  }

  @Test
  public final void bounceDelete_Get() {
    given().port(MOCK_PORT).get(ROUTE_BOUNCE_DELETE).then().body(equalTo("")).statusCode(405);
  }

  @Test
  public final void bounceDelete_Post() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .post(ROUTE_BOUNCE_DELETE)
        .then()
        .body(equalTo(""))
        .statusCode(405);
  }

  @Test
  public final void bounceDelete_Put() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .put(ROUTE_BOUNCE_DELETE)
        .then()
        .body(equalTo(""))
        .statusCode(405);
  }

  @Test
  public final void bounceDelete_Delete() {
    given()
        .port(MOCK_PORT)
        .body(json(ParserTest.MOCK_CAR).toString())
        .delete(ROUTE_BOUNCE_DELETE)
        .then()
        .body(equalTo(json(ParserTest.MOCK_CAR).toString()))
        .statusCode(200);
  }
}