Project

General

Profile

1 2741 costa
/**
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$'
7
 *     '$Date$'
8
 * '$Revision$'
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 2822 costa
import java.io.File;
28 2741 costa
import java.io.IOException;
29
30
import javax.servlet.RequestDispatcher;
31 2822 costa
import javax.servlet.ServletConfig;
32 2741 costa
import javax.servlet.ServletContext;
33
import javax.servlet.ServletException;
34
import javax.servlet.http.HttpServlet;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.servlet.http.HttpSession;
38
39
import edu.ucsb.nceas.metacat.client.*;
40 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
41 5015 daigle
import edu.ucsb.nceas.metacat.shared.ServiceException;
42 4080 daigle
import edu.ucsb.nceas.metacat.util.SystemUtil;
43
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
44 2741 costa
45
/**
46
 * @author dcosta
47
 *
48
 * The LoginServlet class executes a metacat login via a metacat client.
49
 */
50
public class LoginServlet extends HttpServlet {
51
52 2822 costa
  /*
53
   * Class fields
54
   */
55
  private static final String CONFIG_DIR = "WEB-INF";
56
  private static final String CONFIG_NAME = "metacat.properties";
57 3010 costa
  static final long serialVersionUID = 0;  // Needed for Eclipse warning.
58 2822 costa
59
  // Instance Variables
60
  private String contextString = "";
61 2741 costa
62
  // Methods
63
64
  /**
65
   * Executes a metacat login action. On a successful login, the metacat object
66
   * is stored as an attribute of the HttpSession object.
67
   */
68
  public void doPost(HttpServletRequest request, HttpServletResponse response)
69
          throws IOException, ServletException {
70
    RequestDispatcher dispatcher;
71
    HttpSession httpSession = request.getSession();
72
    LoginBean loginBean;
73
    boolean loginSuccess = false;
74
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
75
    MetacatLogin metacatLogin;
76
    String metacatURL;
77
    ServletContext servletContext = httpSession.getServletContext();
78
    String username;
79
80
    // First check whether the metacat URL was set as a context-param.
81
    metacatURL = servletContext.getInitParameter("metacatURL");
82
83
    // If no metacat URL was configured, then derive the metacat URL from the
84
    // server name and server port.
85
    if (metacatURL == null || metacatURL.equals("")) {
86
      MetacatHelper metacatHelper = new MetacatHelper();
87
      String serverName = request.getServerName();
88
      int serverPort = request.getServerPort();
89 3010 costa
      metacatURL =
90
       metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
91 2741 costa
    }
92
93
    if (metacat == null) {
94
      try {
95
        metacat = MetacatFactory.createMetacatConnection(metacatURL);
96
      }
97
      catch (MetacatInaccessibleException mie) {
98
        System.err.println("Metacat Inaccessible:\n" + mie.getMessage());
99
      }
100
    }
101
102
    loginBean = (LoginBean) request.getAttribute("loginBean");
103
    metacatLogin = new MetacatLogin(loginBean);
104
    loginSuccess = metacatLogin.executeLogin(metacatURL, metacat);
105
    httpSession.setAttribute("metacat", metacat);
106
107
    // If login succeeds, forward to the simple search page. If login fails,
108
    // forward to the login page where an error message will be displayed.
109
    if (loginSuccess) {
110
      httpSession.setAttribute("loggedIn", new Boolean(true));
111
      username = loginBean.getUsername();
112
      httpSession.setAttribute("username", username);
113 3010 costa
      //dispatcher = request.getRequestDispatcher("/style/skins/lter/metacatsearch.jsp");
114
      dispatcher = request.getRequestDispatcher("/style/skins/lter/index.jsp");
115 2741 costa
    }
116
    else {
117 3010 costa
      httpSession.setAttribute("loggedIn", new Boolean(false));
118
      request.setAttribute("loginFailure", "true");
119
      dispatcher = request.getRequestDispatcher("/style/skins/lter/index_login.jsp");
120 2741 costa
    }
121
122
    dispatcher.forward(request, response);
123
  }
124
125 2822 costa
126
  /**
127 4080 daigle
	 * Initializes the servlet. Reads properties and initializes object fields.
128
	 *
129
	 * @throws ServletException
130
	 */
131
	public void init(ServletConfig config) throws ServletException {
132
		ServletContext context = null;
133
		String dirPath;
134 2822 costa
135 4080 daigle
		super.init(config);
136
		context = config.getServletContext();
137
		dirPath = context.getRealPath(CONFIG_DIR);
138 2822 costa
139 4080 daigle
		try {
140 4708 daigle
			PropertyService.getInstance();
141 4080 daigle
			this.contextString = PropertyService.getProperty("application.context");
142 4124 daigle
		} catch (ServiceException se) {
143
			System.err.println("Error in loading properties: " + se.getMessage());
144 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
145
			System.err.println("couldn't read property during initialization: "
146
					+ pnfe.getMessage());
147
		}
148
	}
149 2741 costa
}