コード例 #1
0
  @Test
  public void testSpecExample3() throws Exception {
    WsServerContainer sc = new WsServerContainer(new TesterServletContext());

    ServerEndpointConfig configA =
        ServerEndpointConfig.Builder.create(Object.class, "/a/{var}/c").build();
    ServerEndpointConfig configB =
        ServerEndpointConfig.Builder.create(Object.class, "/a/b/c").build();
    ServerEndpointConfig configC =
        ServerEndpointConfig.Builder.create(Object.class, "/a/{var1}/{var2}").build();

    sc.addEndpoint(configA);
    sc.addEndpoint(configB);
    sc.addEndpoint(configC);

    Assert.assertEquals(configB, sc.findMapping("/a/b/c").getConfig());
    Assert.assertEquals(configA, sc.findMapping("/a/d/c").getConfig());
    Assert.assertEquals(configC, sc.findMapping("/a/x/y").getConfig());
  }
コード例 #2
0
  @Test(expected = javax.websocket.DeploymentException.class)
  public void testDuplicatePaths_03() throws Exception {
    WsServerContainer sc = new WsServerContainer(new TesterServletContext());

    ServerEndpointConfig configA =
        ServerEndpointConfig.Builder.create(Object.class, "/a/b/{var1}").build();
    ServerEndpointConfig configB =
        ServerEndpointConfig.Builder.create(Object.class, "/a/b/{var2}").build();

    sc.addEndpoint(configA);
    sc.addEndpoint(configB);
  }
コード例 #3
0
  /**
   * Provides the equivalent of {@link #addEndpoint(ServerEndpointConfig)} for publishing plain old
   * java objects (POJOs) that have been annotated as WebSocket endpoints.
   *
   * @param pojo The annotated POJO
   */
  @Override
  public void addEndpoint(Class<?> pojo) throws DeploymentException {

    ServerEndpoint annotation = pojo.getAnnotation(ServerEndpoint.class);
    if (annotation == null) {
      throw new DeploymentException(
          sm.getString("serverContainer.missingAnnotation", pojo.getName()));
    }
    String path = annotation.value();

    // Validate encoders
    validateEncoders(annotation.encoders());

    // Method mapping
    PojoMethodMapping methodMapping = new PojoMethodMapping(pojo, annotation.decoders(), path);

    // ServerEndpointConfig
    ServerEndpointConfig sec;
    Class<? extends Configurator> configuratorClazz = annotation.configurator();
    Configurator configurator = null;
    if (!configuratorClazz.equals(Configurator.class)) {
      try {
        configurator = annotation.configurator().newInstance();
      } catch (InstantiationException | IllegalAccessException e) {
        throw new DeploymentException(
            sm.getString(
                "serverContainer.configuratorFail",
                annotation.configurator().getName(),
                pojo.getClass().getName()),
            e);
      }
    }
    sec =
        ServerEndpointConfig.Builder.create(pojo, path)
            .decoders(Arrays.asList(annotation.decoders()))
            .encoders(Arrays.asList(annotation.encoders()))
            .subprotocols(Arrays.asList(annotation.subprotocols()))
            .configurator(configurator)
            .build();
    sec.getUserProperties().put(PojoEndpointServer.POJO_METHOD_MAPPING_KEY, methodMapping);

    addEndpoint(sec);
  }