Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements login methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    ors: Michael Daigle
7
 *
8
 *   '$or: daigle $'
9
 *     '$Date: 2008-11-25 09:54:32 -0800 (Tue, 25 Nov 2008) $'
10
 * '$Revision: 4628 $'
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
import edu.ucsb.nceas.metacat.util.AuthUtil;
39
import edu.ucsb.nceas.metacat.util.RequestUtil;
40
import edu.ucsb.nceas.metacat.util.UtilException;
41

    
42
/**
43
 * Control the display of the login page 
44
 */
45
public class LoginAdmin extends MetaCatAdmin {
46

    
47
	private static LoginAdmin Admin = null;
48
	private static Logger logMetacat = Logger.getLogger(LoginAdmin.class);
49

    
50
	/**
51
	 * private constructor since this is a singleton
52
	 */
53
	private LoginAdmin() {}
54

    
55
	/**
56
	 * Get the single instance of LoginAdmin.
57
	 * 
58
	 * @return the single instance of LoginAdmin
59
	 */
60
	public static LoginAdmin getInstance() {
61
		if (Admin == null) {
62
			Admin = new LoginAdmin();
63
		}
64
		return Admin;
65
	}
66
	
67
	/**
68
	 * Handle configuration of the Authentication properties
69
	 * 
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
			String userName = "";
106

    
107
				userName = request.getParameter("username");
108
				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
					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
				}
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
					processingSuccess.add("User logged in as: " + userName);
136
					RequestUtil.clearRequestMessages(request);
137
					RequestUtil.setUserId(request, userName);
138
					RequestUtil.setRequestSuccess(request, processingSuccess);
139
					RequestUtil.forwardRequest(request, response,
140
							"/admin?configureType=configure&processForm=false");
141
				}
142
			} catch (IOException ioe) {
143
				throw new AdminException("IO problem while processing login page: " 
144
						+ ioe.getMessage());
145
			} catch (ServletException se) {
146
				throw new AdminException("problem forwarding request while "
147
						+ "processoing login page: " + se.getMessage());
148
			}
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
}
(6-6/10)