1 22 23 package org.cofax.cms; 24 25 import org.cofax.*; 26 import javax.servlet.http.*; 27 import java.util.*; 28 import org.apache.commons.net.ftp.*; 29 import java.io.*; 30 31 38 39 public class CofaxToolsFTP { 40 41 61 62 public static String uploadArticleMedia(String localPath, String articleImageName, String year, String month, String day, DataStore db, HttpSession session) { 63 64 CofaxToolsUser user = (CofaxToolsUser) session.getAttribute("user"); 65 if (!localPath.endsWith(File.separator)) { 66 localPath += File.separator; 67 } 68 69 FTPClient ftp = new FTPClient(); 70 String liveFTPLogin = (String ) user.workingPubConfigElementsHash.get("LIVEFTPLOGIN"); 71 String liveFTPPassword = (String ) user.workingPubConfigElementsHash.get("LIVEFTPPASSWORD"); 72 String liveImagesServer = (String ) user.workingPubConfigElementsHash.get("LIVEFTPSERVER"); 73 String liveImagesFolder = (String ) user.workingPubConfigElementsHash.get("LIVEIMAGESFOLDER"); 74 if (!liveImagesFolder.endsWith("/")) { 76 liveImagesFolder = liveImagesFolder + "/"; 77 } 78 String liveImagesYearFolder = ""; 79 String liveImagesMonthFolder = ""; 80 String fileLocation = ""; 81 fileLocation += "/" + year + "/" + month + "/" + day; 82 liveImagesYearFolder = liveImagesFolder + year; 83 liveImagesMonthFolder = (liveImagesYearFolder + "/" + month); 84 liveImagesFolder = (liveImagesMonthFolder + "/" + day); 85 86 CofaxToolsUtil.log("CofaxToolsFTP: liveImagesServer: " + liveImagesServer); 87 CofaxToolsUtil.log("CofaxToolsFTP: liveImagesFolder: " + liveImagesFolder); 88 boolean stored = false; 89 ArrayList servers = splitServers(liveImagesServer); 90 for (int count = 0; count < servers.size(); count++) { 91 String server = (String ) servers.get(count); 92 try { 93 int reply; 94 ftp.connect(server); 95 CofaxToolsUtil.log("CofaxToolsFTP: uploadArticleMedia connecting to server : " + server); 96 CofaxToolsUtil.log("CofaxToolsFTP: uploadArticleMedia results: " + ftp.getReplyString()); 97 CofaxToolsUtil.log(ftp.getReplyString()); 98 reply = ftp.getReplyCode(); 99 if (!FTPReply.isPositiveCompletion(reply)) { 100 ftp.disconnect(); 101 return ("CofaxToolsFTP uploadArticleMedia ERROR: FTP server refused connection."); 102 } else { 103 ftp.login(liveFTPLogin, liveFTPPassword); 104 } 105 try { 106 ftp.setFileType(FTP.IMAGE_FILE_TYPE); 107 InputStream input; 108 CofaxToolsUtil.log("CofaxToolsFTP: opening file stream: " + localPath + articleImageName); 109 input = new FileInputStream(localPath + articleImageName); 110 111 CofaxToolsUtil.log("CofaxToolsFTP: attempting to change working directory to: " + liveImagesFolder); 112 boolean changed = ftp.changeWorkingDirectory(liveImagesFolder); 113 CofaxToolsUtil.log("CofaxToolsFTP: uploadArticleMedia results: " + changed); 114 if (changed == false) { 115 CofaxToolsUtil.log("CofaxToolsFTP: uploadArticleMedia attempting to create directory :" + liveImagesFolder); 116 boolean newDirYear = ftp.makeDirectory(liveImagesYearFolder); 117 boolean newDirMonth = ftp.makeDirectory(liveImagesMonthFolder); 118 boolean newDir = ftp.makeDirectory(liveImagesFolder); 119 CofaxToolsUtil.log("CofaxToolsFTP: uploadArticleMedia results: YearDir: " + newDirYear + " MonthDir: " + newDirMonth + " finalDir: " 120 + newDir); 121 changed = ftp.changeWorkingDirectory(liveImagesFolder); 122 } 123 if (changed) { 124 CofaxToolsUtil.log("CofaxToolsFTP: storing " + articleImageName + " to " + liveImagesFolder); 125 stored = ftp.storeFile(articleImageName, input); 126 } else { 127 CofaxToolsUtil.log("CofaxToolsFTP: failed changing: " + liveImagesFolder); 128 } 129 130 if (stored) { 131 CofaxToolsUtil.log("CofaxToolsFTP: Successfully ftped file."); 132 } else { 133 CofaxToolsUtil.log("CofaxToolsFTP: Failed ftping file."); 134 } 135 136 input.close(); 137 ftp.logout(); 138 ftp.disconnect(); 139 } catch (org.apache.commons.net.io.CopyStreamException e) { 140 CofaxToolsUtil.log("CofaxToolsFTP: Failed ftping file." + e.toString()); 141 CofaxToolsUtil.log("CofaxToolsFTP: " + e.getIOException().toString()); 142 return ("Cannot upload file " + liveImagesFolder + "/" + articleImageName); 143 } catch (IOException e) { 144 CofaxToolsUtil.log("CofaxToolsFTP: Failed ftping file." + e.toString()); 145 return ("Cannot upload file " + liveImagesFolder + "/" + articleImageName); 146 } catch (Exception e) { 147 CofaxToolsUtil.log("CofaxToolsFTP: Failed ftping file." + e.toString()); 148 return ("Cannot upload file " + liveImagesFolder + "/" + articleImageName); 149 } 150 151 } catch (IOException e) { 152 return ("Could not connect to server: " + e); 153 } 154 155 } 156 return (""); 157 } 158 159 166 public static String stripPath(String mediaName) { 167 168 int forwardSlash = mediaName.lastIndexOf("/"); 169 int backSlash = mediaName.lastIndexOf("\\"); 170 if (forwardSlash > -1) { 171 mediaName = mediaName.substring(forwardSlash + 1); 172 } 173 if (backSlash > -1) { 174 mediaName = mediaName.substring(backSlash + 1); 175 } 176 return (mediaName); 177 } 178 179 186 public static String stripName(String mediaName) { 187 188 int forwardSlash = mediaName.lastIndexOf("/"); 189 int backSlash = mediaName.lastIndexOf("\\"); 190 if (forwardSlash > -1) { 191 mediaName = mediaName.substring(0, forwardSlash + 1); 192 } 193 if (backSlash > -1) { 194 mediaName = mediaName.substring(0, backSlash + 1); 195 } 196 return (mediaName); 197 } 198 199 208 public static ArrayList splitServers(String servers) { 209 ArrayList al = new ArrayList(); 210 if ((servers != null) && (!servers.equals(""))) { 211 if (servers.indexOf(",") > -1) { 212 StringTokenizer st = new StringTokenizer(servers, ","); 213 while (st.hasMoreTokens()) { 214 al.add(st.nextToken(",")); 215 } 216 } else { 217 al.add(servers); 218 } 219 return (al); 220 } else { 221 al.add("NoServerListedInConfig"); 222 return (al); 223 } 224 } 225 226 234 235 public static String [] viewFilesToImport(HttpSession session) { 236 FTPClient ftp = new FTPClient(); 237 238 CofaxToolsUser user = (CofaxToolsUser) session.getAttribute("user"); 239 240 String importFTPServer = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPSERVER") + ""; 241 String importFTPLogin = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPLOGIN") + ""; 242 String importFTPPassword = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPPASSWORD") + ""; 243 String importFTPFilePath = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPFILEPATH"); 244 String [] dirList = null; 245 246 if (importFTPServer.equals("") || importFTPLogin.equals("") || importFTPPassword.equals("")) { 247 return dirList; 248 } 249 boolean loggedIn = false; 250 try { 251 int reply; 252 ftp.connect(importFTPServer); 253 CofaxToolsUtil.log("CofaxToolsFTP viewFilesToImport connecting: " + ftp.getReplyString()); 254 reply = ftp.getReplyCode(); 255 if (!FTPReply.isPositiveCompletion(reply)) { 256 ftp.logout(); 257 ftp.disconnect(); 258 CofaxToolsUtil.log("CofaxToolsFTP viewFilesToImport ERROR: FTP server refused connection."); 259 } else { 260 loggedIn = ftp.login(importFTPLogin, importFTPPassword); 261 CofaxToolsUtil.log("CofaxToolsFTP viewFilesToImport Logging in: " + importFTPLogin + " " + importFTPPassword); 262 } 263 if (loggedIn) { 264 try { 265 ftp.changeWorkingDirectory(importFTPFilePath); 266 CofaxToolsUtil.log("CofaxToolsFTP viewFilesToImport changing dir: " + importFTPFilePath); 267 if (!FTPReply.isPositiveCompletion(reply)) { 268 CofaxToolsUtil.log("ERROR: cannot change directory"); 269 } 270 FTPFile[] remoteFileList = ftp.listFiles(); 271 ArrayList tmpArray = new ArrayList(); 272 for (int i = 0; i < remoteFileList.length; i++) { 273 FTPFile testFile = remoteFileList[i]; 274 if (testFile.getType() == FTP.ASCII_FILE_TYPE) { 275 tmpArray.add(testFile.getName()); 276 } 277 } 278 dirList = (String []) tmpArray.toArray(new String [0]); 279 281 ftp.logout(); 282 ftp.disconnect(); 283 } catch (java.io.IOException e) { 284 CofaxToolsUtil.log("CofaxToolsFTP viewFilesToImport cannot read directory: " + importFTPFilePath); 285 } 286 } 287 } catch (IOException e) { 288 CofaxToolsUtil.log("CofaxToolsFTP viewFilesToImport could not connect to server: " + e); 289 } 290 return (dirList); 291 } 292 293 304 public static String getImportFileBody(String fileName, HttpSession session) { 305 FTPClient ftp = new FTPClient(); 306 CofaxToolsUser user = (CofaxToolsUser) session.getAttribute("user"); 307 String fileTransferFolder = CofaxToolsServlet.fileTransferFolder; 308 String importFTPServer = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPSERVER"); 309 String importFTPLogin = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPLOGIN"); 310 String importFTPPassword = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPPASSWORD"); 311 String importFTPFilePath = (String ) user.workingPubConfigElementsHash.get("IMPORTFTPFILEPATH"); 312 String body = (""); 313 try { 314 int reply; 315 ftp.connect(importFTPServer); 316 CofaxToolsUtil.log("CofaxToolsFTP getImportFileBody connecting to server " + importFTPServer); 317 reply = ftp.getReplyCode(); 318 if (!FTPReply.isPositiveCompletion(reply)) { 319 ftp.disconnect(); 320 return ("CofaxToolsFTP getImportFileBody ERROR: FTP server refused connection."); 321 } else { 322 ftp.login(importFTPLogin, importFTPPassword); 323 } 324 try { 325 boolean change = ftp.changeWorkingDirectory(importFTPFilePath); 326 CofaxToolsUtil.log("CofaxToolsFTP getImportFileBody changing to directory: " + importFTPFilePath); 327 CofaxToolsUtil.log("Results: " + change); 328 OutputStream output; 329 output = new FileOutputStream(fileTransferFolder + fileName); 330 boolean retrieve = ftp.retrieveFile(fileName, output); 331 CofaxToolsUtil.log("CofaxToolsFTP getImportFileBody retrieving file: " + fileName); 332 CofaxToolsUtil.log("CofaxToolsFTP getImportFileBody results: " + change); 333 output.close(); 334 body = CofaxToolsUtil.readFile(fileTransferFolder + fileName, true); 335 CofaxToolsUtil.log("CofaxToolsFTP getImportFileBody deleting remote file: " + importFTPFilePath + fileName); 336 boolean delete = ftp.deleteFile(importFTPFilePath + fileName); 337 CofaxToolsUtil.log("CofaxToolsFTP getImportFileBody results: " + delete); 338 CofaxToolsUtil.log("CofaxToolsFTP getImportFileBody disconnecting from server:" + importFTPServer); 339 ftp.logout(); 340 ftp.disconnect(); 341 } catch (java.io.IOException e) { 342 return ("CofaxToolsFTP getImportFileBody ERROR: cannot write file: " + fileName); 343 } 344 345 } catch (IOException e) { 346 return ("CofaxToolsFTP getImportFileBody ERROR: could not connect to server: " + e); 347 } 348 return (body); 349 } 350 351 367 public static String getUploadDeleteComboBox(String fromMode, String fromFolder, String action, String object, HttpSession session) { 368 FTPClient ftp = new FTPClient(); 369 CofaxToolsUser user = (CofaxToolsUser) session.getAttribute("user"); 370 StringBuffer links = new StringBuffer (); 371 StringBuffer folders = new StringBuffer (); 372 String folder = ""; 373 String server = ""; 374 String login = ""; 375 String password = ""; 376 int i = 0; 377 378 String liveFTPServer = (String ) user.workingPubConfigElementsHash.get("LIVEFTPSERVER") + ""; 379 liveFTPServer = liveFTPServer.trim(); 380 if ((liveFTPServer == null) || (liveFTPServer.equals("null")) || (liveFTPServer.equals(""))) { 381 return ("This tool is not " + "configured and will not operate. " + "If you wish it to do so, please edit " 382 + "this publication's properties and add correct values to " + " the Remote Server Upstreaming section."); 383 } 384 385 if (action.equals("Upload")) { 386 server = (String ) user.workingPubConfigElementsHash.get("TESTFTPSERVER"); 387 login = (String ) user.workingPubConfigElementsHash.get("TESTFTPLOGIN"); 388 password = (String ) user.workingPubConfigElementsHash.get("TESTFTPPASSWORD"); 389 CofaxToolsUtil.log("server= " + server + " , login= " + login + " , password=" + password); 390 if (object.equals("Media")) { 391 folder = (String ) user.workingPubConfigElementsHash.get("TESTIMAGESFOLDER"); 392 } 393 if (object.equals("Templates")) { 394 folder = (String ) user.workingPubConfigElementsHash.get("TESTTEMPLATEFOLDER"); 395 CofaxToolsUtil.log("testTemplateFolder= " + folder); 396 } 397 } 398 399 if (action.equals("Delete")) { 400 login = (String ) user.workingPubConfigElementsHash.get("LIVEFTPLOGIN"); 401 password = (String ) user.workingPubConfigElementsHash.get("LIVEFTPPASSWORD"); 402 if (object.equals("Media")) { 403 server = (String ) user.workingPubConfigElementsHash.get("LIVEIMAGESSERVER"); 404 folder = (String ) user.workingPubConfigElementsHash.get("LIVEIMAGESFOLDER"); 405 } 406 if (object.equals("Templates")) { 407 server = (String ) user.workingPubConfigElementsHash.get("LIVEFTPSERVER"); 408 folder = (String ) user.workingPubConfigElementsHash.get("LIVETEMPLATEFOLDER"); 409 } 410 } 411 412 ArrayList servers = splitServers(server); 413 try { 414 int reply; 415 ftp.connect((String ) servers.get(0)); 416 CofaxToolsUtil.log("CofaxToolsFTP getUploadDeleteComboBox connecting to server: " + (String ) servers.get(0)); 417 CofaxToolsUtil.log("CofaxToolsFTP getUploadDeleteComboBox results: " + ftp.getReplyString()); 418 reply = ftp.getReplyCode(); 419 if (!FTPReply.isPositiveCompletion(reply)) { 420 ftp.disconnect(); 421 return ("CofaxToolsFTP getUploadDeleteComboBox ERROR: FTP server refused connection: " + server); 422 } else { 423 ftp.login(login, password); 424 CofaxToolsUtil.log("CofaxToolsFTP getUploadDeleteComboBox login / pass " + login + " " + password); 425 } 426 try { 427 String tempParentFolderName = folder; 428 CofaxToolsUtil.log("fromfolder is " + fromFolder); 429 if ((fromFolder != null) && (fromFolder.length() > folder.length())) { 430 folder = fromFolder; 431 tempParentFolderName = folder; 432 int subSt = folder.lastIndexOf("/"); 433 tempParentFolderName = tempParentFolderName.substring(0, subSt); 434 String publicName = ""; 435 int subStri = folder.lastIndexOf((String ) user.workingPubName); 436 if (subStri > -1) { 437 publicName = folder.substring(subStri); 438 } 439 folders.append("<B><A HREF=\'/tools/?mode=" + fromMode + "&hl=templates_view_templates_images&fromFolder=" + tempParentFolderName + "\'>" 440 + tempParentFolderName + "</A></B><BR>\n"); 441 } else if ((fromFolder != null) && (fromFolder.length() == folder.length())) { 442 folder = fromFolder; 443 tempParentFolderName = folder; 444 int subSt = folder.lastIndexOf("/"); 445 tempParentFolderName = tempParentFolderName.substring(0, subSt); 446 } 447 448 boolean changed = ftp.changeWorkingDirectory(folder); 449 CofaxToolsUtil.log("CofaxToolsFTP getUploadDeleteComboBox changing working directory to " + folder); 450 CofaxToolsUtil.log("CofaxToolsFTP getUploadDeleteComboBox results " + changed); 451 FTPFile[] files = null; 452 if ((action.equals("Delete")) && (object.equals("Templates"))) { 453 files = ftp.listFiles(new CofaxToolsNTFileListParser()); 455 } else { 458 files = ftp.listFiles(new CofaxToolsNTFileListParser()); 460 } 463 464 if (files == null) { 465 CofaxToolsUtil.log("null"); 466 } 467 468 for (int ii = 0; ii < files.length; ii++) { 469 FTPFile thisFile = (FTPFile) files[ii]; 470 String name = thisFile.getName(); 471 472 if (!thisFile.isDirectory()) { 473 links.append("<INPUT TYPE=CHECKBOX NAME=" + i + " VALUE=" + folder + "/" + name + ">" + name + "<BR>\n"); 474 i++; 475 } 476 if ((thisFile.isDirectory()) && (!name.startsWith(".")) && (!name.endsWith("."))) { 477 int subStr = folder.lastIndexOf((String ) user.workingPubName); 478 String tempFolderName = ""; 479 if (subStr > -1) { 480 tempFolderName = folder.substring(subStr); 481 } else { 482 tempFolderName = folder; 483 } 484 folders.append("<LI><A HREF=\'/tools/?mode=" + fromMode + "&hl=templates_view_templates_images&fromFolder=" + folder + "/" + name 485 + "\'>" + tempFolderName + "/" + name + "</A><BR>"); 486 } 487 } 488 ftp.logout(); 489 ftp.disconnect(); 490 } catch (java.io.IOException e) { 491 return ("CofaxToolsFTP getUploadDeleteComboBox cannot read directory: " + folder); 492 } 493 494 } catch (IOException e) { 495 return ("Could not connect to server: " + e); 496 } 497 498 links.append("<INPUT TYPE=HIDDEN NAME=numElements VALUE=" + i + " >\n"); 499 links.append("<INPUT TYPE=SUBMIT VALUE=\"" + action + " " + object + "\">\n"); 500 return (folders.toString() + links.toString()); 501 } 502 503 519 public static String uploadOrDeleteMediaOrTemplates(String action, String object, String numElements, HttpServletRequest req, HttpSession session) { 520 FTPClient ftp = new FTPClient(); 521 CofaxToolsUser user = (CofaxToolsUser) session.getAttribute("user"); 522 StringBuffer links = new StringBuffer (); 523 StringBuffer folders = new StringBuffer (); 524 String folder = ""; 525 String server = ""; 526 String login = ""; 527 String password = ""; 528 String fileTransferFolder = CofaxToolsServlet.fileTransferFolder; 529 String liveFolder = ""; 530 ArrayList servers = new ArrayList(); 531 StringBuffer message = new StringBuffer (); 532 message.append("Status:<BR>"); 533 534 if (action.equals("Upload")) { 535 server = (String ) user.workingPubConfigElementsHash.get("TESTFTPSERVER"); 536 login = (String ) user.workingPubConfigElementsHash.get("TESTFTPLOGIN"); 537 password = (String ) user.workingPubConfigElementsHash.get("TESTFTPPASSWORD"); 538 if (object.equals("Media")) { 539 folder = (String ) user.workingPubConfigElementsHash.get("TESTIMAGESFOLDER"); 540 } 541 if (object.equals("Templates")) { 542 folder = (String ) user.workingPubConfigElementsHash.get("TESTTEMPLATEFOLDER"); 543 } 544 } 545 546 if (action.equals("Delete")) { 547 login = (String ) user.workingPubConfigElementsHash.get("LIVEFTPLOGIN"); 548 password = (String ) user.workingPubConfigElementsHash.get("LIVEFTPPASSWORD"); 549 if (object.equals("Media")) { 550 server = (String ) user.workingPubConfigElementsHash.get("LIVEIMAGESSERVER"); 551 folder = (String ) user.workingPubConfigElementsHash.get("LIVEIMAGESFOLDER"); 552 } 553 if (object.equals("Templates")) { 554 server = (String ) user.workingPubConfigElementsHash.get("LIVEFTPSERVER"); 555 folder = (String ) user.workingPubConfigElementsHash.get("LIVETEMPLATEFOLDER"); 556 } 557 } 558 559 ArrayList al = new ArrayList(); 560 int numElement = Integer.parseInt(numElements); 561 for (int i = 0; i < numElement; i++) { 562 String key = String.valueOf(i); 563 String file = req.getParameter(key); 564 if (file != null) { 565 al.add(file); 566 } 567 } 568 569 if (action.equals("Upload")) { 571 try { 572 int reply; 573 ftp.connect(server); 574 CofaxToolsUtil.log(ftp.getReplyString()); 575 reply = ftp.getReplyCode(); 576 if (!FTPReply.isPositiveCompletion(reply)) { 577 ftp.disconnect(); 578 return ("FTP server refused connection."); 579 } else { 580 ftp.login(login, password); 581 } 582 583 for (int ii = 0; ii < al.size(); ii++) { 584 String fileName = (String ) al.get(ii); 585 String folderName = stripName(fileName); 586 fileName = stripPath(fileName); 587 try { 588 ftp.changeWorkingDirectory(folderName); 589 OutputStream output; 590 output = new FileOutputStream(fileTransferFolder + fileName); 591 ftp.retrieveFile(fileName, output); 592 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates retrieving file: " + ftp.getReplyString()); 593 message.append("Retrieving file " + fileName + " to local disk.<BR>"); 594 output.close(); 595 } catch (java.io.IOException e) { 596 return ("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR: cannot write file" + e); 597 } 598 } 599 ftp.logout(); 600 ftp.disconnect(); 601 } catch (IOException e) { 602 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR: Could not connect to server: " + e); 603 return ("Could not connect to server: " + e); 604 } 605 606 login = (String ) user.workingPubConfigElementsHash.get("LIVEFTPLOGIN"); 608 password = (String ) user.workingPubConfigElementsHash.get("LIVEFTPPASSWORD"); 609 if (object.equals("Media")) { 610 server = (String ) user.workingPubConfigElementsHash.get("LIVEIMAGESSERVER"); 611 liveFolder = (String ) user.workingPubConfigElementsHash.get("LIVEIMAGESFOLDER"); 612 } 613 if (object.equals("Templates")) { 614 server = (String ) user.workingPubConfigElementsHash.get("LIVEFTPSERVER"); 615 liveFolder = (String ) user.workingPubConfigElementsHash.get("LIVETEMPLATEFOLDER"); 616 } 617 servers = splitServers(server); 618 619 for (int iii = 0; iii < servers.size(); iii++) { 620 try { 621 int reply; 622 String connectServer = (String ) servers.get(iii); 623 ftp.connect(connectServer); 624 CofaxToolsUtil.log(ftp.getReplyString()); 625 reply = ftp.getReplyCode(); 626 if (!FTPReply.isPositiveCompletion(reply)) { 627 ftp.disconnect(); 628 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR: server refused connection: " + connectServer); 629 return ("CofaxToolsFTP uploadOrDeleteMediaOrTemplates FTP server refused connection."); 630 } else { 631 ftp.login(login, password); 632 } 633 634 for (int ii = 0; ii < al.size(); ii++) { 635 String fileName = (String ) al.get(ii); 636 CofaxToolsUtil.log("Original String " + fileName); 637 CofaxToolsUtil.log("Search for " + folder); 638 CofaxToolsUtil.log("Replace " + liveFolder); 639 String folderName = CofaxToolsUtil.replace(fileName, folder, liveFolder); 640 CofaxToolsUtil.log("Results: " + folderName); 641 folderName = stripName(folderName); 642 fileName = stripPath(fileName); 643 try { 644 InputStream io; 645 io = new FileInputStream(fileTransferFolder + fileName); 646 CofaxToolsUtil.log("Reading file : " + fileTransferFolder + fileName); 647 boolean directoryExists = ftp.changeWorkingDirectory(folderName); 648 if (directoryExists == false) { 649 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates directory: " + folderName 650 + " does not exist. Attempting to create."); 651 message.append("Directory: " + folderName + " does not exist. Attempting to create.<BR>"); 652 boolean canCreatDir = ftp.makeDirectory(folderName); 653 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates results: " + canCreatDir); 654 message.append("Results: " + canCreatDir + "<BR>"); 655 } 656 boolean isStored = ftp.storeFile(fileName, io); 657 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates storing file: " + fileName + " in directory: " + folderName); 658 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates on server : " + connectServer); 659 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates results: " + isStored + " : " + ftp.getReplyString()); 660 message.append("Storing file " + fileName + "<BR> to location " + folderName + "<BR> on server " + connectServer + ".<BR>"); 661 } catch (java.io.IOException e) { 662 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates cannot upload file" + fileName + "<BR>To path: " + folderName 663 + "<BR>On server " + connectServer); 664 return ("Cannot upload file" + fileName + "<BR>To path: " + folderName + "<BR>On server " + connectServer); 665 } 666 } 667 ftp.logout(); 668 ftp.disconnect(); 669 message.append("Success<BR><BR>"); 670 } catch (IOException e) { 671 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates could not connect to server: " + e); 672 return ("Could not connect to server: " + e); 673 } 674 } 675 676 if (object.equals("Templates")) { 678 String cSServers = (String ) user.workingPubConfigElementsHash.get("CACHESERVERS"); 679 System.out.println("getting cache servers: " + cSServers); 680 ArrayList cServers = splitServers(cSServers); 681 682 for (int iiii = 0; iiii < cServers.size(); iiii++) { 683 String thisClearCacheServer = (String ) cServers.get(iiii); 684 try { 685 String connectServer = (String ) cServers.get(iiii); 686 for (int iiiii = 0; iiiii < al.size(); iiiii++) { 687 String thisFilePath = (String ) al.get(iiiii); 688 String folderNameFileName = CofaxToolsUtil.replace(thisFilePath, folder, liveFolder); 689 String URLToClear = CofaxToolsServlet.removeTemplateCache + folderNameFileName; 690 CofaxToolsClearCache clear = new CofaxToolsClearCache("HTTP://" + thisClearCacheServer + URLToClear); 691 clear.start(); 692 message.append("Clearing Cache for " + folderNameFileName + "<BR>"); 693 message.append("on server " + thisClearCacheServer + "<BR>Success<BR><BR>"); 694 } 695 } catch (Exception e) { 696 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR: could not connect to server clearing cache " + e); 697 } 698 } 699 } 700 for (int i = 0; i < al.size(); i++) { 702 String fileName = (String ) al.get(i); 703 String folderName = stripName(fileName); 704 fileName = stripPath(fileName); 705 File file = new File(fileTransferFolder + fileName); 706 boolean delete = file.delete(); 707 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates deleting file from local drive: " + fileTransferFolder + fileName); 708 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates results: " + delete); 709 } 710 711 } 712 713 servers = splitServers(server); 714 715 if (action.equals("Delete")) { 717 for (int iii = 0; iii < servers.size(); iii++) { 718 try { 719 int reply; 720 String connectServer = (String ) servers.get(iii); 721 ftp.connect(connectServer); 722 CofaxToolsUtil.log(ftp.getReplyString()); 723 reply = ftp.getReplyCode(); 724 if (!FTPReply.isPositiveCompletion(reply)) { 725 ftp.disconnect(); 726 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR: FTP server refused connection: " + connectServer); 727 return ("FTP server refused connection."); 728 } else { 729 ftp.login(login, password); 730 } 731 732 for (int ii = 0; ii < al.size(); ii++) { 733 String fileName = (String ) al.get(ii); 734 String folderName = stripName(fileName); 735 fileName = stripPath(fileName); 736 try { 737 ftp.changeWorkingDirectory(folderName); 738 ftp.deleteFile(fileName); 739 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates deleting file: " + fileName + " from directory: " + folderName); 740 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates on server : " + connectServer); 741 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates results: " + ftp.getReplyString()); 742 message.append("Deleting file " + fileName + "<BR>"); 743 message.append("from folder " + folderName + "<BR>"); 744 message.append("on server " + connectServer + "<BR>"); 745 } catch (java.io.IOException e) { 746 return ("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR: cannot delete file" + fileName); 747 } 748 } 749 message.append("Success<BR><BR>"); 750 ftp.logout(); 751 ftp.disconnect(); 752 } catch (IOException e) { 753 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR: Could not connect to server: " + e); 754 return ("Could not connect to server: " + e); 755 } 756 } 757 if (object.equals("Templates")) { 759 String cISServers = (String ) user.workingPubConfigElementsHash.get("CACHESERVERS"); 760 ArrayList cIServers = splitServers(cISServers); 761 762 for (int iiiiii = 0; iiiiii < cIServers.size(); iiiiii++) { 763 String thisClearCacheIServer = (String ) cIServers.get(iiiiii); 764 try { 765 String connectServer = (String ) cIServers.get(iiiiii); 766 for (int iiiiiii = 0; iiiiiii < al.size(); iiiiiii++) { 767 String thisFilePathI = (String ) al.get(iiiiiii); 768 String URLToClearI = CofaxToolsServlet.removeTemplateCache + thisFilePathI; 769 CofaxToolsClearCache clearI = new CofaxToolsClearCache("HTTP://" + thisClearCacheIServer + URLToClearI); 770 clearI.start(); 771 message.append("Clearing Cache for " + thisFilePathI + "<BR>"); 772 message.append("on server " + thisClearCacheIServer + "<BR>Success<BR><BR>"); 773 } 774 } catch (Exception e) { 775 CofaxToolsUtil.log("CofaxToolsFTP uploadOrDeleteMediaOrTemplates ERROR clearing cache " + e); 776 } 777 } 778 } 779 } 780 return (message.toString()); 781 } 782 783 792 public static boolean containsFourDigits(String name) { 793 char[] charChars = name.toCharArray(); 794 int test = 0; 795 for (int i = 0; i < charChars.length; i++) { 796 if (Character.isDigit(charChars[i])) { 797 test++; 798 } else if (test > 0) { 799 test--; 800 } 801 if (test >= 3) { 802 return (true); 803 } 804 } 805 return (false); 806 } 807 } 808 | Popular Tags |