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