@Test public void commence() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); String authExceptionMessage = "EXCEPTION MESSAGE"; AuthenticationException authException = mock(AuthenticationException.class); when(messageConverter.getSupportedMediaTypes()).thenReturn(Lists.newArrayList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)); when(authException.getMessage()).thenReturn(authExceptionMessage); serviceBasicAuthenticationEntryPoint.commence(request, response, authException); verify(response).addHeader("WWW-Authenticate", "Basic realm=\"" + basicRealm +"\""); verify(response).setStatus(HttpStatus.UNAUTHORIZED.value()); verify(response).setContentType(MediaType.APPLICATION_JSON.toString()); ArgumentCaptor<ServiceResponse> serviceResponseArg = ArgumentCaptor.forClass(ServiceResponse.class); ArgumentCaptor<ServletServerHttpResponse> servletServerHttpResponseArg = ArgumentCaptor.forClass(ServletServerHttpResponse.class); verify(messageConverter).write(serviceResponseArg.capture(), Mockito.eq(MediaType.APPLICATION_JSON), servletServerHttpResponseArg.capture()); assertEquals(ServiceResponseFactory.createErrorResponse(mainMessage, authExceptionMessage), serviceResponseArg.getValue()); assertTrue(servletServerHttpResponseArg.getValue().getServletResponse() == response); }
@Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.addHeader("WWW-Authenticate", "Basic realm=\"" + basicRealm + "\""); response.setStatus(HttpStatus.UNAUTHORIZED.value()); MediaType mediaType = messageConverter.getSupportedMediaTypes().get(0); response.setContentType(mediaType.toString()); ServiceResponse serviceResponse = ServiceResponseFactory.createErrorResponse(mainMessage, authException.getMessage()); messageConverter.write(serviceResponse, mediaType, new ServletServerHttpResponse(response)); }
ServiceBasicAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.addHeader("WWW-Authenticate", "Basic realm=\"" + basicRealm + "\""); response.setStatus(HttpStatus.UNAUTHORIZED.value()); MediaType mediaType = messageConverter.getSupportedMediaTypes().get(0); response.setContentType(mediaType.toString()); ServiceResponse serviceResponse = ServiceResponseFactory.createErrorResponse(mainMessage, authException.getMessage()); messageConverter.write(serviceResponse, mediaType, new ServletServerHttpResponse(response)); } }
ServiceBasicAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.addHeader("WWW-Authenticate", "Basic realm=\"" + basicRealm + "\""); response.setStatus(HttpStatus.UNAUTHORIZED.value()); MediaType mediaType = messageConverter.getSupportedMediaTypes().get(0); response.setContentType(mediaType.toString()); ServiceResponse serviceResponse = ServiceResponseFactory.createErrorResponse(mainMessage, authException.getMessage()); messageConverter.write(serviceResponse, mediaType, new ServletServerHttpResponse(response)); }  }
ServiceBasicAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.addHeader("WWW-Authenticate", "Basic realm=\"" + basicRealm + "\""); response.setStatus(HttpStatus.UNAUTHORIZED.value()); MediaType mediaType = messageConverter.getSupportedMediaTypes().get(0); response.setContentType(mediaType.toString()); ServiceResponse serviceResponse = ServiceResponseFactory.createErrorResponse(mainMessage, authException.getMessage()); messageConverter.write(serviceResponse, mediaType, new ServletServerHttpResponse(response)); }  @Override void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException); @PostConstruct void postConstruct(); void setMessageConverter(HttpMessageConverter<ServiceResponse> messageConverter); void setBasicRealm(String basicRealm); void setMainMessage(String mainMessage); }
ServiceBasicAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.addHeader("WWW-Authenticate", "Basic realm=\"" + basicRealm + "\""); response.setStatus(HttpStatus.UNAUTHORIZED.value()); MediaType mediaType = messageConverter.getSupportedMediaTypes().get(0); response.setContentType(mediaType.toString()); ServiceResponse serviceResponse = ServiceResponseFactory.createErrorResponse(mainMessage, authException.getMessage()); messageConverter.write(serviceResponse, mediaType, new ServletServerHttpResponse(response)); }  @Override void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException); @PostConstruct void postConstruct(); void setMessageConverter(HttpMessageConverter<ServiceResponse> messageConverter); void setBasicRealm(String basicRealm); void setMainMessage(String mainMessage);  }