RequestUtil { public static URI getForwardedHost( final ContainerRequestContext request) { MultivaluedMap<String, String> headers = request.getHeaders(); String rawForwardedHost = headers.getFirst(HttpHeaders.X_FORWARDED_HOST); String rawForwardedPort = headers.getFirst(HttpHeaders.X_FORWARDED_PORT); String rawForwardedProto = headers.getFirst(HttpHeaders.X_FORWARDED_PROTO); try { Integer forwardedPort = rawForwardedPort == null ? -1 : Integer.valueOf(rawForwardedPort, 10); URI result = new URIBuilder() .setHost(rawForwardedHost) .setPort(forwardedPort) .setScheme(rawForwardedProto) .build(); return Strings.isNullOrEmpty(result.toString()) ? null : result; } catch (URISyntaxException | NumberFormatException use) { logger.debug("Cannot parse forwarded header.", use); return null; } } private  RequestUtil(); static String getCORSRequestedMethod(
            final ContainerRequestContext request); static List<String> getCORSRequestedHeaders(
            final ContainerRequestContext request); static URI getOrigin(final ContainerRequestContext request); static URI getReferer(final ContainerRequestContext request); static URI getHost(final ContainerRequestContext request); static URI getForwardedHost(
            final ContainerRequestContext request); static Boolean isCrossOriginRequest(
            final ContainerRequestContext request);  }
@Test public void testForwardedHost() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.X_FORWARDED_PROTO, "https"); headers.add(HttpHeaders.X_FORWARDED_PORT, "8080"); headers.add(HttpHeaders.X_FORWARDED_HOST, "example.com"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getForwardedHost(mockContext); assertEquals("https: result.toString()); }
@Test public void testForwardedHostNoPort() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.X_FORWARDED_PROTO, "https"); headers.add(HttpHeaders.X_FORWARDED_HOST, "example.com"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getForwardedHost(mockContext); assertEquals("https: result.toString()); }
@Test public void testForwardedHostNoHost() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.X_FORWARDED_PROTO, "https"); headers.add(HttpHeaders.X_FORWARDED_PORT, "8080"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getForwardedHost(mockContext); assertNull(result); }
@Test public void testForwardedHostInvalidHost() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.X_FORWARDED_PROTO, "https"); headers.add(HttpHeaders.X_FORWARDED_PORT, "8080"); headers.add(HttpHeaders.X_FORWARDED_HOST, " "); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getForwardedHost(mockContext); assertNull(result); }
@Test public void testForwardedHostInvalidPort() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.X_FORWARDED_PROTO, "https"); headers.add(HttpHeaders.X_FORWARDED_PORT, "string"); headers.add(HttpHeaders.X_FORWARDED_HOST, "example.com"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getForwardedHost(mockContext); assertNull(result); }
@Test public void testForwardedHostNoProtocol() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.X_FORWARDED_PORT, "string"); headers.add(HttpHeaders.X_FORWARDED_HOST, "example.com"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getForwardedHost(mockContext); assertNull(result); }
@Test public void testForwardedHostInvalidProtocol() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.X_FORWARDED_PROTO, "&&$$"); headers.add(HttpHeaders.X_FORWARDED_PORT, "string"); headers.add(HttpHeaders.X_FORWARDED_HOST, "example.com"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getForwardedHost(mockContext); assertNull(result); }