Exemple #1
0
 private File getAndValidateFile(String pFile, String pWhat) throws IOException {
   File ret = new File(pFile);
   if (!ret.exists()) {
     throw new FileNotFoundException("No such " + pWhat + " " + pFile);
   }
   if (!ret.canRead()) {
     throw new IOException(
         pWhat.substring(0, 1).toUpperCase()
             + pWhat.substring(1)
             + " "
             + pFile
             + " is not readable");
   }
   return ret;
 }
 private ArrayList<HashMap<String, String>> getLogs(boolean asList) {
   Set<File> files = new DbgPacker().getItems();
   ArrayList<HashMap<String, String>> logs =
       asList ? new ArrayList<HashMap<String, String>>() : null;
   for (File f : files) {
     if (f.exists()) {
       String id = String.valueOf(parent.getResources().add(f));
       if (asList) {
         HashMap<String, String> item = new HashMap<>();
         item.put("filename", f.getName());
         item.put("id", id);
         logs.add(item);
       }
     }
   }
   return logs;
 }
Exemple #3
0
    public void run() {
      try {
        URL url = new URL(protocol + "://localhost:" + port + "/test1/" + f);
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        if (urlc instanceof HttpsURLConnection) {
          HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
          urlcs.setHostnameVerifier(
              new HostnameVerifier() {
                public boolean verify(String s, SSLSession s1) {
                  return true;
                }
              });
          urlcs.setSSLSocketFactory(ctx.getSocketFactory());
        }
        byte[] buf = new byte[4096];

        if (fixedLen) {
          urlc.setRequestProperty("XFixed", "yes");
        }
        InputStream is = urlc.getInputStream();
        File temp = File.createTempFile("Test1", null);
        temp.deleteOnExit();
        OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp));
        int c, count = 0;
        while ((c = is.read(buf)) != -1) {
          count += c;
          fout.write(buf, 0, c);
        }
        is.close();
        fout.close();

        if (count != size) {
          throw new RuntimeException("wrong amount of data returned");
        }
        String orig = root + "/" + f;
        compare(new File(orig), temp);
        temp.delete();
      } catch (Exception e) {
        e.printStackTrace();
        fail = true;
      }
    }
 private void readCred() throws IOException {
   String cPath = (String) configuration.getCustomProperty("cred.path");
   if (StringUtils.isEmpty(cPath)) {
     return;
   }
   File f = new File(cPath);
   if (!f.exists()) {
     return;
   }
   try (BufferedReader in =
       new BufferedReader(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8))) {
     String str;
     while ((str = in.readLine()) != null) {
       str = str.trim();
       if (StringUtils.isEmpty(str) || str.startsWith("#")) {
         continue;
       }
       String[] s = str.split("\\s*=\\s*", 2);
       if (s.length < 2) {
         continue;
       }
       if (!s[0].startsWith("web")) {
         continue;
       }
       String[] s1 = s[0].split("\\.", 2);
       String[] s2 = s[1].split(",", 2);
       if (s2.length < 2) {
         continue;
       }
       // s2[0] == usr s2[1] == pwd s1[1] == tag
       users.put(s2[0], s2[1]);
       if (s1.length > 1) {
         // there is a tag here
         tags.put(s2[0], s1[1]);
       }
     }
   }
 }
    @Override
    public void handle(HttpExchange t) throws IOException {
      LOGGER.debug("file req " + t.getRequestURI());

      String path = t.getRequestURI().getPath();
      String response = null;
      String mime = null;
      int status = 200;

      if (path.contains("crossdomain.xml")) {
        response =
            "<?xml version=\"1.0\"?>"
                + "<!-- http://www.bitsontherun.com/crossdomain.xml -->"
                + "<cross-domain-policy>"
                + "<allow-access-from domain=\"*\" />"
                + "</cross-domain-policy>";
        mime = "text/xml";

      } else if (path.startsWith("/files/log/")) {
        String filename = path.substring(11);
        if (filename.equals("info")) {
          String log = PMS.get().getFrame().getLog();
          log = log.replaceAll("\n", "<br>");
          String fullLink = "<br><a href=\"/files/log/full\">Full log</a><br><br>";
          String x = fullLink + log;
          if (StringUtils.isNotEmpty(log)) {
            x = x + fullLink;
          }
          response = "<html><title>UMS LOG</title><body>" + x + "</body></html>";
        } else {
          File file = parent.getResources().getFile(filename);
          if (file != null) {
            filename = file.getName();
            HashMap<String, Object> vars = new HashMap<>();
            vars.put("title", filename);
            vars.put(
                "brush",
                filename.endsWith("debug.log")
                    ? "debug_log"
                    : filename.endsWith(".log") ? "log" : "conf");
            vars.put("log", RemoteUtil.read(file).replace("<", "&lt;"));
            response = parent.getResources().getTemplate("util/log.html").execute(vars);
          } else {
            status = 404;
          }
        }
        mime = "text/html";

      } else if (parent.getResources().write(path.substring(7), t)) {
        // The resource manager found and sent the file, all done.
        return;

      } else {
        status = 404;
      }

      if (status == 404 && response == null) {
        response = "<html><body>404 - File Not Found: " + path + "</body></html>";
        mime = "text/html";
      }

      RemoteUtil.respond(t, response, status, mime);
    }