| 1 34 35 36 package com.micronova.jsp.tag; 37 38 import com.micronova.util.*; 39 import com.micronova.util.servlet.*; 40 import java.util.*; 41 import java.util.regex.*; 42 import javax.servlet.*; 43 import javax.servlet.jsp.*; 44 import javax.servlet.http.*; 45 46 import javax.mail.*; 47 import javax.mail.internet.*; 48 49 50 51 public class MailFolderTag extends ParamTag 52 { 53 public static final String MESSAGES = "messages"; 54 public static final String OPERATION = "operation"; 55 public static final String CONTROL = "control"; 56 57 public static final String READ = "read"; 58 public static final String DELETE = "delete"; 59 60 protected String _url; 61 protected Folder _folder; 62 63 protected void init() 64 { 65 super.init(); 66 67 _url = null; 68 _folder = null; 69 } 70 71 public Folder getFolder() 72 { 73 Folder folder = _folder; 74 75 if (folder != null) 76 { 77 return folder; 78 } 79 80 String url = _url; 81 82 if (url == null) 83 { 84 MailFolderTag folderSource = (MailFolderTag)getAncestorTag("com.micronova.jsp.tag.MailFolderTag"); 85 86 if (folderSource != null) 87 { 88 folder = folderSource.getFolder(); 89 } 90 } 91 else 92 { 93 try 94 { 95 Session session = Session.getInstance(new Properties()); 96 97 folder = session.getFolder(new URLName(url)); 98 99 _folder = folder; 100 } 101 catch (Exception e) 102 { 103 } 104 } 105 106 return folder; 107 } 108 109 public void closeFolder() 110 { 111 Folder folder = _folder; 112 113 if (folder != null) 114 { 115 try 116 { 117 folder.close(true); 118 } 119 catch (Exception e) 120 { 121 e.printStackTrace(); 122 } 123 124 _folder = null; 125 } 126 } 127 128 protected void cleanup() 129 { 130 closeFolder(); 131 132 super.cleanup(); 133 } 134 135 public void setUrl(Object expression) throws Exception  136 { 137 _url = (String )evaluateAttribute("url", expression, String .class); 138 139 closeFolder(); 140 } 141 142 public void setOperation(Object expression) throws Exception  143 { 144 _map.put(OPERATION, (String )evaluateAttribute(OPERATION, expression, String .class)); 145 } 146 147 public void setMessages(Object expression) throws Exception  148 { 149 _map.put(MESSAGES, evaluateAttribute(MESSAGES, expression, Object .class)); 150 } 151 152 public void setControl(Object expression) throws Exception  153 { 154 _map.put(CONTROL, evaluateAttribute(CONTROL, expression, Object .class)); 155 } 156 157 public static int[] mark(String specs, int min, int max) 158 { 159 int size = max + 1; 160 161 boolean mark[] = new boolean[size]; 162 163 int markCount = 0; 164 165 String [] specArray = specs.split(","); 166 167 for (int i = 0; i < specArray.length; i ++) 168 { 169 String spec = specArray[i]; 170 171 int firstIndex = min; 172 int lastIndex = max; 173 174 if (!("*".equals(spec))) 175 { 176 int separator = spec.indexOf('-'); 177 178 if (separator >= 0) 179 { 180 String first = spec.substring(0, separator); 181 String last = spec.substring(separator + 1); 182 183 if (!("*".equals(first))) 184 { 185 firstIndex = Integer.parseInt(first); 186 } 187 188 if (!("*".equals(last))) 189 { 190 lastIndex = Integer.parseInt(last); 191 } 192 } 193 else 194 { 195 firstIndex = Integer.parseInt(spec); 196 lastIndex = firstIndex; 197 } 198 } 199 200 if (firstIndex < min) 201 { 202 firstIndex = min; 203 } 204 205 if (lastIndex > max) 206 { 207 lastIndex = max; 208 } 209 210 for (int j = firstIndex; j <= lastIndex; j ++) 211 { 212 boolean current = mark[j]; 213 214 if (!current) 215 { 216 mark[j] = true; 217 markCount ++; 218 } 219 } 220 } 221 222 int[] marked = new int[markCount]; 223 224 int j = 0; 225 226 for (int i = min; i < size; i ++) 227 { 228 if (mark[i]) 229 { 230 marked[j ++] = i; 231 } 232 } 233 234 return marked; 235 } 236 237 public Object processValue(Object tagValue) throws Exception  238 { 239 Folder folder = getFolder(); 240 241 if (!folder.isOpen()) 242 { 243 folder.open(Folder.READ_WRITE); 244 } 245 246 NestedMap map = _map; 247 248 Map controlMap = new NestedMap(map.get(CONTROL)); 249 Object messagesSpec = map.get(MESSAGES); 250 String operation = map.getString(OPERATION, READ); 251 252 int[] messages = null; 253 254 if (messagesSpec instanceof NestedMap) 255 { 256 messagesSpec = ((NestedMap)messagesSpec).getSubList(false); 257 } 258 259 if (messagesSpec instanceof int[]) 260 { 261 messages = (int[])messagesSpec; 262 } 263 else if (messagesSpec instanceof String ) 264 { 265 messages = mark(messagesSpec.toString(), 1, folder.getMessageCount()); 266 } 267 else if (messagesSpec != null) 268 { 269 List messageList = TypeUtil.isList(messagesSpec); 270 271 if (messageList != null) 272 { 273 int messageListSize = messageList.size(); 274 275 messages = new int[messageListSize]; 276 277 for (int i = 0; i < messageListSize; i ++) 278 { 279 Object element = messageList.get(i); 280 281 if (element instanceof Message) 282 { 283 messages[i] = ((Message)element).getMessageNumber(); 284 } 285 else 286 { 287 messages[i] = TypeUtil.isInteger(messageList.get(i)).intValue(); 288 } 289 } 290 } 291 } 292 293 if (READ.equals(operation)) 294 { 295 _map.copyFromSource(MailFolder.readMessages(folder, messages, controlMap)); 296 } 297 else if (DELETE.equals(operation)) 298 { 299 MailFolder.deleteMessages(folder, messages); 300 } 301 302 return tagValue; 303 } 304 } 305 | Popular Tags |