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.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.commons.httpclient.HttpClient;
37
import org.apache.log4j.Logger;
38

    
39
import edu.ucsb.nceas.metacat.service.PropertyService;
40
import edu.ucsb.nceas.metacat.util.GeoserverUtil;
41
import edu.ucsb.nceas.metacat.util.RequestUtil;
42
import edu.ucsb.nceas.metacat.util.UtilException;
43
import edu.ucsb.nceas.utilities.GeneralPropertyException;
44

    
45
/**
46
 * Control the display of the database configuration page and the processing
47
 * of the configuration values.
48
 */
49
public class GeoserverAdmin extends MetaCatAdmin {
50

    
51
	private static GeoserverAdmin geoserverAdmin = null;
52
	private Logger logMetacat = Logger.getLogger(GeoserverAdmin.class);
53

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

    
59
	}
60

    
61
	/**
62
	 * Get the single instance of GeoserverAdmin.
63
	 * 
64
	 * @return the single instance of GeoserverAdmin
65
	 */
66
	public static GeoserverAdmin getInstance() throws AdminException {
67
		if (geoserverAdmin == null) {
68
			geoserverAdmin = new GeoserverAdmin();
69
		}
70
		return geoserverAdmin;
71
	}
72

    
73
	/**
74
	 * Handle configuration of the database the first time that Metacat starts
75
	 * or when it is explicitly called. Collect necessary update information
76
	 * from the administrator.
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 configureGeoserver(HttpServletRequest request,
84
			HttpServletResponse response) throws AdminException {
85

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

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

    
95
			try {
96
				// get the current metacat version and the database version. If
97
				// the database version is older that the metacat version, run
98
				// the appropriate scripts to get them synchronized.
99

    
100
				String username = PropertyService.getProperty("geoserver.username");
101
				String password = PropertyService.getProperty("geoserver.password");
102
				
103
				request.setAttribute("geoserver.username", username);
104
				request.setAttribute("geoserver.password", password);
105

    
106
				// Forward the request to the JSP page
107
				RequestUtil.forwardRequest(request, response,
108
						"/admin/geoserver-configuration.jsp");
109
			} catch (GeneralPropertyException gpe) {
110
				throw new AdminException("Problem getting or setting property while " 
111
						+ "initializing system properties page: " + gpe.getMessage());
112
			} catch (IOException ioe) {
113
				throw new AdminException("IO problem while initializing "
114
						+ "system properties page:" + ioe.getMessage());
115
			} catch (ServletException se) {
116
				throw new AdminException("problem forwarding request while "
117
						+ "initializing system properties page: " + se.getMessage());
118
			}  
119
		} else if (bypass != null && bypass.equals("true")) {
120
			Vector<String> processingErrors = new Vector<String>();
121
			Vector<String> processingSuccess = new Vector<String>();
122
			
123
			// Bypass the geoserver configuration. This will not keep
124
			// Metacat from running.
125
			try {
126
				PropertyService.setProperty("configutil.geoserverConfigured",
127
						PropertyService.BYPASSED);
128
				
129
			} catch (GeneralPropertyException gpe) {
130
				String errorMessage = "Problem getting or setting property while "
131
					+ "processing system properties page: " + gpe.getMessage();
132
				logMetacat.error(errorMessage);
133
				processingErrors.add(errorMessage);
134
			}
135
			try {
136
				if (processingErrors.size() > 0) {
137
					RequestUtil.clearRequestMessages(request);
138
					RequestUtil.setRequestErrors(request, processingErrors);
139
					RequestUtil.forwardRequest(request, response, "/admin");
140
				} else {			
141
					// Reload the main metacat configuration page
142
					processingSuccess.add("Geoserver configuration successfully bypassed");
143
					RequestUtil.clearRequestMessages(request);
144
					RequestUtil.setRequestSuccess(request, processingSuccess);
145
					RequestUtil.forwardRequest(request, response, 
146
							"/admin?configureType=configure&processForm=false");
147
				}
148
			} catch (ServletException se) {
149
				throw new AdminException("problem forwarding request while "
150
						+ "processing geoservices configuration page: " + se.getMessage());
151
			} catch (IOException ioe) {
152
				throw new AdminException("IO problem while processing geoservices "
153
						+ "geoservices page: " + ioe.getMessage());
154
			} 
155
		
156
		} else {
157
			// The configuration form is being submitted and needs to be
158
			// processed, setting the properties in the configuration file
159
			// then restart metacat
160

    
161
			// The configuration form is being submitted and needs to be
162
			// processed.
163
			Vector<String> validationErrors = new Vector<String>();
164
			Vector<String> processingErrors = new Vector<String>();
165
			Vector<String> processingSuccess = new Vector<String>();
166

    
167
			try {
168
				// Validate that the options provided are legitimate. Note that
169
				// we've allowed them to persist their entries. As of this point
170
				// there is no other easy way to go back to the configure form
171
				// and preserve their entries.
172
				validationErrors.addAll(validateOptions(request));
173
				
174
				String username = (String)request.getParameter("geoserver.username");
175
				String password = (String)request.getParameter("geoserver.password");
176
				
177
				if (username == null || password == null) {
178
					validationErrors.add("User Name and Password can not be null");
179
				} else {
180
					HttpClient httpClient = new HttpClient();
181
					GeoserverUtil.loginAdmin(httpClient);
182
					GeoserverUtil.changePassword(httpClient, username, password);
183

    
184
					PropertyService.setPropertyNoPersist("geoserver.username", username);
185
					PropertyService.setPropertyNoPersist("geoserver.password", password);
186
					PropertyService.persistProperties();
187

    
188
					// write the backup properties to a location outside the
189
					// application directories so they will be available after
190
					// the next upgrade
191
					PropertyService.persistMainBackupProperties(request.getSession()
192
							.getServletContext());
193
				}
194
			} catch (UtilException ue) {
195
				String errorMessage = "Error updating geoserver password: " + ue.getMessage();
196
				logMetacat.error(errorMessage);
197
				processingErrors.add(errorMessage);
198
			} catch (GeneralPropertyException gpe) {
199
				String errorMessage = "Problem getting or setting property while "
200
						+ "processing system properties page: " + gpe.getMessage();
201
				logMetacat.error(errorMessage);
202
				processingErrors.add(errorMessage);
203
			}
204

    
205
			try {
206
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
207
					RequestUtil.clearRequestMessages(request);
208
					RequestUtil.setRequestFormErrors(request, validationErrors);
209
					RequestUtil.setRequestErrors(request, processingErrors);
210
					RequestUtil.forwardRequest(request, response, "/admin");
211
				} else {
212
					// Now that the options have been set, change the
213
					// 'propertiesConfigured' option to 'true'
214
					PropertyService.setProperty("configutil.geoserverConfigured",
215
							PropertyService.CONFIGURED);
216
					
217
					// Reload the main metacat configuration page
218
					processingSuccess.add("Geoserver successfully configured");
219
					RequestUtil.clearRequestMessages(request);
220
					RequestUtil.setRequestSuccess(request, processingSuccess);
221
					RequestUtil.forwardRequest(request, response, 
222
							"/admin?configureType=configure&processForm=false");
223
				}
224
			} catch (ServletException se) {
225
				throw new AdminException("problem forwarding request while "
226
						+ "processing geoservices configuration page: " + se.getMessage());
227
			} catch (IOException ioe) {
228
				throw new AdminException("IO problem while processing geoservices "
229
						+ "geoservices page: " + ioe.getMessage());
230
			} catch (GeneralPropertyException gpe) {
231
				throw new AdminException("problem with properties while "
232
						+ "processing geoservices configuration page: " + gpe.getMessage());
233
			}
234
		}
235
	}
236

    
237
	/**
238
	 * Validate the most important configuration options submitted by the user.
239
	 * 
240
	 * @return a vector holding error message for any fields that fail
241
	 *         validation.
242
	 */
243
	protected Vector<String> validateOptions(HttpServletRequest request) {
244
		Vector<String> errorVector = new Vector<String>();
245

    
246
		// TODO MCD validate options.
247

    
248
		return errorVector;
249
	}
250
}
(4-4/10)