@Override
public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo();
Endpoint endpoint = EndpointFactory.getEndpoint(getEndpointName(pathInfo)); //remove hardcoded name ..get second token from path info
PrintWriter writer = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "utf-8"));
JsonObject jsonResponse = new JsonJavaObject();
if(getAuthAction(pathInfo).equals(LOG_OUT)){
boolean logoutSuccessful = true;
try {
endpoint.logout();
} catch (AuthenticationException e) {
logoutSuccessful = false;
}
try {
if(endpoint.isAuthenticated()){
logoutSuccessful = false;
}
} catch (ClientServicesException e) {
logoutSuccessful = false;
}
if(logoutSuccessful){
jsonResponse.putJsonProperty("success", true);
jsonResponse.putJsonProperty("status", 200);
}else{
jsonResponse.putJsonProperty("success", false);
jsonResponse.putJsonProperty("status", 500);
}
}
if(getAuthAction(pathInfo).equals(IS_AUTHENTICATED)){
try {
jsonResponse.putJsonProperty("result", endpoint.isAuthenticated());
jsonResponse.putJsonProperty("status", 200);
} catch (ClientServicesException e) {
jsonResponse.putJsonProperty("result", false);
jsonResponse.putJsonProperty("status", 500);
}
}
if(getAuthAction(pathInfo).equals(IS_AUTHENTICATION_VALID)){
try {
jsonResponse.putJsonProperty("result", endpoint.isAuthenticationValid());
jsonResponse.putJsonProperty("status", 200);
} catch (ClientServicesException e) {
jsonResponse.putJsonProperty("result", false);
jsonResponse.putJsonProperty("status", 500);
}
}
response.setContentType(APPLICATION_JSON);
writer.write(jsonResponse.toString());
writer.flush();
}