86 |
86 |
PrintWriter out = response.getWriter();
|
87 |
87 |
Hashtable params = new Hashtable();
|
88 |
88 |
Enumeration paramlist = request.getParameterNames();
|
89 |
|
String servletAction = null;
|
90 |
89 |
|
91 |
|
String requestingServerIP = request.getRemoteAddr();
|
92 |
|
InetAddress iaddr = InetAddress.getByName(requestingServerIP);
|
93 |
|
String requestingServer = iaddr.getHostName();
|
|
90 |
// NOT NEEDED - doesn't provide enough security because of possible IP spoofing
|
|
91 |
// REPLACED with running replication comminications over HTTPS
|
|
92 |
// String requestingServerIP = request.getRemoteAddr();
|
|
93 |
// InetAddress iaddr = InetAddress.getByName(requestingServerIP);
|
|
94 |
// String requestingServer = iaddr.getHostName();
|
94 |
95 |
|
95 |
|
while (paramlist.hasMoreElements())
|
96 |
|
{
|
|
96 |
while (paramlist.hasMoreElements()) {
|
97 |
97 |
String name = (String)paramlist.nextElement();
|
98 |
98 |
String[] value = request.getParameterValues(name);
|
99 |
99 |
params.put(name, value);
|
100 |
100 |
}
|
101 |
101 |
|
102 |
|
servletAction = ((String[])params.get("action"))[0];
|
|
102 |
String servletAction = ((String[])params.get("action"))[0];
|
|
103 |
String server = ((String[])params.get("server"))[0];
|
103 |
104 |
|
104 |
|
//if the requesting Server is not in the server list
|
105 |
|
//reject this request
|
106 |
|
//this does not protect against IP spoofing but it does
|
107 |
|
//protect against simple URL parameter spoofing.
|
108 |
|
//We need to add an authenticated "replication" user who can perform the
|
109 |
|
//actions listed below in the !servletAction.equals calls.
|
110 |
|
try
|
111 |
|
{
|
112 |
|
if(getServerCode(requestingServer + "%") == 0 &&
|
113 |
|
!servletAction.equals("servercontrol") &&
|
114 |
|
!servletAction.equals("stop") &&
|
115 |
|
!servletAction.equals("start") &&
|
116 |
|
!servletAction.equals("getall"))
|
117 |
|
{
|
118 |
|
System.out.println("action rejected for server: " + requestingServer);
|
|
105 |
try {
|
|
106 |
// check if the server is included in the list of replicated servers
|
|
107 |
if ( getServerCode(server) == 0 &&
|
|
108 |
!servletAction.equals("servercontrol") &&
|
|
109 |
!servletAction.equals("stop") &&
|
|
110 |
!servletAction.equals("start") &&
|
|
111 |
!servletAction.equals("getall") ) {
|
|
112 |
System.out.println("action rejected for server: " + server);
|
119 |
113 |
return;
|
120 |
114 |
}
|
121 |
|
System.out.println("action accepted for server: " + requestingServer);
|
122 |
|
}
|
123 |
|
catch(Exception e)
|
124 |
|
{
|
125 |
|
System.out.println("error in MetacatReplication.handleGetOrPost" +
|
126 |
|
": error authenticating server");
|
|
115 |
System.out.println("action accepted for server: " + server);
|
|
116 |
} catch (Exception e) {
|
|
117 |
System.out.println("Error in MetacatReplication.handleGetOrPost: " +
|
|
118 |
e.getMessage() );
|
127 |
119 |
return;
|
128 |
120 |
}
|
129 |
121 |
|
130 |
|
if(params.containsKey("action"))
|
131 |
|
{
|
132 |
|
if(servletAction.equals("stop"))
|
133 |
|
{ //stop the replication server
|
134 |
|
replicationDaemon.cancel();
|
135 |
|
replicationDaemon = new Timer(true);
|
136 |
|
out.println("Replication Handler Stopped");
|
137 |
|
System.out.println("Replication Handler Stopped");
|
138 |
|
MetacatReplication.replLog("deltaT handler stopped");
|
139 |
|
}
|
140 |
|
else if(servletAction.equals("start"))
|
141 |
|
{ //start the replication server
|
142 |
|
int rate;
|
143 |
|
if(params.containsKey("rate"))
|
144 |
|
{
|
145 |
|
rate = new Integer(
|
146 |
|
new String(((String[])params.get("rate"))[0])).intValue();
|
147 |
|
if(rate < 30)
|
148 |
|
{
|
|
122 |
if ( servletAction.equals("stop") ) {
|
|
123 |
//stop the replication server
|
|
124 |
replicationDaemon.cancel();
|
|
125 |
replicationDaemon = new Timer(true);
|
|
126 |
out.println("Replication Handler Stopped");
|
|
127 |
MetacatReplication.replLog("deltaT handler stopped");
|
|
128 |
|
|
129 |
} else if ( servletAction.equals("start") ) {
|
|
130 |
//start the replication server
|
|
131 |
int rate;
|
|
132 |
if ( params.containsKey("rate") ) {
|
|
133 |
rate = new Integer(
|
|
134 |
new String(((String[])params.get("rate"))[0])).intValue();
|
|
135 |
if(rate < 30) {
|
149 |
136 |
out.println("Replication deltaT rate cannot be less than 30!");
|
150 |
137 |
//deltaT<30 is a timing mess!
|
151 |
138 |
rate = 1000;
|
152 |
|
}
|
153 |
139 |
}
|
154 |
|
else
|
155 |
|
{
|
156 |
|
rate = 1000;
|
157 |
|
}
|
158 |
|
|
159 |
|
out.println("New rate is: " + rate + " seconds.");
|
160 |
|
replicationDaemon.cancel();
|
161 |
|
replicationDaemon = new Timer(true);
|
162 |
|
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(out), 0,
|
163 |
|
rate * 1000);
|
164 |
|
MetacatReplication.replLog("deltaT handler started with rate=" +
|
|
140 |
} else {
|
|
141 |
rate = 1000;
|
|
142 |
}
|
|
143 |
out.println("New rate is: " + rate + " seconds.");
|
|
144 |
replicationDaemon.cancel();
|
|
145 |
replicationDaemon = new Timer(true);
|
|
146 |
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(out), 0,
|
|
147 |
rate * 1000);
|
|
148 |
out.println("Replication Handler Started");
|
|
149 |
MetacatReplication.replLog("deltaT handler started with rate=" +
|
165 |
150 |
rate + " seconds");
|
166 |
|
out.println("Replication Handler Started");
|
167 |
|
System.out.println("Replication Handler Started");
|
168 |
|
}
|
169 |
|
else if(servletAction.equals("getall"))
|
170 |
|
{ //updates this server exactly once
|
171 |
|
replicationDaemon.schedule(new ReplicationHandler(out), 0);
|
172 |
|
response.setContentType("text/html");
|
173 |
|
out.println("<html><body>\"Get All\" Done</body></html>");
|
174 |
|
}
|
175 |
|
else if(servletAction.equals("forcereplicate"))
|
176 |
|
{
|
177 |
|
handleForceReplicateRequest(out, params, response);
|
178 |
|
}
|
179 |
|
else if(servletAction.equals("update"))
|
180 |
|
{ //request an update list from the server
|
181 |
|
handleUpdateRequest(out, params, response);
|
182 |
|
}
|
183 |
|
else if(servletAction.equals("read"))
|
184 |
|
{ //request a specific document from the server
|
185 |
|
//note that this could be replaced by a call to metacatServlet
|
186 |
|
//handleGetDocumentAction().
|
187 |
|
handleGetDocumentRequest(out, params, response);
|
188 |
|
}
|
189 |
|
else if(servletAction.equals("getlock"))
|
190 |
|
{
|
191 |
|
handleGetLockRequest(out, params, response);
|
192 |
|
}
|
193 |
|
else if(servletAction.equals("getdocumentinfo"))
|
194 |
|
{
|
195 |
|
handleGetDocumentInfoRequest(out, params, response);
|
196 |
|
}
|
197 |
|
else if(servletAction.equals("gettime"))
|
198 |
|
{
|
199 |
|
handleGetTimeRequest(out, params, response);
|
200 |
|
}
|
201 |
|
else if(servletAction.equals("getcatalog"))
|
202 |
|
{
|
203 |
|
handleGetCatalogRequest(out, params, response, true);
|
204 |
|
}
|
205 |
|
else if(servletAction.equals("servercontrol"))
|
206 |
|
{
|
207 |
|
handleServerControlRequest(out, params, response);
|
208 |
|
}
|
|
151 |
|
|
152 |
} else if ( servletAction.equals("getall") ) {
|
|
153 |
//updates this server exactly once
|
|
154 |
replicationDaemon.schedule(new ReplicationHandler(out), 0);
|
|
155 |
response.setContentType("text/html");
|
|
156 |
out.println("<html><body>\"Get All\" Done</body></html>");
|
|
157 |
|
|
158 |
} else if ( servletAction.equals("forcereplicate") ) {
|
|
159 |
handleForceReplicateRequest(out, params, response);
|
|
160 |
|
|
161 |
} else if ( servletAction.equals("update") ) {
|
|
162 |
//request an update list from the server
|
|
163 |
handleUpdateRequest(out, params, response);
|
|
164 |
|
|
165 |
} else if ( servletAction.equals("read") ) {
|
|
166 |
//request a specific document from the server
|
|
167 |
//note that this could be replaced by a call to metacatServlet
|
|
168 |
//handleGetDocumentAction().
|
|
169 |
handleGetDocumentRequest(out, params, response);
|
|
170 |
|
|
171 |
} else if ( servletAction.equals("getlock") ) {
|
|
172 |
handleGetLockRequest(out, params, response);
|
|
173 |
|
|
174 |
} else if ( servletAction.equals("getdocumentinfo") ) {
|
|
175 |
handleGetDocumentInfoRequest(out, params, response);
|
|
176 |
|
|
177 |
} else if ( servletAction.equals("gettime") ) {
|
|
178 |
handleGetTimeRequest(out, params, response);
|
|
179 |
|
|
180 |
} else if ( servletAction.equals("getcatalog") ) {
|
|
181 |
handleGetCatalogRequest(out, params, response, true);
|
|
182 |
|
|
183 |
} else if ( servletAction.equals("servercontrol") ) {
|
|
184 |
handleServerControlRequest(out, params, response);
|
|
185 |
}
|
209 |
186 |
|
210 |
|
}
|
211 |
187 |
}
|
212 |
188 |
|
213 |
189 |
/**
|
... | ... | |
224 |
200 |
{
|
225 |
201 |
String subaction = ((String[])params.get("subaction"))[0];
|
226 |
202 |
Connection conn = null;
|
227 |
|
try
|
228 |
|
{
|
229 |
|
conn = MetacatReplication.getDBConnection("MetacatReplication." +
|
230 |
|
"handleServerControlRequest");
|
|
203 |
|
|
204 |
try {
|
|
205 |
conn = util.openDBConnection();
|
231 |
206 |
PreparedStatement pstmt = null;
|
232 |
|
if(subaction.equals("add"))
|
233 |
|
{
|
|
207 |
|
|
208 |
// add server to server list
|
|
209 |
if ( subaction.equals("add") ) {
|
234 |
210 |
String replicate = ((String[])params.get("replicate"))[0];
|
235 |
211 |
String server = ((String[])params.get("server"))[0];
|
236 |
|
pstmt = conn.prepareStatement("insert into xml_replication (server, " +
|
237 |
|
"last_checked, replicate) values ('" + server + "', to_date(" +
|
238 |
|
"'01/01/00', 'MM/DD/YY'), '" + replicate + "')");
|
|
212 |
pstmt = conn.prepareStatement("INSERT INTO xml_replication " +
|
|
213 |
"(server, last_checked, replicate) " +
|
|
214 |
"VALUES ('" + server + "', to_date(" +
|
|
215 |
"'01/01/00', 'MM/DD/YY'), '" +
|
|
216 |
replicate + "')");
|
239 |
217 |
pstmt.execute();
|
240 |
|
out.println("Server says: server " + server + " added");
|
|
218 |
pstmt.close();
|
|
219 |
conn.commit();
|
|
220 |
out.println("Server " + server + " added");
|
241 |
221 |
response.setContentType("text/html");
|
242 |
222 |
out.println("<html><body><table border=\"1\">");
|
243 |
223 |
out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
|
244 |
224 |
out.println("<b>replicate</b></td></tr>");
|
245 |
|
pstmt = conn.prepareStatement("select * from xml_replication");
|
|
225 |
pstmt = conn.prepareStatement("SELECT * FROM xml_replication");
|
246 |
226 |
pstmt.execute();
|
247 |
227 |
ResultSet rs = pstmt.getResultSet();
|
248 |
228 |
boolean tablehasrows = rs.next();
|
249 |
|
while(tablehasrows)
|
250 |
|
{
|
|
229 |
while(tablehasrows) {
|
251 |
230 |
out.println("<tr><td>" + rs.getString(2) + "</td><td>");
|
252 |
231 |
out.println(rs.getString(3) + "</td><td>");
|
253 |
232 |
out.println(rs.getString(4) + "</td></tr>");
|
254 |
233 |
tablehasrows = rs.next();
|
255 |
234 |
}
|
256 |
235 |
out.println("</table></body></html>");
|
257 |
|
}
|
258 |
|
else if(subaction.equals("delete"))
|
259 |
|
{
|
|
236 |
|
|
237 |
// delete server from server list
|
|
238 |
} else if ( subaction.equals("delete") ) {
|
260 |
239 |
String server = ((String[])params.get("server"))[0];
|
261 |
|
pstmt = conn.prepareStatement("delete from xml_replication where " +
|
262 |
|
"server like '" + server + "'");
|
|
240 |
pstmt = conn.prepareStatement("DELETE FROM xml_replication " +
|
|
241 |
"WHERE server LIKE '" + server + "'");
|
263 |
242 |
pstmt.execute();
|
264 |
|
out.println("Server says: server " + server + " deleted");
|
|
243 |
pstmt.close();
|
|
244 |
conn.commit();
|
|
245 |
out.println("Server " + server + " deleted");
|
265 |
246 |
response.setContentType("text/html");
|
266 |
247 |
out.println("<html><body><table border=\"1\">");
|
267 |
248 |
out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
|
268 |
249 |
out.println("<b>replicate</b></td></tr>");
|
269 |
|
pstmt = conn.prepareStatement("select * from xml_replication");
|
|
250 |
pstmt = conn.prepareStatement("SELECT * FROM xml_replication");
|
270 |
251 |
pstmt.execute();
|
271 |
252 |
ResultSet rs = pstmt.getResultSet();
|
272 |
253 |
boolean tablehasrows = rs.next();
|
... | ... | |
278 |
259 |
tablehasrows = rs.next();
|
279 |
260 |
}
|
280 |
261 |
out.println("</table></body></html>");
|
281 |
|
}
|
282 |
|
else if(subaction.equals("list"))
|
283 |
|
{
|
|
262 |
|
|
263 |
// list servers in server list
|
|
264 |
} else if ( subaction.equals("list") ) {
|
284 |
265 |
response.setContentType("text/html");
|
285 |
266 |
out.println("<html><body><table border=\"1\">");
|
286 |
267 |
out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
|
287 |
268 |
out.println("<b>replicate</b></td></tr>");
|
288 |
|
pstmt = conn.prepareStatement("select * from xml_replication");
|
|
269 |
pstmt = conn.prepareStatement("SELECT * FROM xml_replication");
|
289 |
270 |
pstmt.execute();
|
290 |
271 |
ResultSet rs = pstmt.getResultSet();
|
291 |
272 |
boolean tablehasrows = rs.next();
|
292 |
|
while(tablehasrows)
|
293 |
|
{
|
|
273 |
while(tablehasrows) {
|
294 |
274 |
out.println("<tr><td>" + rs.getString(2) + "</td><td>");
|
295 |
275 |
out.println(rs.getString(3) + "</td><td>");
|
296 |
276 |
out.println(rs.getString(4) + "</td></tr>");
|
... | ... | |
300 |
280 |
}
|
301 |
281 |
pstmt.close();
|
302 |
282 |
conn.close();
|
303 |
|
}
|
304 |
|
catch(Exception e)
|
305 |
|
{
|
306 |
|
System.out.println("error in " +
|
|
283 |
|
|
284 |
} catch(Exception e) {
|
|
285 |
System.out.println("Error in " +
|
307 |
286 |
"MetacatReplication.handleServerControlRequest " +
|
308 |
287 |
e.getMessage());
|
309 |
288 |
e.printStackTrace(System.out);
|
... | ... | |
317 |
296 |
private void handleForceReplicateRequest(PrintWriter out, Hashtable params,
|
318 |
297 |
HttpServletResponse response)
|
319 |
298 |
{
|
320 |
|
//System.out.println("in handleforcereplicaterequest");
|
321 |
|
String server = ((String[])params.get("server"))[0];
|
322 |
|
// BUG: it only receives the document from "server" and no need for this check here
|
323 |
|
// if(!(replToServer(server)))
|
324 |
|
// { //do not get the server's new document if we are not replicating from there
|
325 |
|
// return;
|
326 |
|
// }
|
327 |
|
|
328 |
|
//the server that the request came from
|
329 |
|
String docid = ((String[])params.get("docid"))[0];
|
330 |
|
//the docid of the document to get
|
331 |
|
String dbaction = "UPDATE";
|
332 |
|
//default action is update
|
|
299 |
String server = ((String[])params.get("server"))[0]; // the server that
|
|
300 |
String docid = ((String[])params.get("docid"))[0]; // sent the document
|
|
301 |
String dbaction = "UPDATE"; // the default action is UPDATE
|
333 |
302 |
boolean override = false;
|
334 |
303 |
int serverCode = 1;
|
335 |
304 |
|
336 |
|
try
|
337 |
|
{
|
338 |
|
if(params.containsKey("dbaction"))
|
339 |
|
{ //if the url contains a dbaction then the default action is overridden
|
|
305 |
try {
|
|
306 |
//if the url contains a dbaction then the default action is overridden
|
|
307 |
if(params.containsKey("dbaction")) {
|
340 |
308 |
dbaction = ((String[])params.get("dbaction"))[0];
|
341 |
309 |
serverCode = MetacatReplication.getServerCode(server);
|
342 |
310 |
override = true; //we are now overriding the default action
|
343 |
311 |
}
|
344 |
|
MetaCatUtil.debugMessage("action in forcereplicate is: " + dbaction);
|
345 |
|
MetaCatUtil.debugMessage("serverCode in forcereplicate is: " + serverCode);
|
346 |
312 |
MetacatReplication.replLog("force replication request from " + server);
|
347 |
313 |
|
348 |
|
int serverCheckCode = MetacatReplication.getServerCode(server);
|
349 |
|
URL u = new URL("http://" + server + "?action=read&docid=" + docid);
|
350 |
|
MetaCatUtil.debugMessage("sending message: " + u.toString());
|
|
314 |
// sending back read request to server
|
|
315 |
URL u = new URL("https://" + server + "?action=read&docid=" + docid);
|
351 |
316 |
String xmldoc = MetacatReplication.getURLContent(u);
|
352 |
|
MetaCatUtil.debugMessage("document: " + xmldoc);
|
353 |
|
//get the document to write
|
354 |
|
URL docinfourl = new URL("http://" + server +
|
355 |
|
"?action=getdocumentinfo&docid=" +
|
356 |
|
docid);
|
357 |
|
//we need to get the document's info so we can set the correct user
|
358 |
|
//and groups once we get the document and write it to our DB
|
359 |
|
MetaCatUtil.debugMessage("sending message: " + docinfourl.toString());
|
|
317 |
|
|
318 |
// get the document info from server
|
|
319 |
URL docinfourl = new URL("https://" + server +
|
|
320 |
"?action=getdocumentinfo&docid=" + docid);
|
360 |
321 |
String docInfoStr = MetacatReplication.getURLContent(docinfourl);
|
361 |
|
MetaCatUtil.debugMessage("docInfo: " + docInfoStr);
|
|
322 |
|
|
323 |
//dih is the parser for the docinfo xml format
|
362 |
324 |
DocInfoHandler dih = new DocInfoHandler();
|
363 |
|
//dih is the parser for the docinfo xml format
|
364 |
325 |
XMLReader docinfoParser = ReplicationHandler.initParser(dih);
|
365 |
326 |
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
366 |
327 |
Hashtable docinfoHash = dih.getDocInfo();
|
367 |
328 |
String user = (String)docinfoHash.get("user_owner");
|
368 |
|
//String[] groups = null;
|
369 |
|
//right now the user and group are the same.
|
370 |
|
Connection conn = null;
|
371 |
|
|
372 |
|
conn = MetacatReplication.getDBConnection("MetacatReplication." +
|
373 |
|
"handleForceReplicateRequest");
|
374 |
|
|
|
329 |
|
|
330 |
// write the document here
|
|
331 |
System.out.println("Open db connection");
|
|
332 |
Connection conn = util.openDBConnection();
|
|
333 |
System.out.println("DB connection opened");
|
375 |
334 |
DocumentImpl.write(conn, new StringReader(xmldoc), null, dbaction, docid,
|
376 |
335 |
user, null, serverCode, override);
|
|
336 |
conn.close();
|
|
337 |
|
377 |
338 |
MetacatReplication.replLog("document " + docid + " added to DB with " +
|
378 |
339 |
"action " + dbaction);
|
379 |
|
conn.close();
|
|
340 |
} catch(Exception e) {
|
|
341 |
System.out.println("ERROR in MetacatReplication.handleForceReplicate" +
|
|
342 |
"Request(): " + e.getMessage());
|
380 |
343 |
}
|
381 |
|
catch(Exception e)
|
382 |
|
{
|
383 |
|
System.out.println("error in metacatReplication.handleForceReplicate" +
|
384 |
|
"Request: " + e.getMessage());
|
385 |
|
}
|
386 |
344 |
}
|
387 |
345 |
|
388 |
346 |
/**
|
... | ... | |
798 |
756 |
{
|
799 |
757 |
Connection conn = null;
|
800 |
758 |
PreparedStatement pstmt = null;
|
801 |
|
try
|
802 |
|
{
|
803 |
|
conn = MetacatReplication.getDBConnection("MetacatReplication.getServerCode");
|
804 |
|
pstmt = conn.prepareStatement("select serverid from " +
|
805 |
|
"xml_replication where server " +
|
806 |
|
"like '" + server + "'");
|
|
759 |
int serverCode = 0;
|
|
760 |
|
|
761 |
try {
|
|
762 |
|
|
763 |
conn = util.openDBConnection();
|
|
764 |
pstmt = conn.prepareStatement("SELECT serverid FROM xml_replication " +
|
|
765 |
"WHERE server LIKE '" + server + "'");
|
807 |
766 |
pstmt.execute();
|
808 |
767 |
ResultSet rs = pstmt.getResultSet();
|
809 |
768 |
boolean tablehasrows = rs.next();
|
810 |
|
int serverCode = 0;
|
811 |
|
if(tablehasrows)
|
812 |
|
{
|
813 |
|
int ret = rs.getInt(1);
|
|
769 |
if ( tablehasrows ) {
|
|
770 |
serverCode = rs.getInt(1);
|
814 |
771 |
pstmt.close();
|
815 |
772 |
conn.close();
|
816 |
|
return ret;
|
|
773 |
return serverCode;
|
817 |
774 |
}
|
818 |
|
else
|
819 |
|
{
|
|
775 |
|
|
776 |
} catch(Exception e) {
|
|
777 |
throw e;
|
|
778 |
|
|
779 |
} finally {
|
|
780 |
try {
|
820 |
781 |
pstmt.close();
|
821 |
782 |
conn.close();
|
822 |
|
return 0;
|
823 |
|
}
|
|
783 |
} catch(Exception ee) {}
|
824 |
784 |
}
|
825 |
|
catch(Exception e)
|
826 |
|
{
|
827 |
|
throw e;
|
828 |
|
}
|
829 |
|
finally
|
830 |
|
{
|
831 |
|
try
|
832 |
|
{
|
833 |
|
pstmt.close();
|
834 |
|
conn.close();
|
835 |
|
}
|
836 |
|
catch(Exception ee) {}
|
837 |
|
}
|
|
785 |
|
|
786 |
return serverCode;
|
838 |
787 |
}
|
839 |
788 |
|
840 |
789 |
/**
|
changes in replication to use https