es.uma.inftel.blog.servlet.RegistroServlet.java Source code

Java tutorial

Introduction

Here is the source code for es.uma.inftel.blog.servlet.RegistroServlet.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package es.uma.inftel.blog.servlet;

import es.uma.inftel.blog.domain.PostFacade;
import es.uma.inftel.blog.domain.UsuarioFacade;
import es.uma.inftel.blog.model.Usuario;
import es.uma.inftel.blog.presentation.RegistroViewFacade;
import java.io.IOException;
import java.io.InputStream;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author miguel
 */
@MultipartConfig
@WebServlet(name = "RegistroServlet", urlPatterns = { "/registro" })
public class RegistroServlet extends HttpServlet {
    @EJB
    private PostFacade postFacade;

    @EJB
    private UsuarioFacade usuarioFacade;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        RegistroViewFacade registroViewFacade = new RegistroViewFacade(postFacade);

        RequestDispatcher registroRequestDispatcher = request.getRequestDispatcher("registro.jsp");
        if (username == null && password == null) {
            request.setAttribute("registroView", registroViewFacade.createView(false));
            registroRequestDispatcher.forward(request, response);
            return;
        }

        String email = request.getParameter("email");

        Part filePart = request.getPart("avatar");
        InputStream inputStream = filePart.getInputStream();
        byte[] avatar = IOUtils.toByteArray(inputStream);

        Usuario usuario = registrarUsuario(username, password, email, avatar);
        if (usuario == null) {
            request.setAttribute("registroView", registroViewFacade.createView(true));
            registroRequestDispatcher.forward(request, response);
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("usuario", usuario);
            response.sendRedirect("index");
        }
    }

    private Usuario registrarUsuario(String username, String password, String email, byte[] avatar) {
        if (usuarioFacade.findByName(username) != null) {
            return null;
        }
        Usuario usuario = new Usuario();
        usuario.setUsername(username);
        usuario.setPassword(password);
        usuario.setEmail(email);
        usuario.setTipo(Usuario.TIPO_NORMAL);
        usuario.setAvatar(avatar);

        usuarioFacade.create(usuario);

        return usuario;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}