Exemplo n.º 1
0
  public CtssResourceClient(String endpoint) {
    // parse csv output from ctss-resource-v1 service
    Client client = new Client(Protocol.HTTP);
    Response response = client.get(endpoint);

    // Parse the response into a SystemDTO object
    Representation output = response.getEntity();
    InputStream in = null;
    try {
      in = output.getStream();
      byte[] b = new byte[1024];
      String resources = "";
      while ((in.read(b)) > -1) {
        resources += new String(b);
      }
      if (resources != null && !resources.equals("")) {
        for (String line : resources.split("\n")) {
          if (line.startsWith("Site")) continue;
          log.debug("Processing line " + line);
          String[] values = line.split(",");
          if (values.length == 1) continue;
          ComputeDTO sysDto = new ComputeDTO();
          sysDto.setSite(values[0].replaceAll("\"", ""));
          sysDto.setResourceId(values[1].replaceAll("\"", ""));
          sysDto.setTgcdbName(values[2].replaceAll("\"", ""));
          sysDto.setName(
              values[3].replaceAll("\"", "").equals("")
                  ? values[1]
                  : values[3].replaceAll("\"", ""));
          sysDto.setType("compute");
          sysDto.setStatus(Service.UP);
          //				sysDto.setGridftpHostname(values[4].replaceAll("\"", ""));

          ctssSystems.add(sysDto);
        }
      }
    } catch (Exception e) {
      log.debug("Failed to retrieve output from " + endpoint, e);
    } finally {
      try {
        in.close();
      } catch (Exception e) {
        log.error(e);
      }
    }
  }
  public void testTemplateFilter() {
    try {
      // Create a temporary directory for the tests
      this.testDir = new File(System.getProperty("java.io.tmpdir"), "TemplateFilterTestCase");
      deleteDir(this.testDir);
      this.testDir.mkdir();

      // Create temporary template files
      // Will be templated
      final File testFileFm1 = new File(this.testDir, "testFm1.txt.fmt");
      FileWriter fw = new FileWriter(testFileFm1);
      fw.write("Method=${m}/Authority=${ra}");
      fw.close();

      // Will not be templated
      final File testFileFm2 = new File(this.testDir, "testFm2.txt");
      fw = new FileWriter(testFileFm2);
      fw.write("Method=${m}/Authority=${ra}");
      fw.close();

      // Will be templated
      final File testFileVl1 = new File(this.testDir, "testVl1.txt.vm");
      fw = new FileWriter(testFileVl1);
      fw.write("Method=${m}/Path=${rp}");
      fw.close();

      // Will not be templated
      final File testFileVl2 = new File(this.testDir, "testVl2.txt");
      fw = new FileWriter(testFileVl2);
      fw.write("Method=${m}/Path=${rp}");
      fw.close();

      // Create a new component
      final Component component = new Component();
      component.getServers().add(Protocol.HTTP, 8182);
      component.getClients().add(Protocol.FILE);

      // Create an application filtered with Freemarker
      final MyFreemakerApplication freemarkerApplication = new MyFreemakerApplication(this.testDir);
      // Create an application filtered with Velocity
      final MyVelocityApplication velocityApplication = new MyVelocityApplication(this.testDir);

      // Attach the applications to the component and start it
      component.getDefaultHost().attach("/freemarker", freemarkerApplication);
      component.getDefaultHost().attach("/velocity", velocityApplication);

      // Now, let's start the component!
      component.start();

      // Allow extensions tunneling
      freemarkerApplication.getTunnelService().setExtensionsTunnel(true);
      velocityApplication.getTunnelService().setExtensionsTunnel(true);
      final Client client = new Client(Protocol.HTTP);
      Response response = client.get("http://localhost:8182/freemarker/" + testFileFm1.getName());
      if (response.isEntityAvailable()) {
        assertEquals(response.getEntity().getText(), "Method=GET/Authority=localhost:8182");
      }
      response = client.get("http://localhost:8182/freemarker/" + testFileFm2.getName());
      assertTrue(response.getStatus().isSuccess());
      if (response.isEntityAvailable()) {
        assertEquals(response.getEntity().getText(), "Method=${m}/Authority=${ra}");
      }

      response = client.get("http://localhost:8182/velocity/" + testFileVl1.getName());
      if (response.isEntityAvailable()) {
        assertEquals(response.getEntity().getText(), "Method=GET/Path=/velocity/testVl1");
      }
      response = client.get("http://localhost:8182/velocity/" + testFileVl2.getName());
      assertTrue(response.getStatus().isSuccess());
      if (response.isEntityAvailable()) {
        assertEquals(response.getEntity().getText(), "Method=${m}/Path=${rp}");
      }
      // Now, let's stop the component!
      component.stop();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }