Storage.java
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:11k
- /* CVS ID: $Id: Storage.java,v 1.5 2000/04/06 08:02:02 wastl Exp $ */
- package net.wastl.webmail.server;
- import net.wastl.webmail.config.*;
- import net.wastl.webmail.xml.*;
- import java.io.*;
- import java.util.*;
- import org.apache.xalan.xslt.*;
- /*
- * Storage.java
- *
- * Created: Feb 1999
- *
- * Copyright (C) 1999-2000 Sebastian Schaffert
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /**
- *
- *
- * This provides a generic interface for getting and setting stored
- * data in WebMail.
- *
- * @see SimpleStorage
- * @author Sebastian Schaffert
- * @versin $Revision: 1.5 $
- */
- public abstract class Storage {
- public static final int LOG_DEBUG=10;
- public static final int LOG_INFO=5;
- public static final int LOG_WARN=3;
- public static final int LOG_ERR=1;
- public static final int LOG_CRIT=0;
- protected static boolean debug;
- protected WebMailServer parent;
- protected ConfigScheme cs;
- protected XMLSystemData sysdata;
- protected XMLGenericModel generic_model;
- public Storage(WebMailServer parent) {
- debug=WebMailServer.getDebug();
- this.parent=parent;
- cs=parent.getConfigScheme();
- cs.configRegisterYesNoKey("FOLDER TRY LOGIN PASSWORD","Try to connect folders with the user's login password if authentication fails");
- }
- public void initConfigKeys() {
- // Initialize the configuration file with the default or set parameters
- // needed to complete the XML file
- Enumeration enum=cs.getPossibleKeys();
- while(enum.hasMoreElements()) {
- String key=(String)enum.nextElement();
- if(!sysdata.isConfigSet(key)) {
- // We must use the raw method so the input doesn't get filtered.
- sysdata.setConfig(key,(String)cs.getDefaultValue(key),false,false);
- }
- }
- saveXMLSysData();
- }
- public void setDebug(boolean b) {
- debug=b;
- }
- public boolean getDebug() {
- return debug;
- }
- /**
- * Fetch all keys of the current configuration.
- */
- public Enumeration getConfigKeys() {
- return cs.getPossibleKeys();
- }
- /**
- * Fetch the configuration associated with the specified key.
- * @param key Identifier for the configuration
- */
- public String getConfig(String key) {
- return sysdata.getConfig(key);
- }
- /**
- * Set a configuration "key" to the specified value.
- * @param key Identifier for the configuration
- * @param value value to set
- */
- public void setConfig(String key, String value) throws IllegalArgumentException {
- if(!value.equals(getConfig(key))) {
- log(LOG_DEBUG,"Storage API: Setting configuration for '"+key+"' to '"+value+"'.");
- sysdata.setConfig(key,value);
- saveXMLSysData();
- }
- }
- /**
- * Get the String for key and the specified locale.
- * @param key Identifier for the String
- * @param locale locale of the String to fetch
- */
- public abstract String getStringResource(String key, Locale locale);
- /**
- * Get a xsl stylesheet for the specified locale and theme.
- * @param key Identifier for the String
- * @param locale locale of the String to fetch
- * @param theme theme where to look for the file
- */
- public abstract StylesheetRoot getStylesheet(String name, Locale locale, String theme) throws StylesheetNotFoundException;
- /**
- * Get a binary file (most likely an image) for the specified locale and theme.
- * @param key Identifier for the String
- * @param locale locale of the String to fetch
- * @param theme theme where to look for the file
- */
- public abstract byte[] getBinaryFile(String name, Locale locale, String theme) throws BinaryNotFoundException;
- /**
- * Calculate the document base path for a given locale and theme
- */
- public String getBasePath(Locale locale, String theme) {
- String language_path=(parent.getProperty("webmail.template.path")+
- System.getProperty("file.separator")+locale.getLanguage());
- File f=new File(language_path);
- if(!f.exists()) {
- language_path=(parent.getProperty("webmail.template.path")+
- System.getProperty("file.separator")+"default");
- }
- String theme_path=language_path+System.getProperty("file.separator")+theme;
- f=new File(theme_path);
- if(!f.exists()) {
- theme_path=language_path+System.getProperty("file.separator")+"default";
- }
- String basepath=theme_path+System.getProperty("file.separator");
- return basepath;
- }
- public XMLSystemData getSystemData() {
- return sysdata;
- }
- public XMLUserModel createXMLUserModel(XMLUserData data) {
- return new XMLUserModel(parent,sysdata.getSysData(),data.getUserData());
- }
- /**
- * Return a XML model that contains state and system information for administrator use
- */
- public XMLAdminModel createXMLAdminModel() {
- XMLAdminModel model=new XMLAdminModel(parent,sysdata.getSysData());
- model.init();
- model.update();
- return model;
- }
- /**
- * Return a generic XML model that only contains some state and system information.
- * This cannot be changed by a single session.
- */
- public XMLGenericModel createXMLGenericModel() {
- XMLGenericModel model=new XMLGenericModel(parent,sysdata.getSysData());
- model.init();
- model.update();
- return model;
- }
- /**
- * Return userlist for a given domain.
- */
- public abstract Enumeration getUsers(String domain);
- /**
- * @deprecated Use getUsers(String domain) instead
- */
- public Enumeration getUsers() {
- final Enumeration domains=getVirtualDomains();
- return new Enumeration() {
- Enumeration enum=null;
- public boolean hasMoreElements() {
- return (domains.hasMoreElements() || (enum != null && enum.hasMoreElements()));
- }
- public Object nextElement() {
- if(enum == null || !enum.hasMoreElements()) {
- if(domains.hasMoreElements()) {
- enum=getUsers((String)domains.nextElement());
- } else {
- return null;
- }
- }
- return enum.nextElement();
- }
- };
- }
- /**
- * Get the userdata for the specified user.
- * @param user Name of the user
- */
- public XMLUserData getUserData(String user, String domain,String passwd) throws InvalidPasswordException {
- return getUserData(user,domain,passwd,true);
- }
- /**
- * get the userdata for the specified user.
- * Should do some sort of authentication if authentication is set. See Authenticator class for example.
- */
- public abstract XMLUserData getUserData(String user, String domain, String password, boolean authenticate)
- throws InvalidPasswordException;
- /**
- * Compatibility wrapper for WebMail before 0.7
- * @deprecated use new domain-aware method instead
- */
- // public UserData getUserData(String user, String passwd, boolean authenticate)
- // throws InvalidPasswordException {
- // StringTokenizer tok=new StringTokenizer(user,"@");
- // String login=tok.nextToken();
- // String domain="nodomain";
- // if(tok.hasMoreTokens()) {
- // domain=tok.nextToken();
- // }
- // return (UserData)getUserData(login,domain,passwd,authenticate);
- // }
- /**
- * Save the userdata for the given user.
- * @param user Username of this user
- * @param domain Domain of this user
- */
- public abstract void saveUserData(String user, String domain);
- /**
- * Set the userdata for the specified user.
- * @param user Name of the user
- * @param userdata Data to store
- * @deprecated use saveUserData instead.
- */
- public void setUserData(String user, UserData userdata) {
- // Call saveUserData, do nothing with "userdata"
- StringTokenizer tok=new StringTokenizer(user,"@");
- String login=tok.nextToken();
- String domain="nodomain";
- if(tok.hasMoreTokens()) {
- domain=tok.nextToken();
- }
- saveUserData(login,domain);
- }
- /**
- * Delete a WebMail user
- * @param user Name of the user to delete
- * @param domain Domain of that user
- */
- public abstract void deleteUserData(String user, String domain);
- /**
- * Delete a WebMail user
- * @param user Name of the user to delete
- * @deprecated use deleteUserData(String user, String domain) instead.
- */
- public void deleteUserData(String user) {
- StringTokenizer tok=new StringTokenizer(user,"@");
- String login=tok.nextToken();
- String domain="nodomain";
- if(tok.hasMoreTokens()) {
- domain=tok.nextToken();
- }
- deleteUserData(user,domain);
- }
- /**
- * Set/add a WebMail virtual domain
- */
- public void setVirtualDomain(String name,WebMailVirtualDomain v) {
- sysdata.setVirtualDomain(name,v);
- saveXMLSysData();
- }
- /**
- * Get a WebMail virtual domain
- */
- public WebMailVirtualDomain getVirtualDomain(String name) {
- return sysdata.getVirtualDomain(name);
- }
- public WebMailVirtualDomain createVirtualDomain(String name) throws Exception {
- sysdata.createVirtualDomain(name);
- return sysdata.getVirtualDomain(name);
- }
- /**
- * Delete a WebMail virtual domain
- */
- public void deleteVirtualDomain(String name) {
- sysdata.deleteVirtualDomain(name);
- }
- /**
- * Return a list of virtual domains
- */
- public Enumeration getVirtualDomains() {
- return sysdata.getVirtualDomains();
- }
- /**
- * Return this Storage's Authenticator.
- * This is necessary for changing passwords or re-checking authentication.
- */
- public abstract Authenticator getAuthenticator();
- /**
- * Send a message to the logging facility.
- * @param level severity level of the message
- * @param message the message
- */
- public abstract void log(int level, String message);
- public abstract void shutdown();
- public void save() {
- saveXMLSysData();
- }
- protected abstract void loadXMLSysData();
- protected abstract void saveXMLSysData();
- public String getMimeType(String name) {
- String content_type;
- if(name != null && (name.toLowerCase().endsWith("jpg") || name.toLowerCase().endsWith("jpeg"))) {
- content_type="IMAGE/JPEG";
- } else if(name != null && name.toLowerCase().endsWith("gif")) {
- content_type="IMAGE/GIF";
- } else if(name != null && name.toLowerCase().endsWith("png")) {
- content_type="IMAGE/PNG";
- } else if(name != null && name.toLowerCase().endsWith("txt")) {
- content_type="TEXT/PLAIN";
- } else if(name != null && (name.toLowerCase().endsWith("htm") || name.toLowerCase().endsWith("html"))) {
- content_type="TEXT/HTML";
- } else {
- content_type="APPLICATION/OCTET-STREAM";
- }
- return content_type;
- }
-
- }