KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > base > secureApp > LoginHandler


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.base.secureApp;
13
14 import org.openbravo.xmlEngine.XmlDocument;
15 import org.openbravo.erpCommon.reference.PreferencesData;
16 import org.openbravo.erpCommon.utility.Utility;
17 import org.openbravo.utils.FormatUtilities;
18 import org.openbravo.base.*;
19 import org.openbravo.erpCommon.security.*;
20 import org.openbravo.erpCommon.ad_combos.*;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26 import java.sql.*;
27
28
29
30 public class LoginHandler extends HttpBaseServlet{
31   String JavaDoc strServletPorDefecto;
32
33
34   public void init (ServletConfig config) {
35     super.init(config);
36     strServletPorDefecto = config.getServletContext().getInitParameter("DefaultServlet");
37   }
38
39   public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
40  
41    log4j.info("start doPost");
42     VariablesSecureApp vars = new VariablesSecureApp(req);
43
44         // Empty session
45
req.getSession(true).setAttribute("#Authenticated_user", null);
46       
47       if (vars.getStringParameter("user").equals("")) {
48         res.sendRedirect(res.encodeRedirectURL(strDireccion + "/security/Login_F1.html"));
49       } else {
50
51         if (log4j.isDebugEnabled()) log4j.debug("Base path: " + strBaseDesignPath);
52
53             String JavaDoc strUser = vars.getRequiredStringParameter("user");
54             String JavaDoc strPass = FormatUtilities.sha1Base64(vars.getRequiredStringParameter("password"));
55             String JavaDoc strUserAuth = SeguridadData.valido(this, strUser, strPass);
56             
57             if (!strUserAuth.equals("-1")) {
58                 req.getSession(true).setAttribute("#Authenticated_user", strUserAuth);
59                 goToTarget(res, vars);
60
61             } else {
62                 goToRetry(res);
63       }
64     }
65   }
66
67     private void goToTarget(HttpServletResponse response, VariablesSecureApp vars) throws IOException {
68         
69         String JavaDoc target = vars.getSessionValue("target");
70         if (target.equals("")) {
71             response.sendRedirect(strDireccion + "/security/Menu.html");
72         } else {
73             response.sendRedirect(target);
74         }
75   }
76
77   private void goToRetry(HttpServletResponse res) throws IOException {
78     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/base/secureApp/HtmlErrorLogin").createXmlDocument();
79
80     res.setContentType("text/html");
81     PrintWriter out = res.getWriter();
82     out.println(xmlDocument.print());
83     out.close();
84   }
85
86   public String JavaDoc getServletInfo() {
87     return "User-login control Servlet";
88   } // end of getServletInfo() method
89
}
90
Popular Tags