From a9a6fdaf0c871b76d046403bce4eae005132c870 Mon Sep 17 00:00:00 2001 From: Greg Huber Date: Wed, 14 Jan 2015 15:12:33 +0000 Subject: [PATCH] Build fail debugging git-svn-id: https://svn.apache.org/repos/asf/roller/trunk@1651697 13f79535-47bb-0310-9956-ffa450edef68 --- app/pom.xml | 2 +- .../business/FileContentManagerImpl.java | 266 +++++++++--------- 2 files changed, 136 insertions(+), 132 deletions(-) diff --git a/app/pom.xml b/app/pom.xml index 6d62021f0f..02393d14cd 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -356,7 +356,7 @@ org.apache.maven.plugins maven-war-plugin - 2.3 + 2.4 true diff --git a/app/src/main/java/org/apache/roller/weblogger/business/FileContentManagerImpl.java b/app/src/main/java/org/apache/roller/weblogger/business/FileContentManagerImpl.java index 7d37f6a4f8..d7012b0660 100644 --- a/app/src/main/java/org/apache/roller/weblogger/business/FileContentManagerImpl.java +++ b/app/src/main/java/org/apache/roller/weblogger/business/FileContentManagerImpl.java @@ -35,88 +35,92 @@ import org.apache.roller.weblogger.pojos.Weblog; import org.apache.roller.weblogger.util.RollerMessages; - /** - * Manages contents of the file uploaded to Roller weblogs. + * Manages contents of the file uploaded to Roller weblogs. * * This base implementation writes file content to a file system. */ public class FileContentManagerImpl implements FileContentManager { - + private static Log log = LogFactory.getLog(FileContentManagerImpl.class); - + private String storageDir = null; /** * Create file content manager. */ public FileContentManagerImpl() { - - String inStorageDir = WebloggerConfig.getProperty("mediafiles.storage.dir"); - + + String inStorageDir = WebloggerConfig + .getProperty("mediafiles.storage.dir"); + // Note: System property expansion is now handled by WebloggerConfig. - + if (inStorageDir == null || inStorageDir.trim().length() < 1) { - inStorageDir = System.getProperty("user.home") - + File.separator+"roller_data"+File.separator+"mediafiles"; + inStorageDir = System.getProperty("user.home") + File.separator + + "roller_data" + File.separator + "mediafiles"; } - + if (!inStorageDir.endsWith(File.separator)) { inStorageDir += File.separator; } - - this.storageDir = inStorageDir.replace('/',File.separatorChar); - } - - public void initialize() { + this.storageDir = inStorageDir.replace('/', File.separatorChar); + + System.out + .println(" ***** FileContentManager mediafiles.storage.dir : " + + inStorageDir); } + public void initialize() { + } /** - * @see org.apache.roller.weblogger.business.FileContentManager#getFileContent(Weblog, String) + * @see org.apache.roller.weblogger.business.FileContentManager#getFileContent(Weblog, + * String) */ - public FileContent getFileContent(Weblog weblog, String fileId) + public FileContent getFileContent(Weblog weblog, String fileId) throws FileNotFoundException, FilePathException { - + // get a reference to the file, checks that file exists & is readable File resourceFile = this.getRealFile(weblog, fileId); - + // make sure file is not a directory - if(resourceFile.isDirectory()) { - throw new FilePathException("Invalid file id ["+fileId+"], "+ - "path is a directory."); + if (resourceFile.isDirectory()) { + throw new FilePathException("Invalid file id [" + fileId + "], " + + "path is a directory."); } - + // everything looks good, return resource return new FileContent(weblog, fileId, resourceFile); } - + /** - * @see org.apache.roller.weblogger.business.FileContentManager#saveFileContent(Weblog, String, - * java.io.InputStream) + * @see org.apache.roller.weblogger.business.FileContentManager#saveFileContent(Weblog, + * String, java.io.InputStream) */ - public void saveFileContent(Weblog weblog, - String fileId, - InputStream is) + public void saveFileContent(Weblog weblog, String fileId, InputStream is) throws FileNotFoundException, FilePathException, FileIOException { - + // make sure uploads area exists for this weblog File dirPath = this.getRealFile(weblog, null); - + // create File that we are about to save - File saveFile = new File(dirPath.getAbsolutePath() + File.separator + fileId); - + File saveFile = new File(dirPath.getAbsolutePath() + File.separator + + fileId); + byte[] buffer = new byte[RollerConstants.EIGHT_KB_IN_BYTES]; int bytesRead; OutputStream bos = null; try { bos = new FileOutputStream(saveFile); - while ((bytesRead = is.read(buffer, 0, RollerConstants.EIGHT_KB_IN_BYTES)) != -1) { + while ((bytesRead = is.read(buffer, 0, + RollerConstants.EIGHT_KB_IN_BYTES)) != -1) { bos.write(buffer, 0, bytesRead); - } - log.debug("The file has been written to ["+saveFile.getAbsolutePath()+"]"); + } + log.debug("The file has been written to [" + + saveFile.getAbsolutePath() + "]"); } catch (Exception e) { throw new FileIOException("ERROR uploading file", e); } finally { @@ -125,50 +129,52 @@ public void saveFileContent(Weblog weblog, bos.flush(); bos.close(); } - } catch (Exception ignored) {} + } catch (Exception ignored) { + } } - - + } - - + /** - * @see org.apache.roller.weblogger.business.FileContentManager#deleteFile(Weblog, String) + * @see org.apache.roller.weblogger.business.FileContentManager#deleteFile(Weblog, + * String) */ - public void deleteFile(Weblog weblog, String fileId) + public void deleteFile(Weblog weblog, String fileId) throws FileNotFoundException, FilePathException, FileIOException { - + // get path to delete file, checks that path exists and is readable File delFile = this.getRealFile(weblog, fileId); - + if (!delFile.delete()) { - log.warn("Delete appears to have failed for ["+fileId+"]"); + log.warn("Delete appears to have failed for [" + fileId + "]"); } } - + /** * @inheritDoc */ public void deleteAllFiles(Weblog weblog) throws FileIOException { // TODO: Implement } - + /** * @see org.apache.roller.weblogger.business.FileContentManager#overQuota(Weblog) */ public boolean overQuota(Weblog weblog) { - - String maxDir = WebloggerRuntimeConfig.getProperty("uploads.dir.maxsize"); + + String maxDir = WebloggerRuntimeConfig + .getProperty("uploads.dir.maxsize"); // maxDirSize in megabytes BigDecimal maxDirSize = new BigDecimal(maxDir); - long maxDirBytes = (long)(RollerConstants.ONE_MB_IN_BYTES * maxDirSize.doubleValue()); - + long maxDirBytes = (long) (RollerConstants.ONE_MB_IN_BYTES * maxDirSize + .doubleValue()); + try { File storageDirectory = this.getRealFile(weblog, null); long weblogDirSize = this.getDirSize(storageDirectory, true); - + return weblogDirSize > maxDirBytes; } catch (Exception ex) { // shouldn't ever happen, this means user's uploads dir is bad @@ -176,44 +182,41 @@ public boolean overQuota(Weblog weblog) { throw new RuntimeException(ex); } } - - + public void release() { } - - + /** - * @see org.apache.roller.weblogger.business.FileContentManager#canSave( - * Weblog, String, String, long, RollerMessages) + * @see org.apache.roller.weblogger.business.FileContentManager#canSave(Weblog, + * String, String, long, RollerMessages) */ - public boolean canSave(Weblog weblog, - String fileName, - String contentType, - long size, - RollerMessages messages) { - + public boolean canSave(Weblog weblog, String fileName, String contentType, + long size, RollerMessages messages) { + // first check, is uploading enabled? - if(!WebloggerRuntimeConfig.getBooleanProperty("uploads.enabled")) { + if (!WebloggerRuntimeConfig.getBooleanProperty("uploads.enabled")) { messages.addError("error.upload.disabled"); return false; } - + // second check, does upload exceed max size for file? BigDecimal maxFileMB = new BigDecimal( WebloggerRuntimeConfig.getProperty("uploads.file.maxsize")); - int maxFileBytes = (int)(RollerConstants.ONE_MB_IN_BYTES * maxFileMB.doubleValue()); - log.debug("max allowed file size = "+maxFileBytes); - log.debug("attempted save file size = "+size); + int maxFileBytes = (int) (RollerConstants.ONE_MB_IN_BYTES * maxFileMB + .doubleValue()); + log.debug("max allowed file size = " + maxFileBytes); + log.debug("attempted save file size = " + size); if (size > maxFileBytes) { - String[] args = {fileName, maxFileMB.toString()}; + String[] args = { fileName, maxFileMB.toString() }; messages.addError("error.upload.filemax", args); return false; } - + // third check, does file cause weblog to exceed quota? BigDecimal maxDirMB = new BigDecimal( WebloggerRuntimeConfig.getProperty("uploads.dir.maxsize")); - long maxDirBytes = (long)(RollerConstants.ONE_MB_IN_BYTES * maxDirMB.doubleValue()); + long maxDirBytes = (long) (RollerConstants.ONE_MB_IN_BYTES * maxDirMB + .doubleValue()); try { File storageDirectory = this.getRealFile(weblog, null); long userDirSize = getDirSize(storageDirectory, true); @@ -222,33 +225,37 @@ public boolean canSave(Weblog weblog, return false; } } catch (Exception ex) { - // shouldn't ever happen, means the weblogs uploads dir is bad somehow + // shouldn't ever happen, means the weblogs uploads dir is bad + // somehow // rethrow as a runtime exception throw new RuntimeException(ex); } - + // fourth check, is upload type allowed? - String allows = WebloggerRuntimeConfig.getProperty("uploads.types.allowed"); - String forbids = WebloggerRuntimeConfig.getProperty("uploads.types.forbid"); - String[] allowFiles = StringUtils.split(StringUtils.deleteWhitespace(allows), ","); - String[] forbidFiles = StringUtils.split(StringUtils.deleteWhitespace(forbids), ","); + String allows = WebloggerRuntimeConfig + .getProperty("uploads.types.allowed"); + String forbids = WebloggerRuntimeConfig + .getProperty("uploads.types.forbid"); + String[] allowFiles = StringUtils.split( + StringUtils.deleteWhitespace(allows), ","); + String[] forbidFiles = StringUtils.split( + StringUtils.deleteWhitespace(forbids), ","); if (!checkFileType(allowFiles, forbidFiles, fileName, contentType)) { - String[] args = {fileName, contentType}; + String[] args = { fileName, contentType }; messages.addError("error.upload.forbiddenFile", args); return false; } - + return true; } - - + /** * Get the size in bytes of given directory. * * Optionally works recursively counting subdirectories if they exist. */ private long getDirSize(File dir, boolean recurse) { - + long size = 0; if (dir.exists() && dir.isDirectory() && dir.canRead()) { @@ -258,7 +265,7 @@ private long getDirSize(File dir, boolean recurse) { for (File file : files) { if (!file.isDirectory()) { dirSize += file.length(); - } else if(recurse) { + } else if (recurse) { // count a subdirectory dirSize += getDirSize(file, recurse); } @@ -266,59 +273,58 @@ private long getDirSize(File dir, boolean recurse) { } size += dirSize; } - + return size; } - - + /** * Return true if file is allowed to be uplaoded given specified allowed and * forbidden file types. */ private boolean checkFileType(String[] allowFiles, String[] forbidFiles, - String fileName, String contentType) { - + String fileName, String contentType) { + // TODO: Atom Publishing Protocol figure out how to handle file // allow/forbid using contentType. // TEMPORARY SOLUTION: In the allow/forbid lists we will continue to // allow user to specify file extensions (e.g. gif, png, jpeg) but will // now also allow them to specify content-type rules (e.g. */*, image/*, // text/xml, etc.). - + // if content type is invalid, reject file - if (contentType == null || contentType.indexOf('/') == -1) { + if (contentType == null || contentType.indexOf('/') == -1) { return false; } - + // default to false boolean allowFile = false; - + // if this person hasn't listed any allows, then assume they want // to allow *all* filetypes, except those listed under forbid - if(allowFiles == null || allowFiles.length < 1) { + if (allowFiles == null || allowFiles.length < 1) { allowFile = true; } - + // First check against what is ALLOWED - + // check file against allowed file extensions if (allowFiles != null && allowFiles.length > 0) { - for (int y=0; y 0) { - for (int y=0; y 0) { - for (int x=0; x 0) { - for (int x=0; x