KickJava   Java API By Example, From Geeks To Geeks.

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


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.AccessControlException;
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.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * List Command for listing some mailboxes.
33  * See RFC 2060 for more details.
34  *
35  * @version 0.2 on 04 Aug 2002
36  */

37
38 class ListCommand extends AuthenticatedSelectedStateCommand
39 {
40     public ListCommand()
41     {
42         this.commandName = "LIST";
43
44         this.getArgs().add( new AstringArgument( "reference name" ) );
45         this.getArgs().add( new AstringArgument( "mailbox" ) );
46     }
47
48     protected boolean doProcess( ImapRequest request, ImapSession session, List JavaDoc argValues )
49     {
50         String JavaDoc command = this.commandName;
51
52         boolean subscribeOnly;
53         if ( command.equalsIgnoreCase( "LIST" ) ) {
54             subscribeOnly = false;
55         }
56         else {
57             subscribeOnly = true;
58         }
59
60         String JavaDoc reference = (String JavaDoc) argValues.get( 0 );
61         String JavaDoc folder = (String JavaDoc) argValues.get( 1 );
62
63         Collection JavaDoc list = null;
64         try {
65         System.out.println("getImapHost: "+session.getImapHost().getClass().getName());
66             list = session.getImapHost().listMailboxes( session.getCurrentUser(), reference, folder,
67                                                         subscribeOnly );
68             if ( list == null ) {
69                 session.noResponse( command, " unable to interpret mailbox" );
70             }
71             else if ( list.size() == 0 ) {
72                 getLogger().debug( "List request matches zero mailboxes: " + request.getCommandRaw() );
73                 session.okResponse( command );
74             }
75             else {
76                 Iterator JavaDoc it = list.iterator();
77                 while ( it.hasNext() ) {
78                     String JavaDoc listResponse = (String JavaDoc) it.next();
79                     session.getOut().println( UNTAGGED + SP + command.toUpperCase()
80                                                + SP + listResponse );
81                     getLogger().debug( UNTAGGED + SP + command.toUpperCase()
82                                        + SP + listResponse );
83                 }
84                 session.okResponse( command );
85             }
86         }
87         catch ( MailboxException mbe ) {
88             if ( mbe.isRemote() ) {
89                 session.noResponse( command, "[REFERRAL "
90                                            + mbe.getRemoteServer() + "]"
91                                            + SP + "Wrong server. Try remote." );
92             }
93             else {
94                 session.noResponse( command, "No such mailbox" );
95             }
96             return true;
97         }
98         catch ( AccessControlException ace ) {
99             session.noResponse( command, "No such mailbox" );
100             session.logACE( ace );
101             return true;
102         }
103
104         if ( session.getState() == ImapSessionState.SELECTED ) {
105             session.checkSize();
106             session.checkExpunge();
107         }
108         return true;
109     }
110 }
111
Popular Tags