Projects‎ > ‎

HarLib

HarLib is an open source Java library for the HTTP Archive Specification (HAR) v1.2.

HarLib v1.1.2 is now available (see download link at the bottom of this page).

Main features

  • Read/write HAR files from Java
  • Database mapping of HAR objects into any databases through JDBC
  • Open Source - Apache v2 license

Usage

The API is very intuitive and maps all JSON objects to Java objects. HarLib relies on Jackson for the parsing. You can use the HarLib version that is already integrated with Jackson or add the Jackson jar to your classpath.
Here is an example on how to read/write HAR files with HarLib.

import edu.umass.cs.benchlab.har.*
...
    File f = new File(fileName);

    HarFileReader r = new HarFileReader();
    HarFileWriter w = new HarFileWriter();
    try
    {
      System.out.println("Reading " + fileName);
      HarLog log = r.readHarFile(f);

      // Access all elements as objects
      HarBrowser browser = log.getBrowser();
      HarEntries entries = log.getEntries();
      List<HarPage> pages = log.getPages().getPages();
      for (HarPage page : pages)
      {
        System.out.println("page start time: "
            + ISO8601DateFormatter.format(page.getStartedDateTime()));
        System.out.println("page id: " + page.getId());
        System.out.println("page title: "+page.getTitle());
      }

               // Once you are done manipulating the objects, write back to a file
      System.out.println("Writing " + fileName + ".test");
      File f2 = new File(fileName + ".test");
      w.writeHarFile(log, f2);
    }
    catch (JsonParseException e)
    {
      e.printStackTrace();
      fail("Parsing error during test");
    }
    catch (IOException e)
    {
      e.printStackTrace();
      fail("IO exception during test");
    }

Here is another example using a Derby database for storing HAR data.

    // Read a HAR file
    File f = new File("test.har");

    HarFileReader r = new HarFileReader();
    HarLog log = r.readHarFile(f);

    // Define your JDBC data source and write to the database
    HarDatabaseConfig config = new HarDatabaseConfig(
       "org.apache.derby.jdbc.EmbeddedDriver",
       "jdbc:derby:harlibtest;create=true", "SA", "", "writetest", null,
       null, null, null, null);
    log.writeJDBC(config);


New support for corrupted HAR files that have missing elements or other violations to the specification in version 1.1. This stills allows you to read most of a file even if some parts are corrupted.

// Read a potentially corrupted HAR file
File f = new File(fileName);

HarFileReader r = new HarFileReader();
try
{
  // All violations of the specification generate warnings
  List<HarWarning> warnings = new ArrayList<HarWarning>();

  HarLog l = r.readHarFile(f, warnings);
  for (HarWarning w : warnings)
    System.out.println("File:" + fileName + " - Warning:" + w);
}
catch (JsonParseException e)
{
  e.printStackTrace();
  fail("Parsing error during test");
}
catch (IOException e)
{
  e.printStackTrace();
  fail("IO exception during test");
}

Download


HarLib can be downloaded from the BenchLab project repository at https://sourceforge.net/projects/benchlab/files/HarLib/

You will find the following files (where xxx is the version number):
  • harlib-xxx.jar: Binary distribution with Javadoc not including the Jackson parser
  • harlib-src-xxx.jar: Source distribution (no test suite)
  • harlib-nodoc-xxx.jar: Binary distribution without Javadoc or Jackson parser
  • harlib-jackson-xxx.jar: Binary distribution with Javadoc AND Jackson parser (core)
  • harlib-all-xxx.zip: Complete zip of the project with Eclipse settings and test suite.

Comments