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: 2012-08-03 14:27:10 -0700 (Fri, 03 Aug 2012) $'
10
 * '$Revision: 7346 $'
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.io.PrintWriter;
31
import java.util.Enumeration;
32
import java.util.Hashtable;
33
import java.util.Vector;
34

    
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.servlet.http.HttpSession;
38

    
39
import org.apache.log4j.Logger;
40

    
41
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
42
import edu.ucsb.nceas.metacat.replication.ReplicationService;
43
import edu.ucsb.nceas.metacat.service.SessionService;
44
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
45
import edu.ucsb.nceas.metacat.shared.ServiceException;
46
import edu.ucsb.nceas.metacat.util.AuthUtil;
47
import edu.ucsb.nceas.metacat.util.RequestUtil;
48
import edu.ucsb.nceas.metacat.util.SessionData;
49

    
50
/**
51
 * Control the display of the database configuration page and the processing
52
 * of the configuration values.
53
 */
54
public class ReplicationAdmin extends MetacatAdmin {
55

    
56
	private static ReplicationAdmin instance = null;
57
	private Logger logMetacat = Logger.getLogger(ReplicationAdmin.class);
58

    
59
	/**
60
	 * private constructor since this is a singleton
61
	 */
62
	private ReplicationAdmin() throws AdminException {
63

    
64
	}
65

    
66
	/**
67
	 * Get the single instance of D1Admin.
68
	 * 
69
	 * @return the single instance of D1Admin
70
	 */
71
	public static ReplicationAdmin getInstance() throws AdminException {
72
		if (instance == null) {
73
			instance = new ReplicationAdmin();
74
		}
75
		return instance;
76
	}
77

    
78
	/**
79
	 * Handle configuration of replication -- pass through to the other handler
80
	 * 
81
	 * @param request
82
	 *            the http request information
83
	 * @param response
84
	 *            the http response to be sent back to the client
85
	 * @throws ServiceException 
86
	 * @throws IOException 
87
	 * @throws MetacatUtilException 
88
	 */
89
	public void handleRequest(HttpServletRequest request,
90
			HttpServletResponse response) throws AdminException, ServiceException, IOException, MetacatUtilException {
91
		
92
		PrintWriter out = null;
93
		Hashtable<String, String[]> params = new Hashtable<String, String[]>();
94
		Enumeration<String> paramlist = request.getParameterNames();
95

    
96
		while (paramlist.hasMoreElements()) {
97
			String name = (String) paramlist.nextElement();
98
			String[] value = request.getParameterValues(name);
99
			params.put(name, value);
100
		}
101

    
102
		String action = "";
103
		if (!params.isEmpty() && params.get("action") != null) {
104
			action = ((String[]) params.get("action"))[0];
105
		}
106
		
107
		// start, stop, getall and servercontrol need to check if user is administrator
108
		HttpSession session = request.getSession(true);
109
		SessionData sessionData = null;
110
		String sessionId = "";
111
		String username = "";
112
		String[] groupnames = { "" };
113

    
114
		if (params.containsKey("sessionid")) {
115
			sessionId = ((String[]) params.get("sessionid"))[0];
116
			logMetacat.info("sessionid " + sessionId);
117
			if (SessionService.getInstance().isSessionRegistered(sessionId)) {
118
				logMetacat.info("Looking up id " + sessionId + " in registered sessions");
119
				sessionData = SessionService.getInstance().getRegisteredSession(sessionId);
120
			}
121
		}
122
		if (sessionData == null) {
123
			sessionData = new SessionData(session.getId(), 
124
					(String) session.getAttribute("username"), 
125
					(String[]) session.getAttribute("groups"),
126
					(String) session.getAttribute("password"), 
127
					(String) session.getAttribute("name"));
128
		}
129

    
130
		username = sessionData.getUserName();
131
		logMetacat.warn("The user name from session is: " + username);
132
		groupnames = sessionData.getGroupNames();
133
		if (!AuthUtil.isAdministrator(username, groupnames)) {
134
			String msg = "The user \"" + username
135
			+ "\" is not authorized for this action: " + action;
136
			out = response.getWriter();
137
			out.print("<error>");
138
			out.print(msg);
139
			out.print("</error>");
140
			out.close();
141
			logMetacat.warn(msg);
142
			return;
143
		}
144
		
145
		if (action.equals("stop")) {
146
			// stop the replication server
147
			ReplicationService.getInstance().stopReplication();
148
			out = response.getWriter();
149
			out.println("Replication Handler Stopped");
150
		} else if (action.equals("start")) {
151
			ReplicationService.getInstance().startReplication(params);
152
			out = response.getWriter();
153
			out.println("Replication Handler Started");
154
		} else if (action.equals("getall")) {
155
			ReplicationService.getInstance().runOnce();
156
			response.setContentType("text/html");
157
			out = response.getWriter();
158
			out.println("<html><body>\"Get All\" Done</body></html>");
159
		} else if (action.equals("servercontrol")) {
160
			ReplicationService.handleServerControlRequest(params, request, response);
161
		} else if (action.equals("resynchSystemMetadata")) {
162
			HazelcastService.getInstance().resynchInThread();
163
			response.setContentType("text/html");
164
			out = response.getWriter();
165
			out.println("<html><body>SystemMetadata resynch has been started</body></html>");
166
		} else {
167
			// Forward the request to the JSP page
168
			RequestUtil.forwardRequest(request, response, "/admin/replication-configuration.jsp", null);
169
		}
170
		
171
	}
172
	
173
	/**
174
	 * Validate the most important configuration options submitted by the user.
175
	 * 
176
	 * @return a vector holding error message for any fields that fail
177
	 *         validation.
178
	 */
179
	protected Vector<String> validateOptions(HttpServletRequest request) {
180
		Vector<String> errorVector = new Vector<String>();
181

    
182
		// TODO MCD validate options.
183

    
184
		return errorVector;
185
	}
186
}
(12-12/13)