📄 jameshost.java
字号:
* lookup rights. * @throws AuthorizationException if mailbox could be created locally but * user does not have create rights. * @throws MailboxException if mailbox already exists, locally or remotely, * or if mailbox cannot be created locally. * @see FolderRecord */ public synchronized ACLMailbox createMailbox( String user, String mailboxName ) throws AccessControlException, AuthorizationException, MailboxException { Assert.isTrue( Assert.ON && user != null && user.length() > 0 && mailboxName != null ); String absoluteName = getAbsoluteMailboxName( user, mailboxName ); Assert.isTrue( Assert.ON && absoluteName != null ); getLogger().debug( "JamesHost createMailbox() for: " + absoluteName ); return createAbsoluteMailbox( user, absoluteName ); } private synchronized ACLMailbox createAbsoluteMailbox( String user, String absoluteName ) throws AccessControlException, AuthorizationException, MailboxException { Assert.isTrue( Assert.ON && absoluteName.startsWith( NAMESPACE_TOKEN ) && absoluteName.indexOf( HIERARCHY_SEPARATOR ) != -1 ); ACLMailbox mailbox = null; FolderRecord record = null; ACLMailbox parentMailbox = null; // Has a folder with this name ever been created? if ( recordRep.containsRecord( absoluteName ) ) { getLogger().error( "Attempt to create an existing Mailbox." ); throw new MailboxException( "Mailbox already exists", MailboxException.ALREADY_EXISTS_LOCALLY ); } else { // Get the directory holding the new mailbox. String parent = absoluteName.substring( 0, absoluteName.lastIndexOf( HIERARCHY_SEPARATOR ) ); //Recurse to a created and not deleted mailbox try { parentMailbox = getAbsoluteMailbox( user, parent ); } catch ( MailboxException mbe ) { if ( mbe.getStatus().equals( MailboxException.NOT_LOCAL ) || mbe.getStatus().equals( MailboxException.LOCAL_BUT_DELETED ) ) { parentMailbox = createAbsoluteMailbox( user, parent ); } else { throw new MailboxException( mbe.getMessage(), mbe.getStatus() ); } } // Does user have create rights in parent mailbox? boolean hasCreateRights = parentMailbox.hasCreateRights( user ); releaseMailbox( user, parentMailbox ); if ( ! hasCreateRights ) { releaseMailbox( user, parentMailbox ); throw new AuthorizationException( "User does not have create rights." ); } try { //TODO: BUG! Here MUST be used a Factory for using FILE / JDBC !!! mailbox = new FileMailbox(); setupLogger( mailbox ); mailbox.configure( conf ); mailbox.contextualize( context ); mailbox.compose( compMgr ); mailbox.prepareMailbox( user, absoluteName, user, recordRep.nextUIDValidity() ); mailbox.initialize(); } catch ( Exception e ) { getLogger().error( "Exception creating mailbox: " + e ); throw new MailboxException( "Exception creating mailbox: " + e ); } SimpleFolderRecord fr = new SimpleFolderRecord( user, absoluteName ); fr.initialize(); recordRep.store( fr ); openMailboxes.addMailbox( absoluteName, mailbox ); } return mailbox; } /** * Releases a reference to a mailbox, allowing Host to do any housekeeping. * * @param mailbox a non-null reference to an ACL Mailbox. */ public void releaseMailbox( String user, ACLMailbox mailbox ) { if ( mailbox == null ) { getLogger().debug( "Attempt to release mailbox with null reference" ); return; } if ( user != MailServer.MDA ) { mailbox.unsetRecent(); } String absoluteName = mailbox.getAbsoluteName(); int count = openMailboxes.removeReference( absoluteName ); if ( count == 0 ) { openMailboxes.removeMailbox( absoluteName ); try { FolderRecord fr = recordRep.retrieve( absoluteName ); fr.setUidValidity( mailbox.getUIDValidity() ); fr.setHighestUid( mailbox.getNextUID() - 1 ); fr.setLookupRights( mailbox.getUsersWithLookupRights() ); fr.setReadRights( mailbox.getUsersWithReadRights() ); fr.setMarked( mailbox.isMarked() ); fr.setNotSelectableByAnyone( mailbox.isNotSelectableByAnyone() ); fr.setExists( mailbox.getExists() ); fr.setRecent( mailbox.getRecent() ); fr.setUnseenbyUser( mailbox.getUnseenByUser() ); recordRep.store( fr ); mailbox.dispose(); mailbox = null; getLogger().info( "Mailbox object destroyed: " + absoluteName ); } catch ( Exception e ) { getLogger().error( "Exception destroying mailbox object: " + e ); e.printStackTrace(); } } else { getLogger().info( "Mailbox " + absoluteName + " now has " + count + "live references" ); } } /** * Deletes an existing MailBox. Specified mailbox must already exist on * this server, and the user must have rights to delete it. (Mailbox delete * rights are implementation defined, one way is if the user would have the * right to create it). * Implementations must track deleted mailboxes * * @param user email address on whose behalf the request is made. * @param mailboxName String name of the target * @return true if mailbox deleted successfully * @throws MailboxException if mailbox does not exist locally or is any * identities INBOX. * @throws AuthorizationException if mailbox exists locally but user does * not have rights to delete it. * @see FolderRecord */ public boolean deleteMailbox( String user, String mailboxName ) throws MailboxException, AuthorizationException, AccessControlException { Assert.isTrue( Assert.ON && user != null && mailboxName != null && user.length() > 0 && mailboxName.length() > 0 ); String absoluteName = getAbsoluteMailboxName( user, mailboxName ); getLogger().debug( "JamesHost deleteMailbox() called for: " + absoluteName ); return deleteAbsoluteMailbox( user, absoluteName ); } private boolean deleteAbsoluteMailbox( String user, String absoluteName ) throws MailboxException, AuthorizationException, AccessControlException { if ( ! recordRep.containsRecord( absoluteName ) ) { throw new MailboxException( "Mailbox doesn't exist" ); } int count = openMailboxes.getReferenceCount( absoluteName ); if ( count > 0 ) { throw new MailboxException( "Mailbox is currently selected by another user" ); } ACLMailbox mailbox = getAbsoluteMailbox( user, absoluteName ); if ( ! mailbox.hasDeleteRights( user ) ) { throw new AuthorizationException( "No delete rights" ); } // Get child folders of this mailbox Collection childList = listMailboxes( MailServer.MDA, absoluteName, "%", false ); if ( ! childList.isEmpty() ) { if ( mailbox.isNotSelectableByAnyone() ) { throw new MailboxException( "Mailbox with \\Noselect AND subfolders cannot be deleted" ); } else { // Delete and expunge all messages, and set NotSelectableByAnyone. deleteAllMessages( mailbox, user ); mailbox.setNotSelectableByAnyone( true ); releaseMailbox( user, mailbox ); } } else { deleteAllMessages( mailbox, user ); Assert.isTrue( Assert.ON && mailbox.getExists() == 0 ); openMailboxes.removeMailbox( absoluteName ); recordRep.deleteRecord( recordRep.retrieve( absoluteName ) ); mailbox.removeMailbox(); } return true; } private void deleteAllMessages( ACLMailbox mailbox, String user ) throws AccessControlException, AuthorizationException { // Delete all messages in this box int messageCount = mailbox.getExists(); for ( int i = 0; i < messageCount; i++ ) { mailbox.markDeleted( i + 1, user ); } mailbox.expunge( user ); } /** * Renames an existing MailBox. The specified mailbox must already * exist locally, the requested name must not exist locally already but * must be able to be created locally and the user must have rights to * delete the existing mailbox and create a mailbox with the new name. * Any inferior hierarchical names must also be renamed. * If INBOX is renamed, the contents of INBOX are transferred to a new * folder with the new name, but INBOX is not deleted. If INBOX has * inferior mailboxes these are not renamed. * It is an error to create a mailbox with the name of a mailbox that has * been deleted, if that name is still in use. * Implementations must track deleted mailboxes * * @param user email address on whose behalf the request is made. * @param oldMailboxName String name of the existing mailbox * @param newMailboxName String target new name * @return true if rename completed successfully * @throws MailboxException if mailbox does not exist locally, or there * is an existing mailbox with the new name. * @throws AuthorizationException if user does not have rights to delete * the existing mailbox or create the new mailbox. * @see FolderRecord */ public boolean renameMailbox( String user, String oldMailboxName, String newMailboxName ) throws MailboxException, AuthorizationException { // At first we MUST ensure, that the User has the rights for renaming not only the Directory // that he wanted to rename, but all subdiretories too. // Also we have to "rename" the subdirectories as well. try { ACLMailbox mailbox = this.getMailbox(user,oldMailboxName); String oldAbsoluteName = getAbsoluteMailboxName(user, oldMailboxName); //trying to get children Mailboxes Collection coll = this.listMailboxes(user,"",oldMailboxName+"*",false); java.util.TreeMap tr = new java.util.TreeMap(); Iterator it = coll.iterator(); int mindepth=9999; int maxdepth=0; while(it.hasNext()) { // BUG: Whats about Mailbox Names with an Whitespace ? StringTokenizer strl = new StringTokenizer((String) it.next()); String mailboxname = ""; boolean mbxfound=false; while(strl.hasMoreTokens()) { String token = strl.nextToken(); if(token.equals("\""+this.HIERARCHY_SEPARATOR+"\"") || mbxfound) { // This must be the FileSeparator Token. After that comes the name of the Mailbox if(mbxfound){ mailboxname += " "+token; } else { mailboxname += strl.nextToken(); } mbxfound=true; } } // StringTokenizer is only used to get the depth of the Mailboxname strl = new StringTokenizer(mailboxname, this.HIERARCHY_SEPARATOR); mailboxname = mailboxname.substring(1,mailboxname.length()-1); System.out.println("RENAME: MAILBOXNAME FOUND: "+mailboxname); // Create Collection (or first try to get one from the Sorted Tree) and then // save this to the TreeMap. The TreeMap is used to order the found Mailbox Names after their // depth. It's needed to first change the Mailbox Properties for all Subdirectories, and update the // FolderRecords. Object oldcoll = tr.get(new Integer(strl.countTokens())); if (oldcoll==null) { HashSet hs = new HashSet(); hs.add(mailboxname); tr.put(new Integer(strl.countTokens()), hs); } else { HashSet hs = (HashSet) oldcoll; hs.add(mailboxname); tr.put(new Integer(strl.countTokens()), hs); } if(maxdepth<strl.countTokens()) { maxdepth=strl.countTokens(); } if(mindepth>strl.countTokens()) { mindepth=strl.countTokens(); } } System.out.println("RENAME: POSSIBLE MAXDEPTH FOUND: "+maxdepth+" THE MINDEPTH IS "+mindepth); for(int i=maxdepth;i>=0;i--) { Collection cole = (Collection) tr.get(new Integer(i)); if (cole!=null) { Iterator ite = cole.iterator(); while(ite.hasNext()) { String mbxhere = (String) ite.next(); String absoluteName = getAbsoluteMailboxName(user, mbxhere); System.out.println("RENAME: MAILBOXES FOR INTEGER "+i+ " NAME "+mbxhere+" ABSOLUTE "+absoluteName); ACLMailbox mmm = this.getAbsoluteMailbox(user,absoluteName); if (i!=mindepth) { String suboldabsolutename = mmm.getAbsoluteName(); mmm.renameSubMailbox(user, oldAbsoluteName, newMailboxName); // THis is the new AbsoluteName with # and so on String subnewabsolutename = mmm.getAbsoluteName(); // Delete the old FolderRecord FolderRecord fr = recordRep.retrieve(suboldabsolutename); recordRep.deleteRecord(fr); // Create a new FolderRecord and store this in the recordRepository // Can anybody explain me the use of this repository ??? // I think OpenMailbox and Repository is relatively redundant
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -