Esempio n. 1
0
 public byte[] getBytes() {
   try {
     return FileUtil.readBytes(file);
   } catch (IOException ioex) {
     throw new HttpException(ioex);
   }
 }
Esempio n. 2
0
 /**
  * Adds additional file or path to classpath during runtime.
  *
  * @see #addUrlToClassPath(java.net.URL, ClassLoader)
  */
 public static void addFileToClassPath(File path, ClassLoader classLoader) {
   try {
     addUrlToClassPath(FileUtil.toURL(path), classLoader);
   } catch (MalformedURLException muex) {
     throw new IllegalArgumentException("Invalid path: " + path, muex);
   }
 }
Esempio n. 3
0
  @Test
  public void testTwoHtml() throws IOException {
    File file = new File(testDataRoot, "two.html");
    String htmlContent = FileUtil.readString(file);

    Document document = new LagartoDOMBuilder().parse(htmlContent);

    Node html = new NodeSelector(document).select("html").get(0);
    assertNotNull(html);

    Node body = new NodeSelector(html).selectFirst("body");
    Element h1 = body.getFirstChildElement();
    assertEquals("h1", h1.getNodeName());

    Node comment1 = body.getFirstChild().getNextSibling();
    assertEquals(Node.NodeType.COMMENT, comment1.getNodeType());

    Element p = (Element) new NodeSelector(body).selectFirst("p");

    assertEquals(h1, p.getPreviousSiblingElement());
    assertEquals(h1, comment1.getNextSiblingElement());
    assertNull(comment1.getNextSiblingName());

    // check if filter works just for sub elements
    List<Node> p_ems = new NodeSelector(p).select("em");
    assertEquals(1, p_ems.size());

    Element script = (Element) new NodeSelector(html).selectFirst("script");
    assertEquals("text/javascript", script.getAttribute("type"));

    assertTrue(document.check());
  }
Esempio n. 4
0
  public void start() throws Exception {
    webRoot = FileUtil.createTempDirectory("jodd-http", "test");
    webRoot.deleteOnExit();

    // web-inf

    File webInfFolder = new File(webRoot, "WEB-INF");
    webInfFolder.mkdir();

    // web.xml

    URL webXmlUrl = TestServer.class.getResource("web.xml");
    File webXmlFile = FileUtil.toFile(webXmlUrl);

    FileUtil.copy(webXmlFile, webInfFolder);

    // lib folder

    File libFolder = new File(webInfFolder, "lib");
    libFolder.mkdir();

    // classes

    File classes = new File(webInfFolder, "classes/jodd/http");
    classes.mkdirs();

    URL echoServletUrl = TestServer.class.getResource("EchoServlet.class");
    File echoServletFile = FileUtil.toFile(echoServletUrl);
    FileUtil.copyFileToDir(echoServletFile, classes);

    echoServletUrl = TestServer.class.getResource("Echo2Servlet.class");
    echoServletFile = FileUtil.toFile(echoServletUrl);
    FileUtil.copyFileToDir(echoServletFile, classes);
  }
Esempio n. 5
0
  private void _testHtmls(String root) throws IOException {
    FindFile ff = new WildcardFindFile().include("**/*.*ml");
    long reps = 1;
    JStopWatch jsw = new JStopWatch();
    boolean processed = false;
    while (reps-- > 0) {
      ff.searchPath(root);
      File file;
      while ((file = ff.nextFile()) != null) {
        processed = true;
        System.out.println('+' + file.getName());

        String content = FileUtil.readString(file);
        String expectedResult = FileUtil.readString(new File(file.getAbsolutePath() + ".txt"));

        String formatted = null;
        File formattedFile = new File(file.getAbsolutePath() + "-fmt.htm");
        if (formattedFile.exists()) {
          formatted = FileUtil.readString(formattedFile);
        }

        boolean isXml = file.getName().endsWith(".xml");

        String[] results = _parse(content, isXml);
        String result = results[0]; // parsing result
        String result2 = results[1]; // tag writer

        expectedResult = StringUtil.removeChars(expectedResult, '\r');
        result = StringUtil.removeChars(result, '\r').trim();

        assertEquals(expectedResult, result);

        if (formatted != null) {
          assertEquals(formatted, result2);
        } else {
          assertEquals(content, result2);
        }
      }
    }
    assertTrue(processed);
    System.out.println(jsw);
  }
Esempio n. 6
0
 /** @see #findClass(String, java.net.URL[], ClassLoader) */
 public static Class findClass(String className, File[] classPath, ClassLoader parent) {
   URL[] urls = new URL[classPath.length];
   for (int i = 0; i < classPath.length; i++) {
     File file = classPath[i];
     try {
       urls[i] = FileUtil.toURL(file);
     } catch (MalformedURLException ignore) {
     }
   }
   return findClass(className, urls, parent);
 }
Esempio n. 7
0
  @Test
  public void testGroupOfSelectors() throws IOException {
    File file = new File(testDataRoot, "one.html");
    String htmlContent = FileUtil.readString(file);

    Document document = new LagartoDOMBuilder().parse(htmlContent);

    List<Node> nodes = new NodeSelector(document).select("em, b, b");
    assertEquals(9, nodes.size());

    assertTrue(document.check());
  }
Esempio n. 8
0
  @Test
  public void testFileUpload() throws IOException {
    HttpRequest request = HttpRequest.get("http://jodd.org/?id=173");

    request.header("User-Agent", "Scaly").form("one", "funny");

    File tempFile = FileUtil.createTempFile();
    tempFile.deleteOnExit();
    FileUtil.writeString(tempFile, "qwerty");
    request.form("two", tempFile);

    byte[] bytes = request.toByteArray();

    // read
    HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes));
    Map<String, Object[]> httpParams2 = request2.form();

    assertEquals(request.method(), request2.method());
    assertEquals(request.path(), request2.path());
    assertEquals(request.queryString(), request2.queryString());

    assertEquals(request.header("User-Agent"), request2.header("User-Agent"));
    assertEquals(request.header("Content-Type"), request2.header("content-type"));
    assertEquals(request.header("Content-Length"), request2.header("content-length"));

    Map params1 = request.form();
    Map params2 = request2.form();
    assertEquals(params1.size(), params2.size());
    assertEquals(params2.get("one"), params2.get("one"));

    FileUpload fu = (FileUpload) httpParams2.get("two")[0];
    assertEquals(6, fu.getSize());

    String str = new String(fu.getFileContent());
    assertEquals("qwerty", str);

    tempFile.delete();
  }
Esempio n. 9
0
  /**
   * Returns default class path from all available <code>URLClassLoader</code> in classloader
   * hierarchy. The following is added to the classpath list:
   *
   * <ul>
   *   <li>file URLs from <code>URLClassLoader</code> (other URL protocols are ignored)
   *   <li>inner entries from containing <b>manifest</b> files (if exist)
   *   <li>bootstrap classpath
   * </ul>
   */
  public static File[] getDefaultClasspath(ClassLoader classLoader) {
    Set<File> classpaths = new TreeSet<>();

    while (classLoader != null) {
      if (classLoader instanceof URLClassLoader) {
        URL[] urls = ((URLClassLoader) classLoader).getURLs();
        for (URL u : urls) {
          File f = FileUtil.toFile(u);
          if ((f != null) && f.exists()) {
            try {
              f = f.getCanonicalFile();

              boolean newElement = classpaths.add(f);
              if (newElement) {
                addInnerClasspathItems(classpaths, f);
              }
            } catch (IOException ignore) {
            }
          }
        }
      }
      classLoader = classLoader.getParent();
    }

    String bootstrap = SystemUtil.getSunBootClassPath();
    if (bootstrap != null) {
      String[] bootstrapFiles = StringUtil.splitc(bootstrap, File.pathSeparatorChar);
      for (String bootstrapFile : bootstrapFiles) {
        File f = new File(bootstrapFile);
        if (f.exists()) {
          try {
            f = f.getCanonicalFile();

            boolean newElement = classpaths.add(f);
            if (newElement) {
              addInnerClasspathItems(classpaths, f);
            }
          } catch (IOException ignore) {
          }
        }
      }
    }

    File[] result = new File[classpaths.size()];
    return classpaths.toArray(result);
  }
Esempio n. 10
0
 /** 13s */
 @Test
 public void testLiveHtmls() throws IOException {
   FindFile ff = new WildcardFindFile().include("**/*.html");
   ff.searchPath(testLiveRoot);
   File file;
   boolean processed = false;
   while ((file = ff.nextFile()) != null) {
     processed = true;
     String name = file.getName();
     System.out.println('+' + name);
     String content = FileUtil.readString(file);
     try {
       _parseEmpty(content);
     } catch (Exception ex) {
       ex.printStackTrace();
       fail(ex.toString());
     }
   }
   assertTrue(processed);
 }
Esempio n. 11
0
  protected void prepareWebApplication() throws Exception {
    webRoot = FileUtil.createTempDirectory("jodd-madvoc", "test-int");
    webRoot.deleteOnExit();

    // web-inf

    File webInfFolder = new File(webRoot, "WEB-INF");
    webInfFolder.mkdir();

    // web.xml

    URL webXmlUrl = TestServer.class.getResource("web-test-int.xml");
    File webXmlFile = FileUtil.toFile(webXmlUrl);

    FileUtil.copyFile(webXmlFile, new File(webInfFolder, "web.xml"));

    // jsp

    File jspFolder = new File(webXmlFile.getParent(), "jsp");
    FileUtil.copyDir(jspFolder, webRoot);

    // lib folder

    File libFolder = new File(webInfFolder, "lib");
    libFolder.mkdir();

    // classes

    File classes = new File(webInfFolder, "classes");
    classes.mkdirs();

    // classes/madvoc.props

    URL madvocPropsUrl = TestServer.class.getResource("madvoc.props");
    File madvocPropsFile = FileUtil.toFile(madvocPropsUrl);

    FileUtil.copyFileToDir(madvocPropsFile, classes);
  }
Esempio n. 12
0
 public void stop() throws Exception {
   tomcat.stop();
   tomcat.destroy();
   FileUtil.deleteDir(webRoot);
 }
Esempio n. 13
0
 private NodeSelector createNodeFilter() throws IOException {
   File file = new File(testDataRoot, "one.html");
   String html = FileUtil.readString(file);
   return new NodeSelector(new LagartoDOMBuilder().parse(html));
 }