public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String deleteProductValue = "false";

    try {

      // Get the form data
      int productID = Integer.parseInt(request.getParameter("productID"));
      PrintWriter out = response.getWriter();
      DB db = mongo.getDB("FashionFactoryProd");
      DBCollection myProducts = db.getCollection("productInfo");
      DBCollection myTrending = db.getCollection("myTrending");
      BasicDBObject searchQuery = new BasicDBObject();
      searchQuery.put("productID", productID);
      DBCursor cursor = myProducts.find(searchQuery);

      if (cursor.count() == 0) {
        deleteProductValue = "false";
        request.setAttribute("deleteProductValue", deleteProductValue);
        RequestDispatcher rd = request.getRequestDispatcher("deleteProduct.jsp");
        rd.forward(request, response);
      } else {
        int product = 0;
        while (cursor.hasNext()) {

          BasicDBObject obj = (BasicDBObject) cursor.next();
          product = obj.getInt("productID");
          if (product == productID) {
            myProducts.remove(obj);
            myTrending.remove(searchQuery);
            deleteProductValue = "true";

            request.setAttribute("deleteProductValue", deleteProductValue);

            RequestDispatcher rd = request.getRequestDispatcher("deleteProduct.jsp");
            rd.forward(request, response);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 2
0
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String verifypassword = request.getParameter("verifypassword");
    Map<String, String> myResponse = new HashMap<String, String>();
    PrintWriter out = response.getWriter();
    if (email.matches(
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) // make sure email is properly
    // formatted
    {
      try {

        MongoURI mongoURI = new MongoURI(System.getenv("MONGOHQ_URL"));
        DB db = mongoURI.connectDB(); // instance of databse
        db.authenticate(mongoURI.getUsername(), mongoURI.getPassword()); // authenticates d
        // Set<string> accounts = db.getCollectionName("accounts");
        // Mongo mongo = new Mongo("localhost", 27017); //creates new instance of mongo
        // DB db = mongo.getDB("fourup"); //gets fourup database
        DBCollection accounts = db.getCollection("accounts"); // creates collection for accounts	
        BasicDBObject query = new BasicDBObject(); // creates a basic object named query
        query.put("email", email); // sets email to email
        DBCursor cursor = accounts.find(query);
        if (cursor.size() > 0) // check if email has already been registered
        {
          myResponse.put("Status", "Error");
          myResponse.put("Error", "Account already exists using this email address.");
        } else // since email doesn't currently exist in DB, go ahead and register user
        {
          if (password.equals(
              verifypassword)) // check that both of the passwords entered match each other
          {
            BasicDBObject document = new BasicDBObject();
            int salt = getSalt();
            String hpass = passwrdHash(password, salt);
            document.put("email", email);
            document.put("salt", salt);
            document.put("password", hpass); // this is where we need to hash the password
            accounts.insert(document);
            myResponse.put("Status", "Sucess");
            myResponse.put("Sucess", "Account has been Created");
            AccountObject user = new AccountObject(email, hpass);
            // set session
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", email);
            // return cookie
            Cookie cookie = new Cookie("fourupCookie", email); // add the login information here
            response.addCookie(cookie);
            // redirect to homepage
            String message = "this is a test";
            myResponse.put("html", "<html></html>");
            response.setContentType("application/json");
            response.setStatus(HttpServletResponse.SC_OK);
            // response.sendRedirect("index.html"); //should add check to index page for cookie with
            // login information
          } else {
            myResponse.put("Status", "Failed");
            myResponse.put("Failed", "Passwords do not match.");
          }
        }

      } catch (MongoException e) {

        out.write(e.getMessage());
      }
    } else {
      myResponse.put("Status", "Invalid");
      myResponse.put(
          "Invalid", "The email address has not been entered correctly."); // should output error
    }

    String strResponse = new Gson().toJson(myResponse);
    response.getWriter().write(strResponse);
    response.getWriter().close();
  }