@Test public void filter() { Response response = Response.ok().build(); when(containerResponse.getResponse()).thenReturn(response); when(containerResponse.getStatus()).thenReturn(Status.OK.getStatusCode()); when(containerResponse.getHttpHeaders()).thenReturn(new OutBoundHeaders()); when(containerRequest.getMethod()).thenReturn("OPTIONS"); when(containerRequest.getHeaderValue("Access-Control-Request-Headers")).thenReturn("foo"); ContainerResponse returnedContainerResponse = filter.filter(containerRequest, containerResponse); assertNotNull(returnedContainerResponse); assertEquals(Status.OK.getStatusCode(), returnedContainerResponse.getStatus()); response = Response.status(Status.NOT_FOUND.getStatusCode()).build(); when(containerResponse.getStatus()).thenReturn(Status.NOT_FOUND.getStatusCode()); returnedContainerResponse = filter.filter(containerRequest, containerResponse); assertEquals(Status.NOT_FOUND.getStatusCode(), returnedContainerResponse.getStatus()); }
@Override public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) { LOGGER.trace("CORS filter called for request: {}", containerRequest); Response.ResponseBuilder response = Response.fromResponse(containerResponse.getResponse()); if ("OPTIONS".equals(containerRequest.getMethod())) { response.status(Status.NO_CONTENT); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_ORIGIN))) response.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_METHODS))) response.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_REQUEST_METHOD))) response.header(ACCESS_CONTROL_REQUEST_METHOD, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_MAX_AGE))) response.header(ACCESS_CONTROL_MAX_AGE, deltaSeconds); if (Objects.isNull(containerResponse.getHttpHeaders().get(CONTENT_TYPE))) response.header(CONTENT_TYPE, APPLICATION_JSON); if (Objects.isNull(containerResponse.getHttpHeaders().get(X_APPLICATION_ID))) response.header(X_APPLICATION_ID, applicationName); response.entity(null); String requestHeader = containerRequest.getHeaderValue(ACCESS_CONTROL_REQUEST_HEADERS); if (requestHeader != null) { response.header(ACCESS_CONTROL_ALLOW_HEADERS, requestHeader); } } containerResponse.setResponse(response.build()); return containerResponse; }
SimpleCORSResponseFilter implements ContainerResponseFilter { @Override public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) { LOGGER.trace("CORS filter called for request: {}", containerRequest); Response.ResponseBuilder response = Response.fromResponse(containerResponse.getResponse()); if ("OPTIONS".equals(containerRequest.getMethod())) { response.status(Status.NO_CONTENT); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_ORIGIN))) response.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_METHODS))) response.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_REQUEST_METHOD))) response.header(ACCESS_CONTROL_REQUEST_METHOD, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_MAX_AGE))) response.header(ACCESS_CONTROL_MAX_AGE, deltaSeconds); if (Objects.isNull(containerResponse.getHttpHeaders().get(CONTENT_TYPE))) response.header(CONTENT_TYPE, APPLICATION_JSON); if (Objects.isNull(containerResponse.getHttpHeaders().get(X_APPLICATION_ID))) response.header(X_APPLICATION_ID, applicationName); response.entity(null); String requestHeader = containerRequest.getHeaderValue(ACCESS_CONTROL_REQUEST_HEADERS); if (requestHeader != null) { response.header(ACCESS_CONTROL_ALLOW_HEADERS, requestHeader); } } containerResponse.setResponse(response.build()); return containerResponse; } }
SimpleCORSResponseFilter implements ContainerResponseFilter { @Override public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) { LOGGER.trace("CORS filter called for request: {}", containerRequest); Response.ResponseBuilder response = Response.fromResponse(containerResponse.getResponse()); if ("OPTIONS".equals(containerRequest.getMethod())) { response.status(Status.NO_CONTENT); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_ORIGIN))) response.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_METHODS))) response.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_REQUEST_METHOD))) response.header(ACCESS_CONTROL_REQUEST_METHOD, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_MAX_AGE))) response.header(ACCESS_CONTROL_MAX_AGE, deltaSeconds); if (Objects.isNull(containerResponse.getHttpHeaders().get(CONTENT_TYPE))) response.header(CONTENT_TYPE, APPLICATION_JSON); if (Objects.isNull(containerResponse.getHttpHeaders().get(X_APPLICATION_ID))) response.header(X_APPLICATION_ID, applicationName); response.entity(null); String requestHeader = containerRequest.getHeaderValue(ACCESS_CONTROL_REQUEST_HEADERS); if (requestHeader != null) { response.header(ACCESS_CONTROL_ALLOW_HEADERS, requestHeader); } } containerResponse.setResponse(response.build()); return containerResponse; } @Inject  SimpleCORSResponseFilter(final @Named(APPLICATION_ID) String applicationName,
                                    final @Named(ACCESS_CONTROL_MAX_AGE_DELTA_SECONDS) String deltaSeconds); }
SimpleCORSResponseFilter implements ContainerResponseFilter { @Override public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) { LOGGER.trace("CORS filter called for request: {}", containerRequest); Response.ResponseBuilder response = Response.fromResponse(containerResponse.getResponse()); if ("OPTIONS".equals(containerRequest.getMethod())) { response.status(Status.NO_CONTENT); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_ORIGIN))) response.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_METHODS))) response.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_REQUEST_METHOD))) response.header(ACCESS_CONTROL_REQUEST_METHOD, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_MAX_AGE))) response.header(ACCESS_CONTROL_MAX_AGE, deltaSeconds); if (Objects.isNull(containerResponse.getHttpHeaders().get(CONTENT_TYPE))) response.header(CONTENT_TYPE, APPLICATION_JSON); if (Objects.isNull(containerResponse.getHttpHeaders().get(X_APPLICATION_ID))) response.header(X_APPLICATION_ID, applicationName); response.entity(null); String requestHeader = containerRequest.getHeaderValue(ACCESS_CONTROL_REQUEST_HEADERS); if (requestHeader != null) { response.header(ACCESS_CONTROL_ALLOW_HEADERS, requestHeader); } } containerResponse.setResponse(response.build()); return containerResponse; } @Inject  SimpleCORSResponseFilter(final @Named(APPLICATION_ID) String applicationName,
                                    final @Named(ACCESS_CONTROL_MAX_AGE_DELTA_SECONDS) String deltaSeconds); @Override ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse); }
SimpleCORSResponseFilter implements ContainerResponseFilter { @Override public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) { LOGGER.trace("CORS filter called for request: {}", containerRequest); Response.ResponseBuilder response = Response.fromResponse(containerResponse.getResponse()); if ("OPTIONS".equals(containerRequest.getMethod())) { response.status(Status.NO_CONTENT); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_ORIGIN))) response.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_METHODS))) response.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_REQUEST_METHOD))) response.header(ACCESS_CONTROL_REQUEST_METHOD, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_MAX_AGE))) response.header(ACCESS_CONTROL_MAX_AGE, deltaSeconds); if (Objects.isNull(containerResponse.getHttpHeaders().get(CONTENT_TYPE))) response.header(CONTENT_TYPE, APPLICATION_JSON); if (Objects.isNull(containerResponse.getHttpHeaders().get(X_APPLICATION_ID))) response.header(X_APPLICATION_ID, applicationName); response.entity(null); String requestHeader = containerRequest.getHeaderValue(ACCESS_CONTROL_REQUEST_HEADERS); if (requestHeader != null) { response.header(ACCESS_CONTROL_ALLOW_HEADERS, requestHeader); } } containerResponse.setResponse(response.build()); return containerResponse; } @Inject  SimpleCORSResponseFilter(final @Named(APPLICATION_ID) String applicationName,
                                    final @Named(ACCESS_CONTROL_MAX_AGE_DELTA_SECONDS) String deltaSeconds); @Override ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse);  }