⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filemailbox.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }    /**     * Indicates if specified user can change any flag on a permanent basis,     * except for \Recent which can never be changed by a user.     *     * @param username String represnting user     * @return true if specified user can change all flags permanently.     */    public synchronized boolean allFlags(String username)        throws AccessControlException {        // relies on implementation that each right implies those        // before it in list:  l,r,s,w,i,p,c,d,a        return hasDeleteRights(username);    }    /**     * Indicates which flags this user can change permanently. If allFlags()     * returns true for this user, then this method must have the same return     * value as getSupportedFlags.     *     * @param username String represnting user     * @return String a space seperated list of message flags which this user     * can set permanently     */    public  synchronized  String getPermanentFlags(String username)        throws AccessControlException {        if (hasDeleteRights(username)) {            return SYSTEM_FLAGS;        } else if (hasWriteRights(username)) {            return "\\Seen \\Answered \\Flagged \\Draft";        } else if (hasKeepSeenRights(username)) {            return "\\Seen";        } else {            return "";        }    }    /**     * Provides a reference to the access control list for this mailbox.     *     * @return the AccessControlList for this Mailbox     */    //   public ACL getACL();    /**     * Indicates state in which  the mailbox will be opened by specified user.     * A return value of true indicates Read Only, false indicates Read-Write     * and an AccessControlException is thrown if user does not have read     * rights.     * <p>Implementations decide if Read Only means only lookup and read     * rights (lr) or lookup, read and keep seen rights (lrs). This may even     * vary between mailboxes.     *     * @param username String represnting user     * @return true if specified user can only open the mailbox Read-Only.     * @throws AccessControlException if the user can not open this mailbox     * at least Read-Only.     */    public synchronized boolean isReadOnly(String username)        throws AccessControlException {        return (! hasWriteRights(username));    }    // Message handling methods ---------------------------    /**     * Stores a message in this mailbox. User must have insert rights.     *     * @param message the MimeMessage to be stored     * @param username String represnting user     * @return boolean true if successful     * @throws AccessControlException if username does not have lookup rights     * for this mailbox.     * @throws AuthorizationException if username has lookup rights but does     * not have insert rights.     */    public synchronized boolean store(MimeMessage message, String username)        throws AccessControlException, AuthorizationException,               IllegalArgumentException {        if (message == null || username == null) {            getLogger().error("Null argument received in store.");            throw new IllegalArgumentException("Null argument received in store.");        }        if (!hasInsertRights(username)) { //throws AccessControlException            throw new AuthorizationException("Not authorized to insert.");        }        SimpleMessageAttributes attrs = new SimpleMessageAttributes();        try {            setupLogger(attrs);            attrs.setAttributesFor(message);        } catch (javax.mail.MessagingException me) {            throw new RuntimeException("Exception creating SimpleMessageAttributes: " + me);        }        Flags flags = new Flags();        flags.initialize();        return store(message, username, attrs, flags);    }    /**     * Stores a message in this mailbox, using passed MessageAttributes and     * Flags. User must have insert rights.     * <br>Current implementation requires MessageAttributs to be of     * class SimpleMessageAttributes     *     * @param mail the message to be stored     * @param username String represnting user     * @param msgAttrs non-null MessageAttributes for use with this Message     * @return boolean true if successful     * @throws AccessControlException if username does not have lookup     * rights for this mailbox.     * @throws AuthorizationException if username has lookup rights but does     * not have insert rights.     */    public boolean store(MimeMessage message, String username,                         MessageAttributes msgAttrs, Flags flags)        throws AccessControlException, AuthorizationException,               IllegalArgumentException {        if (msgAttrs == null || message == null || username == null) {            getLogger().error("Null argument received in store.");            throw new IllegalArgumentException("Null argument received in store.");        }        if (! (msgAttrs instanceof SimpleMessageAttributes)) {            getLogger().error("Wrong class for Attributes");            throw new IllegalArgumentException("Wrong class for Attributes");        }        SimpleMessageAttributes attrs = (SimpleMessageAttributes)msgAttrs;        highestUID.increase();        int newUID = highestUID.get();        attrs.setUID(newUID);        sequence.add(new Integer(newUID));        attrs.setMessageSequenceNumber(sequence.size());        BufferedOutputStream outMsg = null;        ObjectOutputStream outAttrs = null;        try {            // SK:UPDATE            path = getPath( absoluteName, owner, rootPath );            outMsg = new BufferedOutputStream( new FileOutputStream(path + File.separator + newUID + MESSAGE_EXTENSION));            message.writeTo(outMsg);            outMsg.close();            outAttrs = new ObjectOutputStream( new FileOutputStream(path + File.separator + newUID + ATTRIBUTES_EXTENSION));            outAttrs.writeObject(attrs);            outAttrs.close();        } catch(Exception e) {            getLogger().error("Error writing message to disc: " + e);            e.printStackTrace();            throw new                RuntimeException("Exception caught while storing Mail: "                                 + e);        } finally {            try {                outMsg.close();                outAttrs.close();            } catch (IOException ie) {                getLogger().error("Error closing streams: " + ie);            }        }        marked = true;        if (flags.isRecent()) {            recentMessages.add(new Integer(newUID));        }        if (flags.isDeleted()) {            messagesForDeletion.add(new Integer(newUID));        }        //if (!flags.isSeen(username)) {        //If a user had no unseen messages, they do, now.        Iterator it = oldestUnseenMessage.keySet().iterator();        while (it.hasNext()) {            String user = (String)it.next();            if ( ((Integer)oldestUnseenMessage.get(user)).intValue() == -1) {                oldestUnseenMessage.put(user, new Integer(newUID));            }        }        //}        writeFlags(newUID, flags);        getLogger().info("Mail " + newUID + " written in " + absoluteName);        return true;    }    /**     * Retrieves a message given a message sequence number.     *     * @param msn the message sequence number     * @param username String represnting user     * @return an  MimeMessageWrapper object containing the message, null if no message with     * the given msn.     * @throws AccessControlException if user does not have read rights for     * this mailbox.     * @throws AuthorizationException if user has lookup rights but does not     * have read rights.     */    public synchronized MimeMessageWrapper retrieve(int msn, String user)        throws AccessControlException, AuthorizationException {        if (!hasReadRights(user)) { //throws AccessControlException            throw new AuthorizationException("Not authorized to read.");        }        if (msn > sequence.size()) {            return null;        } else {            int uid = ((Integer)sequence.get(msn - 1)).intValue();            return retrieveUID(uid, user);        }    }    /**     * Retrieves a message given a unique identifier.     *     * @param uid the unique identifier of a message     * @param username String represnting user     * @return an MimeMessageWrapper object containing the message, null if no message with     * the given msn.     * @throws AccessControlException if user does not have read rights for     * this mailbox.     * @throws AuthorizationException if user has lookup rights but does not     * have read rights.     */    public synchronized MimeMessageWrapper retrieveUID(int uid, String user)        throws AccessControlException, AuthorizationException {        if (!hasReadRights(user)) { //throws AccessControlException            throw new AuthorizationException("Not authorized to read.");        }        MimeMessageWrapper response = null;        if (sequence.contains(new Integer(uid))) {            //BufferedInputStream inMsg = null;            try {                path = getPath( absoluteName, owner, rootPath );        MimeMessageFileSource source = new MimeMessageFileSource(path + File.separator + uid + MESSAGE_EXTENSION);                response = new MimeMessageWrapper(source);             //   inMsg.close();            } catch(Exception e) {                getLogger().error("Error reading message from disc: " + e);                e.printStackTrace();                throw new                    RuntimeException("Exception caught while retrieving Mail: "                                     + e);            } //finally {          //      try {            //        inMsg.close();          //      } catch (IOException ie) {          //          getLogger().error("Error closing streams: " + ie);          //      }          //  }            getLogger().info("MimeMessageWrapper " + uid + " read from " + absoluteName);            return response;        } else {            return null;        }    }    /**     * Marks a message for deletion given a message sequence number.     *     * @param msn the message sequence number     * @param username String represnting user     * @return boolean true if successful.     * @throws AccessControlException if user does not have read rights for     * this mailbox.     * @throws AuthorizationException if user has lookup rights but does not     * have delete rights.     */    public synchronized boolean markDeleted(int msn, String user)        throws AccessControlException, AuthorizationException {        if (!hasDeleteRights(user)) { //throws AccessControlException            throw new AuthorizationException("Not authorized to delete.");        }        Assert.notImplemented();        //TBD        return false;    }    /**     * Marks a message for deletion given a unique identifier.     *     * @param uidunique identifier     * @param username String represnting user     * @return boolean true if successful, false if failed including no     * message with the given uid.     * @throws AccessControlException if user does not have read rights for     * this mailbox.     * @throws AuthorizationException if user has lookup rights but does not     * have delete rights.     */    public synchronized boolean markDeletedUID(int uid, String user)        throws AccessControlException, AuthorizationException {        if (!hasDeleteRights(user)) { //throws AccessControlException            throw new AuthorizationException("Not authorized to delete.");        }        Assert.notImplemented();        //TBD        return false;    }    /**     * Returns the message attributes for a message.     *     * @param msn message sequence number     * @param username String represnting user     * @return MessageAttributes for message, null if no such message.     * Changing the MessageAttributes object must not affect the actual     * MessageAttributes.     * @throws AccessControlException if user does not have read rights for     * this mailbox.     * @throws AuthorizationException if user has lookup rights but does not     * have delete rights.     */    public synchronized MessageAttributes getMessageAttributes(int msn, String user)        throws AccessControlException, AuthorizationException {        if (!hasReadRights(user)) { //throws AccessControlException            throw new AuthorizationException("Not authorized to read.");        }        System.out.println("msn: "+msn);        System.out.println("sequence.size: "+sequence.size());        if (msn > sequence.size()) {            return null;        } else {            int uid = ((Integer)sequence.get(msn - 1)).intValue();            return getMessageAttributesUID(uid, user);        }    }

⌨️ 快捷键说明

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