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-12-16 16:24:17 -0800 (Fri, 16 Dec 2011) $'
10
 * '$Revision: 6803 $'
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.BaseException;
40
import org.dataone.service.exceptions.IdentifierNotUnique;
41
import org.dataone.service.exceptions.InvalidRequest;
42
import org.dataone.service.exceptions.InvalidToken;
43
import org.dataone.service.exceptions.NotAuthorized;
44
import org.dataone.service.exceptions.NotFound;
45
import org.dataone.service.exceptions.NotImplemented;
46
import org.dataone.service.exceptions.ServiceFailure;
47
import org.dataone.service.types.v1.Node;
48
import org.dataone.service.types.v1.NodeList;
49
import org.dataone.service.types.v1.NodeReference;
50
import org.dataone.service.types.v1.Session;
51

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

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

    
67
	private static D1Admin instance = null;
68
	private Logger logMetacat = Logger.getLogger(D1Admin.class);
69

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

    
75
	}
76

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

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

    
102
		String processForm = request.getParameter("processForm");
103
		String bypass = request.getParameter("bypass");
104
		String formErrors = (String) request.getAttribute("formErrors");
105

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

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

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

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

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

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

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

    
368
	/**
369
	 * Validate the most important configuration options submitted by the user.
370
	 * 
371
	 * @return a vector holding error message for any fields that fail
372
	 *         validation.
373
	 */
374
	protected Vector<String> validateOptions(HttpServletRequest request) {
375
		Vector<String> errorVector = new Vector<String>();
376

    
377
		// TODO MCD validate options.
378

    
379
		return errorVector;
380
	}
381
}
(4-4/12)