use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.
the class MotechLoginErrorHandlerTest method shouldReturnJSON.
@Test
public void shouldReturnJSON() throws ServletException, IOException {
AuthenticationException exception = new BadCredentialsException("Wrong Password");
exception.setAuthentication(authentication);
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.addHeader("x-requested-with", "XMLHttpRequest");
MockHttpServletResponse mockResponse = new MockHttpServletResponse();
motechLoginErrorHandler.onAuthenticationFailure(mockRequest, mockResponse, exception);
MotechJsonMessage messageObject = new MotechJsonMessage("security.wrongPassword");
assertEquals(messageObject.toJson(), mockResponse.getContentAsString());
}
use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.
the class MotechLoginUrlAuthenticationEntryPoint method commence.
/**
* Performs the redirect (or forward) to the login form URL,
* unless the request is an Ajax request, where a 401 response will be returned.
*/
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
if (HttpRequestEnvironment.isAjax(request)) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setHeader("Content-Type", "application/json");
MotechJsonMessage message = new MotechJsonMessage("ERROR");
response.getWriter().write(message.toJson());
} else {
super.commence(request, response, authException);
}
}
use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.
the class MotechLoginErrorJSONHandler method onAuthenticationFailure.
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
if (HttpRequestEnvironment.isAjax(request)) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setHeader("Content-Type", "application/json");
String message = getMessageForException(exception);
MotechJsonMessage messageObject = new MotechJsonMessage(message);
response.getWriter().write(messageObject.toJson());
} else {
super.onAuthenticationFailure(request, response, exception);
}
}
use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.
the class MotechLoginSuccessJSONHandler method onAuthenticationSuccess.
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
super.onAuthenticationSuccess(request, response, authentication);
if (HttpRequestEnvironment.isAjax(request)) {
response.setHeader("Content-Type", "application/json");
MotechJsonMessage message = new MotechJsonMessage("SUCCESS");
response.getWriter().write(message.toJson());
}
}
use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.
the class MotechLoginSuccessHandlerTest method shouldReturnJSON.
@Test
public void shouldReturnJSON() throws ServletException, IOException {
MockHttpServletResponse mockResponse = new MockHttpServletResponse();
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.addHeader("x-requested-with", "XMLHttpRequest");
MotechUser user = new MotechUser();
user.setUserName("testUser");
user.setFailureLoginCounter(3);
when(authentication.getName()).thenReturn("testUser");
when(motechUsersDao.findByUserName("testUser")).thenReturn(user);
motechLoginSuccessHandler.onAuthenticationSuccess(mockRequest, mockResponse, authentication);
MotechJsonMessage message = new MotechJsonMessage("SUCCESS");
assertEquals(message.toJson(), mockResponse.getContentAsString());
}
Aggregations