1 5 package org.jahia.services.files; 6 7 import java.io.File ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.net.URL ; 13 14 import org.jahia.exceptions.JahiaException; 15 import org.jahia.exceptions.JahiaInitializationException; 16 import org.jahia.settings.SettingsBean; 17 import org.jahia.utils.FileUtils; 18 19 public class JahiaFileTransferBaseService extends JahiaFileTransferService { 20 21 private static org.apache.log4j.Logger logger = 22 org.apache.log4j.Logger.getLogger(JahiaFileTransferBaseService.class); 23 24 private static String serviceName = "JahiaFileTransferBaseService"; 25 private static String componentsDiskPath = ""; 26 private static String tmpDiskPath = ""; 27 28 private static JahiaFileTransferBaseService theObject = null; 29 30 34 protected JahiaFileTransferBaseService() 35 { 36 logger.debug( "Starting " + serviceName ); 37 } 39 40 45 public static synchronized JahiaFileTransferBaseService getInstance() 46 { 47 if (theObject == null) { 48 theObject = new JahiaFileTransferBaseService(); 49 } 50 return theObject; 51 } 53 54 59 public void init( SettingsBean jSettings ) throws JahiaInitializationException 60 { 61 62 logger.debug( "Initializing " + serviceName ); 63 64 componentsDiskPath = jSettings.getComponentsDiskPath(); 65 tmpDiskPath = jSettings.getJahiaTemplatesDiskPath(); 66 } 68 69 70 76 public boolean downloadApp( String fileName, String urlPath, String diskPath ) 77 { 78 logger.debug("ComponentsDiskPath is " + componentsDiskPath ); 79 String completeDiskPath = componentsDiskPath + File.separator + diskPath + "\\"; 80 if (checkFile( completeDiskPath, fileName )) { 85 downloadFile( urlPath + fileName, completeDiskPath + fileName ); 87 return true; 88 } else { 89 return false; 91 } 92 } 94 95 96 102 public boolean downloadTemplate( String fileName, String urlPath, String diskPath ) 103 { 104 String completeDiskPath = tmpDiskPath + diskPath + "\\"; 105 logger.debug("Downloading template to : " + completeDiskPath ); 106 if (checkFile( completeDiskPath, fileName )) { 111 if (downloadFile( urlPath + fileName, completeDiskPath + fileName )) { 113 return true; 115 } else { 116 return false; 118 } 119 } else { 120 return false; 122 } 123 } 125 126 127 133 protected boolean checkFile( String diskPath, String fileName ) 134 { 135 File fAppPath = new File ( diskPath ); 136 if (!fAppPath.exists()) { 137 logger.debug( "Creating " + diskPath + " path " ); 138 fAppPath.mkdirs(); 139 } 140 File fApp = new File ( diskPath + "\\" + fileName ); 141 if (fApp.exists()) { 142 if (fApp.delete()) { 143 return true; 144 } else { 145 String msg = "Failed to replace existing file " + diskPath + "\\" + fileName + " for download"; 146 JahiaException je = new JahiaException( "Download error", msg, 147 JahiaException.TEMPLATE_ERROR, JahiaException.WARNING_SEVERITY ); 148 149 return false; 150 } 151 } else { 152 return true; 153 } 154 } 156 157 158 164 protected boolean downloadFile( String urlPath, String diskPath ) 165 { 166 try { 167 URL url = new URL ( urlPath ); 168 InputStream input = url.openStream(); 169 File myFile = new File ( diskPath ); 170 OutputStream output = (OutputStream ) new FileOutputStream ( myFile ); 171 FileUtils.getInstance().copyStream( input, output ); 172 logger.debug(diskPath + " successfully transfered from " + urlPath ); 173 return true; 174 } catch (IOException ie) { 175 String msg = "IOException while trying to download " + diskPath + " from " + urlPath; 176 JahiaException je = new JahiaException( "Download error", msg, 177 JahiaException.TEMPLATE_ERROR, JahiaException.WARNING_SEVERITY ); 178 return false; 179 } 180 } 182 183 184 187 public void setName(String name){ 188 189 serviceName = name; 190 191 } 192 193 } | Popular Tags |