001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.web;
017
018import java.io.IOException;
019import javax.servlet.ServletContext;
020import javax.servlet.ServletException;
021import javax.servlet.http.HttpServlet;
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025/**
026 * @author kulikov
027 */
028public class LoginServlet extends HttpServlet {
029
030    private static final long serialVersionUID = 1L;
031
032    /**
033     * Processes requests for both HTTP
034     * <code>GET</code> and
035     * <code>POST</code> methods.
036     *
037     * @param request servlet request
038     * @param response servlet response
039     * @throws ServletException if a servlet-specific error occurs
040     * @throws IOException if an I/O error occurs
041     */
042    protected void processRequest(HttpServletRequest request,
043            HttpServletResponse response) throws ServletException, IOException {
044
045        //get user's credentials from the request
046        String uname = request.getParameter("j_username");
047        String pass = request.getParameter("j_password");
048
049        try {
050            //validate given user using container's login mechanism
051            request.login(uname, pass);
052
053            ServletContext context = getServletConfig().getServletContext();
054            
055            //put user's credentials in the servlet context
056            context.setAttribute("uname", uname);
057            context.setAttribute("password", pass);
058            
059            //forward request to the initial page
060            response.sendRedirect(request.getContextPath() + "/Console.html");
061        } catch (ServletException e) {
062            //login failed to forward user to the login form
063            response.sendRedirect(request.getContextPath() + "/loginform.html");
064        }
065    }
066
067    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
068    /**
069     * Handles the HTTP
070     * <code>GET</code> method.
071     *
072     * @param request servlet request
073     * @param response servlet response
074     * @throws ServletException if a servlet-specific error occurs
075     * @throws IOException if an I/O error occurs
076     */
077    @Override
078    protected void doGet(HttpServletRequest request,
079            HttpServletResponse response) throws ServletException, IOException {
080        processRequest(request, response);
081    }
082
083    /**
084     * Handles the HTTP
085     * <code>POST</code> method.
086     *
087     * @param request servlet request
088     * @param response servlet response
089     * @throws ServletException if a servlet-specific error occurs
090     * @throws IOException if an I/O error occurs
091     */
092    @Override
093    protected void doPost(HttpServletRequest request,
094            HttpServletResponse response) throws ServletException, IOException {
095        processRequest(request, response);
096    }
097
098    /**
099     * Returns a short description of the servlet.
100     *
101     * @return a String containing servlet description
102     */
103    @Override
104    public String getServletInfo() {
105        return "Short description";
106    }// </editor-fold>
107}