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

    
37
import edu.ucsb.nceas.metacat.properties.PropertyService;
38
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
39
import edu.ucsb.nceas.metacat.util.GeoserverUtil;
40
import edu.ucsb.nceas.metacat.util.RequestUtil;
41
import edu.ucsb.nceas.utilities.GeneralPropertyException;
42

    
43
/**
44
 * Control the display of the database configuration page and the processing
45
 * of the configuration values.
46
 */
47
public class GeoserverAdmin extends MetacatAdmin {
48

    
49
	private static GeoserverAdmin geoserverAdmin = null;
50
	private Logger logMetacat = Logger.getLogger(GeoserverAdmin.class);
51

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

    
57
	}
58

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

    
71
	/**
72
	 * Handle configuration of the database the first time that Metacat starts
73
	 * or when it is explicitly called. Collect necessary update information
74
	 * from the administrator.
75
	 * 
76
	 * @param request
77
	 *            the http request information
78
	 * @param response
79
	 *            the http response to be sent back to the client
80
	 */
81
	public void configureGeoserver(HttpServletRequest request,
82
			HttpServletResponse response) throws AdminException {
83

    
84
		String processForm = request.getParameter("processForm");
85
		String bypass = request.getParameter("bypass");
86
		String formErrors = (String) request.getAttribute("formErrors");
87

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

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

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

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

    
153
			// The configuration form is being submitted and needs to be
154
			// processed.
155
			Vector<String> validationErrors = new Vector<String>();
156
			Vector<String> processingErrors = new Vector<String>();
157
			Vector<String> processingSuccess = new Vector<String>();
158

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

    
176
					PropertyService.setPropertyNoPersist("geoserver.username", username);
177
					PropertyService.setPropertyNoPersist("geoserver.password", password);
178
					PropertyService.persistProperties();
179

    
180
					// write the backup properties to a location outside the
181
					// application directories so they will be available after
182
					// the next upgrade
183
					PropertyService.persistMainBackupProperties();
184
				}
185
			} catch (MetacatUtilException ue) {
186
				String errorMessage = "GeoserverAdmin.configureGeoserver - Error updating geoserver password: " + ue.getMessage();
187
				logMetacat.error(errorMessage);
188
				processingErrors.add(errorMessage);
189
			} catch (GeneralPropertyException gpe) {
190
				String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
191
						+ "processing system properties page: " + gpe.getMessage();
192
				logMetacat.error(errorMessage);
193
				processingErrors.add(errorMessage);
194
			}
195

    
196
			try {
197
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
198
					RequestUtil.clearRequestMessages(request);
199
					RequestUtil.setRequestFormErrors(request, validationErrors);
200
					RequestUtil.setRequestErrors(request, processingErrors);
201
					RequestUtil.forwardRequest(request, response, "/admin", null);
202
				} else {
203
					// Now that the options have been set, change the
204
					// 'propertiesConfigured' option to 'true'
205
					PropertyService.setProperty("configutil.geoserverConfigured",
206
							PropertyService.CONFIGURED);
207
					
208
					// Reload the main metacat configuration page
209
					processingSuccess.add("Geoserver successfully configured");
210
					RequestUtil.clearRequestMessages(request);
211
					RequestUtil.setRequestSuccess(request, processingSuccess);
212
					RequestUtil.forwardRequest(request, response, 
213
							"/admin?configureType=configure&processForm=false", null);
214
				}
215
			} catch (MetacatUtilException mue) {
216
				throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while processing geoservices "
217
						+ "geoservices page: " + mue.getMessage());
218
			} catch (GeneralPropertyException gpe) {
219
				throw new AdminException("GeoserverAdmin.configureGeoserver - problem with properties while "
220
						+ "processing geoservices configuration page: " + gpe.getMessage());
221
			}
222
		}
223
	}
224

    
225
	/**
226
	 * Validate the most important configuration options submitted by the user.
227
	 * 
228
	 * @return a vector holding error message for any fields that fail
229
	 *         validation.
230
	 */
231
	protected Vector<String> validateOptions(HttpServletRequest request) {
232
		Vector<String> errorVector = new Vector<String>();
233

    
234
		// TODO MCD validate options.
235

    
236
		return errorVector;
237
	}
238
}
(5-5/10)