protected void performTest(MonitorAction action, long count, long interval) throws Exception {
    _monitor.addActions(action);

    for (long cnt = 0; cnt < count; cnt++) {
      try {
        ContentExchange getExchange = new ContentExchange();
        getExchange.setURL(_requestUrl);
        getExchange.setMethod(HttpMethods.GET);

        _client.send(getExchange);
        int state = getExchange.waitForDone();

        String content = "";
        int responseStatus = getExchange.getResponseStatus();
        if (responseStatus == HttpStatus.OK_200) {
          content = getExchange.getResponseContent();
        }

        assertEquals(HttpStatus.OK_200, responseStatus);
        Thread.sleep(interval);
      } catch (InterruptedException ex) {
        break;
      }
    }

    Thread.sleep(interval);

    _monitor.removeActions(action);
  }
Beispiel #2
0
  public void requestControl(
      final Player player,
      Figure.Type type,
      int x,
      int y,
      final Joystick joystick,
      List<Plot> plots)
      throws IOException {
    ContentExchange exchange = new MyContentExchange(joystick, player);

    exchange.setMethod("GET");
    String callbackUrl =
        player.getCallbackUrl().endsWith("/")
            ? player.getCallbackUrl()
            : player.getCallbackUrl() + "/";
    StringBuilder sb = exportGlassState(plots);

    String url =
        callbackUrl
            + "?figure="
            + type
            + "&x="
            + x
            + "&y="
            + y
            + "&glass="
            + URLEncoder.encode(sb.toString(), "UTF-8");
    exchange.setURL(url);
    client.send(exchange);
  }
 /**
  * HTTP notifications will be set out to subscribers interested in resources from the tree where
  * they have have hung onem2m subscription resources
  *
  * @param url where do i send this onem2m notify message
  * @param payload contents of the notification
  */
 @Override
 public void sendNotification(String url, String payload) {
   ContentExchange ex = new ContentExchange();
   ex.setURL(url);
   ex.setRequestContentSource(new ByteArrayInputStream(payload.getBytes()));
   ex.setRequestContentType(Onem2m.ContentType.APP_VND_NTFY_JSON);
   Integer cl = payload != null ? payload.length() : 0;
   ex.setRequestHeader("Content-Length", cl.toString());
   ex.setMethod("post");
   LOG.debug("HTTP: Send notification uri: {}, payload: {}:", url, payload);
   try {
     client.send(ex);
   } catch (IOException e) {
     LOG.error("Dropping notification: uri: {}, payload: {}", url, payload);
   }
 }
Beispiel #4
0
  protected static void runTest(String requestUrl, long count) {
    HttpClient client = new HttpClient();
    client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
    try {
      client.start();
    } catch (Exception ex) {
      LOG.debug(ex);
    }

    if (client != null) {
      for (long cnt = 0; cnt < count; cnt++) {
        try {
          ContentExchange getExchange = new ContentExchange();
          getExchange.setURL(requestUrl);
          getExchange.setMethod(HttpMethods.GET);

          client.send(getExchange);
          int state = getExchange.waitForDone();

          String content = "";
          int responseStatus = getExchange.getResponseStatus();
          if (responseStatus == HttpStatus.OK_200) {
            content = getExchange.getResponseContent();
          }

          Thread.sleep(100);
        } catch (InterruptedException ex) {
          break;
        } catch (IOException ex) {
          LOG.debug(ex);
        }
      }

      try {
        client.stop();
      } catch (Exception ex) {
        LOG.debug(ex);
      }
    }
  }
  @Test
  public void testLastAccessTime() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int maxInactivePeriod = 8;
    int scavengePeriod = 2;
    AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    server1.start();
    int port1 = server1.getPort();
    try {
      AbstractTestServer server2 = createServer(0, maxInactivePeriod, scavengePeriod);
      server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
      server2.start();
      int port2 = server2.getPort();
      try {
        HttpClient client = new HttpClient();
        client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
        client.start();
        try {
          // Perform one request to server1 to create a session
          ContentExchange exchange1 = new ContentExchange(true);
          exchange1.setMethod(HttpMethods.GET);
          exchange1.setURL(
              "http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
          client.send(exchange1);
          exchange1.waitForDone();
          assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
          assertEquals("test", exchange1.getResponseContent());
          String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
          assertTrue(sessionCookie != null);
          // Mangle the cookie, replacing Path with $Path, etc.
          sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");

          // Perform some request to server2 using the session cookie from the previous request
          // This should migrate the session from server1 to server2, and leave server1's
          // session in a very stale state, while server2 has a very fresh session.
          // We want to test that optimizations done to the saving of the shared lastAccessTime
          // do not break the correct working
          int requestInterval = 500;
          for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i) {
            ContentExchange exchange2 = new ContentExchange(true);
            exchange2.setMethod(HttpMethods.GET);
            exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping);
            exchange2.getRequestFields().add("Cookie", sessionCookie);
            client.send(exchange2);
            exchange2.waitForDone();
            assertEquals(HttpServletResponse.SC_OK, exchange2.getResponseStatus());
            assertEquals("test", exchange2.getResponseContent());

            String setCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
            if (setCookie != null)
              sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");

            Thread.sleep(requestInterval);
          }

          // At this point, session1 should be eligible for expiration.
          // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
          Thread.sleep(scavengePeriod * 2500L);

          // Access again server1, and ensure that we can still access the session
          exchange1 = new ContentExchange(true);
          exchange1.setMethod(HttpMethods.GET);
          exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping);
          exchange1.getRequestFields().add("Cookie", sessionCookie);
          client.send(exchange1);
          exchange1.waitForDone();
          assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
          // test that the session was kept alive by server 2 and still contains what server1 put in
          // it
          assertEquals("test", exchange1.getResponseContent());

        } finally {
          client.stop();
        }
      } finally {
        server2.stop();
      }
    } finally {
      server1.stop();
    }
  }