package com.jasamedika.medifirst2000.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import com.jasamedika.medifirst2000.constants.Constants;

/**
 * RestAuthenticationEntryPoint class
 * set Unauthorized response from "Unauthorized client"
 * 
 * @author Roberto
 */
@Component("RestAuthenticationEntryPoint")
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RestAuthenticationEntryPoint.class);
	

	/// Alter Syamsu
	@Override
	public void commence(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException ae)
			throws IOException, ServletException {
		
		LOGGER.error("Mencoba akses tanpa login");
		response.getWriter().write("{" + Constants.MessageInfo.ERROR_MESSAGE + ":'Please login to get access'}");
		response.setHeader(Constants.MessageInfo.ERROR_MESSAGE, "Please login to get access");
		response.setStatus(HttpServletResponse.SC_FORBIDDEN);
		
		//response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
	}

}