1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You 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 implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.geronimo.javamail.store.imap.connection; 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import javax.mail.MessagingException; 23 24 import org.apache.geronimo.mail.util.Base64; 25 26 /** 27 * Util class to represent a response from a IMAP server 28 * 29 * @version $Rev: 594520 $ $Date: 2007-11-13 07:57:39 -0500 (Tue, 13 Nov 2007) $ 30 */ 31 public class IMAPTaggedResponse extends IMAPResponse { 32 33 // the reply state 34 protected String status; 35 // the tag associated with a reply. 36 protected String tag; 37 // the message associated with the completion response 38 protected String message; 39 40 /** 41 * Create a command completion response for a 42 * submitted command. The tag prefix identifies 43 * the command this response is for. 44 * 45 * @param tag The command tag value. 46 * @param status The Status response (OK, BAD, or NO). 47 * @param message The remainder of the response, as a string. 48 * @param response The response data used to create the reply object. 49 */ 50 public IMAPTaggedResponse(String tag, String status, String message, byte [] response) { 51 super(response); 52 this.tag = tag; 53 this.status = status; 54 this.message = message; 55 } 56 57 58 /** 59 * Create a continuation response for a 60 * submitted command. 61 * 62 * @param response The response data used to create the reply object. 63 */ 64 public IMAPTaggedResponse(byte [] response) { 65 super(response); 66 this.tag = ""; 67 this.status = "CONTINUATION"; 68 this.message = message; 69 } 70 71 /** 72 * Test if the response code was "OK". 73 * 74 * @return True if the response status was OK, false for any other status. 75 */ 76 public boolean isOK() { 77 return status.equals("OK"); 78 } 79 80 /** 81 * Test for an error return from a command. 82 * 83 * @return True if the response status was BAD. 84 */ 85 public boolean isBAD() { 86 return status.equals("BAD"); 87 } 88 89 /** 90 * Test for an error return from a command. 91 * 92 * @return True if the response status was NO. 93 */ 94 public boolean isNO() { 95 return status.equals("NO"); 96 } 97 98 /** 99 * 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