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-10-10 17:11:10 -0700 (Fri, 10 Oct 2008) $'
10
 * '$Revision: 4440 $'
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.RequestUtil;
42
import edu.ucsb.nceas.utilities.FileUtil;
43
import edu.ucsb.nceas.utilities.GeneralPropertyException;
44
import edu.ucsb.nceas.utilities.MetaDataProperty;
45
import edu.ucsb.nceas.utilities.PropertiesMetaData;
46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47
import edu.ucsb.nceas.utilities.SortedProperties;
48

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

    
55
	private static LDAPAdmin ldapAdmin = null;
56
	private static Logger logMetacat = Logger.getLogger(LDAPAdmin.class);
57

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

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

    
86
		String processForm = request.getParameter("processForm");
87
		String formErrors = (String) request.getAttribute("formErrors");
88

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

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

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

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

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

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

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

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

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

    
237
		//TODO MCD validate options.
238

    
239
		return errorVector;
240
	}
241
}
(5-5/10)