Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author: costa $'
7
 *     '$Date: 2005-11-16 09:57:33 -0800 (Wed, 16 Nov 2005) $'
8
 * '$Revision: 2741 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

    
27
import java.io.IOException;
28

    
29
import javax.servlet.RequestDispatcher;
30
import javax.servlet.ServletContext;
31
import javax.servlet.ServletException;
32
import javax.servlet.http.HttpServlet;
33
import javax.servlet.http.HttpServletRequest;
34
import javax.servlet.http.HttpServletResponse;
35
import javax.servlet.http.HttpSession;
36

    
37
import edu.ucsb.nceas.metacat.client.*;
38

    
39
/** 
40
 * @author dcosta
41
 * 
42
 * The LoginServlet class executes a metacat login via a metacat client.
43
 */
44
public class LoginServlet extends HttpServlet {
45

    
46
  // Instance Variables -- (Not used because they are not thread-safe.)
47

    
48
  // Methods
49
  
50
  /**
51
   * Executes a metacat login action. On a successful login, the metacat object
52
   * is stored as an attribute of the HttpSession object.
53
   */
54
  public void doPost(HttpServletRequest request, HttpServletResponse response)
55
          throws IOException, ServletException {
56
    RequestDispatcher dispatcher;
57
    HttpSession httpSession = request.getSession();
58
    LoginBean loginBean;
59
    boolean loginSuccess = false;
60
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
61
    MetacatLogin metacatLogin;
62
    String metacatURL;
63
    String organization;
64
    String result;
65
    ServletContext servletContext = httpSession.getServletContext();
66
    String username;
67
    
68
    // First check whether the metacat URL was set as a context-param.
69
    metacatURL = servletContext.getInitParameter("metacatURL");
70
 
71
    // If no metacat URL was configured, then derive the metacat URL from the
72
    // server name and server port.
73
    if (metacatURL == null || metacatURL.equals("")) {
74
      MetacatHelper metacatHelper = new MetacatHelper();
75
      String serverName = request.getServerName();
76
      int serverPort = request.getServerPort();
77
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
78
    }
79
    
80
    if (metacat == null) {
81
      try {
82
        metacat = MetacatFactory.createMetacatConnection(metacatURL);
83
      }
84
      catch (MetacatInaccessibleException mie) {
85
        System.err.println("Metacat Inaccessible:\n" + mie.getMessage());
86
      }
87
    }
88
    
89
    loginBean = (LoginBean) request.getAttribute("loginBean");
90
    metacatLogin = new MetacatLogin(loginBean);
91
    loginSuccess = metacatLogin.executeLogin(metacatURL, metacat);
92
    httpSession.setAttribute("metacat", metacat);
93

    
94
    // If login succeeds, forward to the simple search page. If login fails,
95
    // forward to the login page where an error message will be displayed.
96
    if (loginSuccess) {
97
      httpSession.setAttribute("loggedIn", new Boolean(true));
98
      username = loginBean.getUsername();
99
      httpSession.setAttribute("username", username);
100
      dispatcher = request.getRequestDispatcher("/metacatsearch.jsp");
101
    }
102
    else {
103
      request.setAttribute("loginFailure", new Boolean(true));
104
      dispatcher = request.getRequestDispatcher("/metacatlogin.jsp");
105
    }
106

    
107
    dispatcher.forward(request, response);
108
  }
109

    
110
}
(10-10/14)