Util { public static URI getPossiblyRelativisedUri(URI uri) { URI ret = uri; if (THIS_URI.getScheme().equalsIgnoreCase(uri.getScheme()) && THIS_URI.getAuthority().equals(uri.getAuthority())) { String uripathstring = uri.toString(); uripathstring = uripathstring.substring(THIS_SCHEME_AND_AUTHORITY_STRING.length(), uripathstring.length()); try { ret = new URI(THIS_PATH.relativize(Paths.get(uripathstring)).toString()).normalize(); } catch (URISyntaxException e) { LOG.log(Level.WARNING, "Had an issue relativising URI {0}", new Object[] { uri }); } } return ret; } private  Util(); static URI getPossiblyRelativisedUri(URI uri); static URI properlyResolve(URI uriToResolveAgainst, URI possiblyRelaitveUriToResolve); static final String THIS_SCHEME_AND_AUTHORITY_STRING; static final String THIS_PATH_STRING; static final String THIS_STRING; static final URI THIS_URI; static final Path THIS_PATH; }
@Test public void testRelativisingUsingUri() throws URISyntaxException { String[] relativeUriStrings = new String[] { "#it", "it", "it/123", "123#123", "", "../it", "../123#123", "../../../123#it" }; for (String relativeUriString : relativeUriStrings) { URI relativeUri = new URI(relativeUriString); URI r = Util.THIS_URI.resolve(relativeUri); URI result = Util.getPossiblyRelativisedUri(r); System.err.println("Relative URI: " + relativeUri); System.err.println("Long URI: " + r); System.err.println("Relativised: " + result); System.err.println(); assertEquals(relativeUri, result); } }