001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.javamail.store.imap.connection; 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import javax.mail.MessagingException; 023 024 import org.apache.geronimo.mail.util.Base64; 025 026 /** 027 * Util class to represent a response from a IMAP server 028 * 029 * @version $Rev: 594520 $ $Date: 2007-11-13 07:57:39 -0500 (Tue, 13 Nov 2007) $ 030 */ 031 public class IMAPTaggedResponse extends IMAPResponse { 032 033 // the reply state 034 protected String status; 035 // the tag associated with a reply. 036 protected String tag; 037 // the message associated with the completion response 038 protected String message; 039 040 /** 041 * Create a command completion response for a 042 * submitted command. The tag prefix identifies 043 * the command this response is for. 044 * 045 * @param tag The command tag value. 046 * @param status The Status response (OK, BAD, or NO). 047 * @param message The remainder of the response, as a string. 048 * @param response The response data used to create the reply object. 049 */ 050 public IMAPTaggedResponse(String tag, String status, String message, byte [] response) { 051 super(response); 052 this.tag = tag; 053 this.status = status; 054 this.message = message; 055 } 056 057 058 /** 059 * Create a continuation response for a 060 * submitted command. 061 * 062 * @param response The response data used to create the reply object. 063 */ 064 public IMAPTaggedResponse(byte [] response) { 065 super(response); 066 this.tag = ""; 067 this.status = "CONTINUATION"; 068 this.message = message; 069 } 070 071 /** 072 * Test if the response code was "OK". 073 * 074 * @return True if the response status was OK, false for any other status. 075 */ 076 public boolean isOK() { 077 return status.equals("OK"); 078 } 079 080 /** 081 * Test for an error return from a command. 082 * 083 * @return True if the response status was BAD. 084 */ 085 public boolean isBAD() { 086 return status.equals("BAD"); 087 } 088 089 /** 090 * Test for an error return from a command. 091 * 092 * @return True if the response status was NO. 093 */ 094 public boolean isNO() { 095 return status.equals("NO"); 096 } 097 098 /** 099 * Get the message included on the tagged response. 100 * 101 * @return The String message data. 102 */ 103 public String getMessage() { 104 return message; 105 } 106 107 /** 108 * Decode the message portion of a continuation challenge response. 109 * 110 * @return The byte array containing the decoded data. 111 */ 112 public byte[] decodeChallengeResponse() 113 { 114 // we're passed back a challenge value, Base64 encoded. Decode that portion of the 115 // response data. 116 return Base64.decode(response, 2, response.length - 2); 117 } 118 119 120 /** 121 * Test if this is a continuation response. 122 * 123 * @return True if this a continuation. false for a normal tagged response. 124 */ 125 public boolean isContinuation() { 126 return status.equals("CONTINUATION"); 127 } 128 129 130 /** 131 * Test if the untagged response includes a given 132 * status indicator. Mostly used for checking 133 * READ-ONLY or READ-WRITE status after selecting a 134 * mail box. 135 * 136 * @param name The status name to check. 137 * 138 * @return true if this is found in the "[" "]" delimited 139 * section of the response message. 140 */ 141 public boolean hasStatus(String name) { 142 // see if we have the status bits at all 143 int statusStart = message.indexOf('['); 144 if (statusStart == -1) { 145 return false; 146 } 147 148 int statusEnd = message.indexOf(']'); 149 String statusString = message.substring(statusStart, statusEnd).toUpperCase(); 150 // just search for the status token. 151 return statusString.indexOf(name) != -1; 152 } 153 } 154 155