@Test public void testRequestFilter() throws IOException { ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class); Properties properties = new Properties(); properties.setProperty("origins", ALLOWED_FOREIGN_ORIGIN + "|" + ALLOWED_FOREIGN_ORIGIN_2); when(configurationManager.getConfigurationWithDefaults("cors.config")).thenReturn(properties); CORSFilter filter = new CORSFilter(); filter.configurationManager = configurationManager; ContainerRequestContext requestContext = Mockito.mock(ContainerRequestContext.class); Mockito.when(requestContext.getHeaderString(Headers.ORIGIN)).thenReturn(null); filter.filter(requestContext); Mockito.verify(requestContext, Mockito.never()).getMethod(); Mockito.when(requestContext.getHeaderString(Headers.ORIGIN)).thenReturn(ALLOWED_FOREIGN_ORIGIN); Mockito.when(requestContext.getMethod()).thenReturn("GET"); filter.filter(requestContext); Mockito.when(requestContext.getHeaderString(Headers.ORIGIN)).thenReturn(ALLOWED_FOREIGN_ORIGIN_2); Mockito.when(requestContext.getMethod()).thenReturn("GET"); try { filter.filter(requestContext); } catch (WebApplicationException e) { fail("Allowed origin from config was not actually allowed"); } Mockito.when(requestContext.getHeaderString(Headers.ORIGIN)).thenReturn(ALLOWED_LOCALHOST_ORIGIN); Mockito.when(requestContext.getMethod()).thenReturn("GET"); try { filter.filter(requestContext); } catch (WebApplicationException e) { fail("Allowed localhost origin was not actually allowed"); } Mockito.when(requestContext.getHeaderString(Headers.ORIGIN)).thenReturn("bad.website.com"); Mockito.when(requestContext.getMethod()).thenReturn("GET"); WebApplicationException assertThrows = assertThrows(WebApplicationException.class, () -> filter.filter(requestContext)); assertEquals(403, assertThrows.getResponse().getStatus()); assertEquals("bad.website.com not allowed", assertThrows.getMessage()); Mockito.verify(requestContext).setProperty("access.denied", true); Mockito.when(requestContext.getHeaderString(Headers.ORIGIN)).thenReturn(ALLOWED_FOREIGN_ORIGIN); Mockito.when(requestContext.getMethod()).thenReturn("Options"); filter.filter(requestContext); Mockito.verify(requestContext).abortWith(Mockito.any()); }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { String origin = getOrigin(requestContext); if (origin == null || requestContext.getMethod().equalsIgnoreCase(OPTIONS) || requestContext.getProperty(ACCESS_DENIED) != null) { return; } responseContext.getHeaders().putSingle(Headers.ACCESS_CONTROL_ALLOW_ORIGIN, origin); responseContext.getHeaders().putSingle(Headers.VARY, Headers.ORIGIN); }
CORSFilter implements ContainerResponseFilter, ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { String origin = getOrigin(requestContext); if (origin == null || requestContext.getMethod().equalsIgnoreCase(OPTIONS) || requestContext.getProperty(ACCESS_DENIED) != null) { return; } responseContext.getHeaders().putSingle(Headers.ACCESS_CONTROL_ALLOW_ORIGIN, origin); responseContext.getHeaders().putSingle(Headers.VARY, Headers.ORIGIN); } }
CORSFilter implements ContainerResponseFilter, ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { String origin = getOrigin(requestContext); if (origin == null || requestContext.getMethod().equalsIgnoreCase(OPTIONS) || requestContext.getProperty(ACCESS_DENIED) != null) { return; } responseContext.getHeaders().putSingle(Headers.ACCESS_CONTROL_ALLOW_ORIGIN, origin); responseContext.getHeaders().putSingle(Headers.VARY, Headers.ORIGIN); }  }
CORSFilter implements ContainerResponseFilter, ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { String origin = getOrigin(requestContext); if (origin == null || requestContext.getMethod().equalsIgnoreCase(OPTIONS) || requestContext.getProperty(ACCESS_DENIED) != null) { return; } responseContext.getHeaders().putSingle(Headers.ACCESS_CONTROL_ALLOW_ORIGIN, origin); responseContext.getHeaders().putSingle(Headers.VARY, Headers.ORIGIN); }  @Override void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext); @Override void filter(ContainerRequestContext requestContext); List<String> getAllowedOrigins(); }
CORSFilter implements ContainerResponseFilter, ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { String origin = getOrigin(requestContext); if (origin == null || requestContext.getMethod().equalsIgnoreCase(OPTIONS) || requestContext.getProperty(ACCESS_DENIED) != null) { return; } responseContext.getHeaders().putSingle(Headers.ACCESS_CONTROL_ALLOW_ORIGIN, origin); responseContext.getHeaders().putSingle(Headers.VARY, Headers.ORIGIN); }  @Override void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext); @Override void filter(ContainerRequestContext requestContext); List<String> getAllowedOrigins(); @Reference
	public ConfigurationManager configurationManager; }