001: /*
002: Copyright (C) 2004 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.hipermail;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.io.UnsupportedEncodingException;
038:
039: import java.sql.SQLException;
040: import java.sql.CallableStatement;
041: import java.sql.PreparedStatement;
042: import java.sql.ResultSet;
043: import java.sql.Types;
044:
045: import javax.mail.Address;
046: import javax.mail.Message;
047: import javax.mail.internet.AddressException;
048:
049: import com.knowgate.debug.DebugFile;
050: import com.knowgate.jdc.JDCConnection;
051: import com.knowgate.dataobjs.DB;
052: import com.knowgate.dataobjs.DBPersist;
053:
054: import javax.mail.internet.InternetAddress;
055:
056: /**
057: * <p>Internet Address register from k_inet_addrs</p>
058: * @author Sergio Montoro Ten
059: * @version 2.1
060: */
061:
062: public class DBInetAddr extends InternetAddress {
063: private DBPersist oAddr;
064:
065: public DBInetAddr(String sMsgGUID, int iPart) {
066:
067: oAddr = new DBPersist(DB.k_inet_addrs, "InetAddrDB");
068:
069: oAddr.put(DB.gu_mimemsg, sMsgGUID);
070: oAddr.put(DB.id_part, iPart);
071: }
072:
073: /**
074: * Constructor
075: * @param sMsgGUID Message GUID
076: * @param sMsgId Mime Message Identifier
077: * @param sTxEMail Mail address
078: * @param sTxPersonal Address displyed name
079: * @param sTpRecipient Recipient type { from, to, cc, bcc } case sensitive
080: * @param sGuUser GUID of User which tx_main_email is the same as this address
081: * @param sGuContact GUID of Contact which tx_main_email is the same as this address
082: * @param sGuCompany GUID of Copany which tx_main_email is the same as this address
083: * @throws NullPointerException If sMsgGUID or sMsgId or sTxEMail or sTpRecipient is <b>null</b>.
084: * @throws IllegalArgumentException If sTpRecipient is not one of { from, to, cc, bcc }
085: */
086: public DBInetAddr(String sMsgGUID, String sMsgId, String sTxEMail,
087: String sTxPersonal, String sTpRecipient, String sGuUser,
088: String sGuContact, String sGuCompany)
089: throws NullPointerException, IllegalArgumentException {
090:
091: if (null == sMsgGUID)
092: throw new NullPointerException(
093: "DBInetAddr message GUID cannot be null");
094: if (null == sMsgId)
095: throw new NullPointerException(
096: "DBInetAddr message identifier cannot be null");
097: if (null == sTxEMail)
098: throw new NullPointerException(
099: "DBInetAddr mail address cannot be null");
100: if (null == sTpRecipient)
101: throw new NullPointerException(
102: "DBInetAddr recipient type cannot be null");
103:
104: if (!sTpRecipient.equals("from") && !sTpRecipient.equals("to")
105: && !!sTpRecipient.equals("cc")
106: && !!sTpRecipient.equals("bcc"))
107: throw new java.lang.IllegalArgumentException(
108: "Recipient type must be either from, to, cc or bcc");
109:
110: oAddr = new DBPersist(DB.k_inet_addrs, "InetAddrDB");
111:
112: oAddr.put(DB.gu_mimemsg, sMsgGUID);
113: oAddr.put(DB.id_message, sMsgId);
114: oAddr.put(DB.tx_email, sTxEMail);
115: oAddr.put(DB.tp_recipient, sTpRecipient);
116:
117: if (null != sTxPersonal)
118: oAddr.put(DB.tx_personal, sTxPersonal);
119: if (null != sGuUser)
120: oAddr.put(DB.gu_user, sGuUser);
121: if (null != sGuContact)
122: oAddr.put(DB.gu_contact, sGuContact);
123: if (null != sGuCompany)
124: oAddr.put(DB.gu_company, sGuCompany);
125: }
126:
127: // ---------------------------------------------------------------------------
128:
129: /**
130: * Get e-mail address
131: * @return String
132: */
133: public String getAddress() {
134: return oAddr.getStringNull(DB.tx_email, null);
135: }
136:
137: // ---------------------------------------------------------------------------
138:
139: public String getString(String sKey) {
140: return oAddr.getString(sKey);
141: }
142:
143: // ---------------------------------------------------------------------------
144:
145: public String getStringNull(String sKey, String sDefault) {
146: return oAddr.getStringNull(sKey, sDefault);
147: }
148:
149: // ---------------------------------------------------------------------------
150:
151: /**
152: * Two addresses are equal if they hold the same e-mail
153: * @param oOtherAddr DBInetAddr
154: * @return boolean <b>true</b> if e-mail of this is equal to e-mail of oOtherAddr (case insensitive comparison)
155: * @throws ClassCastException if oOtherAddr is not of type DBInetAddr
156: */
157: public boolean equals(Object oOtherAddr) throws ClassCastException {
158: DBInetAddr oAddr2 = (DBInetAddr) oOtherAddr;
159:
160: if (getAddress() == null || oAddr2.getAddress() == null)
161: return false;
162: else
163: return getAddress().equalsIgnoreCase(oAddr2.getAddress());
164: }
165:
166: // ---------------------------------------------------------------------------
167:
168: /**
169: *
170: * @return String "rfc822"
171: */
172: public String getType() {
173: return "rfc822";
174: }
175:
176: // ---------------------------------------------------------------------------
177:
178: /**
179: * Get display (personal) name
180: * @return String
181: */
182: public String getPersonal() {
183: return oAddr.getStringNull(DB.tx_personal, null);
184: }
185:
186: // ---------------------------------------------------------------------------
187:
188: /**
189: * This method is unsupported and will always throw an exception when called
190: * @throws UnsupportedOperationException
191: */
192: public boolean store(JDCConnection oConn)
193: throws UnsupportedOperationException {
194: if (true)
195: throw new UnsupportedOperationException(
196: "Method InetAddrDB.store() is not supported");
197: return false;
198: }
199:
200: // ---------------------------------------------------------------------------
201:
202: /**
203: * <p>Write address resolving e-mails to contact and company GUIDs</p>
204: * This method writes to k_inet_addr table but it first lookup the given e-mail
205: * at table k_member_address and fills gu_contact and gu_company fields of
206: * k_inet_addr if there is a contact or company at the specified workarea that
207: * has that e-mail
208: * @param oConn JDCConnection
209: * @param iDomainId int Domain Identifier
210: * @param sWorkAreaId String WorkArea GUID (search for matches will be restricted to this WorkArea)
211: * @param sGuMimeMsg String Message GUID
212: * @param sIdMimeMsg String Message Identifier
213: * @param sTxEMail String e-mail
214: * @param sTpRecipient String One of {to, cc, bcc}
215: * @param sTxPersonal String Display Name
216: * @return boolean <b>true</b>
217: * @throws SQLException
218: */
219: public static boolean write(JDCConnection oConn, int iDomainId,
220: String sWorkAreaId, String sGuMimeMsg, String sIdMimeMsg,
221: String sTxEMail, String sTpRecipient, String sTxPersonal)
222: throws SQLException {
223: if (DebugFile.trace) {
224: DebugFile.writeln("Begin DBInetAddr.write([Connection], "
225: + String.valueOf(iDomainId) + "," + sWorkAreaId
226: + "," + sGuMimeMsg + "," + sIdMimeMsg + ","
227: + sTxEMail + "," + sTpRecipient + "," + sTxPersonal
228: + ")");
229: DebugFile.incIdent();
230: }
231: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
232: if (DebugFile.trace)
233: DebugFile.writeln("SELECT k_sp_write_inet_addr ("
234: + String.valueOf(iDomainId)
235: + ",'"
236: + sWorkAreaId
237: + "',"
238: + (sGuMimeMsg == null ? "null" : "'"
239: + sGuMimeMsg + "'")
240: + ",'"
241: + sIdMimeMsg
242: + "','"
243: + sTxEMail
244: + "','"
245: + sTpRecipient
246: + "','"
247: + (sTxPersonal == null ? "null" : "'"
248: + sTxPersonal + "'") + "')");
249: PreparedStatement oStmt = oConn
250: .prepareStatement("SELECT k_sp_write_inet_addr (?,?,?,?,?,?,?)");
251: oStmt.setInt(1, iDomainId);
252: oStmt.setString(2, sWorkAreaId);
253: oStmt.setString(3, sGuMimeMsg);
254: oStmt.setString(4, sIdMimeMsg);
255: oStmt.setString(5, sTxEMail);
256: oStmt.setString(6, sTpRecipient);
257: if (sTxPersonal != null)
258: oStmt.setString(7, sTxPersonal);
259: else
260: oStmt.setNull(7, Types.VARCHAR);
261: if (DebugFile.trace)
262: DebugFile.writeln("PreparedStatement.executeQuery()");
263: oStmt.executeQuery();
264: oStmt.close();
265: } else if (oConn.getDataBaseProduct() == JDCConnection.DBMS_ORACLE) {
266: if (DebugFile.trace)
267: DebugFile
268: .writeln("{call K_SP_WRITE_INET_ADDR (?,?,?,?,?,?,?)}");
269: CallableStatement oCall = oConn
270: .prepareCall("{call K_SP_WRITE_INET_ADDR (?,?,?,?,?,?,?)}");
271: oCall.setBigDecimal(1, new java.math.BigDecimal(iDomainId));
272: oCall.setString(2, sWorkAreaId);
273: oCall.setString(3, sGuMimeMsg);
274: oCall.setString(4, sIdMimeMsg);
275: if (sTpRecipient != null)
276: oCall.setString(5, sTpRecipient);
277: else
278: oCall.setNull(5, Types.VARCHAR);
279: oCall.setString(6, sTxEMail);
280: if (sTxPersonal != null)
281: oCall.setString(7, sTxPersonal);
282: else
283: oCall.setNull(7, Types.VARCHAR);
284: if (DebugFile.trace)
285: DebugFile.writeln("CallableStatement.execute()");
286: oCall.execute();
287: oCall.close();
288: } else {
289: if (DebugFile.trace)
290: DebugFile
291: .writeln("{call k_sp_write_inet_addr (?,?,?,?,?,?,?)}");
292: CallableStatement oCall = oConn
293: .prepareCall("{call k_sp_write_inet_addr (?,?,?,?,?,?,?)}");
294: oCall.setInt(1, iDomainId);
295: oCall.setString(2, sWorkAreaId);
296: oCall.setString(3, sGuMimeMsg);
297: oCall.setString(4, sIdMimeMsg);
298: oCall.setString(5, sTpRecipient);
299: oCall.setString(6, sTxEMail);
300: if (sTxPersonal != null)
301: oCall.setString(7, sTxPersonal);
302: else
303: oCall.setNull(7, Types.VARCHAR);
304: if (DebugFile.trace)
305: DebugFile.writeln("CallableStatement.execute()");
306: oCall.execute();
307: oCall.close();
308: }
309: if (DebugFile.trace) {
310: DebugFile.decIdent();
311: DebugFile.writeln("End DBInetAddr.write()");
312: }
313: return true;
314: } // write
315:
316: // ---------------------------------------------------------------------------
317:
318: /**
319: * <p>Forward call to static method DBInetAddr.write()</p>
320: * @param oConn JDCConnection
321: * @param iDomainId int Domain Identifier
322: * @param sWorkAreaId String WorkArea GUID (search for matches will be restricted to this WorkArea)
323: * @return boolean <b>true</b>
324: * @throws SQLException
325: */
326: public boolean write(JDCConnection oConn, int iDomainId,
327: String sWorkAreaId) throws SQLException {
328: if (DebugFile.trace) {
329: DebugFile.writeln("Begin DBInetAddrDB.write([Connection], "
330: + String.valueOf(iDomainId) + "," + sWorkAreaId
331: + ")");
332: DebugFile.incIdent();
333: }
334: boolean bRetVal = DBInetAddr.write(oConn, iDomainId,
335: sWorkAreaId, oAddr.getString(DB.gu_mimemsg), oAddr
336: .getStringNull(DB.id_message, null), oAddr
337: .getString(DB.tx_email), oAddr
338: .getString(DB.tp_recipient), oAddr
339: .getStringNull(DB.tx_personal, null));
340: if (DebugFile.trace) {
341: DebugFile.decIdent();
342: DebugFile.writeln("End InetAddrDB.write()");
343: }
344: return bRetVal;
345: } // write
346:
347: // ---------------------------------------------------------------------------
348:
349: /**
350: * Parse address of the form (Name) <user@domain.com>, <user@domain.com> (Name),
351: * "Name" <user@domain.com>, <user@domain.com> "Name", Name <user@domain.com>,
352: * <user@domain.com> , user@domain.com
353: * @param sNamePlusEMail String Display name and e-mail address
354: * @return InternetAddress
355: * @throws AddressException
356: * @throws NullPointerException
357: * @throws UnsupportedEncodingException
358: */
359: public static InternetAddress parseAddress(String sNamePlusEMail)
360: throws AddressException, NullPointerException,
361: UnsupportedEncodingException {
362: InternetAddress oRetAdr = null;
363:
364: String sAddr = sNamePlusEMail.trim();
365:
366: int iLeftAng = sAddr.indexOf('<');
367: int iRightAng = sAddr.indexOf('>');
368: int iLeftPar = sAddr.indexOf('(');
369: int iRightPar = sAddr.indexOf(')');
370: int iLeftQuo = sAddr.indexOf('"');
371: int iRightQuo;
372: if (iLeftQuo >= 0)
373: iRightQuo = sAddr.indexOf('"', iLeftQuo + 1);
374: else
375: iRightQuo = -1;
376:
377: if (iRightAng < iLeftAng)
378: throw new AddressException("Misplaced right angle");
379: if (iLeftAng < 0 && iRightAng >= 0)
380: throw new AddressException("Missing left angle");
381: if (iLeftAng >= 0 && iRightAng < 0)
382: throw new AddressException("Missing right angle");
383: if (iLeftPar < 0 && iRightPar >= 0)
384: throw new AddressException("Missing left parenthesis");
385: if (iLeftPar >= 0 && iRightPar < 0)
386: throw new AddressException("Missing right parenthesis");
387: if (iRightPar < iLeftPar)
388: throw new AddressException("Misplaced right parenthesis");
389: if (iLeftQuo >= 0 && iRightQuo < 0)
390: throw new AddressException("Unclosed quote");
391:
392: if (iLeftAng >= 0 && iRightAng >= 0 && iLeftPar >= 0
393: && iRightPar >= 0) {
394: // Address is (Name) <user@domain.com> or <user@domain.com> (Name)
395: oRetAdr = new InternetAddress(sAddr.substring(iLeftAng + 1,
396: iRightAng), sAddr
397: .substring(iLeftPar + 1, iRightPar));
398: } else if (iLeftAng >= 0 && iRightAng >= 0 && iLeftQuo >= 0
399: && iRightQuo >= 0) {
400: // Address is "Name" <user@domain.com> or "Name" <user@domain.com>
401: oRetAdr = new InternetAddress(sAddr.substring(iLeftAng + 1,
402: iRightAng), sAddr
403: .substring(iLeftQuo + 1, iRightQuo));
404: } else if (iLeftAng >= 0 && iRightAng >= 0) {
405: // Address is Name <user@domain.com> or <user@domain.com> Name
406: if (0 == iLeftAng)
407: oRetAdr = new InternetAddress(sAddr.substring(1,
408: iRightAng), sAddr.substring(iRightAng + 1));
409: else
410: oRetAdr = new InternetAddress(sAddr.substring(
411: iLeftAng + 1, iRightAng), sAddr.substring(0,
412: iLeftAng));
413: } else {
414: oRetAdr = new InternetAddress(sAddr);
415: }
416: return oRetAdr;
417: }
418:
419: // ---------------------------------------------------------------------------
420:
421: /**
422: * Get Display name concatenated with e-mail into angles
423: * @return String "Personal Name <user@domain.com>"
424: */
425: public String toString() {
426: return getPersonal() + " <" + getAddress() + ">";
427: }
428: }
|