KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > commands > RenameCommand


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver.commands;
19
20 import org.apache.james.imapserver.AuthorizationException;
21 import org.apache.james.imapserver.ImapRequest;
22 import org.apache.james.imapserver.ImapSession;
23 import org.apache.james.imapserver.ImapSessionState;
24 import org.apache.james.imapserver.MailboxException;
25
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.List JavaDoc;
28
29 class RenameCommand extends AuthenticatedSelectedStateCommand
30 {
31     public RenameCommand()
32     {
33         this.commandName = "RENAME";
34
35         this.getArgs().add( new AstringArgument( "oldName" ) );
36         this.getArgs().add( new AstringArgument( "newName" ) );
37     }
38
39     public boolean doProcess( ImapRequest request, ImapSession session, List JavaDoc argValues )
40     {
41         String JavaDoc command = this.getCommand();
42
43         String JavaDoc folder = (String JavaDoc) argValues.get( 0 );
44         String JavaDoc newName = (String JavaDoc) argValues.get( 1 );
45
46         if ( session.getCurrentFolder() == folder ) {
47             session.noResponse( command, "You can't rename a folder while you have it selected." );
48             return true;
49         }
50         try {
51             if ( session.getImapHost().renameMailbox( session.getCurrentUser(), folder, newName ) ) {
52                 session.okResponse( command );
53             }
54             else {
55                 session.noResponse( command, "Rename failed, unknown error" );
56                 getLogger().info( "Attempt to rename mailbox " + folder
57                                   + " to " + newName
58                                   + " by user " + session.getCurrentUser() + " failed." );
59             }
60         }
61         catch ( MailboxException mbe ) {
62             if ( mbe.getStatus().equals( MailboxException.NOT_LOCAL ) ) {
63                 session.taggedResponse( NO_NOTLOCAL_MSG );
64             }
65             else {
66                 session.noResponse( command, mbe.getMessage() );
67             }
68             return true;
69         }
70         catch ( AuthorizationException aze ) {
71             session.noResponse( command, "You do not have the rights to delete mailbox: " + folder );
72             return true;
73         }
74         if ( session.getState() == ImapSessionState.SELECTED ) {
75             session.checkSize();
76             session.checkExpunge();
77         }
78         return true;
79     }
80 }
81
Popular Tags