Project

General

Profile

« Previous | Next » 

Revision 5755

only call response.getWriter() when we are about to send text/xml to the client, otherwise we end of calling both getWriter() and getOutputStream() - resulting in an illegal state.

View differences:

src/edu/ucsb/nceas/metacat/replication/ReplicationService.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.replication;
28 28

  
29
import java.util.*;
29
import java.io.BufferedInputStream;
30
import java.io.BufferedOutputStream;
31
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.FileNotFoundException;
34
import java.io.FileOutputStream;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.InputStreamReader;
38
import java.io.OutputStream;
39
import java.io.StringReader;
40
import java.io.Writer;
41
import java.net.MalformedURLException;
42
import java.net.URL;
43
import java.sql.PreparedStatement;
44
import java.sql.ResultSet;
45
import java.sql.SQLException;
46
import java.text.SimpleDateFormat;
30 47
import java.util.Date;
31
import java.io.*;
32
import java.sql.*;
33
import java.net.*;
34
import java.text.*;
48
import java.util.Hashtable;
49
import java.util.Timer;
50
import java.util.Vector;
35 51

  
36
import javax.servlet.ServletOutputStream;
37
import javax.servlet.http.*;
52
import javax.servlet.http.HttpServletRequest;
53
import javax.servlet.http.HttpServletResponse;
38 54

  
55
import org.apache.log4j.Logger;
56
import org.xml.sax.InputSource;
57
import org.xml.sax.SAXException;
58
import org.xml.sax.XMLReader;
59

  
39 60
import edu.ucsb.nceas.metacat.DocInfoHandler;
40 61
import edu.ucsb.nceas.metacat.DocumentImpl;
41 62
import edu.ucsb.nceas.metacat.DocumentImplWrapper;
42 63
import edu.ucsb.nceas.metacat.EventLog;
64
import edu.ucsb.nceas.metacat.IdentifierManager;
65
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
43 66
import edu.ucsb.nceas.metacat.McdbException;
44 67
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
45 68
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
......
55 78
import edu.ucsb.nceas.metacat.util.DocumentUtil;
56 79
import edu.ucsb.nceas.metacat.util.MetacatUtil;
57 80
import edu.ucsb.nceas.metacat.util.SystemUtil;
58
import edu.ucsb.nceas.metacat.IdentifierManager;
59
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
60 81
import edu.ucsb.nceas.utilities.FileUtil;
61 82
import edu.ucsb.nceas.utilities.GeneralPropertyException;
62 83
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
63 84

  
64
import org.apache.log4j.Logger;
65
import org.xml.sax.*;
66

  
67 85
public class ReplicationService extends BaseService {
68 86

  
69 87
	private static ReplicationService replicationService = null;
......
256 274
	 * servercontrol    delete               server
257 275
	 * servercontrol    list
258 276
	 */
259
	protected static void handleServerControlRequest(PrintWriter out,
277
	protected static void handleServerControlRequest(
260 278
			Hashtable<String, String[]> params, HttpServletResponse response) {
261 279
		String subaction = ((String[]) params.get("subaction"))[0];
262 280
		DBConnection dbConn = null;
......
266 284
		String server = null;
267 285
		String dataReplicate = null;
268 286
		String hub = null;
287
		Writer out = null;
269 288
		try {
289
			response.setContentType("text/xml");
290
			out = response.getWriter();
291
			
270 292
			//conn = util.openDBConnection();
271 293
			dbConn = DBConnectionPool
272 294
					.getDBConnection("MetacatReplication.handleServerControlRequest");
......
303 325
				pstmt.execute();
304 326
				pstmt.close();
305 327
				dbConn.commit();
306
				out.println("Server " + server + " added");
328
				out.write("Server " + server + " added");
307 329
				response.setContentType("text/html");
308
				out.println("<html><body><table border=\"1\">");
309
				out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
310
				out.println("<b>replicate</b></td>");
311
				out.println("<td><b>datareplicate</b></td>");
312
				out.println("<td><b>hub</b></td></tr>");
330
				out.write("<html><body><table border=\"1\">");
331
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
332
				out.write("<b>replicate</b></td>");
333
				out.write("<td><b>datareplicate</b></td>");
334
				out.write("<td><b>hub</b></td></tr>");
313 335
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
314 336
				//increase dbconnection usage
315 337
				dbConn.increaseUsageCount(1);
......
318 340
				ResultSet rs = pstmt.getResultSet();
319 341
				boolean tablehasrows = rs.next();
320 342
				while (tablehasrows) {
321
					out.println("<tr><td>" + rs.getString(2) + "</td><td>");
322
					out.println(rs.getString(3) + "</td><td>");
323
					out.println(rs.getString(4) + "</td><td>");
324
					out.println(rs.getString(5) + "</td><td>");
325
					out.println(rs.getString(6) + "</td></tr>");
343
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
344
					out.write(rs.getString(3) + "</td><td>");
345
					out.write(rs.getString(4) + "</td><td>");
346
					out.write(rs.getString(5) + "</td><td>");
347
					out.write(rs.getString(6) + "</td></tr>");
326 348

  
327 349
					tablehasrows = rs.next();
328 350
				}
329
				out.println("</table></body></html>");
351
				out.write("</table></body></html>");
330 352

  
331 353
				// download certificate with the public key on this server
332 354
				// and import it as a trusted certificate
......
343 365
				pstmt.execute();
344 366
				pstmt.close();
345 367
				dbConn.commit();
346
				out.println("Server " + server + " deleted");
368
				out.write("Server " + server + " deleted");
347 369
				response.setContentType("text/html");
348
				out.println("<html><body><table border=\"1\">");
349
				out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
350
				out.println("<b>replicate</b></td>");
351
				out.println("<td><b>datareplicate</b></td>");
352
				out.println("<td><b>hub</b></td></tr>");
370
				out.write("<html><body><table border=\"1\">");
371
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
372
				out.write("<b>replicate</b></td>");
373
				out.write("<td><b>datareplicate</b></td>");
374
				out.write("<td><b>hub</b></td></tr>");
353 375

  
354 376
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
355 377
				//increase dbconnection usage
......
358 380
				ResultSet rs = pstmt.getResultSet();
359 381
				boolean tablehasrows = rs.next();
360 382
				while (tablehasrows) {
361
					out.println("<tr><td>" + rs.getString(2) + "</td><td>");
362
					out.println(rs.getString(3) + "</td><td>");
363
					out.println(rs.getString(4) + "</td><td>");
364
					out.println(rs.getString(5) + "</td><td>");
365
					out.println(rs.getString(6) + "</td></tr>");
383
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
384
					out.write(rs.getString(3) + "</td><td>");
385
					out.write(rs.getString(4) + "</td><td>");
386
					out.write(rs.getString(5) + "</td><td>");
387
					out.write(rs.getString(6) + "</td></tr>");
366 388
					tablehasrows = rs.next();
367 389
				}
368
				out.println("</table></body></html>");
390
				out.write("</table></body></html>");
369 391

  
370 392
				// list servers in server list
371 393
			} else if (subaction.equals("list")) {
372 394
				response.setContentType("text/html");
373
				out.println("<html><body><table border=\"1\">");
374
				out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
375
				out.println("<b>replicate</b></td>");
376
				out.println("<td><b>datareplicate</b></td>");
377
				out.println("<td><b>hub</b></td></tr>");
395
				out.write("<html><body><table border=\"1\">");
396
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
397
				out.write("<b>replicate</b></td>");
398
				out.write("<td><b>datareplicate</b></td>");
399
				out.write("<td><b>hub</b></td></tr>");
378 400
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
379 401
				pstmt.execute();
380 402
				ResultSet rs = pstmt.getResultSet();
381 403
				boolean tablehasrows = rs.next();
382 404
				while (tablehasrows) {
383
					out.println("<tr><td>" + rs.getString(2) + "</td><td>");
384
					out.println(rs.getString(3) + "</td><td>");
385
					out.println(rs.getString(4) + "</td><td>");
386
					out.println(rs.getString(5) + "</td><td>");
387
					out.println(rs.getString(6) + "</td></tr>");
405
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
406
					out.write(rs.getString(3) + "</td><td>");
407
					out.write(rs.getString(4) + "</td><td>");
408
					out.write(rs.getString(5) + "</td><td>");
409
					out.write(rs.getString(6) + "</td></tr>");
388 410
					tablehasrows = rs.next();
389 411
				}
390
				out.println("</table></body></html>");
412
				out.write("</table></body></html>");
391 413
			} else {
392 414

  
393
				out.println("<error>Unkonwn subaction</error>");
415
				out.write("<error>Unkonwn subaction</error>");
394 416

  
395 417
			}
396 418
			pstmt.close();
......
413 435
			finally {
414 436
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
415 437
			}//finally
438
			if (out != null) {
439
				try {
440
					out.close();
441
				} catch (IOException e) {
442
					logMetacat.error(e.getMessage(), e);
443
				}
444
			}
416 445
		}//finally
417 446

  
418 447
	}
......
483 512
	 * to the requesting server (remote server) for the specified docid. Then
484 513
	 * store it in local database.
485 514
	 */
486
	protected static void handleForceReplicateRequest(PrintWriter out,
515
	protected static void handleForceReplicateRequest(
487 516
			Hashtable<String, String[]> params, HttpServletResponse response,
488 517
			HttpServletRequest request) {
489 518
		String server = ((String[]) params.get("server"))[0]; // the server that
......
691 720
	 * when a forcereplication delete request comes in, local host will delete this
692 721
	 * document
693 722
	 */
694
	protected static void handleForceReplicateDeleteRequest(PrintWriter out,
723
	protected static void handleForceReplicateDeleteRequest(
695 724
			Hashtable<String, String[]> params, HttpServletResponse response,
696 725
			HttpServletRequest request) {
697 726
		String server = ((String[]) params.get("server"))[0]; // the server that
......
868 897
	 * currentdate: the timestamp of the document on the remote server
869 898
	 *
870 899
	 */
871
	protected static void handleGetLockRequest(PrintWriter out,
900
	protected static void handleGetLockRequest(
872 901
			Hashtable<String, String[]> params, HttpServletResponse response) {
873 902

  
874 903
		try {
......
880 909
			int localRevInt = requestDoc.getRev();
881 910
			int remoteRevInt = Integer.parseInt(remoteRev);
882 911

  
912
			// get a writer for sending back to response
913
			response.setContentType("text/xml");
914
			Writer out = response.getWriter();
915
			
883 916
			if (remoteRevInt >= localRevInt) {
884 917
				if (!fileLocks.contains(docid)) { //grant the lock if it is not already locked
885 918
					fileLocks.add(0, docid); //insert at the beginning of the queue Vector
886 919
					//send a message back to the the remote host authorizing the insert
887
					out
888
							.println("<lockgranted><docid>" + docid
920
					out.write("<lockgranted><docid>" + docid
889 921
									+ "</docid></lockgranted>");
890 922
					//          lockThread = new Thread(this);
891 923
					//          lockThread.setPriority(Thread.MIN_PRIORITY);
892 924
					//          lockThread.start();
893 925
					logReplication.info("ReplicationService.handleGetLockRequest - lock granted for " + docid);
894 926
				} else { //deny the lock
895
					out.println("<filelocked><docid>" + docid + "</docid></filelocked>");
927
					out.write("<filelocked><docid>" + docid + "</docid></filelocked>");
896 928
					logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
897 929
							+ "reason: file already locked");
898 930
				}
899 931
			} else {//deny the lock.
900
				out.println("<outdatedfile><docid>" + docid + "</docid></filelocked>");
932
				out.write("<outdatedfile><docid>" + docid + "</docid></filelocked>");
901 933
				logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
902 934
						+ "reason: client has outdated file");
903 935
			}
936
			out.close();
904 937
			//conn.close();
905 938
		} catch (Exception e) {
906 939
			logMetacat.error("ReplicationService.handleGetLockRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
......
917 950
	 *                  user_updated, home_server, public_access, rev)/>
918 951
	 * all of the subelements of document info are #PCDATA
919 952
	 */
920
	protected static void handleGetDocumentInfoRequest(PrintWriter out,
953
	protected static void handleGetDocumentInfoRequest(
921 954
			Hashtable<String, String[]> params, HttpServletResponse response) {
922 955
		String docid = ((String[]) (params.get("docid")))[0];
923 956
		StringBuffer sb = new StringBuffer();
......
969 1002
			sb.append("</accessControl>");
970 1003

  
971 1004
			sb.append("</documentinfo>");
1005
			// get a writer for sending back to response
972 1006
			response.setContentType("text/xml");
973
			out.println(sb.toString());
1007
			Writer out = response.getWriter();
1008
			out.write(sb.toString());
1009
			out.close();
974 1010

  
975 1011
		} catch (Exception e) {
976 1012
			logMetacat.error("ReplicationService.handleGetDocumentInfoRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
......
1079 1115
	/**
1080 1116
	 * Sends a document to a remote host
1081 1117
	 */
1082
	protected static void handleGetDocumentRequest(PrintWriter out,
1118
	protected static void handleGetDocumentRequest(
1083 1119
			Hashtable<String, String[]> params, HttpServletResponse response) {
1084 1120

  
1085 1121
		String urlString = null;
1086 1122
		String documentPath = null;
1123
		String errorMsg = null;
1087 1124
		try {
1088 1125
			// try to open a https stream to test if the request server's public
1089 1126
			// key
......
1096 1133
			// couldn't pass the test
1097 1134
			if (test.indexOf("successfully") == -1) {
1098 1135
				response.setContentType("text/xml");
1099
				out.println("<error>Couldn't pass the trust test " + test + " </error>");
1136
				Writer out = response.getWriter();
1137
				out.write("<error>Couldn't pass the trust test " + test + " </error>");
1100 1138
				out.close();
1101 1139
				return;
1102 1140
			}
......
1122 1160
			OutputStream outputStream = response.getOutputStream();
1123 1161
			di.readFromFileSystem(outputStream, null, null, documentPath);
1124 1162

  
1125
			// response.setContentType("text/xml");
1126
			// out.print(di.toString(null, null, true));
1127

  
1128 1163
			logReplication.info("ReplicationService.handleGetDocumentRequest - document " + docid + " sent");
1129 1164

  
1165
			// return to avoid continuing to the error reporting section at the end
1166
			return;
1167
			
1130 1168
		} catch (MalformedURLException mue) {
1131 1169
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1132 1170
			logReplication.error("ReplicationService.handleGetDocumentRequest - Url error when getting document from MetacatReplication."
1133 1171
					+ "handlGetDocumentRequest for url: " + urlString + " : "
1134 1172
					+ mue.getMessage());
1135 1173
			// e.printStackTrace(System.out);
1136
			response.setContentType("text/xml");
1137
			out.println("<error>" + mue.getMessage() + "</error>");
1174
			
1138 1175
		} catch (IOException ioe) {
1139 1176
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1140 1177
			logReplication.error("ReplicationService.handleGetDocumentRequest - I/O error when getting document from MetacatReplication."
1141 1178
					+ "handlGetDocumentRequest for file: " + documentPath + " : "
1142 1179
					+ ioe.getMessage());
1143
			// e.printStackTrace(System.out);
1144
			response.setContentType("text/xml");
1145
			out.println("<error>" + ioe.getMessage() + "</error>");
1180
			errorMsg = ioe.getMessage();
1146 1181
		} catch (PropertyNotFoundException pnfe) {
1147 1182
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1148 1183
			logReplication
......
1152 1187
							+ " : "
1153 1188
							+ pnfe.getMessage());
1154 1189
			// e.printStackTrace(System.out);
1155
			response.setContentType("text/xml");
1156
			out.println("<error>" + pnfe.getMessage() + "</error>");
1190
			errorMsg = pnfe.getMessage();
1157 1191
		} catch (McdbException me) {
1158 1192
			logReplication
1159 1193
					.error("ReplicationService.handleGetDocumentRequest - Document implementation error  getting property when getting document from MetacatReplication."
......
1162 1196
							+ " : "
1163 1197
							+ me.getMessage());
1164 1198
			// e.printStackTrace(System.out);
1165
			response.setContentType("text/xml");
1166
			out.println("<error>" + me.getMessage() + "</error>");
1199
			errorMsg = me.getMessage();
1167 1200
		}
1201
		
1202
		// report any errors if we got here
1203
		response.setContentType("text/xml");
1204
		Writer out = null;
1205
		try {
1206
			response.getWriter();
1207
			out = response.getWriter();
1208
			out.write("<error>" + errorMsg + "</error>");
1209
		} catch (Exception e) {
1210
			logMetacat.error(e.getMessage(), e);
1211
		} finally {
1212
			try {
1213
				out.close();
1214
			} catch (IOException e) {
1215
				logMetacat.error(e.getMessage(), e);
1216
			}
1217
		}
1218
		
1168 1219

  
1169 1220
	}
1170 1221

  
......
1179 1230
	 * in deletedDocument is always empty. I just left it in there to make the
1180 1231
	 * parser implementation easier.
1181 1232
	 */
1182
	protected static void handleUpdateRequest(PrintWriter out, Hashtable<String, String[]> params,
1233
	protected static void handleUpdateRequest(Hashtable<String, String[]> params,
1183 1234
			HttpServletResponse response) {
1184 1235
		// Checked out DBConnection
1185 1236
		DBConnection dbConn = null;
......
1188 1239
		PreparedStatement pstmt = null;
1189 1240
		// Server list to store server info of xml_replication table
1190 1241
		ReplicationServerList serverList = null;
1242
		
1243
		// a writer for response
1244
		Writer out = null;
1191 1245

  
1192 1246
		try {
1247
			// get writer, TODO: encoding?
1248
			response.setContentType("text/xml");
1249
			out = response.getWriter();
1250
			
1193 1251
			// Check out a DBConnection from pool
1194 1252
			dbConn = DBConnectionPool
1195 1253
					.getDBConnection("MetacatReplication.handleUpdateRequest");
......
1201 1259
			String server = ((String[]) params.get("server"))[0];
1202 1260
			// If no servr name in param, return a error
1203 1261
			if (server == null || server.equals("")) {
1204
				response.setContentType("text/xml");
1205
				out.println("<error>Request didn't specify server name</error>");
1262
				out.write("<error>Request didn't specify server name</error>");
1206 1263
				out.close();
1207 1264
				return;
1208 1265
			}//if
......
1218 1275
			//couldn't pass the test
1219 1276
			if (test.indexOf("successfully") == -1) {
1220 1277
			    logReplication.error("Trust test failed.");
1221
				response.setContentType("text/xml");
1222
				out.println("<error>Couldn't pass the trust test</error>");
1278
				out.write("<error>Couldn't pass the trust test</error>");
1223 1279
				out.close();
1224 1280
				return;
1225 1281
			}
......
1228 1284
			// Check if local host configure to replicate xml documents to remote
1229 1285
			// server. If not send back a error message
1230 1286
			if (!serverList.getReplicationValue(server)) {
1231
				response.setContentType("text/xml");
1232
				out
1233
						.println("<error>Configuration not allow to replicate document to you</error>");
1287
				out.write("<error>Configuration not allow to replicate document to you</error>");
1234 1288
				out.close();
1235 1289
				return;
1236 1290
			}//if
......
1388 1442
			logReplication.info("ReplicationService.handleUpdateRequest - doclist: " + doclist.toString());
1389 1443
			pstmt.close();
1390 1444
			//conn.close();
1391
			response.setContentType("text/xml");
1392
			out.println(doclist.toString());
1445
			out.write(doclist.toString());
1393 1446

  
1394 1447
		} catch (Exception e) {
1395 1448
			logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1396 1449
			logReplication.error("ReplicationService.handleUpdateRequest - error in MetacatReplication." + "handleupdaterequest: "
1397 1450
					+ e.getMessage());
1398 1451
			//e.printStackTrace(System.out);
1399
			response.setContentType("text/xml");
1400
			out.println("<error>" + e.getMessage() + "</error>");
1452
			try {
1453
				out.write("<error>" + e.getMessage() + "</error>");
1454
			} catch (IOException e1) {
1455
				logMetacat.error(e1.getMessage(), e1);
1456
			}
1401 1457
		} finally {
1402 1458
			try {
1403 1459
				pstmt.close();
......
1410 1466
			finally {
1411 1467
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1412 1468
			}//finally
1469
			try {
1470
				out.close();
1471
			} catch (IOException e) {
1472
				logMetacat.error(e.getMessage(), e);
1473
			}
1413 1474
		}//finally
1414 1475

  
1415 1476
	}//handlUpdateRequest
......
1462 1523
	 * Returns the xml_catalog table encoded in xml
1463 1524
	 */
1464 1525
	public static String getCatalogXML() {
1465
		return handleGetCatalogRequest(null, null, null, false);
1526
		return handleGetCatalogRequest(null, null, false);
1466 1527
	}
1467 1528

  
1468 1529
	/**
......
1475 1536

  
1476 1537
	 * If printFlag == false then do not print to out.
1477 1538
	 */
1478
	protected static String handleGetCatalogRequest(PrintWriter out,
1539
	protected static String handleGetCatalogRequest(
1479 1540
			Hashtable<String, String[]> params, HttpServletResponse response,
1480 1541
			boolean printFlag) {
1481 1542
		DBConnection dbConn = null;
1482 1543
		int serialNumber = -1;
1483 1544
		PreparedStatement pstmt = null;
1545
		Writer out = null;
1484 1546
		try {
1547
			// get writer, TODO: encoding?
1548
			response.setContentType("text/xml");
1549
			out = response.getWriter();
1550
			
1485 1551
			/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
1486 1552
			                                          "handleGetCatalogRequest");*/
1487 1553
			dbConn = DBConnectionPool
......
1514 1580
			//conn.close();
1515 1581
			if (printFlag) {
1516 1582
				response.setContentType("text/xml");
1517
				out.println(sb.toString());
1583
				out.write(sb.toString());
1518 1584
			}
1519 1585
			pstmt.close();
1520 1586
			return sb.toString();
......
1524 1590
					+ e.getMessage());
1525 1591
			e.printStackTrace(System.out);
1526 1592
			if (printFlag) {
1527
				out.println("<error>" + e.getMessage() + "</error>");
1593
				try {
1594
					out.write("<error>" + e.getMessage() + "</error>");
1595
				} catch (IOException e1) {
1596
					logMetacat.error(e1.getMessage(), e1);
1597
				}
1528 1598
			}
1529 1599
		} finally {
1530 1600
			try {
......
1538 1608
			finally {
1539 1609
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1540 1610
			}//finally
1611
			if (out != null) {
1612
				try {
1613
					out.close();
1614
				} catch (IOException e1) {
1615
					logMetacat.error(e1.getMessage(), e1);
1616
				}
1617
			}
1541 1618
		}//finally
1542 1619

  
1543 1620
		return null;
......
1548 1625
	 * for replication gets rid of any problems with syncronizing clocks
1549 1626
	 * because a time specific to a document is always kept on its home server.
1550 1627
	 */
1551
	protected static void handleGetTimeRequest(PrintWriter out,
1628
	protected static void handleGetTimeRequest(
1552 1629
			Hashtable<String, String[]> params, HttpServletResponse response) {
1553 1630
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
1554 1631
		java.util.Date localtime = new java.util.Date();
1555 1632
		String dateString = formatter.format(localtime);
1633
		
1634
		// get a writer for sending back to response
1556 1635
		response.setContentType("text/xml");
1557

  
1558
		out.println("<timestamp>" + dateString + "</timestamp>");
1636
		Writer out = null;
1637
		try {
1638
			out = response.getWriter();
1639
			out.write("<timestamp>" + dateString + "</timestamp>");
1640
			out.close();
1641
		} catch (IOException e) {
1642
			logMetacat.error(e.getMessage(), e);
1643
		}
1644
		
1559 1645
	}
1560 1646

  
1561 1647
	/**
src/edu/ucsb/nceas/metacat/replication/ReplicationServlet.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.replication;
28 28

  
29
import java.util.*;
30
import java.io.*;
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;
31 34

  
32
import javax.servlet.*;
33
import javax.servlet.http.*;
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;
34 41

  
42
import org.apache.log4j.Logger;
43

  
35 44
import edu.ucsb.nceas.metacat.service.ServiceService;
36 45
import edu.ucsb.nceas.metacat.service.SessionService;
37 46
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
......
39 48
import edu.ucsb.nceas.metacat.util.AuthUtil;
40 49
import edu.ucsb.nceas.metacat.util.SessionData;
41 50

  
42
import org.apache.log4j.Logger;
43

  
44 51
public class ReplicationServlet extends HttpServlet {
45 52

  
46 53
	private static final long serialVersionUID = -2898600143193513155L;
......
85 92

  
86 93
	private void handleGetOrPost(HttpServletRequest request, HttpServletResponse response)
87 94
			throws ServletException, IOException {
88
		PrintWriter out = response.getWriter();
95
		PrintWriter out = null;
89 96
		Hashtable<String, String[]> params = new Hashtable<String, String[]>();
90 97
		Enumeration<String> paramlist = request.getParameterNames();
91 98

  
......
167 174
			} else if (action.equals("stop")) {
168 175
				// stop the replication server
169 176
				ReplicationService.getInstance().stopReplication();
177
				out = response.getWriter();
170 178
				out.println("Replication Handler Stopped");
171 179
			} else if (action.equals("start")) {
172 180
				ReplicationService.getInstance().startReplication(params);
181
				out = response.getWriter();
173 182
				out.println("Replication Handler Started");
174 183
			} else if (action.equals("getall")) {
175 184
				ReplicationService.getInstance().runOnce();
176 185
				response.setContentType("text/html");
186
				out = response.getWriter();
177 187
				out.println("<html><body>\"Get All\" Done</body></html>");
178 188
			} else if (action.equals("forcereplicate")) {
179 189
				// read a specific docid from remote host, and store it into
180 190
				// local host
181
				ReplicationService.handleForceReplicateRequest(out, params, response,
191
				ReplicationService.handleForceReplicateRequest(params, response,
182 192
						request);
183 193
			} else if (action.equals("forcereplicatedelete")) {
184 194
				// read a specific docid from remote host, and store it into
185 195
				// local host
186
				ReplicationService.handleForceReplicateDeleteRequest(out, params,
196
				ReplicationService.handleForceReplicateDeleteRequest(params,
187 197
						response, request);
188 198
			} else if (action.equals("update")) {
189 199
				// request an update list from the server
190
				ReplicationService.handleUpdateRequest(out, params, response);
200
				ReplicationService.handleUpdateRequest(params, response);
191 201
			} else if (action.equals("read")) {
192 202
				// request a specific document from the server
193 203
				// note that this could be replaced by a call to metacatServlet
194 204
				// handleGetDocumentAction().
195
				ReplicationService.handleGetDocumentRequest(out, params, response);				
205
				ReplicationService.handleGetDocumentRequest(params, response);				
196 206
			} else if (action.equals("getlock")) {
197
				ReplicationService.handleGetLockRequest(out, params, response);
207
				ReplicationService.handleGetLockRequest(params, response);
198 208
			} else if (action.equals("getdocumentinfo")) {
199
				ReplicationService.handleGetDocumentInfoRequest(out, params, response);
209
				ReplicationService.handleGetDocumentInfoRequest(params, response);
200 210
			} else if (action.equals("gettime")) {
201
				ReplicationService.handleGetTimeRequest(out, params, response);
211
				ReplicationService.handleGetTimeRequest(params, response);
202 212
			} else if (action.equals("getcatalog")) {
203
				ReplicationService.handleGetCatalogRequest(out, params, response, true);
213
				ReplicationService.handleGetCatalogRequest(params, response, true);
204 214
			} else if (action.equals("servercontrol")) {
205
				ReplicationService.handleServerControlRequest(out, params, response);
215
				ReplicationService.handleServerControlRequest(params, response);
206 216
			} else if (action.equals("test")) {
207 217
				response.setContentType("text/html");
218
				out = response.getWriter();
208 219
				out.println("<html><body>Test successfully</body></html>");
209 220
			}
210 221

  
......
216 227
			logReplication.error("ReplicationServlet.handleGetOrPost - Metacat utility error in ReplicationServlet.handleGetOrPost: "
217 228
							+ mue.getMessage());
218 229
		} finally {
219
			out.close();
230
			if (out != null) {
231
				out.close();
232
			}
220 233
		}
221 234
	}
222 235
}

Also available in: Unified diff