Esempio n. 1
0
  /**
   * @param uri Url.
   * @param params Params.
   * @param demo Use demo node.
   * @param mtd Method.
   * @param headers Headers.
   * @param body Body.
   */
  protected RestResult executeRest(
      String uri,
      Map<String, Object> params,
      boolean demo,
      String mtd,
      Map<String, Object> headers,
      String body)
      throws IOException, URISyntaxException {
    if (log.isDebugEnabled())
      log.debug(
          "Start execute REST command [method="
              + mtd
              + ", uri=/"
              + (uri == null ? "" : uri)
              + ", parameters="
              + params
              + "]");

    final URIBuilder builder;

    if (demo) {
      // try start demo if needed.
      AgentClusterDemo.testDrive(cfg);

      // null if demo node not started yet.
      if (cfg.demoNodeUri() == null) return RestResult.fail("Demo node is not started yet.", 404);

      builder = new URIBuilder(cfg.demoNodeUri());
    } else builder = new URIBuilder(cfg.nodeUri());

    if (builder.getPort() == -1) builder.setPort(DFLT_NODE_PORT);

    if (uri != null) {
      if (!uri.startsWith("/") && !cfg.nodeUri().endsWith("/")) uri = '/' + uri;

      builder.setPath(uri);
    }

    if (params != null) {
      for (Map.Entry<String, Object> entry : params.entrySet()) {
        if (entry.getValue() != null)
          builder.addParameter(entry.getKey(), entry.getValue().toString());
      }
    }

    HttpRequestBase httpReq = null;

    try {
      if ("GET".equalsIgnoreCase(mtd)) httpReq = new HttpGet(builder.build());
      else if ("POST".equalsIgnoreCase(mtd)) {
        HttpPost post;

        if (body == null) {
          List<NameValuePair> nvps = builder.getQueryParams();

          builder.clearParameters();

          post = new HttpPost(builder.build());

          if (!nvps.isEmpty()) post.setEntity(new UrlEncodedFormEntity(nvps));
        } else {
          post = new HttpPost(builder.build());

          post.setEntity(new StringEntity(body));
        }

        httpReq = post;
      } else throw new IOException("Unknown HTTP-method: " + mtd);

      if (headers != null) {
        for (Map.Entry<String, Object> entry : headers.entrySet())
          httpReq.addHeader(
              entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString());
      }

      try (CloseableHttpResponse resp = httpClient.execute(httpReq)) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        resp.getEntity().writeTo(out);

        Charset charset = Charsets.UTF_8;

        Header encodingHdr = resp.getEntity().getContentEncoding();

        if (encodingHdr != null) {
          String encoding = encodingHdr.getValue();

          charset = Charsets.toCharset(encoding);
        }

        return RestResult.success(
            resp.getStatusLine().getStatusCode(), new String(out.toByteArray(), charset));
      } catch (ConnectException e) {
        log.info("Failed connect to node and execute REST command [uri=" + builder.build() + "]");

        return RestResult.fail("Failed connect to node and execute REST command.", 404);
      }
    } finally {
      if (httpReq != null) httpReq.reset();
    }
  }
Esempio n. 2
0
  /** Start ignite node with cacheEmployee and populate it with data. */
  public static boolean testDrive(AgentConfiguration acfg) {
    if (initLatch.compareAndSet(false, true)) {
      log.info("DEMO: Starting embedded nodes for demo...");

      System.setProperty(IGNITE_ATOMIC_CACHE_DELETE_HISTORY_SIZE, "1");

      System.setProperty(IGNITE_JETTY_PORT, "60800");
      System.setProperty(IGNITE_NO_ASCII, "true");

      try {
        IgniteEx ignite = (IgniteEx) Ignition.start(igniteConfiguration(0, false));

        final AtomicInteger cnt = new AtomicInteger(0);

        final ScheduledExecutorService execSrv = Executors.newSingleThreadScheduledExecutor();

        execSrv.scheduleAtFixedRate(
            new Runnable() {
              @Override
              public void run() {
                int idx = cnt.incrementAndGet();

                try {
                  Ignition.start(igniteConfiguration(idx, idx == NODE_CNT));
                } catch (Throwable e) {
                  log.error("DEMO: Failed to start embedded node: " + e.getMessage());
                } finally {
                  if (idx == NODE_CNT) execSrv.shutdown();
                }
              }
            },
            10,
            10,
            TimeUnit.SECONDS);

        if (log.isDebugEnabled())
          log.debug("DEMO: Started embedded nodes with indexed enabled caches...");

        Collection<String> jettyAddrs = ignite.localNode().attribute(ATTR_REST_JETTY_ADDRS);

        String host = jettyAddrs == null ? null : jettyAddrs.iterator().next();

        Integer port = ignite.localNode().attribute(ATTR_REST_JETTY_PORT);

        if (F.isEmpty(host) || port == null) {
          log.error("DEMO: Failed to start embedded node with rest!");

          return false;
        }

        acfg.demoNodeUri(String.format("http://%s:%d", host, port));

        log.info("DEMO: Embedded nodes for sql test-drive successfully started");

        startLoad(ignite, 20);
      } catch (Exception e) {
        log.error("DEMO: Failed to start embedded node for sql test-drive!", e);

        return false;
      }
    }

    return true;
  }