Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements ldap configuration methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-07-24 13:52:51 -0700 (Thu, 24 Jul 2008) $'
10
 * '$Revision: 4159 $'
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.Set;
31
import java.util.SortedMap;
32
import java.util.Vector;
33

    
34
import javax.servlet.ServletException;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37

    
38
import org.apache.log4j.Logger;
39

    
40
import edu.ucsb.nceas.metacat.service.PropertyService;
41
import edu.ucsb.nceas.metacat.util.LDAPUtil;
42
import edu.ucsb.nceas.metacat.util.RequestUtil;
43
import edu.ucsb.nceas.metacat.util.UtilException;
44
import edu.ucsb.nceas.utilities.FileUtil;
45
import edu.ucsb.nceas.utilities.GeneralPropertyException;
46
import edu.ucsb.nceas.utilities.MetaDataProperty;
47
import edu.ucsb.nceas.utilities.PropertiesMetaData;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49
import edu.ucsb.nceas.utilities.SortedProperties;
50

    
51
/**
52
 * Control the display of the LDAP configuration page and the processing
53
 * of the configuration values.
54
 */
55
public class LDAPAdmin extends MetaCatAdmin {
56

    
57
	private static LDAPAdmin ldapAdmin = null;
58
	private static Logger logMetacat = Logger.getLogger(LDAPAdmin.class);
59

    
60
	/**
61
	 * private constructor since this is a singleton
62
	 */
63
	private LDAPAdmin() {}
64

    
65
	/**
66
	 * Get the single instance of the MetaCatConfig.
67
	 * 
68
	 * @return the single instance of MetaCatConfig
69
	 */
70
	public static LDAPAdmin getInstance() {
71
		if (ldapAdmin == null) {
72
			ldapAdmin = new LDAPAdmin();
73
		}
74
		return ldapAdmin;
75
	}
76
	
77
	/**
78
	 * Handle configuration of the LDAP properties
79
	 * 
80
	 * @param request
81
	 *            the http request information
82
	 * @param response
83
	 *            the http response to be sent back to the client
84
	 */
85
	public void configureLDAP(HttpServletRequest request,
86
			HttpServletResponse response) throws AdminException {
87

    
88
		String processForm = request.getParameter("processForm");
89
		String formErrors = (String) request.getAttribute("formErrors");
90

    
91
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
92
			// The servlet configuration parameters have not been set, or there
93
			// were form errors on the last attempt to configure, so redirect to
94
			// the web form for configuring metacat
95
			
96
			try {
97
				// Load the properties metadata file so that the JSP page can
98
				// use the metadata to construct the editing form
99
				PropertiesMetaData metadata = PropertyService.getLDAPMetaData();
100
				request.setAttribute("metadata", metadata);
101
				request.setAttribute("groupMap", metadata.getGroups());
102

    
103
				// add the list of ldap options and their values to the request
104
				Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("ldap");
105
				for (String name : propertyNames) {
106
					request.setAttribute(name, PropertyService.getProperty(name));
107
				} 
108

    
109
				// Check for any backup properties and apply them to the request.
110
				// These are properties from previous configurations. They keep
111
				// the user from having to re-enter all values when upgrading.
112
				// If this is a first time install, getBackupProperties will return
113
				// null.
114
				SortedProperties backupProperties = PropertyService.getLDAPBackupProperties();
115
				if (backupProperties != null) {
116
					Vector<String> backupKeys = backupProperties.getPropertyNames();
117
					for (String key : backupKeys) {
118
						String value = backupProperties.getProperty(key);
119
						if (value != null) {
120
							request.setAttribute(key, value);
121
						}
122
					}
123
				}
124
				// Forward the request to the JSP page
125
				RequestUtil.forwardRequest(request, response,
126
						"/admin/ldap-configuration.jsp");
127
			} catch (PropertyNotFoundException pnfe) {
128
				throw new AdminException("Problem getting property while initializing "
129
						+ "LDAP properties page: " + pnfe.getMessage());
130
			} catch (IOException ioe) {
131
				throw new AdminException("IO problem while initializing "
132
						+ "LDAP properties page:" + ioe.getMessage());
133
			} catch (ServletException se) {
134
				throw new AdminException("problem forwarding request while " 
135
						+ "initializing LDAP properties page: " + se.getMessage());
136
			}
137
		} else {
138
			// The configuration form is being submitted and needs to be
139
			// processed.
140
			Vector<String> processingSuccess = new Vector<String>();
141
			Vector<String> processingErrors = new Vector<String>();
142
			Vector<String> validationErrors = new Vector<String>();
143

    
144
			try {
145
				// For each property, check if it is changed and save it
146
				PropertiesMetaData ldapMetaData = PropertyService
147
						.getLDAPMetaData();
148

    
149
				// process the fields for the global options (group 1)
150
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = ldapMetaData
151
						.getPropertiesInGroup(1);
152
				Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
153
				for (Integer globalPropertyIndex : globalPropertyIndexes) {
154
					String globalPropertyKey = globalPropertyMap.get(
155
							globalPropertyIndex).getKey();
156
					PropertyService.checkAndSetProperty(request,
157
							globalPropertyKey);
158
				}
159

    
160
				// we need to write the options from memory to the properties
161
				// file
162
				PropertyService.persistProperties();
163

    
164
				// Validate that the options provided are legitimate. Note that
165
				// we've allowed them to persist their entries. As of this point
166
				// there is no other easy way to go back to the configure form
167
				// and preserve their entries.
168
				validationErrors.addAll(validateOptions(request));
169

    
170
				// Try to create data file and backup directories if
171
				// necessary.
172
				String backupDir = PropertyService.getBackupDir();
173
				if (!FileUtil.createDirectory(backupDir)) {
174
					String errorString = "Could not create directory: " + backupDir;
175
					logMetacat.error(errorString);
176
					validationErrors.add(errorString);
177
				}
178

    
179
				// Write out the configurable properties to a backup file
180
				// outside the install directory.  Note that we allow them to
181
				// do this even if they have validation errors.  They will
182
				// need to go back and fix the errors before they can run metacat.
183
				PropertyService.persistLDAPBackupProperties(request.getSession()
184
						.getServletContext());
185
			
186
			} catch (GeneralPropertyException gpe) {
187
				String errorMessage = "Problem getting or setting property while "
188
					+ "processing LDAP properties page: " + gpe.getMessage();
189
				logMetacat.error(errorMessage);
190
				processingErrors.add(errorMessage);
191
			} catch (IOException ioe) {
192
				String errorMessage = "IO problem while processing LDAP "
193
						+ "properties page: " + ioe.getMessage();
194
				logMetacat.error(errorMessage);
195
				processingErrors.add(errorMessage);
196
			} 
197
			
198
			try {
199
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
200
					RequestUtil.clearRequestMessages(request);
201
					RequestUtil.setRequestFormErrors(request, validationErrors);
202
					RequestUtil.setRequestErrors(request, processingErrors);
203
					RequestUtil.forwardRequest(request, response, "/admin");
204
				} else {
205
					// Now that the options have been set, change the
206
					// 'ldapConfigured' option to 'true'
207
					PropertyService.setProperty("configutil.ldapConfigured",
208
							PropertyService.CONFIGURED);
209
					
210
					// Reload the main metacat configuration page
211
					processingSuccess.add("LDAP successfully configured");
212
					RequestUtil.clearRequestMessages(request);
213
					RequestUtil.setRequestSuccess(request, processingSuccess);
214
					RequestUtil.forwardRequest(request, response,
215
							"/admin?configureType=configure&processForm=false");
216
				}
217
			} catch (ServletException se) {
218
				throw new AdminException("problem forwarding request while "
219
						+ "processing LDAP properties page: " + se.getMessage());
220
			} catch (IOException ioe) {
221
				throw new AdminException("IO problem while processing LDAP "
222
						+ "properties page: " + ioe.getMessage());
223
			} catch (GeneralPropertyException gpe) {
224
				String errorMessage = "Problem getting or setting property while "
225
					+ "processing LDAP properties page: " + gpe.getMessage();
226
				logMetacat.error(errorMessage);
227
				processingErrors.add(errorMessage);
228
			}
229
		}
230
	}
231
	
232
	/**
233
	 * Validate the most important configuration options submitted by the user.
234
	 * 
235
	 * @return a vector holding error message for any fields that fail
236
	 *         validation.
237
	 */
238
	protected Vector<String> validateOptions(HttpServletRequest request) {
239
		Vector<String> errorVector = new Vector<String>();
240

    
241
		//TODO MCD validate options.
242

    
243
		return errorVector;
244
	}
245
}
(4-4/9)