dbattachmentmanager.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 346 行

JAVA
346
字号
/** * $RCSfile: DbAttachmentManager.java,v $ * $Revision: 1.5 $ * $Date: 2002/07/26 14:34:57 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import com.jivesoftware.forum.*;import com.jivesoftware.forum.util.*;import com.jivesoftware.jdom.*;import com.jivesoftware.jdom.output.*;import java.util.*;import java.io.*;/** * Database implementation of the AttachmentManager Interface. * * @author Matt Tucker */public class DbAttachmentManager implements AttachmentManager {    private static XMLProperties properties = null;    private int maxAttachmentSize = 1024;    private int maxAttachmentsPerMessage = 5;    private List allowedList = new Vector();    private List disallowedList = new Vector();    boolean allowAllByDefault = true;    private static File attachmentDir = null;    boolean imagePreviewEnabled = false;    int imagePreviewMaxSize = 25;    boolean imagePreviewRatioEnabled = true;    public DbAttachmentManager() {        loadProperties();        // General attachment settings        try {            maxAttachmentSize = Integer.parseInt(properties.getProperty(                "maxAttachmentSize"));            maxAttachmentsPerMessage = Integer.parseInt(properties.getProperty(                "maxAttachmentsPerMessage"));        } catch (NumberFormatException e) { }        try {            allowedList = stringToList(properties.getProperty("allowedTypes"));            disallowedList = stringToList(properties.getProperty(                    "disallowedTypes"));            String propVal = properties.getProperty("allowAllByDefault");            if (propVal != null) {                allowAllByDefault = Boolean.valueOf(propVal).booleanValue();            }        } catch (Exception e) { }        // Image preview settings.        try {            String val = properties.getProperty("imagePreview.enabled");            if (val != null) {                imagePreviewEnabled = Boolean.valueOf(val).booleanValue();            }        } catch (Exception e) { }        try {            String val = properties.getProperty("imagePreview.maxSize");            if (val != null) {                imagePreviewMaxSize = Integer.parseInt(val);            }        } catch (Exception e) { }        try {            String val = properties.getProperty("imagePreview.preserveAspectRatio");            if (val != null) {                imagePreviewRatioEnabled = Boolean.valueOf(val).booleanValue();            }        } catch (Exception e) { }    }    public int getMaxAttachmentSize() {        return maxAttachmentSize;    }    public void setMaxAttachmentSize(int maxAttachmentSize) {        this.maxAttachmentSize = maxAttachmentSize;        properties.setProperty("maxAttachmentSize", ""+maxAttachmentSize);    }    public int getMaxAttachmentsPerMessage() {        return maxAttachmentsPerMessage;    }    public void setMaxAttachmentsPerMessage(int maxAttachmentsPerMessage) {        this.maxAttachmentsPerMessage = maxAttachmentsPerMessage;        properties.setProperty("maxAttachmentsPerMessage",                ""+maxAttachmentsPerMessage);    }    public boolean isValidType(String contentType) {        if (allowAllByDefault) {            return !disallowedList.contains(contentType);        }        else {            return allowedList.contains(contentType);        }    }    public void addAllowedType(String contentType) {        if (!allowedList.contains(contentType)) {            allowedList.add(contentType);        }        properties.setProperty("allowedTypes", listToString(allowedList));    }    public void removeAllowedType(String contentType) {        allowedList.remove(contentType);        properties.setProperty("allowedTypes", listToString(allowedList));    }    public Iterator allowedTypes() {        return Collections.unmodifiableList(allowedList).iterator();    }    public void addDisallowedType(String contentType) {         if (!disallowedList.contains(contentType)) {            disallowedList.add(contentType);        }        properties.setProperty("disallowedTypes", listToString(disallowedList));    }    public void removeDisallowedType(String contentType) {        disallowedList.remove(contentType);        properties.setProperty("disallowedTypes", listToString(disallowedList));    }    public Iterator disallowedTypes() {        return Collections.unmodifiableList(disallowedList).iterator();    }    public boolean getAllowAllByDefault() {        return allowAllByDefault;    }    public void setAllowAllByDefault(boolean allowAllByDefault) {        this.allowAllByDefault = allowAllByDefault;        properties.setProperty("allowAllByDefault",                String.valueOf(allowAllByDefault));    }    public boolean isImagePreviewEnabled() {        return imagePreviewEnabled;    }    public void setImagePreviewEnabled(boolean imagePreviewEnabled) {        this.imagePreviewEnabled = imagePreviewEnabled;        properties.setProperty("imagePreview.enabled",                String.valueOf(imagePreviewEnabled));    }    public int getImagePreviewMaxSize() {        return imagePreviewMaxSize;    }    public void setImagePreviewMaxSize(int imagePreviewMaxSize) {        this.imagePreviewMaxSize = imagePreviewMaxSize;        properties.setProperty("imagePreview.maxSize", "" + imagePreviewMaxSize);        clearImagePreviewCache();    }    public boolean isImagePreviewRatioEnabled() {        return imagePreviewRatioEnabled;    }    public void setImagePreviewRatioEnabled(boolean imagePreviewRatioEnabled) {        this.imagePreviewRatioEnabled = imagePreviewRatioEnabled;        properties.setProperty("imagePreview.preserveAspectRatio",            String.valueOf(imagePreviewEnabled));        clearImagePreviewCache();    }    // OTHER METHODS //    /**     * Returns a Map for the list of content types to image names. The     * JiveServlet class uses this method.     */    public static Map getImagePreviewMappings() {        loadProperties();        String [] propNames = properties.getChildrenProperties("imagePreview.mappings");        Map map = new HashMap();        for (int i=0; i<propNames.length; i++) {            String prop = properties.getProperty("imagePreview.mappings." +                    propNames[i]);            int commaIdx = prop.indexOf(",");            if (commaIdx > 0) {                String name = prop.substring(0, commaIdx);                String value = prop.substring(commaIdx+1);                map.put(name, value);            }        }        return map;    }    /**     * Returns the directory that attachments are stored in.     */    synchronized static File getAttachmentDir() {        loadProperties();        if (attachmentDir == null) {            // See if the directory is set as a property. If not, assume            // attachments are stored in jiveHome/attachments            String dir = properties.getProperty("directory");            if (dir != null) {                attachmentDir = new File(dir);            }            else {                attachmentDir = new File(JiveGlobals.getJiveHome() +                        File.separator + "attachments");            }        }        return attachmentDir;    }    /**     * Deletes all images in the image preview cache directory.     */    private synchronized void clearImagePreviewCache() {        try {            File dir = new File(getAttachmentDir(), "cache");            if (dir.exists()) {                File [] files = dir.listFiles();                for (int i=0; i<files.length; i++) {                    files[i].delete();                }            }        }        catch (Exception e) {            e.printStackTrace();        }    }    /**     * Loads a property manager for attachment properties if it isn't already     * loaded. If an XML file for the attachment properties isn't already     * created, it will attempt to make a file with default values.     */    private static synchronized void loadProperties() {        if (properties == null) {            String gatewayXML = JiveGlobals.getJiveHome() + File.separator +                "jive_attachments.xml";            // Make sure the file actually exists. If it doesn't, a new file            // will be created.            File file = new File(gatewayXML);            // If it doesn't exists we have to create it.            if (!file.exists()) {                Document doc = new Document(new Element("jiveAttachments"));                // Now, write out to the file.                OutputStream out = null;                try {                    // Use JDOM's XMLOutputter to do the writing and formatting.                    XMLOutputter outputter = new XMLOutputter("    ", true);                    out = new BufferedOutputStream(new FileOutputStream(file));                    outputter.output(doc, out);                }                catch (Exception e) {                    e.printStackTrace();                }                finally {                    try {  out.close();  }                    catch (Exception e) { }                }            }            // Finally, create xml properties with the file.            XMLProperties p = null;            try {                p = new XMLProperties(gatewayXML);            }            catch (IOException ioe) {                ioe.printStackTrace();            }            // Add in image preview mappings.            p.setProperty("imagePreview.mappings.entry0",                    "application/msword,msword.gif");            p.setProperty("imagePreview.mappings.entry1",                    "application/vnd.ms-excel,msexcel.gif");            p.setProperty("imagePreview.mappings.entry2",                    "application/vnd.ms-powerpoint,mspowerpoint.gif");            p.setProperty("imagePreview.mappings.entry3","application/pdf,pdf.gif");            p.setProperty("imagePreview.mappings.entry4","application/zip,zip.gif");            p.setProperty("imagePreview.mappings.entry5",                    "application/x-gzip-compressed,zip.gif");            p.setProperty("imagePreview.mappings.entry6",                    "application/x-zip-compressed,zip.gif");            p.setProperty("imagePreview.mappings.entry7","image/bmp,image.gif");            p.setProperty("imagePreview.mappings.entry8","image/gif,image.gif");            p.setProperty("imagePreview.mappings.entry9","image/jpeg,image.gif");            p.setProperty("imagePreview.mappings.entry10","image/pjpeg,image.gif");            p.setProperty("imagePreview.mappings.entry11","image/jpg,image.gif");            p.setProperty("imagePreview.mappings.entry12","image/pjpg,image.gif");            p.setProperty("imagePreview.mappings.entry13","image/psd,image.gif");            p.setProperty("imagePreview.mappings.entry14","image/tiff,image.gif");            p.setProperty("imagePreview.mappings.entry15","image/x-photoshop,image.gif");            p.setProperty("imagePreview.mappings.entry16","image/x-png,image.gif");            p.setProperty("imagePreview.mappings.entry17","text/richtext,rtf.gif");            p.setProperty("imagePreview.mappings.entry18","text/rtf,rtf.gif");            p.setProperty("imagePreview.mappings.entry19","text/plain,txt.gif");            properties = p;        }    }    /**     * Turns a list into a String.     */    private static String listToString(List list) {        StringBuffer buf = new StringBuffer();        for (Iterator iter = list.iterator(); iter.hasNext(); ) {            String element = (String)iter.next();            buf.append(element).append(",");        }        return buf.toString();    }    /**     * Turns a String into a List.     */    private static List stringToList(String string) {        List list = new Vector();        if (string != null) {            StringTokenizer tokens = new StringTokenizer(string, ",");            while (tokens.hasMoreTokens()) {                list.add(tokens.nextToken());            }        }        return list;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?