001: package dalma.endpoints.email.spi;
002:
003: import dalma.EndPoint;
004: import dalma.endpoints.email.EmailEndPoint;
005: import dalma.endpoints.email.Listener;
006: import dalma.endpoints.email.MailDirListener;
007: import dalma.endpoints.email.POP3Listener;
008: import dalma.endpoints.email.TCPListener;
009: import dalma.spi.EndPointFactory;
010: import dalma.spi.UrlQueryParser;
011:
012: import javax.mail.Session;
013: import javax.mail.internet.InternetAddress;
014: import java.io.File;
015: import java.io.UnsupportedEncodingException;
016: import java.net.URI;
017: import java.net.URISyntaxException;
018: import java.net.InetSocketAddress;
019: import java.text.ParseException;
020: import java.util.Properties;
021:
022: /**
023: * {@link EndPointFactory} for the e-mail port.
024: *
025: * @author Kohsuke Kawaguchi
026: */
027: public class EmailEndPointFactory implements EndPointFactory {
028: public EndPoint create(String endPointName, String endpointURL)
029: throws ParseException {
030: // split into the SMTP part and listener part
031: int idx = endpointURL.indexOf('!');
032: if (idx < 0)
033: throw new ParseException(
034: "the smtp protocol string needs to contain '!'", -1);
035:
036: try {
037: URI smtp = new URI(endpointURL.substring(0, idx));
038: String listener = endpointURL.substring(idx + 1);
039:
040: Listener listenerObject;
041:
042: if (listener.startsWith("pop3://"))
043: listenerObject = createPop3Listener(listener, idx + 1);
044: else if (listener.startsWith("imap4://"))
045: listenerObject = createImap4Listener(listener, idx + 1);
046: else if (listener.startsWith("maildir://"))
047: listenerObject = createMailDirListener(listener,
048: idx + 1);
049: else if (listener.startsWith("tcp://"))
050: listenerObject = createTcpListener(listener, idx + 1);
051: else
052: throw new ParseException("Unsupported scheme: "
053: + listener, idx + 1);
054:
055: if (smtp.getUserInfo() == null)
056: throw new ParseException("user name is missing", -1);
057: UrlQueryParser smtpQuery = new UrlQueryParser(smtp);
058:
059: Properties props = new Properties(System.getProperties());
060: smtpQuery.addTo(props);
061:
062: // translate known query parameters
063: if (smtpQuery.getValue("host") != null)
064: props.put("mail.smtp.host", smtpQuery.getValue("host"));
065:
066: return new EmailEndPoint(endPointName, new InternetAddress(
067: smtp.getUserInfo() + '@' + smtp.getHost(),
068: smtpQuery.getValue("personal")), listenerObject,
069: Session.getInstance(props));
070:
071: } catch (URISyntaxException e) {
072: throw new ParseException(e.getMessage(), e.getIndex());
073: } catch (UnsupportedEncodingException e) {
074: throw new ParseException("Unsupported encoding: "
075: + e.getMessage(), -1);
076: }
077: }
078:
079: private Listener createImap4Listener(String listener, int startIndex)
080: throws URISyntaxException, ParseException {
081: try {
082: URI uri = new URI(listener);
083: UrlQueryParser query = new UrlQueryParser(uri);
084:
085: String userInfo = uri.getUserInfo();
086: if (userInfo == null)
087: throw new ParseException("imap4 needs a user name",
088: startIndex);
089: int idx = userInfo.indexOf(':');
090: if (idx < 0)
091: throw new ParseException("imap4 needs a password",
092: startIndex);
093:
094: return new POP3Listener(uri.getHost(), userInfo.substring(
095: 0, idx), userInfo.substring(idx + 1), query
096: .getValue("interval", 3000));
097: } catch (URISyntaxException e) {
098: throw new URISyntaxException(e.getInput(), e.getReason(), e
099: .getIndex()
100: + startIndex);
101: }
102: }
103:
104: private Listener createPop3Listener(String listener, int startIndex)
105: throws URISyntaxException, ParseException {
106: try {
107: URI uri = new URI(listener);
108: UrlQueryParser query = new UrlQueryParser(uri);
109:
110: String userInfo = uri.getUserInfo();
111: if (userInfo == null)
112: throw new ParseException("pop3 needs a user name",
113: startIndex);
114: int idx = userInfo.indexOf(':');
115: if (idx < 0)
116: throw new ParseException("pop3 needs a password",
117: startIndex);
118:
119: return new POP3Listener(uri.getHost(), userInfo.substring(
120: 0, idx), userInfo.substring(idx + 1), query
121: .getValue("interval", 3000));
122: } catch (URISyntaxException e) {
123: throw new URISyntaxException(e.getInput(), e.getReason(), e
124: .getIndex()
125: + startIndex);
126: }
127: }
128:
129: private Listener createMailDirListener(String listener,
130: int startIndex) throws ParseException {
131: listener = listener.substring("maildir://".length());
132: int questionMark = listener.indexOf('?');
133: // parse the query parameter
134: UrlQueryParser query = new UrlQueryParser(
135: questionMark < 0 ? null : listener
136: .substring(questionMark + 1));
137:
138: // extract the file name portion
139: String filePath = questionMark < 0 ? listener : listener
140: .substring(0, questionMark);
141:
142: File dir = new File(filePath);
143: if (!dir.isDirectory())
144: throw new ParseException("no such directory exists: "
145: + filePath, -1);
146:
147: int interval = query.getValue("interval", 3000);
148:
149: return new MailDirListener(dir, interval);
150: }
151:
152: private Listener createTcpListener(String listener, int startIndex)
153: throws URISyntaxException, ParseException {
154: try {
155: URI uri = new URI(listener);
156: if (uri.getPort() == -1)
157: throw new ParseException(
158: "tcp protocol requres a port number", -1);
159: return new TCPListener(new InetSocketAddress(uri.getHost(),
160: uri.getPort()));
161: } catch (URISyntaxException e) {
162: throw new URISyntaxException(e.getInput(), e.getReason(), e
163: .getIndex()
164: + startIndex);
165: }
166: }
167: }
|