Project

General

Profile

1
/**
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
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

    
35
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

    
42
import org.apache.log4j.Logger;
43

    
44
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
		PrintWriter out = null;
96
		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
					if (SessionService.getInstance().isSessionRegistered(sess_id)) {
137
						logReplication.info("ReplicationServlet.handleGetOrPost - find the id " + sess_id + " in hash table");
138
						sessionData = SessionService.getInstance().getRegisteredSession(sess_id);
139
					}
140
				}
141
				if (sessionData == null) {
142
					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
				}
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
				out = response.getWriter();
178
				out.println("Replication Handler Stopped");
179
			} else if (action.equals("start")) {
180
				ReplicationService.getInstance().startReplication(params);
181
				out = response.getWriter();
182
				out.println("Replication Handler Started");
183
			} else if (action.equals("getall")) {
184
				ReplicationService.getInstance().runOnce();
185
				response.setContentType("text/html");
186
				out = response.getWriter();
187
				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
				ReplicationService.handleForceReplicateRequest(params, response,
192
						request);
193
			} else if (action.equals("forcereplicatesystemmetadata")) {
194
				ReplicationService.handleForceReplicateSystemMetadataRequest(params, response,
195
						request);
196
			} else if (action.equals("forcereplicatedelete")) {
197
				// read a specific docid from remote host, and store it into
198
				// local host
199
				ReplicationService.handleForceReplicateDeleteRequest(params,
200
						response, request);
201
			} else if (action.equals("update")) {
202
				// request an update list from the server
203
				ReplicationService.handleUpdateRequest(params, response);
204
			} else if (action.equals("read")) {
205
				// request a specific document from the server
206
				// note that this could be replaced by a call to metacatServlet
207
				// handleGetDocumentAction().
208
				ReplicationService.handleGetDocumentRequest(params, response);				
209
			} else if (action.equals("getlock")) {
210
				ReplicationService.handleGetLockRequest(params, response);
211
			} else if (action.equals("getdocumentinfo")) {
212
				ReplicationService.handleGetDocumentInfoRequest(params, response);
213
			} else if (action.equals("getsystemmetadata")) {
214
				ReplicationService.handleGetSystemMetadataRequest(params, response);
215
			} else if (action.equals("gettime")) {
216
				ReplicationService.handleGetTimeRequest(params, response);
217
			} else if (action.equals("getcatalog")) {
218
				ReplicationService.handleGetCatalogRequest(params, response, true);
219
			} else if (action.equals("servercontrol")) {
220
				ReplicationService.handleServerControlRequest(params, response);
221
			} else if (action.equals("test")) {
222
				response.setContentType("text/html");
223
				out = response.getWriter();
224
				out.println("<html><body>Test successfully</body></html>");
225
			}
226

    
227
		} catch (ServiceException e) {
228
			logMetacat.error("ReplicationServlet.handleGetOrPost - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
229
			logReplication.error("ReplicationServlet.handleGetOrPost - Error in ReplicationServlet.handleGetOrPost: " + e.getMessage());
230
		} catch (MetacatUtilException mue) {
231
			logMetacat.error("ReplicationServlet.handleGetOrPost - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
232
			logReplication.error("ReplicationServlet.handleGetOrPost - Metacat utility error in ReplicationServlet.handleGetOrPost: "
233
							+ mue.getMessage());
234
		} finally {
235
			if (out != null) {
236
				out.close();
237
			}
238
		}
239
	}
240
}
(8-8/8)