1 28 29 package com.caucho.vfs; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.util.StringCharCursor; 33 34 import java.io.IOException ; 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 39 67 public class MailtoPath extends Path { 68 protected String url; 69 private ArrayList <Recipient> _to; 70 private ArrayList cc; 71 private ArrayList bcc; 72 private HashMap <String ,Object > _attributes; 73 74 MailtoPath(MailtoPath parent, String path, 75 ArrayList <Recipient> to, 76 HashMap <String ,Object > attr) 77 { 78 super(parent); 79 80 this.url = path; 81 _to = to; 82 _attributes = attr; 83 } 84 85 88 protected Path schemeWalk(String userPath, Map <String ,Object > attributes, 89 String uri, int offset) 90 { 91 StringCharCursor cursor = new StringCharCursor(uri, offset); 92 93 ArrayList <Recipient> to = parseAddressList(cursor); 94 HashMap <String ,Object > attr = new HashMap <String ,Object >(); 95 96 CharBuffer buf = new CharBuffer(); 97 if (cursor.current() == '?') { 98 char ch = cursor.next(); 99 while (isUserChar(ch)) { 100 buf.clear(); 101 for (; isUserChar(ch); ch = cursor.next()) 102 buf.append(ch); 103 String key = buf.toString(); 104 105 if (ch != '=') 106 throw new RuntimeException ("broken attribute at: " + ch); 107 buf.clear(); 108 for (ch = cursor.next(); 109 ch != cursor.DONE && ch != '&'; 110 ch = cursor.next()) 111 buf.append(ch); 112 113 attr.put(key, buf.toString()); 114 115 while (ch == '&' || ch == ' ' || ch == '\t') 116 ch = cursor.next(); 117 } 118 } 119 120 return new MailtoPath(this, userPath, to, attr); 121 } 122 123 129 static ArrayList <Recipient> parseAddressList(StringCharCursor cursor) 130 { 131 ArrayList <Recipient> to = new ArrayList <Recipient>(); 132 133 char ch = cursor.current(); 134 CharBuffer buf = new CharBuffer(); 135 136 while (Character.isWhitespace(ch)) 137 ch = cursor.next(); 138 139 while (isUserChar(ch)) { 140 buf.clear(); 141 for (; isUserChar(ch); ch = cursor.next()) 142 buf.append(ch); 143 144 Recipient rcpt = new Recipient(); 145 to.add(rcpt); 146 rcpt.user = buf.toString(); 147 148 if (ch == '@') { 149 ch = cursor.next(); 150 if (! isUserChar(ch)) 151 throw new RuntimeException ("bad url"); 152 153 buf.clear(); 154 for (; isUserChar(ch); ch = cursor.next()) 155 buf.append(ch); 156 157 rcpt.host = buf.toString(); 158 } 159 160 while (ch == ',' || ch == ' ' || ch == '\t' || 161 ch == '\n' || ch == '\r') { 162 ch = cursor.next(); 163 } 164 } 165 166 return to; 167 } 168 169 172 private static boolean isUserChar(int ch) 173 { 174 switch (ch) { 175 case '.': case '-': case '_': case '!': case '$': 176 case '~': case '^': case '*': case '/': case '+': 177 return true; 178 179 default: 180 return (ch >= 'a' && ch <= 'z' || 181 ch >= 'A' && ch <= 'Z' || 182 ch >= '0' && ch <= '9'); 183 } 184 } 185 186 189 public String getURL() 190 { 191 return getPath(); 192 } 193 194 197 public String getScheme() 198 { 199 return "mailto"; 200 } 201 202 205 public String getPath() 206 { 207 return "mailto:" + url; 208 } 209 210 213 public Object getAttribute(String name) 214 { 215 return _attributes.get(name); 216 } 217 218 221 public StreamImpl openWriteImpl() 222 throws IOException 223 { 224 return new SmtpStream(_to, _attributes); 225 } 226 227 static class Recipient { 228 String user; 229 String host; 230 } 231 } 232 | Popular Tags |