package com.etherios.gae.connector.tank;

import java.io.IOException;

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

import com.etherios.gae.connector.tank.datastore.UserData;

@SuppressWarnings("serial")
public class LogoutServlet extends BaseServlet {
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		clearErrorMessage();
		
		UserData user = getUserFromSession(req);
		if (user == null) {
			req.setAttribute("error", errorMessage);
			loginDispatcher.forward(req, resp);
			return;
		}
		
		user = null;
		HttpSession session = req.getSession(false);
		if(session != null) {
			session.removeAttribute(SESSION_USER);
			session.removeAttribute(SESSION_USER_ID);
			session.invalidate();
		}
		
		resp.sendRedirect(PAGE_LOGIN_SERVLET);
	}
	
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}
