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-17 12:19:22 -0800 (Thu, 17 Nov 2011) $'
10
 * '$Revision: 6669 $'
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.replication.ReplicationService;
42
import edu.ucsb.nceas.metacat.service.SessionService;
43
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
44
import edu.ucsb.nceas.metacat.shared.ServiceException;
45
import edu.ucsb.nceas.metacat.util.AuthUtil;
46
import edu.ucsb.nceas.metacat.util.SessionData;
47

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

    
54
	private static ReplicationAdmin instance = null;
55
	private Logger logMetacat = Logger.getLogger(ReplicationAdmin.class);
56

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

    
62
	}
63

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

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

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

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

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

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

    
176
		// TODO MCD validate options.
177

    
178
		return errorVector;
179
	}
180
}
(11-11/12)