Package com.sahuaro.software

Source Code of com.sahuaro.software.ContactServlet

//Author: Alberto Rubalcaba
package com.sahuaro.software;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.googlecode.objectify.Key;
import com.sahuaro.captcha.ReCaptchaUtils;
import com.sahuaro.domain.Contact;
import com.sahuaro.domain.Message;
import com.sahuaro.domain.Prospect;
import com.sahuaro.interfaces.IContactRepository;
import com.sahuaro.interfaces.IMessageRepository;
import com.sahuaro.interfaces.IProspectRepository;
import com.sahuaro.mail.EmailSender;
import com.sahuaro.repositories.ContactRepository;
import com.sahuaro.repositories.MessageRepository;
import com.sahuaro.repositories.ProspectRepository;

@SuppressWarnings("serial")
public class ContactServlet extends HttpServlet {

 
 
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException{
    // Evita continuar si la validación del captcha no fue exitosa.
    if (!ReCaptchaUtils.checkRecaptchaResponse(req)) {
      System.out.println("Recaptcha validation failed.");
      // TODO: Redirigir o otra página o algo asi.
      resp.sendRedirect("/index.jsp");
      return;
    }
   
    String firstName = (String) req.getParameter("firstname");
    String lastName = (String) req.getParameter("lastname");
    String email = (String) req.getParameter("email");
    String telephone = (String) req.getParameter("telephone");
    String company = (String) req.getParameter("company");
    String msg = (String) req.getParameter("message");
   
    IProspectRepository prospectsRepo = new ProspectRepository();
    IContactRepository contactsRepo = new ContactRepository();
    IMessageRepository messagesRepo = new MessageRepository();
   
    //creates the prospect and saves it
    Prospect prospect = new Prospect(firstName, lastName, email);
    Key<Prospect> prospectKey = prospectsRepo.save(prospect);
    System.out.println("Prospect saved");
    //it get it back
   
   
    //creates the contact message
    Message message = new Message(msg, prospectKey.getId());
    Key<Message> messageKey = messagesRepo.save(message);
    System.out.println("Message saved");
    //get the message back to use the id
    Contact contact = new Contact(company, telephone, messageKey.getId());
    //saves the contact at Google servers
    Key<Contact> contactKey = contactsRepo.save(contact);
    System.out.println("Contact saved");
   
    //creates a new email container
    EmailSender<Contact> sender = new EmailSender<Contact>(contact);
    sender.sendEmail(email, msg, prospect);
    resp.sendRedirect("/about.jsp");
  }

}
TOP

Related Classes of com.sahuaro.software.ContactServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.