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: leinfelder $'
9
 *     '$Date: 2011-11-23 11:31:29 -0800 (Wed, 23 Nov 2011) $'
10
 * '$Revision: 6690 $'
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.File;
30
import java.util.Vector;
31

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

    
35
import org.apache.log4j.Logger;
36
import org.dataone.client.CNode;
37
import org.dataone.client.D1Client;
38
import org.dataone.client.auth.CertificateManager;
39
import org.dataone.service.exceptions.IdentifierNotUnique;
40
import org.dataone.service.exceptions.InvalidRequest;
41
import org.dataone.service.exceptions.NotAuthorized;
42
import org.dataone.service.exceptions.NotFound;
43
import org.dataone.service.exceptions.NotImplemented;
44
import org.dataone.service.exceptions.ServiceFailure;
45
import org.dataone.service.types.v1.Node;
46
import org.dataone.service.types.v1.NodeList;
47
import org.dataone.service.types.v1.NodeReference;
48
import org.dataone.service.types.v1.Session;
49

    
50
import edu.ucsb.nceas.metacat.dataone.MNodeService;
51
import edu.ucsb.nceas.metacat.properties.PropertyService;
52
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
53
import edu.ucsb.nceas.metacat.util.RequestUtil;
54
import edu.ucsb.nceas.metacat.util.SystemUtil;
55
import edu.ucsb.nceas.utilities.FileUtil;
56
import edu.ucsb.nceas.utilities.GeneralPropertyException;
57
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
58

    
59
/**
60
 * Control the display of the database configuration page and the processing
61
 * of the configuration values.
62
 */
63
public class D1Admin extends MetacatAdmin {
64

    
65
	private static D1Admin instance = null;
66
	private Logger logMetacat = Logger.getLogger(D1Admin.class);
67

    
68
	/**
69
	 * private constructor since this is a singleton
70
	 */
71
	private D1Admin() throws AdminException {
72

    
73
	}
74

    
75
	/**
76
	 * Get the single instance of D1Admin.
77
	 * 
78
	 * @return the single instance of D1Admin
79
	 */
80
	public static D1Admin getInstance() throws AdminException {
81
		if (instance == null) {
82
			instance = new D1Admin();
83
		}
84
		return instance;
85
	}
86

    
87
	/**
88
	 * Handle configuration of the database the first time that Metacat starts
89
	 * or when it is explicitly called. Collect necessary update information
90
	 * from the administrator.
91
	 * 
92
	 * @param request
93
	 *            the http request information
94
	 * @param response
95
	 *            the http response to be sent back to the client
96
	 */
97
	public void configureDataONE(HttpServletRequest request,
98
			HttpServletResponse response) throws AdminException {
99

    
100
		String processForm = request.getParameter("processForm");
101
		String bypass = request.getParameter("bypass");
102
		String formErrors = (String) request.getAttribute("formErrors");
103

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

    
109
			try {
110
				
111
				// get the current configuration values
112
				String nodeName = PropertyService.getProperty("dataone.nodeName");
113
				String nodeDescription = PropertyService.getProperty("dataone.nodeDescription");
114
				String memberNodeId = PropertyService.getProperty("dataone.memberNodeId");
115
				String nodeSynchronize = PropertyService.getProperty("dataone.nodeSynchronize");
116
				String nodeReplicate = PropertyService.getProperty("dataone.nodeReplicate");
117
				String subject = PropertyService.getProperty("dataone.subject");
118
				String certpath = PropertyService.getProperty("D1Client.certificate.file");
119
				
120
				//the synch schedule
121
				String year = PropertyService.getProperty("dataone.nodeSynchronization.schedule.year");
122
				String mon = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mon");
123
				String mday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mday");
124
				String wday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.wday");
125
				String hour = PropertyService.getProperty("dataone.nodeSynchronization.schedule.hour");
126
				String min = PropertyService.getProperty("dataone.nodeSynchronization.schedule.min");
127
				String sec = PropertyService.getProperty("dataone.nodeSynchronization.schedule.sec");
128
				
129
				/**
130
				dataone.nodeSynchronization.schedule.year=*
131
				dataone.nodeSynchronization.schedule.mon=*
132
				dataone.nodeSynchronization.schedule.mday=*
133
				dataone.nodeSynchronization.schedule.wday=?
134
				dataone.nodeSynchronization.schedule.hour=*
135
				dataone.nodeSynchronization.schedule.min=0/3
136
				dataone.nodeSynchronization.schedule.sec=10
137
				**/
138
				
139
				boolean synchronize = false;
140
				if (nodeSynchronize != null) {
141
					synchronize = Boolean.parseBoolean(nodeSynchronize);
142
				}
143
				boolean replicate = false;
144
				if (nodeReplicate != null) {
145
					replicate = Boolean.parseBoolean(nodeReplicate);
146
				}
147
				
148
				request.setAttribute("dataone.nodeName", nodeName);
149
				request.setAttribute("dataone.nodeDescription", nodeDescription);
150
				request.setAttribute("dataone.memberNodeId", memberNodeId);
151
				request.setAttribute("dataone.nodeSynchronize", synchronize);
152
				request.setAttribute("dataone.nodeReplicate", replicate);
153
				request.setAttribute("dataone.subject", subject);
154
				request.setAttribute("D1Client.certificate.file", certpath);
155
				
156
				// synch schedule
157
				request.setAttribute("dataone.nodeSynchronization.schedule.year", year);
158
				request.setAttribute("dataone.nodeSynchronization.schedule.mon", mon);
159
				request.setAttribute("dataone.nodeSynchronization.schedule.mday", mday);
160
				request.setAttribute("dataone.nodeSynchronization.schedule.wday", wday);
161
				request.setAttribute("dataone.nodeSynchronization.schedule.hour", hour);
162
				request.setAttribute("dataone.nodeSynchronization.schedule.min", min);
163
				request.setAttribute("dataone.nodeSynchronization.schedule.sec", sec);
164

    
165
				// Forward the request to the JSP page
166
				RequestUtil.forwardRequest(request, response, "/admin/dataone-configuration.jsp", null);
167
			} catch (GeneralPropertyException gpe) {
168
				throw new AdminException("D1Admin.configureDataONE - Problem getting or " + 
169
						"setting property while initializing system properties page: " + gpe.getMessage());
170
			} catch (MetacatUtilException mue) {
171
				throw new AdminException("D1Admin.configureDataONE - utility problem while initializing "
172
						+ "system properties page:" + mue.getMessage());
173
			} 
174
		} else if (bypass != null && bypass.equals("true")) {
175
			Vector<String> processingErrors = new Vector<String>();
176
			Vector<String> processingSuccess = new Vector<String>();
177
			
178
			// Bypass the D1 configuration. 
179
			// This will keep Metacat running
180
			try {
181
				PropertyService.setProperty("configutil.dataoneConfigured", PropertyService.BYPASSED);
182
				
183
			} catch (GeneralPropertyException gpe) {
184
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
185
					+ "processing system properties page: " + gpe.getMessage();
186
				logMetacat.error(errorMessage);
187
				processingErrors.add(errorMessage);
188
			}
189
			try {
190
				if (processingErrors.size() > 0) {
191
					RequestUtil.clearRequestMessages(request);
192
					RequestUtil.setRequestErrors(request, processingErrors);
193
					RequestUtil.forwardRequest(request, response, "/admin", null);
194
				} else {			
195
					// Reload the main metacat configuration page
196
					processingSuccess.add("DataONE configuration successfully bypassed");
197
					RequestUtil.clearRequestMessages(request);
198
					RequestUtil.setRequestSuccess(request, processingSuccess);
199
					RequestUtil.forwardRequest(request, response, 
200
							"/admin?configureType=configure&processForm=false", null);
201
				}
202
			} catch (MetacatUtilException mue) {
203
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing dataone page: "
204
						+ mue.getMessage());
205
			} 
206
		
207
		} else {
208
			// The configuration form is being submitted and needs to be
209
			// processed, setting the properties in the configuration file
210
			// then restart metacat
211

    
212
			// The configuration form is being submitted and needs to be
213
			// processed.
214
			Vector<String> validationErrors = new Vector<String>();
215
			Vector<String> processingErrors = new Vector<String>();
216
			Vector<String> processingSuccess = new Vector<String>();
217

    
218
			try {
219
				// Validate that the options provided are legitimate. Note that
220
				// we've allowed them to persist their entries. As of this point
221
				// there is no other easy way to go back to the configure form
222
				// and preserve their entries.
223
				validationErrors.addAll(validateOptions(request));
224
				
225
				String nodeName = (String)request.getParameter("dataone.nodeName");
226
				String nodeDescription = (String)request.getParameter("dataone.nodeDescription");
227
				String memberNodeId = (String)request.getParameter("dataone.memberNodeId");
228
				String nodeSynchronize = (String)request.getParameter("dataone.nodeSynchronize");
229
				String nodeReplicate = (String)request.getParameter("dataone.nodeReplicate");
230
				String subject = (String)request.getParameter("dataone.subject");
231
				String certpath = (String)request.getParameter("D1Client.certificate.file");
232
				
233
				// the synch schedule
234
				String year = (String) request.getParameter("dataone.nodeSynchronization.schedule.year");
235
				String mon = (String) request.getParameter("dataone.nodeSynchronization.schedule.mon");
236
				String mday = (String) request.getParameter("dataone.nodeSynchronization.schedule.mday");
237
				String wday = (String) request.getParameter("dataone.nodeSynchronization.schedule.wday");
238
				String hour = (String) request.getParameter("dataone.nodeSynchronization.schedule.hour");
239
				String min = (String) request.getParameter("dataone.nodeSynchronization.schedule.min");
240
				String sec = (String) request.getParameter("dataone.nodeSynchronization.schedule.sec");
241
				
242
				boolean synchronize = false;
243
				if (nodeSynchronize != null) {
244
					synchronize = Boolean.parseBoolean(nodeSynchronize);
245
				}
246
				boolean replicate = false;
247
				if (nodeReplicate != null) {
248
					replicate = Boolean.parseBoolean(nodeReplicate);
249
				}
250
				
251
				// process the values, checking for nulls etc..
252
				if (nodeName == null ) {
253
					validationErrors.add("nodeName cannot be null");
254
				} else {
255
					
256
					PropertyService.setPropertyNoPersist("dataone.nodeName", nodeName);
257
					PropertyService.setPropertyNoPersist("dataone.nodeDescription", nodeDescription);
258
					PropertyService.setPropertyNoPersist("dataone.memberNodeId", memberNodeId);
259
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronize", Boolean.toString(synchronize));
260
					PropertyService.setPropertyNoPersist("dataone.nodeReplicate", Boolean.toString(replicate));
261
					PropertyService.setPropertyNoPersist("dataone.subject", subject);
262
					PropertyService.setPropertyNoPersist("D1Client.certificate.file", certpath);
263
					
264
					// the synch schedule
265
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.year", year);
266
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mon", mon);
267
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mday", mday);
268
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.wday", wday);
269
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.hour", hour);
270
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.min", min);
271
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.sec", sec);
272
					
273
					PropertyService.persistProperties();
274

    
275
					// write the backup properties to a location outside the
276
					// application directories so they will be available after
277
					// the next upgrade
278
					PropertyService.persistMainBackupProperties();
279
					
280
			        // Register as a DataONE Member Node
281
					if (replicate) {
282
						registerDataONEMemberNode();
283
					}
284
				}
285
			} catch (GeneralPropertyException gpe) {
286
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
287
						+ "processing system properties page: " + gpe.getMessage();
288
				logMetacat.error(errorMessage);
289
				processingErrors.add(errorMessage);
290
			}
291

    
292
			try {
293
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
294
					RequestUtil.clearRequestMessages(request);
295
					RequestUtil.setRequestFormErrors(request, validationErrors);
296
					RequestUtil.setRequestErrors(request, processingErrors);
297
					RequestUtil.forwardRequest(request, response, "/admin", null);
298
				} else {
299
					// Now that the options have been set, change the
300
					// 'dataoneConfigured' option to 'true'
301
					PropertyService.setProperty("configutil.dataoneConfigured",
302
							PropertyService.CONFIGURED);
303
					
304
					// Reload the main metacat configuration page
305
					processingSuccess.add("DataONE successfully configured");
306
					RequestUtil.clearRequestMessages(request);
307
					RequestUtil.setRequestSuccess(request, processingSuccess);
308
					RequestUtil.forwardRequest(request, response, 
309
							"/admin?configureType=configure&processForm=false", null);
310
				}
311
			} catch (MetacatUtilException mue) {
312
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing geoservices "
313
						+ "geoservices page: " + mue.getMessage());
314
			} catch (GeneralPropertyException gpe) {
315
				throw new AdminException("D1Admin.configureDataONE - problem with properties while "
316
						+ "processing geoservices configuration page: " + gpe.getMessage());
317
			}
318
		}
319
	}
320
	
321
	/**
322
	 * Register as a member node on the DataONE network.  The node description is
323
	 * retrieved from the getCapabilities() service, and so this should only be called
324
	 * after the properties have been properly set up in Metacat.
325
	 */
326
	private void registerDataONEMemberNode() {
327
        CNode cn;
328
        try {
329
            logMetacat.debug("Get the Node description.");
330
            Node node = MNodeService.getInstance(null).getCapabilities();
331
            logMetacat.debug("Setting client certificate location.");
332
            String mnCertificatePath = PropertyService.getProperty("D1Client.certificate.file");
333
            CertificateManager.getInstance().setCertificateLocation(mnCertificatePath);
334
            cn = D1Client.getCN();
335
            // check if this is new or an update
336
            boolean update = false;
337
            NodeList nodes = cn.listNodes();
338
            for (Node n: nodes.getNodeList()) {
339
            	if (n.getIdentifier().getValue().equals(node.getIdentifier().getValue())) {
340
            		update = true;
341
            		break;
342
            	}
343
            }
344
            // Session is null, because the libclient code automatically sets up an
345
            // SSL session for us using the client certificate provided
346
            Session session = null;
347
            if (update) {
348
            	logMetacat.debug("Updating node with DataONE. " + cn.getNodeBaseServiceUrl());
349
	            boolean result = cn.updateNodeCapabilities(session, node.getIdentifier(), node);
350
            } else {
351
	            logMetacat.debug("Registering node with DataONE. " + cn.getNodeBaseServiceUrl());
352
	            NodeReference mnodeRef = cn.register(session, node);
353
	            // save this assigned node id
354
	            PropertyService.setProperty("dataone.memberNodeId", mnodeRef.getValue());
355
	            
356
            }
357
        } catch (ServiceFailure e) {
358
            logMetacat.warn("Could not register as node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
359
        } catch (NotFound e) {
360
            logMetacat.warn("Could not register as node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
361
        } catch (NotImplemented e) {
362
            logMetacat.warn("Could not register as node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
363
        } catch (NotAuthorized e) {
364
            logMetacat.warn("Could not register as node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
365
        } catch (InvalidRequest e) {
366
            logMetacat.warn("Could not register as node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
367
        } catch (IdentifierNotUnique e) {
368
            logMetacat.warn("Could not register as node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
369
        } catch (PropertyNotFoundException e) {
370
            logMetacat.warn("Could not find the location for client certificates: " + e.getMessage());
371
        } catch (GeneralPropertyException e) {
372
            logMetacat.warn("Could not set the assigned node id: " + e.getMessage());
373
		}
374
	}
375

    
376
	/**
377
	 * Validate the most important configuration options submitted by the user.
378
	 * 
379
	 * @return a vector holding error message for any fields that fail
380
	 *         validation.
381
	 */
382
	protected Vector<String> validateOptions(HttpServletRequest request) {
383
		Vector<String> errorVector = new Vector<String>();
384

    
385
		// TODO MCD validate options.
386

    
387
		return errorVector;
388
	}
389
}
(4-4/12)