📄 xmluserhandler.java
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * * * Copyright 2008 Jun Li(SiChuan University, the School of * * Software Engineering). All rights reserved. * * * * Licensed to the JMS under one or more contributor license * * agreements. See the LICENCE file distributed with this * * work for additional information regarding copyright * * ownership. The JMS licenses this file you may not use this * * file except in compliance with the License. * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.jpxx.mail.User;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.apache.log4j.Logger;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;import org.jpxx.mail.Constants;import org.jpxx.mail.Exception.InvalidEmailAddressException;import org.jpxx.mail.Factory;import org.jpxx.mail.Domain.DomainHandler;import org.jpxx.mail.Util.EmailAddress;import org.jpxx.mail.Util.FileUtils;import org.jpxx.mail.Util.Security.MD5;import org.jpxx.mail.Config.SysConfig;/** * Xml database * * @author Jun Li * @version $Revision: 0.0.3 $, $Date: 2008/10/27 $ * * @since version 0.0.1 * */public class XmlUserHandler extends AbstractMailUserHandler { /** * Creates an instance of Logger and initializes it. * It is to write log for <code>XmlUserHandler</code>. */ private Logger log = Factory.getSingletonInstance().getLogger(this); private SysConfig sc = null; private DomainHandler dc = null; public XmlUserHandler() { sc = new SysConfig(); dc = new DomainHandler(); } /** * @see org.jpxx.mail.User.MailUserHandler#check( * EmailAddress emailAddress, String password, String id) */ public boolean check(EmailAddress emailAddress, String password, String id) { String domain = emailAddress.getDomain(); if (!dc.isExist(domain)) { return false; } String path = getFilePath(domain); User user = null; try { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(path)); user = this.getUser(doc, emailAddress.getUserName(), domain); } catch (Exception ex) { return false; } if (user == null) { return false; } String pwd = user.getPassword(); if (id == null || id.trim().equals("")) { return password.equals(pwd); } String encryptedPwd = MD5.encode(id + pwd); if (password.equals(encryptedPwd)) { return true; } return false; } /** * Check user exist or not * * @see org.jpxx.mail.User.MailUserHandler#checkUser(String user,String domain) */ public boolean checkUser(String user, String domain) { try { EmailAddress ea = new EmailAddress(user + "@" + domain); return checkUser(ea); } catch (InvalidEmailAddressException ex) { log.info(ex.getMessage()); return false; } } /** * @see org.jpxx.mail.User.MailUserHandler#checkUser(EmailAddress emailAddress) */ public boolean checkUser(EmailAddress emailAddress) { String domain = emailAddress.getDomain(); if (!dc.isExist(domain)) { return false; } String path = getFilePath(domain); try { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(path)); User user = this.getUser(doc, emailAddress.getUserName(), domain); if (user != null) { return true; } } catch (Exception ex) { log.error(ex.toString()); } return false; } public boolean addUser(String userName, String password, long size) { String domain = dc.getDefaultDomain(); if (domain == null) { log.error("Please add a domain to server!"); return false; } return addUser(userName, password, domain, size); } /** * @param userName * @param password * @param domain * @param size * @return If add successfully then reutrn true, else return false. */ public boolean addUser( String userName, String password, String domain, long size) { if (!dc.isExist(domain) || this.checkUser(userName, domain)) { return false; } String path = getFilePath(domain); try { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(path)); Element root = doc.getRootElement(); Element _name = new Element("Name"); _name.setText(userName); Element _password = new Element("Password"); _password.setText(password); Element _size = new Element("Size"); _size.setText(String.valueOf(size)); Element user = new Element("User"); user.addContent(_name); user.addContent(_password); user.addContent(_size); root.addContent(user); File f = new File(sc.getHomeDir(userName, domain)); if (!f.exists()) { f.mkdirs(); } return this.save(doc, path); } catch (Exception ex) { log.error(ex.toString()); } return false; } /** * * @param userName Current user's name * @param password User's new password * @param size Reset mailbox size * @return If modify Successfully return true, otherwise return false */ public boolean modifyUser(String userName, String password, long size) { String domain = dc.getDefaultDomain(); if (domain == null) { log.error("Please add a domain to server!"); return false; } return modifyUser(userName, password, domain, size); } /** * @see MailUserHandler#modifyUser(java.lang.String, java.lang.String, * java.lang.String, long) * @param userName Current user's name * @param password User's new password * @param domain User's mailbox domain * @param size Reset mailbox size * @return If modify Successfully return true, otherwise return false */ public boolean modifyUser( String userName, String password, String domain, long size) { if (!dc.isExist(domain)) { return false; } String path = getFilePath(domain); try { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(path)); Element root = doc.getRootElement(); List list = root.getChildren("User"); for (Iterator i = list.iterator(); i.hasNext();) { Element user = (Element) i.next(); Element _name = user.getChild("Name"); if (_name.getTextTrim().equals(userName)) { if (password != null && !password.trim().equals("")) { Element _password = user.getChild("Password"); _password.setText(password); } if (size > 0) { Element _size = user.getChild("Size"); _size.setText(String.valueOf(size)); } return this.save(doc, path); } } } catch (Exception ex) { ex.printStackTrace(); log.error(ex.toString()); } return false; } /** * @see MailUserHandler#deleteUser(java.lang.String, java.lang.String) */ public boolean deleteUser(String userName, String domain) { if (!dc.isExist(domain) || !this.checkUser(userName, domain)) { return false; } String path = getFilePath(domain); try { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(path)); Element root = doc.getRootElement(); List list = root.getChildren("User"); for (Iterator i = list.iterator(); i.hasNext();) { Element user = (Element) i.next(); Element _name = user.getChild("Name"); if (_name != null && _name.getTextTrim().equals(userName)) { root.removeContent(user); FileUtils.delete( Constants.HOME_DIR + domain + File.separator + userName + File.separator); return this.save(doc, path); } } } catch (Exception ex) { log.error(ex.toString()); } return false; } /** * List all users in a domain. * * @param domain mail server domain * @return users list. * * @since JMS 0.0.3 */ public List<String> list(String domain) { if (!dc.isExist(domain)) { return null; } String path = getFilePath(domain); try { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(path)); Element root = doc.getRootElement(); List list = root.getChildren("User"); List<String> users = new ArrayList<String>(); for (Iterator i = list.iterator(); i.hasNext();) { Element user = (Element) i.next(); Element _name = user.getChild("Name"); if (_name != null) { users.add(_name.getTextTrim()); } } return users; } catch (Exception ex) { log.error(ex.toString()); } return null; } /** * Get a user's information * @param userName User's login name * @param domain mail server domain * @return User object * * @since JMS 0.0.3 */ public User getUser(String userName, String domain) { try { String path = getFilePath(domain); SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build(new File(path)); return getUser(doc, userName, domain); } catch (Exception ex) { log.error(ex.toString()); } return null; } /** * @param doc * @param userName * @return User object */ private User getUser(Document doc, String userName, String domain) { Element root = doc.getRootElement(); List list = root.getChildren("User"); for (Iterator i = list.iterator(); i.hasNext();) { Element e = (Element) i.next(); if (e.getChild("Name").getText().equals(userName)) { User user = new User( userName, e.getChild("Password").getText(), domain, Long.parseLong(e.getChild("Size").getText())); return user; } } return null; } /** * Create Xml config File * * @param path Config file path */ private void create(String filePath) { Element root = new Element("UserList"); Document doc = new Document(); doc.setContent(root); FileWriter writer; XMLOutputter outputter = new XMLOutputter(); try { writer = new FileWriter(filePath); Format format = Format.getPrettyFormat(); format.setEncoding("UTF-8"); outputter.setFormat(format); outputter.output(doc, writer); writer.close(); } catch (IOException e) { } } /** * * @param domain * @return The file path. */ private String getFilePath(String domain) { String userXmlFileDir = Constants.HOME_DIR + domain + File.separator; String filePath = userXmlFileDir + Constants.USER_DATA_FILE; //log.error(emailAddress.toString()); File f = new File(userXmlFileDir); if (!f.exists()) { f.mkdirs(); } f = new File(filePath); if (!f.exists()) { create(filePath); } return filePath; } /** * Save document. * * @param doc Document * @param path The xml file path. * @return If svae successfully then return true. */ private boolean save(Document doc, String path) { XMLOutputter outputter = new XMLOutputter(); try { FileWriter writer = new FileWriter(path); Format format = Format.getPrettyFormat(); format.setEncoding("UTF-8"); format.setIndent(" "); outputter.setFormat(format); outputter.output(doc, writer); writer.close(); return true; } catch (IOException e) { return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -