PageRenderTime 68ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/paypal/controller/UserServlet.java

https://gitlab.com/CORP-RESELLER/rest-api-sample-app-java
Java | 412 lines | 285 code | 58 blank | 69 comment | 53 complexity | 44203dfc889e855f996148c88da0140e MD5 | raw file
  1. package com.paypal.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.sql.SQLException;
  6. import java.util.List;
  7. import javax.net.ssl.HostnameVerifier;
  8. import javax.net.ssl.HttpsURLConnection;
  9. import javax.net.ssl.SSLSession;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15. import com.paypal.api.payments.CreditCard;
  16. import com.paypal.core.ConfigManager;
  17. import com.paypal.core.LoggingManager;
  18. import com.paypal.core.rest.PayPalRESTException;
  19. import com.paypal.core.rest.PayPalResource;
  20. import com.paypal.dao.DBConnection;
  21. import com.paypal.dao.Dao;
  22. import com.paypal.model.CreditCardDetail;
  23. import com.paypal.model.ErrorMessage;
  24. import com.paypal.model.User;
  25. import com.paypal.model.UserPaymentDetail;
  26. import com.paypal.util.AppConstants;
  27. import com.paypal.util.AppHelper;
  28. import com.paypal.util.WebHelper;
  29. /**
  30. * <code>UserServlet</code> handles user management. It keeps track of sessions,
  31. * sign-ups, log-in, log-out order descriptions and profile updates
  32. *
  33. * @author tkanta
  34. *
  35. */
  36. public class UserServlet extends HttpServlet {
  37. private static final long serialVersionUID = 1231435434634644452L;
  38. /**
  39. * Initialize SDK configuration file and database connection
  40. */
  41. @Override
  42. public void init() throws ServletException {
  43. // initialize sdk configuration
  44. InputStream is = PaymentServlet.class
  45. .getResourceAsStream("/sdk_config.properties");
  46. try {
  47. PayPalResource.initConfig(is);
  48. } catch (PayPalRESTException pex) {
  49. LoggingManager.debug(PaymentServlet.class, pex.getMessage());
  50. throw new ServletException(pex);
  51. }
  52. // Initialize the database drivers
  53. DBConnection.getConnection();
  54. }
  55. protected void doGet(HttpServletRequest request,
  56. HttpServletResponse response) throws ServletException, IOException {
  57. // check if session is valid
  58. if (!WebHelper.checkSessionValidity(request)) {
  59. WebHelper.forward(request, response, AppConstants.SHOW_HOME);
  60. } else {
  61. if (request.getRequestURI().contains(AppConstants.HOME)) {
  62. // ------ Show home page ----------
  63. WebHelper.forward(request, response, AppConstants.SHOW_HOME);
  64. } else if (request.getRequestURI().contains(AppConstants.SIGNUP)) {
  65. // ------ Show signup page ----------
  66. WebHelper.forward(request, response, AppConstants.SHOW_SIGNUP);
  67. } else if (request.getRequestURI().contains(AppConstants.SIGNIN)) {
  68. // ------ Show signin page ----------
  69. WebHelper.forward(request, response, AppConstants.SHOW_SIGNIN);
  70. } else if (request.getRequestURI().contains(AppConstants.PROFILE)) {
  71. // ------ Show profile page ----------
  72. handleDisplayProfilePage(request, response);
  73. } else if (request.getRequestURI().contains(
  74. AppConstants.SHOW_ORDERS)) {
  75. // ------ Show order page ----------
  76. handleDisplayOrderPage(request, response);
  77. } else if (request.getRequestURI().contains(AppConstants.SIGNOUT)) {
  78. // ------ Handle signout ----------
  79. handleSignout(request, response);
  80. } else {
  81. throw new ServletException("Unknown resource requested");
  82. }
  83. }
  84. }
  85. protected void doPost(HttpServletRequest request,
  86. HttpServletResponse response) throws ServletException, IOException {
  87. // check if session is valid
  88. if (!WebHelper.checkSessionValidity(request)) {
  89. WebHelper.redirect(request, response, AppConstants.SHOW_SIGNIN);
  90. } else {
  91. if (request.getRequestURI().contains(AppConstants.PLACE_ORDER)) {
  92. // -------- User order ---------
  93. handleOrder(request, response);
  94. } else if (request.getRequestURI().contains(AppConstants.SIGNUP)) {
  95. // -------- User signup ---------
  96. handleSignup(request, response);
  97. } else if (request.getRequestURI().contains(AppConstants.SIGNIN)) {
  98. // -------- User signin ---------
  99. handleSignin(request, response);
  100. } else if (request.getRequestURI().contains(AppConstants.PROFILE)) {
  101. // -------- User profile ---------
  102. handleUpdateProfile(request, response);
  103. } else {
  104. throw new ServletException("Unknown resource requested");
  105. }
  106. }
  107. }
  108. /*
  109. * Handle user Sign out
  110. */
  111. private void handleSignout(HttpServletRequest request,
  112. HttpServletResponse response) throws ServletException, IOException {
  113. HttpSession session = request.getSession(false);
  114. session.invalidate();
  115. // forward user to home
  116. WebHelper.forward(request, response, AppConstants.SHOW_HOME);
  117. }
  118. /*
  119. * display order page
  120. */
  121. private void handleDisplayOrderPage(HttpServletRequest request,
  122. HttpServletResponse response) throws ServletException, IOException {
  123. // Get Logged in User detail
  124. HttpSession session = request.getSession(false);
  125. String email = (String) session.getAttribute("user");
  126. User user = null;
  127. try {
  128. user = Dao.getUser(email);
  129. } catch (SQLException sqlex) {
  130. throw new ServletException(sqlex);
  131. }
  132. List<UserPaymentDetail> paymentList = null;
  133. try {
  134. // Get User payments list
  135. paymentList = Dao.getOrdersByUserId(user.getId());
  136. } catch (SQLException sqlex) {
  137. throw new ServletException(sqlex);
  138. }
  139. request.setAttribute("paymentList", paymentList);
  140. WebHelper.forward(request, response, AppConstants.SHOW_ORDER);
  141. }
  142. /*
  143. * display profile page
  144. */
  145. private void handleDisplayProfilePage(HttpServletRequest request,
  146. HttpServletResponse response) throws ServletException, IOException {
  147. HttpSession session = request.getSession(false);
  148. String email = (String) session.getAttribute("user");
  149. User user = null;
  150. try {
  151. user = Dao.getUser(email);
  152. } catch (SQLException sqlex) {
  153. throw new ServletException(sqlex);
  154. }
  155. // Get Credit Card detail
  156. CreditCardDetail cardDetail = getCreditCardDetail(request, response,
  157. user);
  158. request.setAttribute("cardDetail", cardDetail);
  159. WebHelper.forward(request, response, AppConstants.SHOW_PROFILE);
  160. }
  161. /*
  162. * Get Credit Card detail
  163. */
  164. private CreditCardDetail getCreditCardDetail(HttpServletRequest request,
  165. HttpServletResponse response, User user) throws ServletException,
  166. IOException {
  167. CreditCardDetail cardDetail = null;
  168. try {
  169. cardDetail = AppHelper.getCreditCardDetail(user.getCreditCardId());
  170. } catch (PayPalRESTException pex) {
  171. if (pex.getMessage().contains(AppConstants.VALIDATION_ERROR)) {
  172. WebHelper.formErrorMessage(request, pex);
  173. WebHelper.forward(request, response, AppConstants.SHOW_PROFILE);
  174. } else {
  175. LoggingManager.debug(PaymentServlet.class, pex.getMessage());
  176. throw new ServletException(pex);
  177. }
  178. }
  179. return cardDetail;
  180. }
  181. /*
  182. * Handle display of user payment options [ CreditCard , PayPal]
  183. */
  184. private void handleOrder(HttpServletRequest request,
  185. HttpServletResponse response) throws ServletException, IOException {
  186. HttpSession session = request.getSession(false);
  187. if (session != null && session.getAttribute("user") != null) {
  188. String orderString = request.getQueryString();
  189. String[] orderArray = orderString.split("&");
  190. String orderAmount = orderArray[0].split("=")[1];
  191. String orderDesc = orderArray[1].split("=")[1];
  192. request.setAttribute("orderAmount", orderAmount);
  193. request.setAttribute("orderDesc", orderDesc);
  194. WebHelper.forward(request, response, AppConstants.SHOW_PLACE_ORDER);
  195. } else {
  196. response.sendRedirect(WebHelper.getContextPath(request) + "/signin");
  197. return;
  198. }
  199. }
  200. /*
  201. * Handle user sign up
  202. */
  203. private void handleSignup(HttpServletRequest request,
  204. HttpServletResponse response) throws ServletException, IOException {
  205. String email = request.getParameter("user_email");
  206. String password = request.getParameter("user_password");
  207. User user = null;
  208. CreditCard createdCreditCard = null;
  209. // Retrieve User
  210. try {
  211. user = Dao.getUser(email);
  212. } catch (SQLException sqlex) {
  213. throw new ServletException(sqlex);
  214. }
  215. // Check for User existence
  216. if (user != null) {
  217. ErrorMessage errorMsg = new ErrorMessage();
  218. errorMsg.add(AppConstants.USER_EXIST);
  219. request.setAttribute("error", errorMsg);
  220. WebHelper.forward(request, response, AppConstants.SHOW_SIGNUP);
  221. return;
  222. }
  223. // create credit card
  224. try {
  225. createdCreditCard = AppHelper.createCreditCard(request);
  226. } catch (PayPalRESTException pex) {
  227. if (pex.getMessage().contains(AppConstants.VALIDATION_ERROR)) {
  228. WebHelper.formErrorMessage(request, pex);
  229. WebHelper.forward(request, response, AppConstants.SHOW_SIGNUP);
  230. return;
  231. } else {
  232. LoggingManager.debug(PaymentServlet.class, pex.getMessage());
  233. throw new ServletException(pex);
  234. }
  235. }
  236. // Insert User
  237. try {
  238. Dao.insertUser(email, password, createdCreditCard.getId());
  239. } catch (SQLException sqlex) {
  240. throw new ServletException(sqlex);
  241. }
  242. HttpSession session = request.getSession();
  243. session.setAttribute("isSessionActive", true);
  244. session.setAttribute("user", email);
  245. // forward User to home
  246. WebHelper.forward(request, response, AppConstants.SHOW_HOME);
  247. }
  248. /*
  249. * Handle user sign in
  250. */
  251. private void handleSignin(HttpServletRequest request,
  252. HttpServletResponse response) throws ServletException, IOException {
  253. // validate User
  254. String email = request.getParameter("user_email");
  255. String password = request.getParameter("user_password");
  256. User user = null;
  257. try {
  258. user = Dao.getUser(email);
  259. if (user == null
  260. || ((user != null) && !user.getPassword().equals(password))) {
  261. ErrorMessage errMsg = new ErrorMessage();
  262. errMsg.add("User Doesnot Exist.");
  263. request.setAttribute("error", errMsg);
  264. WebHelper.forward(request, response, AppConstants.SHOW_SIGNIN);
  265. } else {
  266. HttpSession session = request.getSession();
  267. session.setAttribute("isSessionActive", true);
  268. session.setAttribute("user", user.getEmail());
  269. // forward User to home
  270. WebHelper.forward(request, response, AppConstants.SHOW_HOME);
  271. }
  272. } catch (SQLException sqlex) {
  273. throw new ServletException(sqlex);
  274. }
  275. }
  276. /*
  277. * Handle User profile updates
  278. */
  279. private void handleUpdateProfile(HttpServletRequest request,
  280. HttpServletResponse response) throws ServletException, IOException {
  281. ErrorMessage errMsg = new ErrorMessage();
  282. // validate User existence
  283. String email = request.getParameter("user_email");
  284. String password = request.getParameter("user_current_password");
  285. User user = null;
  286. try {
  287. user = Dao.getUser(email);
  288. } catch (SQLException sqlex) {
  289. throw new ServletException(sqlex);
  290. }
  291. if (user == null
  292. || ((user != null) && !user.getPassword().equals(password))) {
  293. errMsg.add(AppConstants.USER_PSW_INCORRECT);
  294. }
  295. // Set credit card details for display
  296. HttpSession session = request.getSession(false);
  297. String useremail = (String) session.getAttribute("user");
  298. User userFromSession = null;
  299. try {
  300. userFromSession = Dao.getUser(useremail);
  301. } catch (SQLException sqlex) {
  302. throw new ServletException(sqlex);
  303. }
  304. CreditCardDetail cardDetail = getCreditCardDetail(request, response,
  305. userFromSession);
  306. request.setAttribute("cardDetail", cardDetail);
  307. // Check for password matching
  308. String update_password = request.getParameter("update_password") != "" ? request
  309. .getParameter("update_password") : AppConstants.EMPTY_STRING;
  310. String update_password_confirmation = request
  311. .getParameter("update_password_confirmation") != "" ? request
  312. .getParameter("update_password_confirmation")
  313. : AppConstants.EMPTY_STRING;
  314. if (!update_password.equals(update_password_confirmation)) {
  315. errMsg.add(AppConstants.PASSWORD_NOT_MATCH);
  316. }
  317. // If error exist , display in UI
  318. if (errMsg.getMessageList().size() > 0) {
  319. request.setAttribute("error", errMsg);
  320. WebHelper.forward(request, response, AppConstants.SHOW_PROFILE);
  321. } else {
  322. updateProfile(request, response, user, update_password_confirmation);
  323. }
  324. }
  325. /*
  326. * Update User profile
  327. */
  328. private void updateProfile(HttpServletRequest request,
  329. HttpServletResponse response, User user,
  330. String update_password_confirmation) throws ServletException,
  331. IOException {
  332. // Do a profile update
  333. try {
  334. // create credit card and update User table with new
  335. // creditcardId and new password
  336. CreditCard newCreditCard = AppHelper.createCreditCard(request);
  337. user.setCreditCardId(newCreditCard.getId());
  338. user.setPassword(update_password_confirmation);
  339. Dao.updateUser(user);
  340. } catch (PayPalRESTException pex) {
  341. if (pex.getMessage().contains(AppConstants.VALIDATION_ERROR)) {
  342. WebHelper.formErrorMessage(request, pex);
  343. WebHelper.forward(request, response, AppConstants.SHOW_PROFILE);
  344. return;
  345. } else {
  346. LoggingManager.debug(PaymentServlet.class, pex.getMessage());
  347. throw new ServletException(pex);
  348. }
  349. } catch (SQLException sqlex) {
  350. throw new ServletException(sqlex);
  351. }
  352. WebHelper.forward(request, response, AppConstants.SHOW_HOME);
  353. }
  354. }