1 package org.apache.turbine.services.xmlrpc.util; 2 3 18 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.StringWriter ; 26 27 import javax.mail.internet.MimeUtility ; 28 29 import org.apache.commons.lang.StringUtils; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import org.apache.turbine.Turbine; 35 36 import org.apache.turbine.services.servlet.TurbineServlet; 37 38 69 public class FileHandler 70 { 71 72 private static Log log = LogFactory.getLog(FileHandler.class); 73 74 77 public FileHandler() 78 { 79 } 80 81 108 public boolean send(String fileContents, 109 String targetLocationProperty, 110 String fileName) 111 { 112 119 return writeFileContents(fileContents, targetLocationProperty, 120 fileName); 121 } 122 123 147 public String get(String targetLocationProperty, 148 String fileName) 149 { 150 155 return readFileContents(targetLocationProperty, fileName); 156 } 157 158 165 public static String readFileContents(String targetLocationProperty, 166 String fileName) 167 { 168 String location = 169 Turbine.getConfiguration().getString(targetLocationProperty); 170 171 if (StringUtils.isEmpty(location)) 172 { 173 log.error("Could not load Property for location " 174 + targetLocationProperty); 175 return null; 176 } 177 178 File tmpF = new File ("."); 179 180 StringBuffer sb = new StringBuffer (); 181 sb.append(location); 182 sb.append(File.separator); 183 sb.append(fileName); 184 185 String file = TurbineServlet.getRealPath(sb.toString()); 186 187 StringWriter sw = null; 188 BufferedReader reader = null; 189 try 190 { 191 195 196 sw = new StringWriter (); 197 198 reader = new BufferedReader ( 199 new InputStreamReader ( 200 new FileInputStream (file))); 201 202 char buf[] = new char[1024]; 203 int len = 0; 204 205 while ((len = reader.read(buf, 0, 1024)) != -1) 206 { 207 sw.write(buf, 0, len); 208 } 209 210 return MimeUtility.encodeText(sw.toString(), "UTF-8", "B"); 211 } 212 catch (IOException ioe) 213 { 214 log.error("[FileHandler] Unable to encode the contents " + 215 "of the request file.", ioe); 216 217 return null; 218 } 219 finally 220 { 221 try 222 { 223 if (sw != null) 224 { 225 sw.close(); 226 } 227 if (reader != null) 228 { 229 reader.close(); 230 } 231 } 232 catch (Exception e) 233 { 234 } 235 } 236 } 237 238 public static boolean writeFileContents(String fileContents, 239 String targetLocationProperty, 240 String fileName) 241 { 242 String location = 243 Turbine.getConfiguration().getString(targetLocationProperty); 244 245 if (StringUtils.isEmpty(location)) 246 { 247 log.error("Could not load Property for location " 248 + targetLocationProperty); 249 return false; 250 } 251 252 257 258 File targetLocation = new File ( 259 TurbineServlet.getRealPath(location)); 260 261 if (!targetLocation.exists()) 262 { 263 268 if (!targetLocation.mkdirs()) 269 { 270 log.error("[FileHandler] Could not create target location: " + 271 targetLocation + ". Cannot transfer file from client."); 272 273 return false; 274 } 275 else 276 { 277 log.info("[FileHandler] Creating target location:" + 278 targetLocation + 279 " in order to complete file transfer from client."); 280 } 281 } 282 283 FileWriter fileWriter = null; 284 try 285 { 286 290 fileWriter = new FileWriter ( 291 targetLocation + "/" + fileName); 292 293 298 fileWriter.write(MimeUtility.decodeText(fileContents)); 299 300 return true; 301 } 302 catch (IOException ioe) 303 { 304 log.error("[FileHandler] Could not write the decoded file " + 305 "contents to disk for the following reason.", ioe); 306 307 return false; 308 } 309 finally 310 { 311 try 312 { 313 if (fileWriter != null) 314 { 315 fileWriter.close(); 316 } 317 } 318 catch (Exception e) 319 { 320 } 321 } 322 } 323 324 331 public static void remove(String sourceLocationProperty, 332 String sourceFileName) 333 { 334 String location = 335 Turbine.getConfiguration().getString(sourceLocationProperty); 336 337 if (StringUtils.isEmpty(location)) 338 { 339 log.error("Could not load Property for location " 340 + sourceLocationProperty); 341 return; 342 } 343 344 349 File sourceFile = 350 new File (TurbineServlet.getRealPath(sourceLocationProperty 351 + "/" + sourceFileName)); 352 353 if (sourceFile.exists()) 354 { 355 sourceFile.delete(); 356 } 357 } 358 } 359 | Popular Tags |