1 |
4205
|
daigle
|
/**
|
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 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
|
41 |
4205
|
daigle
|
import edu.ucsb.nceas.metacat.util.GeoserverUtil;
|
42 |
|
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
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 |
5027
|
daigle
|
public class GeoserverAdmin extends MetacatAdmin {
|
50 |
4205
|
daigle
|
|
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 |
5027
|
daigle
|
"/admin/geoserver-configuration.jsp", null);
|
109 |
4205
|
daigle
|
} 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 |
5027
|
daigle
|
RequestUtil.forwardRequest(request, response, "/admin", null);
|
140 |
4205
|
daigle
|
} 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 |
5027
|
daigle
|
"/admin?configureType=configure&processForm=false", null);
|
147 |
4205
|
daigle
|
}
|
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 |
5028
|
daigle
|
PropertyService.persistMainBackupProperties();
|
192 |
4205
|
daigle
|
}
|
193 |
4854
|
daigle
|
} catch (MetacatUtilException ue) {
|
194 |
4205
|
daigle
|
String errorMessage = "Error updating geoserver password: " + ue.getMessage();
|
195 |
|
|
logMetacat.error(errorMessage);
|
196 |
|
|
processingErrors.add(errorMessage);
|
197 |
|
|
} catch (GeneralPropertyException gpe) {
|
198 |
|
|
String errorMessage = "Problem getting or setting property while "
|
199 |
|
|
+ "processing system properties page: " + gpe.getMessage();
|
200 |
|
|
logMetacat.error(errorMessage);
|
201 |
|
|
processingErrors.add(errorMessage);
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
try {
|
205 |
|
|
if (validationErrors.size() > 0 || processingErrors.size() > 0) {
|
206 |
|
|
RequestUtil.clearRequestMessages(request);
|
207 |
|
|
RequestUtil.setRequestFormErrors(request, validationErrors);
|
208 |
|
|
RequestUtil.setRequestErrors(request, processingErrors);
|
209 |
5027
|
daigle
|
RequestUtil.forwardRequest(request, response, "/admin", null);
|
210 |
4205
|
daigle
|
} else {
|
211 |
|
|
// Now that the options have been set, change the
|
212 |
|
|
// 'propertiesConfigured' option to 'true'
|
213 |
|
|
PropertyService.setProperty("configutil.geoserverConfigured",
|
214 |
|
|
PropertyService.CONFIGURED);
|
215 |
|
|
|
216 |
|
|
// Reload the main metacat configuration page
|
217 |
|
|
processingSuccess.add("Geoserver successfully configured");
|
218 |
|
|
RequestUtil.clearRequestMessages(request);
|
219 |
|
|
RequestUtil.setRequestSuccess(request, processingSuccess);
|
220 |
|
|
RequestUtil.forwardRequest(request, response,
|
221 |
5027
|
daigle
|
"/admin?configureType=configure&processForm=false", null);
|
222 |
4205
|
daigle
|
}
|
223 |
|
|
} catch (ServletException se) {
|
224 |
|
|
throw new AdminException("problem forwarding request while "
|
225 |
|
|
+ "processing geoservices configuration page: " + se.getMessage());
|
226 |
|
|
} catch (IOException ioe) {
|
227 |
|
|
throw new AdminException("IO problem while processing geoservices "
|
228 |
|
|
+ "geoservices page: " + ioe.getMessage());
|
229 |
|
|
} catch (GeneralPropertyException gpe) {
|
230 |
|
|
throw new AdminException("problem with properties while "
|
231 |
|
|
+ "processing geoservices configuration page: " + gpe.getMessage());
|
232 |
|
|
}
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
/**
|
237 |
|
|
* Validate the most important configuration options submitted by the user.
|
238 |
|
|
*
|
239 |
|
|
* @return a vector holding error message for any fields that fail
|
240 |
|
|
* validation.
|
241 |
|
|
*/
|
242 |
|
|
protected Vector<String> validateOptions(HttpServletRequest request) {
|
243 |
|
|
Vector<String> errorVector = new Vector<String>();
|
244 |
|
|
|
245 |
|
|
// TODO MCD validate options.
|
246 |
|
|
|
247 |
|
|
return errorVector;
|
248 |
|
|
}
|
249 |
|
|
}
|