1 21 22 27 28 package com.sun.mail.imap.protocol; 29 30 import java.util.Vector ; 31 import javax.mail.internet.ParameterList ; 32 import com.sun.mail.iap.*; 33 34 40 41 public class BODYSTRUCTURE implements Item { 42 43 public static char [] name = {'B','O','D','Y','S','T','R','U','C','T','U','R','E'}; 44 public int msgno; 45 46 public String type; public String subtype; public String encoding; public int lines = -1; public int size = -1; public String disposition; public String id; public String description; public String md5; public String attachment; public ParameterList cParams; public ParameterList dParams; public String [] language; public BODYSTRUCTURE[] bodies; public ENVELOPE envelope; 63 private static int SINGLE = 1; 64 private static int MULTI = 2; 65 private static int NESTED = 3; 66 private int processedType; 68 public BODYSTRUCTURE(FetchResponse r) throws ParsingException { 69 msgno = r.getNumber(); 70 71 r.skipSpaces(); 72 73 if (r.readByte() != '(') 74 throw new ParsingException( 75 "BODYSTRUCTURE parse error: missing ``('' at start"); 76 77 if (r.peekByte() == '(') { type = "multipart"; 79 processedType = MULTI; 80 Vector v = new Vector (1); 81 int i = 1; 82 do { 83 v.addElement(new BODYSTRUCTURE(r)); 84 90 r.skipSpaces(); 91 } while (r.peekByte() == '('); 92 93 bodies = new BODYSTRUCTURE[v.size()]; 95 v.copyInto(bodies); 96 97 subtype = r.readString(); 99 if (r.readByte() == ')') return; 101 102 104 cParams = parseParameters(r); 106 if (r.readByte() == ')') return; 108 109 byte b = r.readByte(); 111 if (b == '(') { 112 disposition = r.readString(); 113 dParams = parseParameters(r); 114 if (r.readByte() != ')') throw new ParsingException( 116 "BODYSTRUCTURE parse error: " + 117 "missing ``)'' at end of disposition in multipart"); 118 } else if (b == 'N' || b == 'n') { 119 r.skip(2); } else { 121 throw new ParsingException( 122 "BODYSTRUCTURE parse error: " + 123 type + "/" + subtype + ": " + 124 "bad multipart disposition, b " + b); 125 } 126 127 if ((b = r.readByte()) == ')') 130 return; 132 if (b != ' ') 133 throw new ParsingException( 134 "BODYSTRUCTURE parse error: " + 135 "missing space after disposition"); 136 137 if (r.peekByte() == '(') language = r.readStringList(); 140 else { 141 String l = r.readString(); 142 if (l != null) { 143 String [] la = { l }; 144 language = la; 145 } 146 } 147 148 151 while (r.readByte() == ' ') 153 parseBodyExtension(r); 154 } 155 else { type = r.readString(); 157 processedType = SINGLE; 158 subtype = r.readString(); 159 160 if (type == null) { 162 type = "application"; 163 subtype = "octet-stream"; 164 } 165 cParams = parseParameters(r); 166 id = r.readString(); 167 description = r.readString(); 168 encoding = r.readString(); 169 size = r.readNumber(); 170 if (size < 0) 171 throw new ParsingException( 172 "BODYSTRUCTURE parse error: bad ``size'' element"); 173 174 if (type.equalsIgnoreCase("text")) { 176 lines = r.readNumber(); 177 if (lines < 0) 178 throw new ParsingException( 179 "BODYSTRUCTURE parse error: bad ``lines'' element"); 180 } else if (type.equalsIgnoreCase("message") && 181 subtype.equalsIgnoreCase("rfc822")) { 182 processedType = NESTED; 184 envelope = new ENVELOPE(r); 185 BODYSTRUCTURE[] bs = { new BODYSTRUCTURE(r) }; 186 bodies = bs; 187 lines = r.readNumber(); 188 if (lines < 0) 189 throw new ParsingException( 190 "BODYSTRUCTURE parse error: bad ``lines'' element"); 191 } else { 192 r.skipSpaces(); 194 byte bn = r.peekByte(); 195 if (Character.isDigit((char)bn)) throw new ParsingException( 197 "BODYSTRUCTURE parse error: server erroneously " + 198 "included ``lines'' element with type " + 199 type + "/" + subtype); 200 } 201 202 if (r.peekByte() == ')') { 203 r.readByte(); 204 return; } 206 207 209 md5 = r.readString(); 211 if (r.readByte() == ')') 212 return; 214 byte b = r.readByte(); 216 if (b == '(') { 217 disposition = r.readString(); 218 dParams = parseParameters(r); 219 if (r.readByte() != ')') throw new ParsingException( 221 "BODYSTRUCTURE parse error: " + 222 "missing ``)'' at end of disposition"); 223 } else if (b == 'N' || b == 'n') { 224 r.skip(2); } else { 226 throw new ParsingException( 227 "BODYSTRUCTURE parse error: " + 228 type + "/" + subtype + ": " + 229 "bad single part disposition, b " + b); 230 } 231 232 if (r.readByte() == ')') 233 return; 235 if (r.peekByte() == '(') language = r.readStringList(); 238 else { String l = r.readString(); 240 if (l != null) { 241 String [] la = { l }; 242 language = la; 243 } 244 } 245 246 249 while (r.readByte() == ' ') 251 parseBodyExtension(r); 252 } 253 } 254 255 public boolean isMulti() { 256 return processedType == MULTI; 257 } 258 259 public boolean isSingle() { 260 return processedType == SINGLE; 261 } 262 263 public boolean isNested() { 264 return processedType == NESTED; 265 } 266 267 private ParameterList parseParameters(Response r) 268 throws ParsingException { 269 r.skipSpaces(); 270 271 ParameterList list = null; 272 byte b = r.readByte(); 273 if (b == '(') { 274 list = new ParameterList (); 275 do { 276 String name = r.readString(); 277 String value = r.readString(); 278 list.set(name, value); 279 } while (r.readByte() != ')'); 280 } else if (b == 'N' || b == 'n') 281 r.skip(2); 282 else 283 throw new ParsingException("Parameter list parse error"); 284 285 return list; 286 } 287 288 private void parseBodyExtension(Response r) throws ParsingException { 289 r.skipSpaces(); 290 291 byte b = r.peekByte(); 292 if (b == '(') { 293 r.skip(1); do { 295 parseBodyExtension(r); 296 } while (r.readByte() != ')'); 297 } else if (Character.isDigit((char)b)) r.readNumber(); 299 else r.readString(); 301 } 302 } 303 | Popular Tags |