1 23 24 package org.infoglue.deliver.controllers.kernel.impl.simple; 25 26 import java.io.BufferedInputStream ; 27 import java.io.BufferedOutputStream ; 28 import java.io.File ; 29 import java.io.FileDescriptor ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.FileWriter ; 33 import java.io.FilenameFilter ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.io.PrintWriter ; 38 import java.nio.channels.FileChannel ; 39 import java.nio.channels.FileLock ; 40 import java.util.Enumeration ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 import java.util.Vector ; 45 import java.util.zip.ZipEntry ; 46 import java.util.zip.ZipFile ; 47 48 import org.apache.log4j.Logger; 49 import org.infoglue.cms.controllers.kernel.impl.simple.DigitalAssetController; 50 import org.infoglue.cms.entities.content.DigitalAsset; 51 import org.infoglue.cms.entities.content.DigitalAssetVO; 52 import org.infoglue.cms.entities.management.Repository; 53 import org.infoglue.cms.exception.SystemException; 54 import org.infoglue.cms.util.CmsPropertyHandler; 55 import org.infoglue.cms.util.graphics.ThumbnailGenerator; 56 import org.infoglue.deliver.applications.databeans.DeliveryContext; 57 import org.infoglue.deliver.controllers.kernel.URLComposer; 58 import org.infoglue.deliver.util.HttpHelper; 59 import org.infoglue.deliver.util.Timer; 60 61 62 public class DigitalAssetDeliveryController extends BaseDeliveryController 63 { 64 private final static Logger logger = Logger.getLogger(DigitalAssetDeliveryController.class.getName()); 65 66 class FilenameFilterImpl implements FilenameFilter 67 { 68 private String filter = "."; 69 70 public FilenameFilterImpl(String aFilter) 71 { 72 filter = aFilter; 73 } 74 75 public boolean accept(File dir, String name) 76 { 77 return name.startsWith(filter); 78 } 79 }; 80 81 82 85 86 private DigitalAssetDeliveryController() 87 { 88 } 89 90 93 94 public static DigitalAssetDeliveryController getDigitalAssetDeliveryController() 95 { 96 return new DigitalAssetDeliveryController(); 97 } 98 99 100 104 105 public String getAssetUrl(DigitalAsset digitalAsset, Repository repository, DeliveryContext deliveryContext) throws SystemException, Exception 106 { 107 String assetUrl = ""; 108 109 if(digitalAsset != null) 110 { 111 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 112 114 int i = 0; 115 String filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i); 116 while(filePath != null) 118 { 119 try 120 { 121 DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath); 122 } 123 catch(Exception e) 124 { 125 logger.warn("An file could not be written:" + e.getMessage(), e); 126 } 127 128 i++; 129 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i); 130 } 132 133 135 String dnsName = CmsPropertyHandler.getWebServerAddress(); 136 if(repository != null && repository.getDnsName() != null && !repository.getDnsName().equals("")) 137 dnsName = repository.getDnsName(); 138 139 assetUrl = URLComposer.getURLComposer().composeDigitalAssetUrl(dnsName, fileName, deliveryContext); 140 } 141 142 return assetUrl; 143 } 144 145 146 150 151 public String getAssetThumbnailUrl(DigitalAsset digitalAsset, Repository repository, int width, int height, DeliveryContext deliveryContext) throws SystemException, Exception 152 { 153 String assetUrl = ""; 154 155 if(digitalAsset != null) 156 { 157 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 158 String thumbnailFileName = "thumbnail_" + width + "_" + height + "_" + fileName; 159 160 int i = 0; 161 String filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i); 162 while(filePath != null) 163 { 164 try 165 { 166 DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath); 167 DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height); 168 } 169 catch(Exception e) 170 { 171 logger.warn("An file could not be written:" + e.getMessage(), e); 172 } 173 174 i++; 175 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i); 176 } 177 178 182 String dnsName = CmsPropertyHandler.getWebServerAddress(); 183 if(repository != null && repository.getDnsName() != null && !repository.getDnsName().equals("")) 184 dnsName = repository.getDnsName(); 185 186 assetUrl = URLComposer.getURLComposer().composeDigitalAssetUrl(dnsName, thumbnailFileName, deliveryContext); 187 } 188 189 return assetUrl; 190 } 191 192 193 198 199 public void dumpAttributeToFile(String attributeValue, String fileName, String filePath) throws Exception 200 { 201 File outputFile = new File (filePath + File.separator + fileName); 202 PrintWriter pw = new PrintWriter (new FileWriter (outputFile)); 203 pw.println(attributeValue); 204 pw.close(); 205 } 206 207 212 213 public File dumpUrlToFile(String fileName, String filePath, String pageContent) throws Exception 214 { 215 File outputFile = new File (filePath + File.separator + fileName); 216 logger.info("outputFile:" + outputFile.getAbsolutePath()); 217 if(!outputFile.exists() || outputFile.length() == 0) 218 { 219 PrintWriter pw = new PrintWriter (new FileWriter (outputFile)); 220 pw.println(pageContent); 221 pw.close(); 222 } 223 224 return outputFile; 225 } 226 227 232 255 256 257 262 351 352 public File dumpDigitalAsset(DigitalAsset digitalAsset, String fileName, String filePath) throws Exception 353 { 354 Timer timer = new Timer(); 355 File tmpOutputFile = new File (filePath + File.separator + Thread.currentThread().getId() + "_tmp_" + fileName); 356 File outputFile = new File (filePath + File.separator + fileName); 357 if(outputFile.exists()) 359 { 360 return outputFile; 362 } 363 364 try 365 { 366 369 InputStream inputStream = digitalAsset.getAssetBlob(); 372 logger.info("inputStream:" + inputStream + ":" + inputStream.getClass().getName() + ":" + digitalAsset); 373 synchronized(inputStream) 374 { 375 logger.info("reading inputStream and writing to disk...."); 376 377 FileOutputStream fos = new FileOutputStream (tmpOutputFile); 378 BufferedOutputStream bos = new BufferedOutputStream (fos); 379 BufferedInputStream bis = new BufferedInputStream (inputStream); 380 381 int character; 382 int i=0; 383 while ((character = bis.read()) != -1) 384 { 385 bos.write(character); 386 i++; 387 } 388 389 if(i == 0) 390 logger.info("Wrote " + i + " chars to " + fileName); 391 392 bos.flush(); 393 fos.close(); 394 bos.close(); 395 396 bis.close(); 397 398 logger.info("done reading inputStream and writing to disk...."); 399 } 400 401 logger.info("Time for dumping file " + fileName + ":" + timer.getElapsedTime()); 402 403 if(tmpOutputFile.length() == 0 || outputFile.exists()) 404 { 405 logger.info("written file:" + tmpOutputFile.length() + " - removing temp and not renaming it..."); 406 tmpOutputFile.delete(); 407 logger.info("Time for deleting file " + timer.getElapsedTime()); 408 } 409 else 410 { 411 logger.info("written file:" + tmpOutputFile.length() + " - renaming it to " + outputFile.getAbsolutePath()); 412 tmpOutputFile.renameTo(outputFile); 413 logger.info("Time for renaming file " + timer.getElapsedTime()); 414 } 415 } 416 catch (IOException e) 417 { 418 throw new Exception ("Could not write file " + outputFile.getAbsolutePath() + " - error reported:" + e.getMessage(), e); 419 } 420 421 return outputFile; 422 } 423 424 425 430 431 public File dumpDigitalAsset(File masterFile, String fileName, String filePath) throws Exception 432 { 433 long timer = System.currentTimeMillis(); 434 435 File outputFile = new File (filePath + File.separator + fileName); 436 if(outputFile.exists() && outputFile.length() > 0) 438 { 439 return outputFile; 441 } 442 443 try 444 { 445 outputFile.createNewFile(); 447 448 FileOutputStream fis = new FileOutputStream (outputFile); 449 BufferedOutputStream bos = new BufferedOutputStream (fis); 450 451 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (masterFile)); 452 453 int character; 454 while ((character = bis.read()) != -1) 455 { 456 bos.write(character); 457 } 458 bos.flush(); 459 460 bis.close(); 461 fis.close(); 462 bos.close(); 463 465 } 468 catch (IOException e) 469 { 470 throw new Exception ("Could not write file " + outputFile.getAbsolutePath() + " - error reported:" + e.getMessage(), e); 471 } 472 473 return outputFile; 474 } 475 476 481 482 public File dumpDigitalAssetThumbnail(String fileName, String thumbnailFile, String filePath, int width, int height) throws Exception 483 { 484 long timer = System.currentTimeMillis(); 485 logger.info("fileName:" + fileName); 486 logger.info("thumbnailFile:" + thumbnailFile); 487 488 File outputFile = new File (filePath + File.separator + thumbnailFile); 489 if(outputFile.exists()) 490 { 491 logger.info("The file allready exists so we don't need to dump it again.."); 492 return outputFile; 493 } 494 495 ThumbnailGenerator tg = new ThumbnailGenerator(); 496 tg.transform(filePath + File.separator + fileName, filePath + File.separator + thumbnailFile, width, height, 100); 497 498 logger.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer)); 499 500 return outputFile; 501 } 502 503 504 509 510 public File dumpAndUnzipDigitalAsset(DigitalAsset digitalAsset, String fileName, String filePath, File unzipDirectory) throws Exception 511 { 512 File zipFile = dumpDigitalAsset(digitalAsset, fileName, filePath); 513 File outputFile = new File (filePath + File.separator + fileName); 514 unzipFile(outputFile, unzipDirectory); 515 return zipFile; 516 } 517 518 523 524 public File dumpAndUnzipDigitalAsset(File masterFile, String fileName, String filePath, File unzipDirectory) throws Exception 525 { 526 File zipFile = dumpDigitalAsset(masterFile, fileName, filePath); 527 File outputFile = new File (filePath + File.separator + fileName); 528 unzipFile(outputFile, unzipDirectory); 529 return zipFile; 530 } 531 532 public Vector dumpAndGetZipEntries(DigitalAsset digitalAsset, String fileName, String filePath, File unzipDirectory) throws Exception 533 { 534 dumpDigitalAsset(digitalAsset, fileName, filePath); 535 File outputFile = new File (filePath + File.separator + fileName); 536 return getZipFileEntries(outputFile, unzipDirectory); 537 } 538 539 public Vector dumpAndGetZipEntries(File masterFile, String fileName, String filePath, File unzipDirectory) throws Exception 540 { 541 dumpDigitalAsset(masterFile, fileName, filePath); 542 File outputFile = new File (filePath + File.separator + fileName); 543 return getZipFileEntries(outputFile, unzipDirectory); 544 } 545 546 547 550 551 public void deleteContentVersionAssets(Integer contentVersionId) throws SystemException, Exception 552 { 553 try 554 { 555 List digitalAssetVOList = DigitalAssetController.getController().getDigitalAssetVOList(contentVersionId); 556 Iterator assetIterator = digitalAssetVOList.iterator(); 557 while(assetIterator.hasNext()) 558 { 559 DigitalAssetVO digitalAssetVO = (DigitalAssetVO)assetIterator.next(); 560 this.deleteDigitalAssets(digitalAssetVO.getId()); 561 } 562 } 563 catch(Exception e) 564 { 565 logger.error("Could not delete the assets for the contentVersion " + contentVersionId + ":" + e.getMessage(), e); 566 } 567 } 568 569 570 571 574 575 public void deleteDigitalAssets(Integer digitalAssetId) throws SystemException, Exception 576 { 577 try 578 { 579 int i = 0; 580 String filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i); 581 while(filePath != null) 582 { 583 File assetDirectory = new File (filePath); 584 File [] files = assetDirectory.listFiles(new FilenameFilterImpl(digitalAssetId.toString())); 585 for(int j=0; j<files.length; j++) 586 { 587 File file = files[j]; 588 logger.info("Deleting file " + file.getPath()); 589 file.delete(); 590 } 591 i++; 592 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i); 593 } 594 595 604 } 605 catch(Exception e) 606 { 607 logger.error("Could not delete the assets for the digitalAsset " + digitalAssetId + ":" + e.getMessage(), e); 608 } 609 } 610 611 614 615 private void unzipFile(File assetFile, File targetFolder) throws Exception 616 { 617 logger.info("Unzipping file " + assetFile.getPath() + " to " + targetFolder); 618 Enumeration entries; 619 ZipFile zipFile = new ZipFile (assetFile); 620 entries = zipFile.entries(); 621 622 while(entries.hasMoreElements()) 623 { 624 ZipEntry entry = (ZipEntry )entries.nextElement(); 625 626 if(entry.isDirectory()) 627 { 628 (new File (targetFolder + File.separator + entry.getName())).mkdirs(); 632 continue; 633 } 634 635 copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream (new FileOutputStream (targetFolder + File.separator + entry.getName()))); 637 } 638 639 zipFile.close(); 640 } 641 642 private Vector getZipFileEntries(File assetFile, File targetFolder) throws Exception 643 { 644 logger.info("Getting entries from " + assetFile.getPath()); 645 Enumeration entries; 646 Vector entryCopies = new Vector (); 647 ZipFile zipFile = new ZipFile (assetFile); 648 entries = zipFile.entries(); 649 650 while(entries.hasMoreElements()) 651 { 652 ZipEntry entry = (ZipEntry )entries.nextElement(); 653 ZipEntry entryCopy = (ZipEntry ) entry.clone(); 654 655 entryCopies.add(entryCopy); 656 } 657 658 zipFile.close(); 659 return entryCopies; 660 } 661 662 663 666 667 private void copyInputStream(InputStream in, OutputStream out) throws IOException 668 { 669 byte[] buffer = new byte[1024]; 670 int len; 671 672 while((len = in.read(buffer)) >= 0) 673 out.write(buffer, 0, len); 674 675 in.close(); 676 out.close(); 677 } 678 679 680 } | Popular Tags |