📄 listcommand.java
字号:
/*********************************************************************** * Copyright (c) 2000-2004 The Apache Software Foundation. * * All rights reserved. * * ------------------------------------------------------------------- * * Licensed under the Apache License, Version 2.0 (the "License"); you * * may not use this file except in compliance with the License. You * * may obtain a copy of the License at: * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * * implied. See the License for the specific language governing * * permissions and limitations under the License. * ***********************************************************************/package org.apache.james.imapserver.commands;import org.apache.james.imapserver.store.ImapMailbox;import org.apache.james.imapserver.ImapRequestLineReader;import org.apache.james.imapserver.ImapResponse;import org.apache.james.imapserver.ImapSession;import org.apache.james.imapserver.store.MailboxException;import org.apache.james.imapserver.ProtocolException;import org.apache.james.util.Assert;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** * Handles processeing for the LIST imap command. * * * @version $Revision: 1.3.2.3 $ */class ListCommand extends AuthenticatedStateCommand{ public static final String NAME = "LIST"; public static final String ARGS = "<reference-name> <mailbox-name-with-wildcards>"; private ListCommandParser parser = new ListCommandParser(); /** @see CommandTemplate#doProcess */ protected void doProcess( ImapRequestLineReader request, ImapResponse response, ImapSession session ) throws ProtocolException, MailboxException { String referenceName = parser.mailbox( request ); String mailboxPattern = parser.listMailbox( request ); parser.endLine( request ); // Should the #user.userName section be removed from names returned? boolean removeUserPrefix; Collection mailboxes; if ( mailboxPattern.length() == 0 ) { // An empty mailboxPattern signifies a request for the hierarchy delimiter // and root name of the referenceName argument String referenceRoot; if ( referenceName.startsWith( NAMESPACE_PREFIX ) ) { // A qualified reference name - get the first element, // and don't remove the user prefix removeUserPrefix = false; int firstDelimiter = referenceName.indexOf( HIERARCHY_DELIMITER_CHAR ); if ( firstDelimiter == -1 ) { referenceRoot = referenceName; } else { referenceRoot = referenceName.substring(0, firstDelimiter ); } } else { // A relative reference name - need to remove user prefix from results. referenceRoot = ""; removeUserPrefix = true; } // Get the mailbox for the reference name. ImapMailbox referenceMailbox = getMailbox( referenceRoot, session, false ); // If it doesn't exist, act as though "" was passed for reference name. if ( referenceMailbox == null ) { referenceMailbox = getMailbox( "", session, true ); removeUserPrefix = true; } mailboxes = new ArrayList( 1 ); mailboxes.add( referenceMailbox ); } else { String searchPattern; // If the mailboxPattern is fully qualified, ignore the // reference name. if ( mailboxPattern.charAt( 0 ) == NAMESPACE_PREFIX_CHAR ) { searchPattern = mailboxPattern; } else { searchPattern = combineSearchTerms( referenceName, mailboxPattern ); } // If the search pattern is relative, need to remove user prefix from results. removeUserPrefix = ( searchPattern.charAt(0) != NAMESPACE_PREFIX_CHAR ); mailboxes = doList( session, searchPattern ); } String personalNamespace = USER_NAMESPACE + HIERARCHY_DELIMITER_CHAR + session.getUser().getUserName(); int prefixLength = personalNamespace.length(); Iterator iterator = mailboxes.iterator(); while ( iterator.hasNext() ) { ImapMailbox mailbox = ( ImapMailbox ) iterator.next(); StringBuffer message = new StringBuffer( "(" ); if ( !mailbox.isSelectable() ) { message.append( "\\Noselect" ); } message.append( ") \"" ); message.append( HIERARCHY_DELIMITER_CHAR ); message.append( "\" " ); String mailboxName = mailbox.getFullName(); if ( removeUserPrefix ) { if ( mailboxName.length() <= prefixLength ) { mailboxName = ""; } else { mailboxName = mailboxName.substring( prefixLength + 1 ); } } // TODO: need to check if the mailbox name needs quoting. if ( mailboxName.length() == 0 ) { message.append("\"\""); } else { message.append( mailboxName ); } response.commandResponse( this, message.toString() ); } session.unsolicitedResponses( response ); response.commandComplete( this ); } protected Collection doList( ImapSession session, String searchPattern ) throws MailboxException { return session.getHost().listMailboxes( session.getUser(), searchPattern ); } private String combineSearchTerms( String referenceName, String mailboxMatch ) { Assert.isTrue( mailboxMatch.length() > 0 ); // Otherwise, combine the referenceName and mailbox name. StringBuffer buffer = new StringBuffer( mailboxMatch ); // Make sure the 2 strings are joined by only one HIERARCHY_DELIMITER_CHAR if ( referenceName.endsWith( HIERARCHY_DELIMITER ) ) { if ( buffer.charAt(0) == HIERARCHY_DELIMITER_CHAR ) { buffer.deleteCharAt( 0 ); } } else { if ( buffer.charAt(0) != HIERARCHY_DELIMITER_CHAR ) { buffer.insert( 0, HIERARCHY_DELIMITER_CHAR ); } } buffer.insert( 0, referenceName ); return buffer.toString(); } /** @see ImapCommand#getName */ public String getName() { return NAME; } /** @see CommandTemplate#getArgSyntax */ public String getArgSyntax() { return ARGS; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -