Project

General

Profile

1 5014 daigle
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements replication for metacat
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Chad Berkley
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-03-25 13:41:15 -0800 (Wed, 25 Mar 2009) $'
10
 * '$Revision: 4861 $'
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.replication;
28
29 5755 leinfelder
import java.io.IOException;
30
import java.io.OutputStream;
31
import java.io.PrintWriter;
32
import java.util.Enumeration;
33
import java.util.Hashtable;
34 5014 daigle
35 5755 leinfelder
import javax.servlet.ServletConfig;
36
import javax.servlet.ServletException;
37
import javax.servlet.http.HttpServlet;
38
import javax.servlet.http.HttpServletRequest;
39
import javax.servlet.http.HttpServletResponse;
40
import javax.servlet.http.HttpSession;
41 5014 daigle
42 5755 leinfelder
import org.apache.log4j.Logger;
43
44 5014 daigle
import edu.ucsb.nceas.metacat.service.ServiceService;
45
import edu.ucsb.nceas.metacat.service.SessionService;
46
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
47
import edu.ucsb.nceas.metacat.shared.ServiceException;
48
import edu.ucsb.nceas.metacat.util.AuthUtil;
49
import edu.ucsb.nceas.metacat.util.SessionData;
50
51
public class ReplicationServlet extends HttpServlet {
52
53
	private static final long serialVersionUID = -2898600143193513155L;
54
55
	private static Logger logReplication = Logger.getLogger("ReplicationLogging");
56
	private static Logger logMetacat = Logger.getLogger(ReplicationServlet.class);
57
58
	/**
59
	 * Initialize the servlet by creating appropriate database connections
60
	 */
61
	public void init(ServletConfig config) throws ServletException {
62
63
		try {
64
			// Register preliminary services
65
			ServiceService.registerService("ReplicationService", ReplicationService
66
					.getInstance());
67
68
		} catch (ServiceException se) {
69
			String errorMessage = "ReplicationServlet.init - Service problem while intializing Replication Servlet: "
70
					+ se.getMessage();
71
			logMetacat.error("ReplicationServlet.init - " + ReplicationService.METACAT_REPL_ERROR_MSG);
72
			logReplication.error(errorMessage);
73
			throw new ServletException(errorMessage);
74
		}
75
	}
76
77
	public void destroy() {
78
		//ServiceService.
79
	}
80
81
	public void doGet(HttpServletRequest request, HttpServletResponse response)
82
			throws ServletException, IOException {
83
		// Process the data and send back the response
84
		handleGetOrPost(request, response);
85
	}
86
87
	public void doPost(HttpServletRequest request, HttpServletResponse response)
88
			throws ServletException, IOException {
89
		// Process the data and send back the response
90
		handleGetOrPost(request, response);
91
	}
92
93
	private void handleGetOrPost(HttpServletRequest request, HttpServletResponse response)
94
			throws ServletException, IOException {
95 5755 leinfelder
		PrintWriter out = null;
96 5014 daigle
		Hashtable<String, String[]> params = new Hashtable<String, String[]>();
97
		Enumeration<String> paramlist = request.getParameterNames();
98
99
		while (paramlist.hasMoreElements()) {
100
			String name = (String) paramlist.nextElement();
101
			String[] value = request.getParameterValues(name);
102
			params.put(name, value);
103
		}
104
105
		String action = "";
106
		if (!params.isEmpty() && params.get("action") != null) {
107
			action = ((String[]) params.get("action"))[0];
108
		}
109
		String server = null;
110
111
		try {
112
			// check if the server is included in the list of replicated servers
113
			if (!action.equals("servercontrol") && !action.equals("stop")
114
					&& !action.equals("start") && !action.equals("getall")) {
115
116
				server = ((String[]) params.get("server"))[0];
117
				if (ReplicationService.getServerCodeForServerName(server) == 0) {
118
					logReplication.debug("ReplicationServlet.handleGetOrPost - Action \"" + action + "\" rejected for server: "
119
							+ server);
120
					return;
121
				} else {
122
					logReplication.debug("ReplicationServlet.handleGetOrPost - Action \"" + action + "\" accepted for server: "
123
							+ server);
124
				}
125
			} else {
126
				// start, stop, getall and servercontrol need to check if user is administor
127
				HttpSession sess = request.getSession(true);
128
				SessionData sessionData = null;
129
				String sess_id = "";
130
				String username = "";
131
				String[] groupnames = { "" };
132
133
				if (params.containsKey("sessionid")) {
134
					sess_id = ((String[]) params.get("sessionid"))[0];
135
					logReplication.info("ReplicationServlet.handleGetOrPost - in has sessionid " + sess_id);
136 5374 berkley
					if (SessionService.getInstance().isSessionRegistered(sess_id)) {
137 5014 daigle
						logReplication.info("ReplicationServlet.handleGetOrPost - find the id " + sess_id + " in hash table");
138 5374 berkley
						sessionData = SessionService.getInstance().getRegisteredSession(sess_id);
139 5014 daigle
					}
140
				}
141
				if (sessionData == null) {
142 5070 daigle
					sessionData = new SessionData(sess.getId(),
143
							(String) sess.getAttribute("username"),
144
							(String[]) sess.getAttribute("groups"),
145
							(String) sess.getAttribute("password"),
146
							(String) sess.getAttribute("name"));
147 5014 daigle
				}
148
149
				username = sessionData.getUserName();
150
				logReplication.warn("ReplicationServlet.handleGetOrPost - The user name from session is: " + username);
151
				groupnames = sessionData.getGroupNames();
152
				if (!AuthUtil.isAdministrator(username, groupnames)) {
153
					out = response.getWriter();
154
					out.print("<error>");
155
					out.print("The user \"" + username
156
							+ "\" is not authorized for this action.");
157
					out.print("</error>");
158
					out.close();
159
					logReplication.warn("ReplicationServlet.handleGetOrPost - The user \"" + username
160
							+ "\" is not authorized for this action: " + action);
161
					return;
162
				}
163
164
			}// else
165
166
			if (action.equals("readdata")) {
167
				OutputStream outStream = response.getOutputStream();
168
				//to get the data file.
169
				ReplicationService.handleGetDataFileRequest(outStream, params, response);
170
				outStream.close();
171
			} else if (action.equals("forcereplicatedatafile")) {
172
				//read a specific docid from remote host, and store it into local host
173
				ReplicationService.handleForceReplicateDataFileRequest(params, request);
174
			} else if (action.equals("stop")) {
175
				// stop the replication server
176
				ReplicationService.getInstance().stopReplication();
177 5755 leinfelder
				out = response.getWriter();
178 5014 daigle
				out.println("Replication Handler Stopped");
179
			} else if (action.equals("start")) {
180
				ReplicationService.getInstance().startReplication(params);
181 5755 leinfelder
				out = response.getWriter();
182 5014 daigle
				out.println("Replication Handler Started");
183
			} else if (action.equals("getall")) {
184
				ReplicationService.getInstance().runOnce();
185
				response.setContentType("text/html");
186 5755 leinfelder
				out = response.getWriter();
187 5014 daigle
				out.println("<html><body>\"Get All\" Done</body></html>");
188
			} else if (action.equals("forcereplicate")) {
189
				// read a specific docid from remote host, and store it into
190
				// local host
191 5755 leinfelder
				ReplicationService.handleForceReplicateRequest(params, response,
192 5014 daigle
						request);
193
			} else if (action.equals("forcereplicatedelete")) {
194
				// read a specific docid from remote host, and store it into
195
				// local host
196 5755 leinfelder
				ReplicationService.handleForceReplicateDeleteRequest(params,
197 5014 daigle
						response, request);
198
			} else if (action.equals("update")) {
199
				// request an update list from the server
200 5755 leinfelder
				ReplicationService.handleUpdateRequest(params, response);
201 5014 daigle
			} else if (action.equals("read")) {
202
				// request a specific document from the server
203
				// note that this could be replaced by a call to metacatServlet
204
				// handleGetDocumentAction().
205 5755 leinfelder
				ReplicationService.handleGetDocumentRequest(params, response);
206 5014 daigle
			} else if (action.equals("getlock")) {
207 5755 leinfelder
				ReplicationService.handleGetLockRequest(params, response);
208 5014 daigle
			} else if (action.equals("getdocumentinfo")) {
209 5755 leinfelder
				ReplicationService.handleGetDocumentInfoRequest(params, response);
210 6118 leinfelder
			} else if (action.equals("getsystemmetadata")) {
211
				ReplicationService.handleGetSystemMetadataRequest(params, response);
212 5014 daigle
			} else if (action.equals("gettime")) {
213 5755 leinfelder
				ReplicationService.handleGetTimeRequest(params, response);
214 5014 daigle
			} else if (action.equals("getcatalog")) {
215 5755 leinfelder
				ReplicationService.handleGetCatalogRequest(params, response, true);
216 5014 daigle
			} else if (action.equals("servercontrol")) {
217 5755 leinfelder
				ReplicationService.handleServerControlRequest(params, response);
218 5014 daigle
			} else if (action.equals("test")) {
219
				response.setContentType("text/html");
220 5755 leinfelder
				out = response.getWriter();
221 5014 daigle
				out.println("<html><body>Test successfully</body></html>");
222
			}
223
224
		} catch (ServiceException e) {
225
			logMetacat.error("ReplicationServlet.handleGetOrPost - " + ReplicationService.METACAT_REPL_ERROR_MSG);
226
			logReplication.error("ReplicationServlet.handleGetOrPost - Error in ReplicationServlet.handleGetOrPost: " + e.getMessage());
227
		} catch (MetacatUtilException mue) {
228
			logMetacat.error("ReplicationServlet.handleGetOrPost - " + ReplicationService.METACAT_REPL_ERROR_MSG);
229
			logReplication.error("ReplicationServlet.handleGetOrPost - Metacat utility error in ReplicationServlet.handleGetOrPost: "
230
							+ mue.getMessage());
231
		} finally {
232 5755 leinfelder
			if (out != null) {
233
				out.close();
234
			}
235 5014 daigle
		}
236
	}
237
}