@Test
  public void testOnClose() throws IOException, ServletException {
    framework.addAtmosphereHandler(
        "/a",
        new AbstractReflectorAtmosphereHandler() {
          @Override
          public void onRequest(AtmosphereResource resource) throws IOException {}

          @Override
          public void destroy() {}
        });

    AtmosphereRequest request = new AtmosphereRequest.Builder().pathInfo("/a").build();

    final AtomicReference<Boolean> closed = new AtomicReference<Boolean>();

    final AtmosphereResourceEventListener listener =
        new AtmosphereResourceEventListenerAdapter() {
          @Override
          public void onClose(AtmosphereResourceEvent event) {
            closed.set(true);
          }
        };

    framework.interceptor(
        new AtmosphereInterceptor() {
          @Override
          public void configure(AtmosphereConfig config) {}

          @Override
          public Action inspect(AtmosphereResource r) {
            r.addEventListener(listener).suspend();
            try {
              r.getBroadcaster().broadcast("void").get();
            } catch (InterruptedException e) {
            } catch (ExecutionException e) {
            }
            return Action.CONTINUE;
          }

          @Override
          public void postInspect(AtmosphereResource r) {
            try {
              r.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        });
    framework.doCometSupport(request, AtmosphereResponse.newInstance());
    assertTrue(closed.get());
  }
Example #2
0
  @Test
  public void setCookieTest()
      throws IOException, ServletException, ExecutionException, InterruptedException {
    final AtomicReference<Cookie> cValue = new AtomicReference<Cookie>();
    final AtomicReference<AtmosphereResource> r = new AtomicReference<AtmosphereResource>();

    framework.addAtmosphereHandler(
        "/*",
        new AtmosphereHandler() {

          @Override
          public void onRequest(AtmosphereResource resource) throws IOException {
            resource.getResponse().addCookie(resource.getRequest().getCookies()[0]);
            r.set(resource);
            resource.getBroadcaster().addAtmosphereResource(resource);
          }

          @Override
          public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            Cookie[] c = event.getResource().getRequest().getCookies();
            cValue.set(c[0]);
          }

          @Override
          public void destroy() {}
        });
    Set<Cookie> c = new HashSet<Cookie>();
    Cookie a = new Cookie("yo", "man");
    a.setComment("kdaskjdaskda");
    a.setDomain("dasdasdasd");
    a.setHttpOnly(true);
    a.setPath("/ya");
    c.add(a);

    AtmosphereRequest request =
        new AtmosphereRequestImpl.Builder().cookies(c).pathInfo("/a").build();
    AtmosphereResponse response =
        AtmosphereResponseImpl.newInstance().delegateToNativeResponse(false);
    response.destroyable(false);
    framework.doCometSupport(request, response);

    r.get().getBroadcaster().broadcast("yo").get();
    assertNotNull(cValue.get());

    Cookie i = c.iterator().next();
    assertEquals(i.getName(), cValue.get().getName());
    assertEquals(i.getValue(), cValue.get().getValue());
    assertEquals(
        "yo=man; Domain=dasdasdasd; Path=/ya; HttpOnly", response.headers().get("Set-Cookie"));
  }
  @Test
  public void testEmptyDestroy() throws IOException, ServletException {
    Broadcaster b = framework.getBroadcasterFactory().lookup(B.class, "/test", true);
    b.setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy.EMPTY_DESTROY);
    AR ah = new AR();

    framework.addAtmosphereHandler("/*", ah, b).init();
    AtmosphereRequest request =
        new AtmosphereRequestImpl.Builder().pathInfo("/a").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    b.removeAtmosphereResource(ah.resource);

    assertFalse(B.class.cast(b).releaseExternalResources.get());
    assertTrue(B.class.cast(b).destroy.get());
  }
  @Test
  public void testIdleResume() throws IOException, ServletException, InterruptedException {
    B b = framework.getBroadcasterFactory().lookup(B.class, "/test", true);
    b.setBroadcasterLifeCyclePolicy(
        new BroadcasterLifeCyclePolicy.Builder()
            .policy(BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_RESUME)
            .idleTimeInMS(500)
            .build());
    b.latch = new CountDownLatch(1);

    AR ah = new AR();

    framework.addAtmosphereHandler("/*", ah, b).init();
    AtmosphereRequest request =
        new AtmosphereRequestImpl.Builder().pathInfo("/a").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());

    b.latch.await();

    assertFalse(B.class.cast(b).releaseExternalResources.get());
    assertTrue(B.class.cast(b).destroy.get());
  }
Example #5
0
  @Test
  public void basicHandlerTest()
      throws IOException, ServletException, ExecutionException, InterruptedException {
    final AtomicReference<Cookie> cValue = new AtomicReference<Cookie>();
    final AtomicReference<AtmosphereResource> r = new AtomicReference<AtmosphereResource>();

    framework.addAtmosphereHandler(
        "/*",
        new AtmosphereHandler() {

          @Override
          public void onRequest(AtmosphereResource resource) throws IOException {
            r.set(resource);
            resource.getBroadcaster().addAtmosphereResource(resource);
          }

          @Override
          public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            Cookie[] c = event.getResource().getRequest().getCookies();
            cValue.set(c[0]);
          }

          @Override
          public void destroy() {}
        });
    Set<Cookie> c = new HashSet<Cookie>();
    c.add(new Cookie("yo", "man"));

    AtmosphereRequest request =
        new AtmosphereRequestImpl.Builder().cookies(c).pathInfo("/a").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());

    r.get().getBroadcaster().broadcast("yo").get();
    assertNotNull(cValue.get());

    Cookie i = c.iterator().next();
    assertEquals(i.getName(), cValue.get().getName());
    assertEquals(i.getValue(), cValue.get().getValue());
  }
 /**
  * Delegate the request processing to an instance of {@link org.atmosphere.cpr.AsyncSupport}
  *
  * @param req the {@link javax.servlet.http.HttpServletRequest}
  * @param res the {@link javax.servlet.http.HttpServletResponse}
  * @throws java.io.IOException
  * @throws javax.servlet.ServletException
  */
 @Override
 public void doPost(HttpServletRequest req, HttpServletResponse res)
     throws IOException, ServletException {
   framework.doCometSupport(AtmosphereRequest.wrap(req), AtmosphereResponse.wrap(res));
 }