LoginAction.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package id.co.hanoman.log.action;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.util.List;
  5. import javax.servlet.ServletContext;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.apache.struts.action.Action;
  12. import org.apache.struts.action.ActionForm;
  13. import org.apache.struts.action.ActionForward;
  14. import org.apache.struts.action.ActionMapping;
  15. import id.co.hanoman.service.PersistanceServiceInterface;
  16. import id.co.hanoman.service.ServiceUtil;
  17. import id.co.hanoman.service.as400.AS400ServiceInterface;
  18. import id.co.hanoman.shared.Constant;
  19. import id.co.hanoman.shared.Util;
  20. /**
  21. * LoginAction merupakan action yang dipanggil untuk menjalankan fungsi-fungsi
  22. * login user ke aplikasi
  23. *
  24. * @author Hanoman Interaktif<BR>
  25. * (C) <a href="http://www.hanoman.co.id">Hanoman Interaktif.</a>
  26. * @version 1.0
  27. */
  28. public class LoginAction extends Action {
  29. protected Log log = LogFactory.getLog(this.getClass());
  30. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  31. System.out.println("******************************* LOGIN ******************************* ");
  32. AS400ServiceInterface as400ServiceInterface = new AS400ServiceInterface();
  33. // System.out.println("queryBiller ::::::::::::::::::;;
  34. // "+as400ServiceInterface.queryBiller(Constant.MWAPP_EPAY_DS, null));
  35. ActionForward forward = null;
  36. String action = request.getParameter("action");
  37. response.setHeader("Pragma", "no-cache");
  38. String themes = (String) request.getSession().getAttribute("user.themes");
  39. if (Util.isNullOrEmpty(themes)) {
  40. themes = Constant.DEFAULT;
  41. request.getSession().setAttribute("user.themes", themes);
  42. }
  43. request.getSession().setAttribute("currentLocale", Constant.locale);
  44. /* todo:sena */
  45. String callerPrincipal = request.getUserPrincipal().getName();
  46. if (callerPrincipal != null) {
  47. clearSession(request);
  48. } else
  49. clearSession(request);
  50. if ("logout".equalsIgnoreCase(action)) {
  51. request.getSession().invalidate();
  52. response.sendRedirect("login.do");
  53. return null;
  54. }
  55. forward = doLoginOk(mapping, form, request, response);
  56. return forward;
  57. }
  58. public void clearSession(HttpServletRequest request) throws Exception {
  59. request.getSession().removeAttribute("login.user");
  60. request.getSession().removeAttribute("userError");
  61. request.getSession().removeAttribute("myMenu");
  62. }
  63. public ActionForward doLoginOk(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  64. if (request.getParameter(Constant.CLEAR) != null && Constant.TRUE.equalsIgnoreCase(request.getParameter(Constant.CLEAR)))
  65. clearSession(request);
  66. ActionForward forward = null;
  67. try {
  68. log.info("remote address == " + request.getRemoteAddr());
  69. Address();
  70. ServletContext ctx = getServlet().getServletContext();
  71. PersistanceServiceInterface psiConfig = ServiceUtil.getDaoClient(ctx);
  72. /* todo:sena */
  73. String callerPrincipal = request.getUserPrincipal().getName();
  74. log.info("User Login ::::::::::::::::::::::::::::::::::::::::::::::::" + callerPrincipal);
  75. // Subject subject = WSSubject.getCallerSubject();
  76. // Set credSet = subject.getPublicCredentials(WSCredential.class);
  77. // List objRoleCaller = new ArrayList();
  78. // for (Iterator iterator = credSet.iterator(); iterator.hasNext();) {
  79. // WSCredential objectWSCredential = (WSCredential) iterator.next();
  80. // List groupIdCallerSubject = objectWSCredential.getGroupIds();
  81. // // log.info("user group"+BeanUtil.dumpBean(groupIdCallerSubject));
  82. // for (Object groupIdCallerSubjectItem : groupIdCallerSubject) {
  83. // String strGroupIdCallerSubjectItem = (String) groupIdCallerSubjectItem;
  84. // strGroupIdCallerSubjectItem =
  85. // strGroupIdCallerSubjectItem.substring(strGroupIdCallerSubjectItem.toLowerCase().indexOf("cn=")
  86. // + 3);
  87. // strGroupIdCallerSubjectItem = strGroupIdCallerSubjectItem.substring(0,
  88. // strGroupIdCallerSubjectItem.indexOf(','));
  89. // objRoleCaller.add(strGroupIdCallerSubjectItem);
  90. // }
  91. // log.info("user group from ldap : " + BeanUtil.dumpBean(objRoleCaller));
  92. //
  93. // }
  94. Util util = new Util();
  95. List objRoleCaller = util.objRoller(callerPrincipal);
  96. if (callerPrincipal != null) {
  97. log.info("START MENU LOGIN ACTION");
  98. String menu = Util.getMenu(psiConfig, request, callerPrincipal, objRoleCaller);
  99. log.info("END MENU LOGIN ACTION");
  100. try {
  101. HttpSession session = request.getSession();
  102. session.setAttribute("login.user", callerPrincipal);
  103. session.setAttribute("objRoleCaller", objRoleCaller);
  104. session.setAttribute("myMenu", menu);
  105. } catch (Exception e) {
  106. log.info(e);
  107. }
  108. // PersistanceServiceInterface psiEpay = ServiceUtil.getEPayDaoClient(ctx);
  109. // AS400ServiceInterface psiBiller = ServiceUtil.getAS400ServiceClient(ctx);
  110. // setBillerDataPageList(request, psiEpay, psiBiller);
  111. // panggil menu
  112. // Util.updateMenu(psiConfig, request, callerPrincipal, objRoleCaller);
  113. } else {
  114. request.getSession().setAttribute("userError", "This user does not exist");
  115. }
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. request.getSession().setAttribute("userError", "Null User");
  119. }
  120. if (Util.isNullOrEmpty((String) request.getSession().getAttribute("userError"))) {
  121. forward = mapping.findForward("success");
  122. } else {
  123. forward = mapping.findForward("failure");
  124. }
  125. return forward;
  126. }
  127. private void Address() throws UnknownHostException {
  128. InetAddress address = InetAddress.getLocalHost();
  129. log.info("address.getHostAddress() == " + address.getHostAddress());
  130. log.info("address.getHostName() == " + address.getHostName());
  131. try {
  132. log.info("address.getLocalHost() == " + InetAddress.getLocalHost().getCanonicalHostName());
  133. } catch (UnknownHostException e) {
  134. // TODO Auto-generated catch block
  135. e.printStackTrace();
  136. }
  137. }
  138. /*
  139. * private void setBillerDataPageList(HttpServletRequest request,
  140. * PersistanceServiceInterface psiEpay, AS400ServiceInterface psiBiller) {
  141. * Thread t = new Thread(new Runnable() { HttpServletRequest request;
  142. * AS400ServiceInterface psiBiller; public Runnable init(HttpServletRequest
  143. * request, AS400ServiceInterface psiBiller) { this.request = request;
  144. * this.psiBiller = psiBiller; return this; }
  145. *
  146. * @Override public void run() { synchronized (LoginAction.class) { try {
  147. * BillerQuery[] billers = psiBiller.queryBiller("");
  148. * request.getSession().setAttribute("billers", billers);
  149. * log.info("biller:::::"+BeanUtil.dumpBean(billers)); } catch (Exception e) {
  150. * log.error(e.getMessage(), e); } LoginAction.class.notifyAll(); } }
  151. * }.init(request, psiBiller)); t.start(); }
  152. */
  153. /**
  154. * Method untuk logout dari web EPAYMENT
  155. *
  156. * @param mapping Action untuk melakukan redirect ke action form yang ingin
  157. * dipanggil.
  158. * @param form Action yang dipanggil sesuai dengan form yang ada di jsp.
  159. * @param request Memanggil informasi HTTP servlet.
  160. * @param response Mengirimkan response informasi HTTP servlet
  161. * @return object forward yang berisi action form yang dipanggil
  162. */
  163. public ActionForward doLogout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  164. ActionForward forward = null;
  165. try {
  166. log.info("after set strust LOGOUT");
  167. request.getSession().invalidate();
  168. HttpSession s = request.getSession(false);
  169. s = null;
  170. // response.sendRedirect("ibm_security_logout?logoutExitPage=login.jsp");
  171. // forward = mapping.findForward("logout");
  172. // response.sendRedirect("logout.jsp");
  173. // return null;
  174. } catch (Exception e) {
  175. e.printStackTrace();
  176. }
  177. return forward;
  178. }
  179. }