package id.co.hanoman.log.action;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import id.co.hanoman.service.PersistanceServiceInterface;
import id.co.hanoman.service.ServiceUtil;
import id.co.hanoman.service.as400.AS400ServiceInterface;
import id.co.hanoman.shared.Constant;
import id.co.hanoman.shared.Util;
/**
* LoginAction merupakan action yang dipanggil untuk menjalankan fungsi-fungsi
* login user ke aplikasi
*
* @author Hanoman Interaktif
* (C) Hanoman Interaktif.
* @version 1.0
*/
public class LoginAction extends Action {
protected Log log = LogFactory.getLog(this.getClass());
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("******************************* LOGIN ******************************* ");
AS400ServiceInterface as400ServiceInterface = new AS400ServiceInterface();
// System.out.println("queryBiller ::::::::::::::::::;;
// "+as400ServiceInterface.queryBiller(Constant.MWAPP_EPAY_DS, null));
ActionForward forward = null;
String action = request.getParameter("action");
response.setHeader("Pragma", "no-cache");
String themes = (String) request.getSession().getAttribute("user.themes");
if (Util.isNullOrEmpty(themes)) {
themes = Constant.DEFAULT;
request.getSession().setAttribute("user.themes", themes);
}
request.getSession().setAttribute("currentLocale", Constant.locale);
/* todo:sena */
String callerPrincipal = request.getUserPrincipal().getName();
if (callerPrincipal != null) {
clearSession(request);
} else
clearSession(request);
if ("logout".equalsIgnoreCase(action)) {
request.getSession().invalidate();
response.sendRedirect("login.do");
return null;
}
forward = doLoginOk(mapping, form, request, response);
return forward;
}
public void clearSession(HttpServletRequest request) throws Exception {
request.getSession().removeAttribute("login.user");
request.getSession().removeAttribute("userError");
request.getSession().removeAttribute("myMenu");
}
public ActionForward doLoginOk(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (request.getParameter(Constant.CLEAR) != null && Constant.TRUE.equalsIgnoreCase(request.getParameter(Constant.CLEAR)))
clearSession(request);
ActionForward forward = null;
try {
log.info("remote address == " + request.getRemoteAddr());
Address();
ServletContext ctx = getServlet().getServletContext();
PersistanceServiceInterface psiConfig = ServiceUtil.getDaoClient(ctx);
/* todo:sena */
String callerPrincipal = request.getUserPrincipal().getName();
log.info("User Login ::::::::::::::::::::::::::::::::::::::::::::::::" + callerPrincipal);
// Subject subject = WSSubject.getCallerSubject();
// Set credSet = subject.getPublicCredentials(WSCredential.class);
// List objRoleCaller = new ArrayList();
// for (Iterator iterator = credSet.iterator(); iterator.hasNext();) {
// WSCredential objectWSCredential = (WSCredential) iterator.next();
// List groupIdCallerSubject = objectWSCredential.getGroupIds();
// // log.info("user group"+BeanUtil.dumpBean(groupIdCallerSubject));
// for (Object groupIdCallerSubjectItem : groupIdCallerSubject) {
// String strGroupIdCallerSubjectItem = (String) groupIdCallerSubjectItem;
// strGroupIdCallerSubjectItem =
// strGroupIdCallerSubjectItem.substring(strGroupIdCallerSubjectItem.toLowerCase().indexOf("cn=")
// + 3);
// strGroupIdCallerSubjectItem = strGroupIdCallerSubjectItem.substring(0,
// strGroupIdCallerSubjectItem.indexOf(','));
// objRoleCaller.add(strGroupIdCallerSubjectItem);
// }
// log.info("user group from ldap : " + BeanUtil.dumpBean(objRoleCaller));
//
// }
Util util = new Util();
List objRoleCaller = util.objRoller(callerPrincipal);
if (callerPrincipal != null) {
log.info("START MENU LOGIN ACTION");
String menu = Util.getMenu(psiConfig, request, callerPrincipal, objRoleCaller);
log.info("END MENU LOGIN ACTION");
try {
HttpSession session = request.getSession();
session.setAttribute("login.user", callerPrincipal);
session.setAttribute("objRoleCaller", objRoleCaller);
session.setAttribute("myMenu", menu);
} catch (Exception e) {
log.info(e);
}
// PersistanceServiceInterface psiEpay = ServiceUtil.getEPayDaoClient(ctx);
// AS400ServiceInterface psiBiller = ServiceUtil.getAS400ServiceClient(ctx);
// setBillerDataPageList(request, psiEpay, psiBiller);
// panggil menu
// Util.updateMenu(psiConfig, request, callerPrincipal, objRoleCaller);
} else {
request.getSession().setAttribute("userError", "This user does not exist");
}
} catch (Exception e) {
e.printStackTrace();
request.getSession().setAttribute("userError", "Null User");
}
if (Util.isNullOrEmpty((String) request.getSession().getAttribute("userError"))) {
forward = mapping.findForward("success");
} else {
forward = mapping.findForward("failure");
}
return forward;
}
private void Address() throws UnknownHostException {
InetAddress address = InetAddress.getLocalHost();
log.info("address.getHostAddress() == " + address.getHostAddress());
log.info("address.getHostName() == " + address.getHostName());
try {
log.info("address.getLocalHost() == " + InetAddress.getLocalHost().getCanonicalHostName());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* private void setBillerDataPageList(HttpServletRequest request,
* PersistanceServiceInterface psiEpay, AS400ServiceInterface psiBiller) {
* Thread t = new Thread(new Runnable() { HttpServletRequest request;
* AS400ServiceInterface psiBiller; public Runnable init(HttpServletRequest
* request, AS400ServiceInterface psiBiller) { this.request = request;
* this.psiBiller = psiBiller; return this; }
*
* @Override public void run() { synchronized (LoginAction.class) { try {
* BillerQuery[] billers = psiBiller.queryBiller("");
* request.getSession().setAttribute("billers", billers);
* log.info("biller:::::"+BeanUtil.dumpBean(billers)); } catch (Exception e) {
* log.error(e.getMessage(), e); } LoginAction.class.notifyAll(); } }
* }.init(request, psiBiller)); t.start(); }
*/
/**
* Method untuk logout dari web EPAYMENT
*
* @param mapping Action untuk melakukan redirect ke action form yang ingin
* dipanggil.
* @param form Action yang dipanggil sesuai dengan form yang ada di jsp.
* @param request Memanggil informasi HTTP servlet.
* @param response Mengirimkan response informasi HTTP servlet
* @return object forward yang berisi action form yang dipanggil
*/
public ActionForward doLogout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
ActionForward forward = null;
try {
log.info("after set strust LOGOUT");
request.getSession().invalidate();
HttpSession s = request.getSession(false);
s = null;
// response.sendRedirect("ibm_security_logout?logoutExitPage=login.jsp");
// forward = mapping.findForward("logout");
// response.sendRedirect("logout.jsp");
// return null;
} catch (Exception e) {
e.printStackTrace();
}
return forward;
}
}