📄 commandfetch.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;import org.apache.james.imapserver.AccessControlException;import org.apache.james.imapserver.AuthorizationException;import org.apache.james.core.MimeMessageWrapper;import org.apache.james.imapserver.commands.ImapCommand;import javax.mail.MessagingException;import javax.mail.internet.InternetHeaders;import java.io.IOException;import java.io.OutputStream;import java.io.PrintWriter;import java.util.*;/** * Implements the IMAP FETCH command for a given ImapRequestImpl. * * References: rfc 2060, rfc 2193, rfc 2221 * @version 0.2 on 04 Aug 2002 */public class CommandFetch extends BaseCommand implements ImapCommand{ //mainly to switch on stack traces and catch responses; private static final boolean DEEP_DEBUG = true; private static final String OK = "OK"; private static final String NO = "NO"; private static final String BAD = "BAD"; private static final String UNTAGGED = "*"; private static final String SP = " "; private StringTokenizer commandLine; private boolean useUIDs; private ACLMailbox currentMailbox; private String commandRaw; private PrintWriter out; private OutputStream outs; private String tag; private String user; private SingleThreadedConnectionHandler caller; private String currentFolder; public boolean validForState( ImapSessionState state ) { return ( state == ImapSessionState.SELECTED ); } public boolean process( ImapRequest request, ImapSession session ) { setRequest( request ); StringTokenizer txt = request.getCommandLine();/* System.out.println("CommandFetch.process: #args="+txt.countTokens()); while (txt.hasMoreTokens()) { System.out.println("CommandFetch.process: arg='"+txt.nextToken()+"'"); }*/ // ((ImapRequestImpl)request).setCommandLine(new java.util.StringTokenizer(request.getCommandRaw())); if ( request.arguments() < 2 ) { session.badResponse( "#args="+request.arguments()+" '"+request.getCommandLine().nextToken()+"', '"+request.getCommandLine().nextToken()+"' Command should be <tag> <FETCH> <message set> <message data item names>" ); return true; } service(); return true; } /** * Debugging method - will probably disappear */ public void setRequest(ImapRequest request) { commandLine = request.getCommandLine(); useUIDs = request.useUIDs(); currentMailbox = request.getCurrentMailbox(); System.out.println("currentMailbox="+((currentMailbox!=null)?currentMailbox.getClass().getName():"null")); commandRaw = request.getCommandRaw(); tag = request.getTag(); currentFolder = request.getCurrentFolder(); caller = request.getCaller(); out = caller.getPrintWriter(); outs = caller.getOutputStream(); user = caller.getUser(); } /** * Implements IMAP fetch commands given an ImapRequestImpl. * This implementation attempts to satisfy the fetch command with the * smallest objects deserialized from storage. * <p>Warning - maybecome service(ImapRequestImpl request) * <p>Not yet complete - no partial (octet-counted or sub-parts) fetches. */ public void service() { // decode the message set List set; List uidsList = null; String setArg = commandLine.nextToken(); if (useUIDs) { uidsList = currentMailbox.listUIDs(user); set = decodeUIDSet(setArg, uidsList); } else { set = decodeSet(setArg, currentMailbox.getExists()); } if (DEEP_DEBUG) { getLogger().debug("Fetching message set of size: " + set.size()); } String firstFetchArg = commandLine.nextToken(); int pos = commandRaw.indexOf(firstFetchArg); //int pos = commandRaw.indexOf(setArg) + setArg.length() + 1; String fetchAttrsRaw = null; if (firstFetchArg.startsWith("(")) { //paranthesised fetch attrs fetchAttrsRaw = commandRaw.substring(pos + 1, commandRaw.lastIndexOf(")")); } else { fetchAttrsRaw = commandRaw.substring(pos); } if (DEEP_DEBUG) { System.out.println("Found fetchAttrsRaw: " + fetchAttrsRaw); getLogger().debug("Found fetchAttrsRaw: " + fetchAttrsRaw); } // decode the fetch attributes List fetchAttrs = new ArrayList(); StringTokenizer fetchTokens = new StringTokenizer(fetchAttrsRaw); while (fetchTokens.hasMoreTokens()) { String attr = fetchTokens.nextToken(); if (attr.indexOf("(") == -1 ) { //not the start of a fields list fetchAttrs.add(attr); } else { StringBuffer attrWithFields = new StringBuffer(); attrWithFields.append(fetchAttrs.remove(fetchAttrs.size() -1)); attrWithFields.append(" " + attr); boolean endOfFields = false; while (! endOfFields && fetchTokens.hasMoreTokens()) { String field = fetchTokens.nextToken(); attrWithFields.append(" " + field); if (field.indexOf(")") != -1) { endOfFields = true; } } fetchAttrs.add(attrWithFields.toString()); } } fetchAttrs = convertMacroCommands( fetchAttrs ); try { for (int i = 0; i < set.size(); i++) { Integer uidObject = null; int uid = 0; int msn = 0; if (useUIDs) { System.out.println("USE UIDS"); uidObject = (Integer)set.get(i); uid = uidObject.intValue(); msn = uidsList.indexOf(uidObject) + 1; } else { msn = ((Integer)set.get(i)).intValue(); } MessageAttributes attrs = null; String flags = null; //EnhancedMimeMessage msg = null; MimeMessageWrapper msg = null; String response = UNTAGGED + SP + msn + SP + "FETCH ("; boolean responseAdded = false; Iterator it = fetchAttrs.iterator(); while(it.hasNext()) { String arg = (String) it.next(); // commands that only need flags object if (arg.equalsIgnoreCase("FLAGS")) { if (flags == null) { if (useUIDs) { System.out.println("TRYING UIDFLAGS"+uid); flags = currentMailbox.getFlagsUID(uid, user); } else { System.out.println("TRYING MSNFLAGS"+msn); flags = currentMailbox.getFlags(msn, user); } } if (flags == null) { // bad //out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message flags."); //System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message flags."); //return; } if (responseAdded) { response += SP + "FLAGS " + flags ; } else { response += "FLAGS " + flags ; responseAdded = true; } } // command that only need MessageAttributes object else if (arg.equalsIgnoreCase("INTERNALDATE")) { System.out.println("Starting INTERNALDATE"); if (attrs == null) { if (useUIDs) { attrs = currentMailbox.getMessageAttributesUID(uid, user); } else { attrs = currentMailbox.getMessageAttributes(msn, user); } } if (attrs == null) { // bad out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes."); System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes."); getLogger().error("Retrieved null attributes for msn:" + msn); return; } if (responseAdded) { response += SP + "INTERNALDATE \"" + attrs.getInternalDateAsString() + "\"" ; } else { response += "INTERNALDATE \"" + attrs.getInternalDateAsString() + "\"" ; responseAdded = true; } } else if (arg.equalsIgnoreCase("RFC822.SIZE")) { System.out.println("Starting RFC822.SIZE"); if (attrs == null) { if (useUIDs) { attrs = currentMailbox.getMessageAttributesUID(uid, user); } else { attrs = currentMailbox.getMessageAttributes(msn, user); } } if (attrs == null) { // bad out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes."); System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes."); getLogger().error("Retrieved null attributes for msn:" + msn); return; } if (responseAdded) { response += SP + "RFC822.SIZE " + attrs.getSize(); } else { response += "RFC822.SIZE " + attrs.getSize(); responseAdded = true; } } else if (arg.equalsIgnoreCase("ENVELOPE")) { System.out.println("Starting ENVELOPE"); if (attrs == null) { if (useUIDs) { attrs = currentMailbox.getMessageAttributesUID(uid, user); } else { attrs = currentMailbox.getMessageAttributes(msn, user); } } if (attrs == null) { // bad out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes."); System.out.println(tag + SP + msn + SP + NO + SP + "Error retrieving message attributes."); getLogger().error("Retrieved null attributes for msn:" + msn); return; } if (responseAdded) { response += SP + "ENVELOPE " + attrs.getEnvelope(); } else { response += "ENVELOPE " + attrs.getEnvelope(); responseAdded = true; } } else if (arg.equalsIgnoreCase("BODY")) { System.out.println("CommandFetch BODY start"); if (attrs == null) { System.out.println("CommandFetch BODY fetching attrs"); if (useUIDs) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -