ProxyFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext ctx) throws IOException { String host = getValue(ctx.getHeaders(), HOST_PROXY_HEADER); String scheme = getValue(ctx.getHeaders(), PROTO_PROXY_HEADER); if (scheme == null && host == null) { return; } UriInfo uriInfo = ctx.getUriInfo(); URI requestUri = uriInfo.getRequestUri(); UriBuilder baseBuilder = uriInfo.getBaseUriBuilder(); UriBuilder requestBuilder = uriInfo.getRequestUriBuilder(); if (scheme == null) { scheme = requestUri.getScheme(); } if (host == null) { host = requestUri.getHost(); int port = requestUri.getPort(); if (port != -1) { host += (":" + port); } } String uriString = scheme + ": URI newBaseUri = null; try { newBaseUri = new URI(uriString); } catch (URISyntaxException e) { logger.error("Invalid X-Forwarded-Proto + X-Forwarded-Host header combination: {}", uriString, e); return; } host = newBaseUri.getHost(); if (host != null) { baseBuilder.host(host); requestBuilder.host(host); } int port = newBaseUri.getPort(); baseBuilder.port(port); requestBuilder.port(port); scheme = newBaseUri.getScheme(); if (scheme != null) { baseBuilder.scheme(scheme); requestBuilder.scheme(scheme); } ctx.setRequestUri(baseBuilder.build(), requestBuilder.build()); }  @Override void filter(ContainerRequestContext ctx);  }
@Test public void basicTest() throws Exception { String baseURI = "http: String requestURI = "http: setupContextURIs(baseURI, requestURI); setupContextHeaders("https", "eclipse.org"); filter.filter(context); URI newBaseURI = new URI("https: URI newRequestURI = new URI("https: verify(context).setRequestUri(eq(newBaseURI), eq(newRequestURI)); }
@Test public void basicTest2() throws Exception { String baseURI = "http: String requestURI = "http: setupContextURIs(baseURI, requestURI); setupContextHeaders("http", "eclipse.org:8081"); filter.filter(context); URI newBaseURI = new URI("http: URI newRequestURI = new URI("http: verify(context).setRequestUri(eq(newBaseURI), eq(newRequestURI)); }
@Test public void noHeaderTest() throws Exception { String baseURI = "http: String requestURI = "http: setupContextURIs(baseURI, requestURI); setupContextHeaders(null, null); filter.filter(context); verify(context, never()).setRequestUri(any(URI.class), any(URI.class)); }
@Test public void onlySchemeTest() throws Exception { String baseURI = "http: String requestURI = "http: setupContextURIs(baseURI, requestURI); setupContextHeaders("https", null); filter.filter(context); URI newBaseURI = new URI("https: URI newRequestURI = new URI("https: verify(context).setRequestUri(eq(newBaseURI), eq(newRequestURI)); }
@Test public void onlySchemeDefaultHostWithoutPortTest() throws Exception { String baseURI = "http: String requestURI = "http: setupContextURIs(baseURI, requestURI); setupContextHeaders("https", null); filter.filter(context); URI newBaseURI = new URI("https: URI newRequestURI = new URI("https: verify(context).setRequestUri(eq(newBaseURI), eq(newRequestURI)); }
@Test public void onlyHostTest() throws Exception { String baseURI = "http: String requestURI = "http: setupContextURIs(baseURI, requestURI); setupContextHeaders(null, "eclipse.org:8081"); filter.filter(context); URI newBaseURI = new URI("http: URI newRequestURI = new URI("http: verify(context).setRequestUri(eq(newBaseURI), eq(newRequestURI)); }
@Test public void invalidHeaderTest() throws Exception { String baseURI = "http: String requestURI = "http: setupContextURIs(baseURI, requestURI); setupContextHeaders("https", ": filter.filter(context); verify(context, never()).setRequestUri(any(URI.class), any(URI.class)); }