public static String addUserHomeDirectoryIfApplicable(String origPathStr, String user)
    throws IOException, URISyntaxException {
    if(origPathStr == null || origPathStr.isEmpty()) {
      return "/user/" + user;
    }
    Path p = new Path(origPathStr);
    if(p.isAbsolute()) {
      return origPathStr;
    }
    if(p.toUri().getPath().isEmpty()) {
      //origPathStr="hdfs://host:99" for example
      return new Path(p.toUri().getScheme(), p.toUri().getAuthority(), "/user/" + user).toString();
    }
    //can't have relative path if there is scheme/authority
    return "/user/" + user + "/" + origPathStr;
  }
@Test
  public void testConstructingUserHomeDirectory() throws Exception {
    String[] sources = new String[] { "output+", "/user/hadoop/output",
      "hdfs://container", "hdfs://container/", "hdfs://container/path",
      "output#link", "hdfs://cointaner/output#link",
      "hdfs://container@acc/test", "/user/webhcat/düsseldorf", "düsseldorf", 
      "䶴狝A﨩O", "hdfs://host:8080"};
    String[] expectedResults = new String[] { "/user/webhcat/output+",
      "/user/hadoop/output", "hdfs://container/user/webhcat",
      "hdfs://container/", "hdfs://container/path",
      "/user/webhcat/output#link", "hdfs://cointaner/output#link",
      "hdfs://container@acc/test", "/user/webhcat/düsseldorf","/user/webhcat/düsseldorf",
      "/user/webhcat/䶴狝A﨩O", "hdfs://host:8080/user/webhcat" };
    for (int i = 0; i < sources.length; i++) {
      String source = sources[i];
      String expectedResult = expectedResults[i];
      String result = TempletonUtils.addUserHomeDirectoryIfApplicable(source,
          "webhcat");
      Assert.assertEquals("i=" + i, expectedResult, result);
    }

    String badUri = "c:\\some\\path";
    try {
      TempletonUtils.addUserHomeDirectoryIfApplicable(badUri, "webhcat");
      Assert.fail("addUserHomeDirectoryIfApplicable should fail for bad URI: "
          + badUri);
    } catch (IllegalArgumentException ex) {
    }
  }