Revision 1020
Added by Jing Tao over 22 years ago
src/edu/ucsb/nceas/metacat/MetacatReplication.java | ||
---|---|---|
83 | 83 |
HttpServletResponse response) |
84 | 84 |
throws ServletException, IOException |
85 | 85 |
{ |
86 |
PrintWriter out = response.getWriter(); |
|
86 |
//PrintWriter out = response.getWriter(); |
|
87 |
//ServletOutputStream outPut = response.getOutputStream(); |
|
87 | 88 |
Hashtable params = new Hashtable(); |
88 |
Enumeration paramlist = request.getParameterNames();
|
|
89 |
Enumeration paramlist = request.getParameterNames(); |
|
89 | 90 |
|
91 |
//First check if administrate conifgure the replcation feature on |
|
92 |
//This configuration is set in build.xml file |
|
93 |
//If it is not on, reject any request. |
|
94 |
if (!(util.getOption("replication")).equals("on")) |
|
95 |
{ |
|
96 |
(response.getWriter()).println("MetaCat is not set to handle replication"); |
|
97 |
(response.getWriter()).close(); |
|
98 |
return; |
|
99 |
} |
|
100 |
|
|
90 | 101 |
// NOT NEEDED - doesn't provide enough security because of possible IP spoofing |
91 | 102 |
// REPLACED with running replication comminications over HTTPS |
92 | 103 |
// String requestingServerIP = request.getRemoteAddr(); |
... | ... | |
124 | 135 |
e.getMessage() ); |
125 | 136 |
return; |
126 | 137 |
} |
127 |
|
|
138 |
if ( action.equals("readdata") ) |
|
139 |
{ |
|
140 |
ServletOutputStream out=response.getOutputStream(); |
|
141 |
//to get the data file. |
|
142 |
handleGetDataFileRequest(out, params, response); |
|
143 |
out.close(); |
|
144 |
} |
|
145 |
else |
|
146 |
{ |
|
147 |
PrintWriter out = response.getWriter(); |
|
128 | 148 |
if ( action.equals("stop") ) { |
129 | 149 |
//stop the replication server |
130 | 150 |
replicationDaemon.cancel(); |
131 | 151 |
replicationDaemon = new Timer(true); |
132 | 152 |
out.println("Replication Handler Stopped"); |
133 | 153 |
MetacatReplication.replLog("deltaT handler stopped"); |
154 |
|
|
134 | 155 |
|
135 | 156 |
} else if ( action.equals("start") ) { |
157 |
|
|
136 | 158 |
//start the replication server |
137 | 159 |
int rate; |
138 | 160 |
if ( params.containsKey("rate") ) { |
... | ... | |
154 | 176 |
out.println("Replication Handler Started"); |
155 | 177 |
MetacatReplication.replLog("deltaT handler started with rate=" + |
156 | 178 |
rate + " seconds"); |
179 |
|
|
157 | 180 |
|
158 | 181 |
} else if ( action.equals("getall") ) { |
159 | 182 |
//updates this server exactly once |
... | ... | |
162 | 185 |
out.println("<html><body>\"Get All\" Done</body></html>"); |
163 | 186 |
|
164 | 187 |
} else if ( action.equals("forcereplicate") ) { |
188 |
//read a specific docid from remote host, and store it into local host |
|
165 | 189 |
handleForceReplicateRequest(out, params, response); |
166 | 190 |
|
167 | 191 |
} else if ( action.equals("update") ) { |
... | ... | |
173 | 197 |
//note that this could be replaced by a call to metacatServlet |
174 | 198 |
//handleGetDocumentAction(). |
175 | 199 |
handleGetDocumentRequest(out, params, response); |
176 |
|
|
177 | 200 |
} else if ( action.equals("getlock") ) { |
178 | 201 |
handleGetLockRequest(out, params, response); |
179 | 202 |
|
... | ... | |
191 | 214 |
} |
192 | 215 |
|
193 | 216 |
out.close(); |
217 |
}//else |
|
194 | 218 |
} |
195 | 219 |
|
196 | 220 |
/** |
... | ... | |
361 | 385 |
} |
362 | 386 |
|
363 | 387 |
/** |
364 |
* when a forcereplication request comes in, this method sends a read request |
|
365 |
* to the requesting server for the specified docid. |
|
388 |
* when a forcereplication request comes in, local host sends a read request |
|
389 |
* to the requesting server (remote server) for the specified docid. |
|
390 |
* Then store it in local database. |
|
366 | 391 |
*/ |
367 | 392 |
private void handleForceReplicateRequest(PrintWriter out, Hashtable params, |
368 | 393 |
HttpServletResponse response) |
... | ... | |
513 | 538 |
} |
514 | 539 |
|
515 | 540 |
} |
541 |
|
|
542 |
/** |
|
543 |
* Sends a datafile to a remote host |
|
544 |
*/ |
|
545 |
private void handleGetDataFileRequest(ServletOutputStream outPut, |
|
546 |
Hashtable params, HttpServletResponse response) |
|
547 |
|
|
548 |
{ |
|
549 |
String filepath = util.getOption("datafilepath"); |
|
550 |
String docId = ((String[])(params.get("docid")))[0]; |
|
516 | 551 |
|
552 |
if(!filepath.endsWith("/")) |
|
553 |
{ |
|
554 |
filepath += "/"; |
|
555 |
} |
|
556 |
String filename = filepath + docId; //MIME type |
|
557 |
System.out.println("filename: "+filename); |
|
558 |
String contentType = null;//getServletContext().getMimeType(filename); |
|
559 |
System.out.println("after getMimeType"); |
|
560 |
if (contentType == null) |
|
561 |
{ |
|
562 |
if (filename.endsWith(".xml")) |
|
563 |
{ |
|
564 |
contentType="text/xml"; |
|
565 |
} |
|
566 |
else if (filename.endsWith(".css")) |
|
567 |
{ |
|
568 |
contentType="text/css"; |
|
569 |
} |
|
570 |
else if (filename.endsWith(".dtd")) |
|
571 |
{ |
|
572 |
contentType="text/plain"; |
|
573 |
} |
|
574 |
else if (filename.endsWith(".xsd")) |
|
575 |
{ |
|
576 |
contentType="text/xml"; |
|
577 |
} |
|
578 |
else if (filename.endsWith("/")) |
|
579 |
{ |
|
580 |
contentType="text/html"; |
|
581 |
} |
|
582 |
else |
|
583 |
{ |
|
584 |
File f = new File(filename); |
|
585 |
if ( f.isDirectory() ) |
|
586 |
{ |
|
587 |
contentType="text/html"; |
|
588 |
} |
|
589 |
else |
|
590 |
{ |
|
591 |
contentType="application/octet-stream"; |
|
592 |
} |
|
593 |
} |
|
594 |
} |
|
595 |
System.out.println("contentye: "+contentType); |
|
596 |
response.setContentType(contentType); |
|
597 |
// if we decide to use "application/octet-stream" for all data returns |
|
598 |
// response.setContentType("application/octet-stream"); |
|
599 |
FileInputStream fin = null; |
|
600 |
try |
|
601 |
{ |
|
602 |
System.out.println("in try"); |
|
603 |
System.out.println(" after get output stream"); |
|
604 |
fin = new FileInputStream(filename); |
|
605 |
System.out.println("here"); |
|
606 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
|
607 |
int b = fin.read(buf); |
|
608 |
while (b != -1) |
|
609 |
{ |
|
610 |
System.out.println("in while loop"); |
|
611 |
outPut.write(buf, 0, b); |
|
612 |
b = fin.read(buf); |
|
613 |
} |
|
614 |
System.out.println(" before close"); |
|
615 |
fin.close(); |
|
616 |
System.out.println(" after close"); |
|
617 |
} |
|
618 |
catch(Exception e) |
|
619 |
{ |
|
620 |
System.out.println("error getting data file from MetacatReplication." + |
|
621 |
"handlGetDataFileRequest " + e.getMessage()); |
|
622 |
e.printStackTrace(System.out); |
|
623 |
} |
|
624 |
|
|
625 |
} |
|
626 |
|
|
627 |
|
|
517 | 628 |
/** |
518 | 629 |
* Sends a document to a remote host |
519 | 630 |
*/ |
... | ... | |
549 | 660 |
* <!ELEMENT replication (server, updates)> |
550 | 661 |
* <!ELEMENT server (#PCDATA)> |
551 | 662 |
* <!ELEMENT updates ((updatedDocument | deleteDocument)*)> |
552 |
* <!ELEMENT updatedDocument (docid, rev)> |
|
553 |
* <!ELEMENT deletedDocument (docid, rev)> |
|
663 |
* <!ELEMENT updatedDocument (docid, rev, datafile*)>
|
|
664 |
* <!ELEMENT deletedDocument (docid, rev, datafile*)>
|
|
554 | 665 |
* <!ELEMENT docid (#PCDATA)> |
555 | 666 |
* <!ELEMENT rev (#PCDATA)> |
667 |
* <!ELEMENT datafile (#PCDATA)> |
|
556 | 668 |
* note that the rev in deletedDocument is always empty. I just left |
557 | 669 |
* it in there to make the parser implementation easier. |
558 | 670 |
*/ |
... | ... | |
588 | 700 |
pstmt.execute(); |
589 | 701 |
ResultSet rs = pstmt.getResultSet(); |
590 | 702 |
boolean tablehasrows = rs.next(); |
591 |
while(tablehasrows) |
|
703 |
//If metacat configed not to replicate data file |
|
704 |
if (!(util.getOption("replicationdata")).equals("on")) |
|
592 | 705 |
{ |
593 |
String recordDoctype = rs.getString(3); |
|
594 |
if(!recordDoctype.equals("BIN")) |
|
595 |
{ //don't replicate data files |
|
596 |
Vector packagedoctypes = MetaCatUtil.getOptionList( |
|
706 |
while(tablehasrows) |
|
707 |
{ |
|
708 |
String recordDoctype = rs.getString(3); |
|
709 |
if(!recordDoctype.equals("BIN")) |
|
710 |
{ //don't replicate data files |
|
711 |
Vector packagedoctypes = MetaCatUtil.getOptionList( |
|
597 | 712 |
MetaCatUtil.getOption("packagedoctype")); |
598 |
if(!packagedoctypes.contains(recordDoctype)) |
|
599 |
{ //if this is a package file, put it at the end |
|
600 |
//because if a package file is read before all of the files it |
|
601 |
//refers to are loaded then there is an error |
|
602 |
doclist.append("<updatedDocument>"); |
|
603 |
doclist.append("<docid>").append(rs.getString(1)); |
|
604 |
doclist.append("</docid><rev>").append(rs.getInt(2)); |
|
605 |
doclist.append("</rev>"); |
|
606 |
doclist.append("</updatedDocument>"); |
|
607 |
} |
|
608 |
else |
|
609 |
{ //the package files are saved to be put into the xml later. |
|
610 |
Vector v = new Vector(); |
|
611 |
v.add(new String(rs.getString(1))); |
|
612 |
v.add(new Integer(rs.getInt(2))); |
|
613 |
packageFiles.add(new Vector(v)); |
|
614 |
} |
|
615 |
} |
|
616 |
tablehasrows = rs.next(); |
|
713 |
if(!packagedoctypes.contains(recordDoctype)) |
|
714 |
{ //if this is a package file, put it at the end |
|
715 |
//because if a package file is read before all of the files it |
|
716 |
//refers to are loaded then there is an error |
|
717 |
doclist.append("<updatedDocument>"); |
|
718 |
doclist.append("<docid>").append(rs.getString(1)); |
|
719 |
doclist.append("</docid><rev>").append(rs.getInt(2)); |
|
720 |
doclist.append("</rev>"); |
|
721 |
doclist.append("</updatedDocument>"); |
|
722 |
} |
|
723 |
else |
|
724 |
{ //the package files are saved to be put into the xml later. |
|
725 |
Vector v = new Vector(); |
|
726 |
v.add(new String(rs.getString(1))); |
|
727 |
v.add(new Integer(rs.getInt(2))); |
|
728 |
packageFiles.add(new Vector(v)); |
|
729 |
} |
|
730 |
}//if |
|
731 |
tablehasrows = rs.next(); |
|
732 |
}//while |
|
733 |
}//if |
|
734 |
else //metacat should send data file too |
|
735 |
{ |
|
736 |
|
|
617 | 737 |
} |
618 | 738 |
|
619 | 739 |
pstmt = conn.prepareStatement(delsql.toString()); |
Also available in: Unified diff
Figure out the configuration issue about replication. If metacat was configured to set replication off in build.xml, it will reject replication.
Adding a method named handleGetDataFileRequest. User can download data file from replcication now.