|
| 1 | +package com.itbulls.learnit.onlinestore.web.owasp.uasbf.solution; |
| 2 | + |
| 3 | +import jakarta.servlet.ServletException; |
| 4 | +import jakarta.servlet.annotation.WebServlet; |
| 5 | +import jakarta.servlet.http.HttpServlet; |
| 6 | +import jakarta.servlet.http.HttpServletRequest; |
| 7 | +import jakarta.servlet.http.HttpServletResponse; |
| 8 | +import jakarta.servlet.http.HttpSession; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +public class SolutionPurchaseServlet extends HttpServlet { |
| 13 | + private static final int MAX_ITEMS_PER_PRODUCT = 10; // Maximum items a user can purchase per product |
| 14 | + |
| 15 | + @Override |
| 16 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 17 | + throws ServletException, IOException { |
| 18 | + HttpSession session = request.getSession(false); |
| 19 | + if (session == null || session.getAttribute("user") == null) { |
| 20 | + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User not logged in."); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + String userId = (String) session.getAttribute("user"); |
| 25 | + String itemId = request.getParameter("itemId"); |
| 26 | + int quantity = Integer.parseInt(request.getParameter("quantity")); |
| 27 | + |
| 28 | + // Check current purchase limits for this specific product |
| 29 | + if (!isPurchaseAllowed(userId, itemId, quantity)) { |
| 30 | + response.sendError(HttpServletResponse.SC_FORBIDDEN, |
| 31 | + "Cannot purchase more than " + MAX_ITEMS_PER_PRODUCT + " items of this product. " |
| 32 | + + "If you want to purchase more, reach to our sales department."); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + boolean success = processPurchase(userId, itemId, quantity); |
| 37 | + |
| 38 | + if (success) { |
| 39 | + response.setStatus(HttpServletResponse.SC_OK); |
| 40 | + } else { |
| 41 | + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Purchase failed."); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private boolean isPurchaseAllowed(String userId, String itemId, int quantity) { |
| 46 | + int totalPurchased = getTotalItemsPurchased(userId, itemId); // Retrieve the total items purchased by the user |
| 47 | + // for this product |
| 48 | + return (totalPurchased + quantity) <= MAX_ITEMS_PER_PRODUCT; |
| 49 | + } |
| 50 | + |
| 51 | + private boolean processPurchase(String userId, String itemId, int quantity) { |
| 52 | + // Implement actual purchase processing logic here, including updating the |
| 53 | + // database |
| 54 | + updateTotalItemsPurchased(userId, itemId, quantity); // Update the number of items purchased by the user for |
| 55 | + // this product |
| 56 | + return true; // Simplified for this example |
| 57 | + } |
| 58 | + |
| 59 | + private int getTotalItemsPurchased(String userId, String itemId) { |
| 60 | + // Retrieve the total number of this specific product purchased by the user from |
| 61 | + // the database |
| 62 | + // This is a placeholder; actual implementation will involve database queries |
| 63 | + return 0; // Simplified for this example |
| 64 | + } |
| 65 | + |
| 66 | + private void updateTotalItemsPurchased(String userId, String itemId, int quantity) { |
| 67 | + // Update the total number of this specific product purchased by the user in the |
| 68 | + // database |
| 69 | + // This is a placeholder; actual implementation will involve database updates |
| 70 | + } |
| 71 | +} |
0 commit comments