Project

General

Profile

1 4080 daigle
/**
2
 *  '$RCSfile$'
3 4591 daigle
 *    Purpose: A Class that implements login methods
4 4080 daigle
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6 4591 daigle
 *    ors: Michael Daigle
7 4080 daigle
 *
8 4591 daigle
 *   '$or: daigle $'
9 4080 daigle
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat.admin;
28
29
import java.io.IOException;
30
import java.util.Vector;
31
32
import javax.servlet.ServletException;
33
import javax.servlet.http.HttpServletRequest;
34
import javax.servlet.http.HttpServletResponse;
35
36
import org.apache.log4j.Logger;
37
38 4591 daigle
import edu.ucsb.nceas.metacat.util.AuthUtil;
39 4080 daigle
import edu.ucsb.nceas.metacat.util.RequestUtil;
40
import edu.ucsb.nceas.metacat.util.UtilException;
41
42
/**
43 4591 daigle
 * Control the display of the login page
44 4080 daigle
 */
45 4591 daigle
public class LoginAdmin extends MetaCatAdmin {
46 4080 daigle
47 4591 daigle
	private static LoginAdmin Admin = null;
48
	private static Logger logMetacat = Logger.getLogger(LoginAdmin.class);
49 4080 daigle
50
	/**
51
	 * private constructor since this is a singleton
52
	 */
53 4591 daigle
	private LoginAdmin() {}
54 4080 daigle
55
	/**
56 4591 daigle
	 * Get the single instance of LoginAdmin.
57 4080 daigle
	 *
58 4591 daigle
	 * @return the single instance of LoginAdmin
59 4080 daigle
	 */
60 4591 daigle
	public static LoginAdmin getInstance() {
61
		if (Admin == null) {
62
			Admin = new LoginAdmin();
63 4080 daigle
		}
64 4591 daigle
		return Admin;
65 4080 daigle
	}
66
67
	/**
68 4591 daigle
	 * Handle configuration of the Authentication properties
69 4080 daigle
	 *
70
	 * @param request
71
	 *            the http request information
72
	 * @param response
73
	 *            the http response to be sent back to the client
74
	 */
75
	public void authenticateUser(HttpServletRequest request,
76
			HttpServletResponse response) throws AdminException {
77
78
		String processForm = request.getParameter("processForm");
79
		String formErrors = (String) request.getAttribute("formErrors");
80
81
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
82
			// The servlet configuration parameters have not been set, or there
83
			// were form errors on the last attempt to configure, so redirect to
84
			// the web form for configuring metacat
85
86
			try {
87
				// Forward the request to the JSP page
88
				RequestUtil.forwardRequest(request, response,
89
						"/admin/admin-login.jsp");
90
			} catch (IOException ioe) {
91
				throw new AdminException("IO problem while initializing "
92
						+ "user login page:" + ioe.getMessage());
93
			} catch (ServletException se) {
94
				throw new AdminException("problem forwarding request while "
95
						+ "initializing user login page: " + se.getMessage());
96
			}
97
		} else {
98
			// The configuration form is being submitted and needs to be
99
			// processed.
100
			Vector<String> processingSuccess = new Vector<String>();
101
			Vector<String> processingErrors = new Vector<String>();
102
			Vector<String> validationErrors = new Vector<String>();
103
104
			Boolean isLoggedIn = false;
105 4591 daigle
			String userName = "";
106 4080 daigle
107 4591 daigle
				userName = request.getParameter("username");
108 4080 daigle
				String password = request.getParameter("password");
109
110
				// Validate that the options provided are legitimate. Note that
111
				// we've allowed them to persist their entries. As of this point
112
				// there is no other easy way to go back to the configure form
113
				// and preserve their entries.
114
				validationErrors.addAll(validateOptions(request));
115
116
				if (validationErrors.size() == 0) {
117 4628 daigle
					try {
118
						isLoggedIn = AuthUtil.logUserIn(request, userName, password);
119
					} catch (UtilException ue) {
120
						String errorMessage = "Could not log in as: " + userName
121
						+ " : " + ue.getMessage() + ". Please try again";
122
						processingErrors.add(errorMessage);
123
						logMetacat.error(errorMessage);
124
					}
125 4080 daigle
				}
126
127
			try {
128
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
129
					RequestUtil.clearRequestMessages(request);
130
					RequestUtil.setRequestFormErrors(request, validationErrors);
131
					RequestUtil.setRequestErrors(request, processingErrors);
132
					RequestUtil.forwardRequest(request, response, "/admin");
133
				} else {
134
					// Reload the main metacat configuration page
135 4591 daigle
					processingSuccess.add("User logged in as: " + userName);
136 4080 daigle
					RequestUtil.clearRequestMessages(request);
137 4591 daigle
					RequestUtil.setUserId(request, userName);
138 4080 daigle
					RequestUtil.setRequestSuccess(request, processingSuccess);
139
					RequestUtil.forwardRequest(request, response,
140
							"/admin?configureType=configure&processForm=false");
141
				}
142
			} catch (IOException ioe) {
143 4591 daigle
				throw new AdminException("IO problem while processing login page: "
144
						+ ioe.getMessage());
145 4080 daigle
			} catch (ServletException se) {
146
				throw new AdminException("problem forwarding request while "
147 4591 daigle
						+ "processoing login page: " + se.getMessage());
148 4080 daigle
			}
149
		}
150
	}
151
152
	/**
153
	 * Validate the most important configuration options submitted by the user.
154
	 *
155
	 * @return a vector holding error message for any fields that fail
156
	 *         validation.
157
	 */
158
	protected Vector<String> validateOptions(HttpServletRequest request) {
159
		Vector<String> errorVector = new Vector<String>();
160
161
		//TODO MCD validate options.
162
163
		return errorVector;
164
	}
165
}