Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements database 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:47:03 -0700 (Thu, 24 Jul 2008) $'
10
 * '$Revision: 4155 $'
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.util.Vector;
30

    
31
import javax.servlet.http.HttpServletRequest;
32
import javax.servlet.http.HttpServletResponse;
33

    
34
import org.apache.log4j.Logger;
35

    
36
import edu.ucsb.nceas.metacat.properties.PropertyService;
37
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
38
import edu.ucsb.nceas.metacat.util.GeoserverUtil;
39
import edu.ucsb.nceas.metacat.util.RequestUtil;
40
import edu.ucsb.nceas.metacat.util.SystemUtil;
41
import edu.ucsb.nceas.utilities.FileUtil;
42
import edu.ucsb.nceas.utilities.GeneralPropertyException;
43
import edu.ucsb.nceas.utilities.SortedProperties;
44

    
45
/**
46
 * Control the display of the ezid configuration page and the processing
47
 * of the configuration values.
48
 */
49
public class EZIDAdmin extends MetacatAdmin {
50

    
51
	private static EZIDAdmin ezidAdmin = null;
52
	private Logger logMetacat = Logger.getLogger(EZIDAdmin.class);
53

    
54
	/**
55
	 * private constructor since this is a singleton
56
	 */
57
	private EZIDAdmin() throws AdminException {
58

    
59
	}
60

    
61
	/**
62
	 * Get the single instance of EZIDAdmin.
63
	 * 
64
	 * @return the single instance of GeoserverAdmin
65
	 */
66
	public static EZIDAdmin getInstance() throws AdminException {
67
		if (ezidAdmin == null) {
68
		    synchronized(EZIDAdmin.class) {
69
		        if(ezidAdmin == null) {
70
		            ezidAdmin = new EZIDAdmin();
71
		        }
72
		    }
73
			
74
		}
75
		return ezidAdmin;
76
	}
77

    
78
	/**
79
	 * Handle configuration of the database the first time that Metacat starts
80
	 * or when it is explicitly called. Collect necessary update information
81
	 * from the administrator.
82
	 * 
83
	 * @param request
84
	 *            the http request information
85
	 * @param response
86
	 *            the http response to be sent back to the client
87
	 */
88
	public void configureEZID(HttpServletRequest request,
89
			HttpServletResponse response) throws AdminException {
90

    
91
		String processForm = request.getParameter("processForm");
92
		String bypass = request.getParameter("bypass");
93
		String formErrors = (String) request.getAttribute("formErrors");
94

    
95
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
96
			// The servlet configuration parameters have not been set, or there
97
			// were form errors on the last attempt to configure, so redirect to
98
			// the web form for configuring metacat
99

    
100
			try {
101
				// get the current configuration values
102
			    String enablestr = PropertyService.getProperty("guid.ezid.enabled");
103
				String username = PropertyService.getProperty("guid.ezid.username");
104
				String password = PropertyService.getProperty("guid.ezid.password");
105
				String baseurl = PropertyService.getProperty("guid.ezid.baseurl");
106
				String doishoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
107
				boolean enable = false;
108
				if (enablestr != null) {
109
					enable = Boolean.parseBoolean(enablestr);
110
				}
111
				
112
				request.setAttribute("guid.ezid.enabled", enable);
113
				request.setAttribute("guid.ezid.username", username);
114
				request.setAttribute("guid.ezid.password", password);
115
				request.setAttribute("guid.ezid.baseurl", baseurl);
116
				request.setAttribute("guid.ezid.doishoulder.1", doishoulder);
117
				
118
				
119
				// try the backup properties
120
                SortedProperties backupProperties = null;
121
                if ((backupProperties = 
122
                        PropertyService.getMainBackupProperties()) != null) {
123
                    Vector<String> backupKeys = backupProperties.getPropertyNames();
124
                    for (String key : backupKeys) {
125
                        String value = backupProperties.getProperty(key);
126
                        if(key != null && value != null && key.equals("guid.ezid.enabled")) {
127
                            enable = Boolean.parseBoolean(value);
128
                            request.setAttribute("guid.ezid.enabled", enable);
129
                        } else if (value != null) {
130
                            request.setAttribute(key, value);
131
                        }
132
                    }
133
                }
134

    
135
				// Forward the request to the JSP page
136
				RequestUtil.forwardRequest(request, response,
137
						"/admin/ezid-configuration.jsp", null);
138
			} catch (GeneralPropertyException gpe) {
139
				throw new AdminException("EZIDAdmin.configureEZID - Problem getting or " + 
140
						"setting property while initializing system properties page: " + gpe.getMessage());
141
			} catch (MetacatUtilException mue) {
142
				throw new AdminException("EZIDAdmin.configureEZID- utility problem while initializing "
143
						+ "system properties page:" + mue.getMessage());
144
			} 
145
		} else if (bypass != null && bypass.equals("true")) {
146
			Vector<String> processingErrors = new Vector<String>();
147
			Vector<String> processingSuccess = new Vector<String>();
148
			
149
			// Bypass the geoserver configuration. This will not keep
150
			// Metacat from running.
151
			try {
152
				PropertyService.setProperty("configutil.ezidConfigured",
153
						PropertyService.BYPASSED);
154
				
155
			} catch (GeneralPropertyException gpe) {
156
				String errorMessage = "EZIDAdmin.configureEZID - Problem getting or setting property while "
157
					+ "processing system properties page: " + gpe.getMessage();
158
				logMetacat.error(errorMessage);
159
				processingErrors.add(errorMessage);
160
			}
161
			try {
162
				if (processingErrors.size() > 0) {
163
					RequestUtil.clearRequestMessages(request);
164
					RequestUtil.setRequestErrors(request, processingErrors);
165
					RequestUtil.forwardRequest(request, response, "/admin", null);
166
				} else {			
167
					// Reload the main metacat configuration page
168
					processingSuccess.add("EZID configuration successfully bypassed");
169
					RequestUtil.clearRequestMessages(request);
170
					RequestUtil.setRequestSuccess(request, processingSuccess);
171
					RequestUtil.forwardRequest(request, response, 
172
							"/admin?configureType=configure&processForm=false", null);
173
				}
174
			} catch (MetacatUtilException mue) {
175
				throw new AdminException("EZID.configureEZID - utility problem while processing ezid services "
176
						+ "ezidservices page: " + mue.getMessage());
177
			} 
178
		
179
		} else {
180
			// The configuration form is being submitted and needs to be
181
			// processed, setting the properties in the configuration file
182
			// then restart metacat
183

    
184
			// The configuration form is being submitted and needs to be
185
			// processed.
186
			Vector<String> validationErrors = new Vector<String>();
187
			Vector<String> processingErrors = new Vector<String>();
188
			Vector<String> processingSuccess = new Vector<String>();
189

    
190
			try {
191
				// Validate that the options provided are legitimate. Note that
192
				// we've allowed them to persist their entries. As of this point
193
				// there is no other easy way to go back to the configure form
194
				// and preserve their entries.
195
				validationErrors.addAll(validateOptions(request));
196
				
197
				String enablestr = (String)request.getParameter("guid.ezid.enabled");
198
				String username = (String)request.getParameter("guid.ezid.username");
199
                String password = (String)request.getParameter("guid.ezid.password");
200
                String baseurl = (String)request.getParameter("guid.ezid.baseurl");
201
                String doishoulder = (String)request.getParameter("guid.ezid.doishoulder.1");
202
                boolean enable = false;
203
                if (enablestr != null) {
204
                    enable = Boolean.parseBoolean(enablestr);
205
                }
206
				
207
				
208
				if (username == null || password == null) {
209
					validationErrors.add("User Name and Password cannot be null");
210
				} else {
211
				    PropertyService.setPropertyNoPersist("guid.ezid.enabled", Boolean.toString(enable));
212
					PropertyService.setPropertyNoPersist("guid.ezid.username", username);
213
					PropertyService.setPropertyNoPersist("guid.ezid.password", password);
214
					PropertyService.setPropertyNoPersist("guid.ezid.baseurl", baseurl);
215
					PropertyService.setPropertyNoPersist("guid.ezid.doishoulder.1", doishoulder);
216
					// persist them all
217
					PropertyService.persistProperties();
218
					PropertyService.syncToSettings();
219
                    // save a backup in case the form has errors, we reload from these
220
                    PropertyService.persistMainBackupProperties();
221
				}
222
			}  catch (GeneralPropertyException gpe) {
223
				String errorMessage = "EZIDAdmin.configureEZID - Problem getting or setting property while "
224
						+ "processing system properties page: " + gpe.getMessage();
225
				logMetacat.error(errorMessage);
226
				processingErrors.add(errorMessage);
227
			}
228

    
229
			try {
230
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
231
					RequestUtil.clearRequestMessages(request);
232
					RequestUtil.setRequestFormErrors(request, validationErrors);
233
					RequestUtil.setRequestErrors(request, processingErrors);
234
					RequestUtil.forwardRequest(request, response, "/admin", null);
235
				} else {
236
					// Now that the options have been set, change the
237
					// 'propertiesConfigured' option to 'true'
238
					PropertyService.setProperty("configutil.ezidConfigured",
239
							PropertyService.CONFIGURED);
240
					
241
					// Reload the main metacat configuration page
242
					processingSuccess.add("EZID successfully configured");
243
					RequestUtil.clearRequestMessages(request);
244
					RequestUtil.setRequestSuccess(request, processingSuccess);
245
					RequestUtil.forwardRequest(request, response, 
246
							"/admin?configureType=configure&processForm=false", null);
247
				}
248
			} catch (MetacatUtilException mue) {
249
				throw new AdminException("EZIDAdmin.configureEZID - utility problem while processing ezidservices "
250
						+ "ezidservices page: " + mue.getMessage());
251
			} catch (GeneralPropertyException gpe) {
252
				throw new AdminException("EZIDAdmin.configureEZID - problem with properties while "
253
						+ "processing ezidservices configuration page: " + gpe.getMessage());
254
			}
255
		}
256
	}
257

    
258
	/**
259
	 * Validate the most important configuration options submitted by the user.
260
	 * 
261
	 * @return a vector holding error message for any fields that fail
262
	 *         validation.
263
	 */
264
	protected Vector<String> validateOptions(HttpServletRequest request) {
265
		Vector<String> errorVector = new Vector<String>();
266

    
267
		// TODO MCD validate options.
268

    
269
		return errorVector;
270
	}
271
}
(6-6/13)