1 22 23 package gnu.mail.providers.nntp; 24 25 import java.io.IOException ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.LinkedList ; 29 import javax.mail.Flags ; 30 import javax.mail.Folder ; 31 import javax.mail.IllegalWriteException ; 32 import javax.mail.Message ; 33 import javax.mail.MessagingException ; 34 35 import gnu.inet.nntp.Group; 36 import gnu.inet.nntp.GroupIterator; 37 import gnu.inet.nntp.NNTPConstants; 38 import gnu.inet.nntp.StatusResponse; 39 40 49 final class NNTPRootFolder 50 extends Folder 51 { 52 53 NNTPRootFolder(NNTPStore store) 54 { 55 super(store); 56 } 57 58 public String getName() 59 { 60 NNTPStore ns = (NNTPStore)store; 61 return ns.getURLName().getHost(); 62 } 63 64 public String getFullName() 65 { 66 NNTPStore ns = (NNTPStore)store; 67 return ns.connection.getWelcome(); 68 } 69 70 74 public Folder [] list(String pattern) 75 throws MessagingException 76 { 77 pattern = pattern.replace('%', '*'); try 79 { 80 NNTPStore ns = (NNTPStore)store; 81 boolean listAll = ns.isListAll(); 83 84 List acc = new LinkedList (); 85 synchronized (ns.connection) 86 { 87 GroupIterator i = listAll ? 88 ns.connection.listActive(pattern) : 89 ns.connection.listSubscriptions(); 90 while (i.hasNext()) 91 { 92 Group group = i.nextGroup(); 93 NNTPFolder folder = new NNTPFolder(ns, group.getName()); 94 acc.add(folder); 95 } 96 } 97 int len = acc.size(); 98 Folder [] folders = new Folder [len]; 99 acc.toArray(folders); 100 return folders; 101 } 102 catch (StatusResponse e) 103 { 104 switch (e.getStatus()) 105 { 106 case NNTPConstants.COMMAND_NOT_RECOGNIZED: 107 case NNTPConstants.SYNTAX_ERROR: 108 case NNTPConstants.INTERNAL_ERROR: 109 return listSubscribed(pattern); 110 default: 111 throw new MessagingException (e.getMessage(), e); 112 } 113 } 114 catch (IOException e) 115 { 116 throw new MessagingException (e.getMessage(), e); 117 } 118 } 119 120 124 public Folder [] listSubscribed(String pattern) 125 throws MessagingException 126 { 127 pattern = pattern.replace('%', '*'); boolean hasWildcard = pattern.indexOf('*')>-1; 130 boolean onlyWildcard = hasWildcard && (pattern.length()==0); 132 133 NNTPStore ns = (NNTPStore)store; 134 List acc = new LinkedList (); 135 Iterator i = ns.newsrc.list(); 136 while (i.hasNext()) 137 { 138 String name = (String )i.next(); 139 if (!onlyWildcard) 141 { 142 if (hasWildcard && matches(name, pattern)) 143 acc.add(new NNTPFolder(ns, name)); 144 else if (!hasWildcard && pattern.equals(name)) 145 acc.add(new NNTPFolder(ns, name)); 146 } 147 } 148 int len = acc.size(); 149 Folder [] folders = new Folder [len]; 150 acc.toArray(folders); 151 return folders; 152 } 153 154 158 boolean matches(String name, String pattern) 159 { 160 int i1 = pattern.indexOf('*'); 161 int pn = 0, pp = 0; 162 while (i1>-1) 163 { 164 if (i1>0) 165 { 166 String ps = pattern.substring(pp, i1); 167 int len = ps.length(); 168 String ns = name.substring(pn, len); 169 if (!ps.equals(ns)) 170 return false; 171 pp = i1+1; 172 pn += len; 173 i1 = 0; 174 } 175 else 176 { 177 pp = i1+1; 178 i1 = pattern.indexOf('*', pp); 179 String ps = null; 180 if (i1==-1) 181 ps = pattern.substring(pp); 182 else 183 ps = pattern.substring(pp, i1); 184 int len = ps.length(); 185 if (len>0) 186 { 187 String ns = name.substring(pn, len); 188 if (!ps.equals(ns)) 189 return false; 190 } 191 } 192 } 193 return true; 194 } 195 196 199 public Folder getFolder(String name) 200 throws MessagingException 201 { 202 NNTPStore ns = (NNTPStore)store; 203 return new NNTPFolder(ns, name); 204 } 205 206 public Folder getParent() 207 throws MessagingException 208 { 209 return null; 210 } 211 212 public boolean exists() 213 throws MessagingException 214 { 215 return true; 216 } 217 218 222 public char getSeparator() 223 throws MessagingException 224 { 225 return '.'; 226 } 227 228 231 public int getType() 232 { 233 return HOLDS_FOLDERS; 234 } 235 236 public void open(int mode) 237 throws MessagingException 238 { 239 if (mode!=READ_ONLY) 242 throw new IllegalWriteException ("Folder is read-only"); 243 } 244 245 public void close(boolean expunge) 246 throws MessagingException 247 { 248 } 249 250 public Message [] expunge() 251 throws MessagingException 252 { 253 throw new IllegalWriteException ("Folder is read-only"); 254 } 255 256 public boolean isOpen() 257 { 258 return false; 259 } 260 261 public Flags getPermanentFlags() 262 { 263 return new Flags (); 264 } 265 266 public int getMessageCount() 267 throws MessagingException 268 { 269 return -1; 270 } 271 272 public Message getMessage(int msgnum) 273 throws MessagingException 274 { 275 throw new IllegalStateException ("Folder not open"); 276 } 277 278 281 public void setSubscribed(boolean flag) 282 throws MessagingException 283 { 284 if (!flag) 285 throw new IllegalWriteException ("Can't unsubscribe root folder"); 286 } 287 288 public boolean hasNewMessages() 289 throws MessagingException 290 { 291 return false; 292 } 293 294 public void appendMessages(Message [] messages) 295 throws MessagingException 296 { 297 throw new IllegalWriteException ("Folder is read-only"); 298 } 299 300 public boolean create(int type) 301 throws MessagingException 302 { 303 throw new MessagingException ("Folder already exists"); 304 } 305 306 public boolean delete(boolean flag) 307 throws MessagingException 308 { 309 throw new IllegalWriteException ("Folder is read-only"); 310 } 311 312 public boolean renameTo(Folder folder) 313 throws MessagingException 314 { 315 throw new IllegalWriteException ("Folder is read-only"); 316 } 317 318 } 319 | Popular Tags |