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 14:47:00 -0800 (Thu, 17 Nov 2011) $'
10
 * '$Revision: 6672 $'
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.RequestUtil;
47
import edu.ucsb.nceas.metacat.util.SessionData;
48

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

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

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

    
63
	}
64

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

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

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

    
101
		String action = "";
102
		if (!params.isEmpty() && params.get("action") != null) {
103
			action = ((String[]) params.get("action"))[0];
104
		}
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
		} else {
161
			// Forward the request to the JSP page
162
			RequestUtil.forwardRequest(request, response, "/admin/replication-configuration.jsp", null);
163
		}
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)