KickJava   Java API By Example, From Geeks To Geeks.

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


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.ACLMailbox;
22 import org.apache.james.imapserver.ImapRequest;
23 import org.apache.james.imapserver.ImapSession;
24 import org.apache.james.imapserver.ImapSessionState;
25
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.List JavaDoc;
28
29 class SelectCommand extends AuthenticatedSelectedStateCommand
30 {
31     public SelectCommand()
32     {
33         this.commandName = "SELECT";
34
35         this.getArgs().add( new AstringArgument( "mailbox" ) );
36     }
37
38     protected boolean doProcess( ImapRequest request, ImapSession session, List JavaDoc argValues )
39     {
40         String JavaDoc command = this.getCommand();
41
42         // selecting a mailbox deselects current mailbox,
43
// even if this select fails
44
if ( session.getState() == ImapSessionState.SELECTED ) {
45             session.getCurrentMailbox().removeMailboxEventListener( session );
46             session.getImapHost().releaseMailbox( session.getCurrentUser(), session.getCurrentMailbox() );
47             session.setState( ImapSessionState.AUTHENTICATED );
48             session.setCurrentMailbox( null );
49             session.setCurrentIsReadOnly( false );
50         }
51
52         String JavaDoc folder = (String JavaDoc) argValues.get( 0 );
53         ACLMailbox mailbox = getMailbox( session, folder, command );
54         if ( mailbox == null ) {
55             return true;
56         }
57         else {
58             session.setCurrentMailbox( mailbox );
59         }
60
61         try { // long tries clause against an AccessControlException
62
if ( !session.getCurrentMailbox().hasReadRights( session.getCurrentUser() ) ) {
63                 session.noResponse( command, "Read access not granted." );
64                 return true;
65             }
66             if ( command.equalsIgnoreCase( "SELECT" ) ) {
67                 if ( !session.getCurrentMailbox().isSelectable( session.getCurrentUser() ) ) {
68                     session.noResponse( "Mailbox exists but is not selectable" );
69                     return true;
70                 }
71             }
72
73             // Have mailbox with at least read rights. Server setup.
74
session.getCurrentMailbox().addMailboxEventListener( session );
75             session.setCurrentFolder( folder );
76             session.setState( ImapSessionState.SELECTED );
77             getLogger().debug( "Current folder for user " + session.getCurrentUser() + " from "
78                                + session.getRemoteHost() + "(" + session.getRemoteIP() + ") is "
79                                + session.getCurrentFolder() );
80
81             // Inform client
82
session.getOut().println( UNTAGGED + SP + "FLAGS ("
83                                        + session.getCurrentMailbox().getSupportedFlags() + ")" );
84             if ( !session.getCurrentMailbox().allFlags( session.getCurrentUser() ) ) {
85                 session.untaggedResponse( " [PERMANENTFLAGS ("
86                                            + session.getCurrentMailbox().getPermanentFlags( session.getCurrentUser() )
87                                            + ") ]" );
88             }
89             session.checkSize();
90             session.getOut().println( UNTAGGED + SP + OK + " [UIDVALIDITY "
91                                        + session.getCurrentMailbox().getUIDValidity() + "]" );
92             int oldestUnseen = session.getCurrentMailbox().getOldestUnseen( session.getCurrentUser() );
93             if ( oldestUnseen > 0 ) {
94                 session.getOut().println( UNTAGGED + SP + OK + " [UNSEEN "
95                                            + oldestUnseen + "] " + oldestUnseen + " is the first unseen" );
96             }
97             else {
98                 session.getOut().println( UNTAGGED + SP + OK + " No unseen messages" );
99             }
100             session.setSequence( session.getCurrentMailbox().listUIDs( session.getCurrentUser() ));
101
102             if ( command.equalsIgnoreCase( "EXAMINE" ) ) {
103                 session.setCurrentIsReadOnly( true );
104
105                 session.okResponse("[READ-ONLY] " + command );
106                 return true;
107
108             }
109             else if ( session.getCurrentMailbox().isReadOnly( session.getCurrentUser() ) ) {
110                 session.setCurrentIsReadOnly( true );
111                 session.okResponse( "[READ-ONLY] " + command );
112                 return true;
113             }
114             session.okResponse( "[READ-WRITE] " + command );
115             return true;
116         }
117         catch ( AccessControlException ace ) {
118             session.noResponse( command, "No such mailbox." );
119             session.logACE( ace );
120             return true;
121         }
122     }
123 }
124
Popular Tags