private URI getTargetUri(final Operation op) { // try to get it directly from the request URI // the request URI should look like ../rp/{http://target-host/target-path} URI uri = op.getUri(); URI targetUri; try { targetUri = getReverseProxyTargetUri(uri); } catch (Exception e) { getHost().log(Level.WARNING, "Exception getting target uri: %s", e.getMessage()); targetUri = null; } if (targetUri == null) { getHost().log(Level.WARNING, "Unable to get target uri!"); return null; } return targetUri; } }
@Test public void testReverseProxyInvalidTransformations() { URI uri = UriUtilsExtended.getReverseProxyUri(null); assertNull(uri); uri = UriUtilsExtended.getReverseProxyTargetUri(null); assertNull(uri); URI opUri = UriUtils.buildUri("http://localhost/rp"); uri = UriUtilsExtended.getReverseProxyTargetUri(opUri); assertNull(uri); opUri = UriUtils.buildUri("http://localhost/rp/f-o-o/bar"); uri = UriUtilsExtended.getReverseProxyTargetUri(opUri); assertNull(uri); opUri = UriUtils.buildUri("http://localhost/rp/" + UriUtilsExtended.getValueEncoded("%0")); try { uri = UriUtilsExtended.getReverseProxyTargetUri(opUri); fail("URI should be invalid!"); } catch (LocalizableValidationException e) { assertTrue(e.getMessage().startsWith("Invalid target URI:")); } }
URI uri = UriUtils.buildUri(url); URI rpUri = UriUtilsExtended.getReverseProxyUri(uri); URI targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/", targetUri.toString()); uri = UriUtils.buildUri(url); rpUri = UriUtilsExtended.getReverseProxyUri(uri); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/?q1=v1", targetUri.toString()); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/ep1/ep2/", targetUri.toString()); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/ep1/ep2/?q1=v1", targetUri.toString()); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/ep1/ep2/?q1=v1&q2=v2", targetUri.toString());
URI uri = UriUtils.buildUri(url); URI rpUri = UriUtilsExtended.getReverseProxyUri(uri); URI targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2", targetUri.toString()); uri = UriUtils.buildUri(url); rpUri = UriUtilsExtended.getReverseProxyUri(uri); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2?q1=v1", targetUri.toString()); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/ep1/ep2", targetUri.toString()); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/ep1/ep2?q1=v1", targetUri.toString()); targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertEquals("http://www.test-url.com:8080/p1/p2/ep1/ep2?q1=v1&q2=v2", targetUri.toString());
@Test public void testReverseProxyLocationTransformationsWithTrailingSlash() { String url = "http://www.test-url.com:8080/p1/p2"; URI uri = UriUtils.buildUri(url); // location: http://localhost/foo/bar -> // http://abc:80/rp/{http://localhost/foo/bar} String location = "http://localhost/foo/bar/"; String targetLocation = UriUtilsExtended.getReverseProxyLocation(location, null, null); assertNotNull(targetLocation); assertTrue(targetLocation.startsWith(ReverseProxyService.SELF_LINK)); URI targetUri = UriUtilsExtended.getReverseProxyTargetUri( UriUtils.buildUri(uri, targetLocation)); assertEquals("http://localhost/foo/bar/", targetUri.toString()); // location: /foo/bar & referer: http://localhost/p -> // http://abc:80/rp/{http://localhost/foo/bar} location = "/foo/bar/"; URI referer = UriUtils.buildUri("http://localhost/p"); targetLocation = UriUtilsExtended.getReverseProxyLocation(location, referer, null); assertNotNull(targetLocation); assertTrue(targetLocation.startsWith(ReverseProxyService.SELF_LINK)); targetUri = UriUtilsExtended.getReverseProxyTargetUri( UriUtils.buildUri(uri, targetLocation)); assertEquals("http://localhost/foo/bar/", targetUri.toString()); }
@Test public void testReverseProxyLocationTransformations() { String url = "http://www.test-url.com:8080/p1/p2"; URI uri = UriUtils.buildUri(url); // location: http://localhost/foo/bar -> // http://abc:80/rp/{http://localhost/foo/bar} String location = "http://localhost/foo/bar"; String targetLocation = UriUtilsExtended.getReverseProxyLocation(location, null, null); assertNotNull(targetLocation); assertTrue(targetLocation.startsWith(ReverseProxyService.SELF_LINK)); URI targetUri = UriUtilsExtended.getReverseProxyTargetUri( UriUtils.buildUri(uri, targetLocation)); assertEquals("http://localhost/foo/bar", targetUri.toString()); // location: /foo/bar & referer: http://localhost/p -> // http://abc:80/rp/{http://localhost/foo/bar} location = "/foo/bar"; URI referer = UriUtils.buildUri("http://localhost/p"); targetLocation = UriUtilsExtended.getReverseProxyLocation(location, referer, null); assertNotNull(targetLocation); assertTrue(targetLocation.startsWith(ReverseProxyService.SELF_LINK)); targetUri = UriUtilsExtended.getReverseProxyTargetUri( UriUtils.buildUri(uri, targetLocation)); assertEquals("http://localhost/foo/bar", targetUri.toString()); }
@Test public void testReverseProxyRequest() { assertNotNull(SAMPLE_URI); URI rpUri = UriUtilsExtended.getReverseProxyUri(SAMPLE_URI); assertNotEquals(SAMPLE_URI, rpUri); URI targetUri = UriUtilsExtended.getReverseProxyTargetUri(rpUri); assertNotEquals(rpUri, targetUri); assertEquals(SAMPLE_URI, targetUri); }
@Test public void testReverseProxyLocationTransformationsWithRelativeToParam() { String RP_RELATIVE_TO = "/relative-path"; String originalUrl = "http://www.test-url.com/path?rp-relative-to=" + RP_RELATIVE_TO; URI originalUri = UriUtils.buildUri(originalUrl); String url = "http://www.test-url.com:8080/p1/p2"; URI uri = UriUtils.buildUri(url); // location: http://localhost/foo/bar -> // http://abc:80/rp/{http://localhost/foo/bar} String location = "http://localhost/foo/bar/"; String targetLocation = UriUtilsExtended.getReverseProxyLocation(location, null, originalUri); assertNotNull(targetLocation); assertTrue(targetLocation.startsWith(RP_RELATIVE_TO + ReverseProxyService.SELF_LINK)); URI targetUri = UriUtilsExtended.getReverseProxyTargetUri( UriUtils.buildUri(uri, targetLocation)); assertEquals("http://localhost/foo/bar/", targetUri.toString()); // location: /foo/bar & referer: http://localhost/p -> // http://abc:80/rp/{http://localhost/foo/bar} location = "/foo/bar/"; URI referer = UriUtils.buildUri("http://localhost/p"); targetLocation = UriUtilsExtended.getReverseProxyLocation(location, referer, originalUri); assertNotNull(targetLocation); assertTrue(targetLocation.startsWith(RP_RELATIVE_TO + ReverseProxyService.SELF_LINK)); targetUri = UriUtilsExtended.getReverseProxyTargetUri( UriUtils.buildUri(uri, targetLocation)); assertEquals("http://localhost/foo/bar/", targetUri.toString()); }