Beispiel #1
0
 private String createSecret() {
   byte[] nonce = new byte[16];
   for (int i = 0; i < 16; i++) {
     nonce[i] = (byte) (Math.random() * 256);
   }
   return Base64.encodeToString(nonce, Base64.DEFAULT).trim();
 }
Beispiel #2
0
  public void request2(String uri, int reqNum, int reqTerm) throws Exception {
    HttpClientContext context = HttpClientContext.create();
    HttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
    HttpRoute route = new HttpRoute(new HttpHost(uri, 80));
    // Request new connection. This can be a long process
    if (reqNum == 0) {
      System.out.println(
          String.format("request to %s infinite times with term '%d' ms", uri, reqTerm));
    } else {
      System.out.println(
          String.format("request to %s '%d' times with term '%d' ms", uri, reqNum, reqTerm));
    }
    int i = 0, tick = 0;

    while (true) {
      usleep(reqTerm);
      tick = (int) (Math.random() * 10) % 2;
      if (tick == 0) {
        continue;
      }
      System.out.println("111111111");
      ConnectionRequest connRequest = connManager.requestConnection(route, null);
      // Wait for connection up to 10 sec
      HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
      System.out.println("222222222");
      try {
        if (!conn.isOpen()) // If not open
        {
          System.out.println("333333333");
          // establish connection based on its route info
          connManager.connect(conn, route, 1000, context);
          // and mark it as route complete
          connManager.routeComplete(conn, route, context);
        }
        // Do useful things with the connection.
        System.out.println("request " + uri);
        System.out.println(
            "--> response status  = " + conn.receiveResponseHeader().getStatusLine());
      } finally {
        connManager.releaseConnection(conn, null, 1, TimeUnit.MINUTES);
      }

      System.out.println("----------------------------------------");
      if (reqNum != 0 && reqNum < ++i) {
        break;
      }
    }
  }
Beispiel #3
0
  public boolean initialize() {

    try {
      initParams = new RemoteWrapperParamParser(getActiveAddressBean(), true);
      uid = Math.random();

      postParameters = new ArrayList<NameValuePair>();
      postParameters.add(
          new BasicNameValuePair(PushDelivery.NOTIFICATION_ID_KEY, Double.toString(uid)));
      postParameters.add(
          new BasicNameValuePair(
              PushDelivery.LOCAL_CONTACT_POINT, initParams.getLocalContactPoint()));
      // Init the http client
      if (initParams.isSSLRequired()) {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(
            new FileInputStream(new File("conf/servertestkeystore")),
            Main.getContainerConfig().getSSLKeyStorePassword().toCharArray());
        SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
        socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        int sslPort =
            Main.getContainerConfig().getSSLPort() > 0
                ? Main.getContainerConfig().getSSLPort()
                : ContainerConfig.DEFAULT_SSL_PORT;
        Scheme sch = new Scheme("https", socketFactory, sslPort);
        httpclient.getConnectionManager().getSchemeRegistry().register(sch);
      }
      Scheme plainsch =
          new Scheme(
              "http",
              PlainSocketFactory.getSocketFactory(),
              Main.getContainerConfig().getContainerPort());
      httpclient.getConnectionManager().getSchemeRegistry().register(plainsch);
      //
      lastReceivedTimestamp = initParams.getStartTime();
      structure = registerAndGetStructure();
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      NotificationRegistry.getInstance().removeNotification(uid);
      return false;
    }

    return true;
  }
Beispiel #4
0
  public void request1(String uri, int reqNum, int reqTerm) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    if (reqNum == 0) {
      System.out.println(
          String.format("request to %s infinite times with term '%d' ms", uri, reqTerm));
    } else {
      System.out.println(
          String.format("request to %s '%d' times with term '%d' ms", uri, reqNum, reqTerm));
    }

    int i = 0, tick = 0;
    HttpGet httpGet = new HttpGet(uri);
    CloseableHttpResponse response;
    HttpEntity entity;

    while (true) {
      usleep(reqTerm);
      tick = (int) (Math.random() * 10) % 2;
      if (tick == 0) {
        continue;
      }
      System.out.println("request " + httpGet.getURI());
      response = httpclient.execute(httpGet);
      System.out.println("--> response status  = " + response.getStatusLine());
      // response handler
      try {
        entity = response.getEntity();
        EntityUtils.consume(entity);
      } catch (Exception e) {
        System.out.println("  --> http fail:" + e.getMessage());
      } finally {
        // Thread.sleep(5000); //테스트에만 썼다. close 지연시키려고.
        response.close();
      }

      System.out.println("----------------------------------------");
      if (reqNum != 0 && reqNum < ++i) {
        break;
      }
    }
  }