RequestUtil { public static URI getHost(final ContainerRequestContext request) { MultivaluedMap<String, String> headers = request.getHeaders(); String rawHost = headers.getFirst(HttpHeaders.HOST); try { String[] parts = rawHost.split(":"); URIBuilder builder = new URIBuilder(); builder.setScheme(request.getUriInfo().getRequestUri().getScheme()); builder.setHost(parts[0]); if (parts.length > 1) { builder.setPort(Integer.valueOf(parts[1])); } return builder.build(); } catch (URISyntaxException | NumberFormatException | NullPointerException e) { throw new InvalidHostException(e); } } 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 testGetHost() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.HOST, "host.example.com"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getHost(mockContext); assertEquals("https: result.toString()); }
@Test public void testGetHostPort() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.HOST, "host.example.com:8080"); ContainerRequestContext mockContext = buildContext( "https: headers ); URI result = RequestUtil.getHost(mockContext); assertEquals("https: result.toString()); }
@Test(expected = InvalidHostException.class) public void testGetHostInvalid() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.HOST, "host.example.com:string"); ContainerRequestContext mockContext = buildContext( "https: headers ); RequestUtil.getHost(mockContext); }
@Test(expected = InvalidHostException.class) public void testGetHostEmpty() throws Exception { MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add(HttpHeaders.HOST, ""); ContainerRequestContext mockContext = buildContext( "https: headers ); RequestUtil.getHost(mockContext); }