Revision 6672
Added by ben leinfelder over 12 years ago
ReplicationService.java | ||
---|---|---|
26 | 26 |
|
27 | 27 |
package edu.ucsb.nceas.metacat.replication; |
28 | 28 |
|
29 |
import java.io.BufferedInputStream; |
|
30 |
import java.io.BufferedOutputStream; |
|
31 | 29 |
import java.io.ByteArrayInputStream; |
32 | 30 |
import java.io.ByteArrayOutputStream; |
33 | 31 |
import java.io.File; |
... | ... | |
376 | 374 |
} |
377 | 375 |
out.write("</table></body></html>"); |
378 | 376 |
|
379 |
// download certificate with the public key on this server |
|
380 |
// and import it as a trusted certificate |
|
381 |
String certURL = ((String[]) params.get("certificate"))[0]; |
|
382 |
if (certURL != null && !certURL.equals("")) { |
|
383 |
downloadCertificate(certURL); |
|
384 |
} |
|
385 |
|
|
386 | 377 |
// delete server from server list |
387 | 378 |
} else if (subaction.equals("delete")) { |
388 | 379 |
server = ((String[]) params.get("server"))[0]; |
... | ... | |
480 | 471 |
|
481 | 472 |
} |
482 | 473 |
|
483 |
// download certificate with the public key from certURL and |
|
484 |
// upload it onto this server; it then must be imported as a |
|
485 |
// trusted certificate |
|
486 |
private static void downloadCertificate(String certURL) throws FileNotFoundException, |
|
487 |
IOException, MalformedURLException, PropertyNotFoundException { |
|
488 |
|
|
489 |
// the path to be uploaded to |
|
490 |
String certPath = SystemUtil.getContextDir(); |
|
491 |
|
|
492 |
// get filename from the URL of the certificate |
|
493 |
String filename = certURL; |
|
494 |
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\')); |
|
495 |
if (slash > -1) { |
|
496 |
filename = filename.substring(slash + 1); |
|
497 |
} |
|
498 |
|
|
499 |
// open file output strem to write the input into it |
|
500 |
File f = new File(certPath, filename); |
|
501 |
synchronized (f) { |
|
502 |
try { |
|
503 |
if (f.exists()) { |
|
504 |
throw new IOException("File already exist: " + f.getCanonicalFile()); |
|
505 |
// if ( f.exists() && !f.canWrite() ) { |
|
506 |
// throw new IOException("Not writable: " + |
|
507 |
// f.getCanonicalFile()); |
|
508 |
} |
|
509 |
} catch (SecurityException se) { |
|
510 |
// if a security manager exists, |
|
511 |
// its checkRead method is called for f.exist() |
|
512 |
// or checkWrite method is called for f.canWrite() |
|
513 |
throw se; |
|
514 |
} |
|
515 |
|
|
516 |
// create a buffered byte output stream |
|
517 |
// that uses a default-sized output buffer |
|
518 |
FileOutputStream fos = new FileOutputStream(f); |
|
519 |
BufferedOutputStream out = new BufferedOutputStream(fos); |
|
520 |
|
|
521 |
// this should be http url |
|
522 |
URL url = new URL(certURL); |
|
523 |
BufferedInputStream bis = null; |
|
524 |
try { |
|
525 |
bis = new BufferedInputStream(url.openStream()); |
|
526 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
|
527 |
int b = bis.read(buf); |
|
528 |
while (b != -1) { |
|
529 |
out.write(buf, 0, b); |
|
530 |
b = bis.read(buf); |
|
531 |
} |
|
532 |
} finally { |
|
533 |
if (bis != null) |
|
534 |
bis.close(); |
|
535 |
} |
|
536 |
// the input and the output streams must be closed |
|
537 |
bis.close(); |
|
538 |
out.flush(); |
|
539 |
out.close(); |
|
540 |
fos.close(); |
|
541 |
} // end of synchronized(f) |
|
542 |
} |
|
543 |
|
|
544 | 474 |
/** |
545 | 475 |
* when a forcereplication request comes in, local host sends a read request |
546 | 476 |
* to the requesting server (remote server) for the specified docid. Then |
Also available in: Unified diff
replication control panel now fully implemented as an admin configuration screen
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5528