1 32 33 package com.knowgate.hipermail; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.UnsupportedEncodingException ; 38 39 import java.sql.SQLException ; 40 import java.sql.CallableStatement ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 import java.sql.Types ; 44 45 import javax.mail.Address ; 46 import javax.mail.Message ; 47 import javax.mail.internet.AddressException ; 48 49 import com.knowgate.debug.DebugFile; 50 import com.knowgate.jdc.JDCConnection; 51 import com.knowgate.dataobjs.DB; 52 import com.knowgate.dataobjs.DBPersist; 53 54 import javax.mail.internet.InternetAddress ; 55 56 61 62 public class DBInetAddr extends InternetAddress { 63 private DBPersist oAddr; 64 65 public DBInetAddr(String sMsgGUID, int iPart) { 66 67 oAddr = new DBPersist(DB.k_inet_addrs, "InetAddrDB"); 68 69 oAddr.put(DB.gu_mimemsg, sMsgGUID); 70 oAddr.put(DB.id_part, iPart); 71 } 72 73 86 public DBInetAddr(String sMsgGUID, String sMsgId, String sTxEMail, 87 String sTxPersonal, String sTpRecipient, 88 String sGuUser, String sGuContact, String sGuCompany) 89 throws NullPointerException , IllegalArgumentException { 90 91 if (null==sMsgGUID) throw new NullPointerException ("DBInetAddr message GUID cannot be null"); 92 if (null==sMsgId) throw new NullPointerException ("DBInetAddr message identifier cannot be null"); 93 if (null==sTxEMail) throw new NullPointerException ("DBInetAddr mail address cannot be null"); 94 if (null==sTpRecipient) throw new NullPointerException ("DBInetAddr recipient type cannot be null"); 95 96 if (!sTpRecipient.equals("from") && !sTpRecipient.equals("to") && !!sTpRecipient.equals("cc") && !!sTpRecipient.equals("bcc")) 97 throw new java.lang.IllegalArgumentException ("Recipient type must be either from, to, cc or bcc"); 98 99 oAddr = new DBPersist(DB.k_inet_addrs, "InetAddrDB"); 100 101 oAddr.put(DB.gu_mimemsg, sMsgGUID); 102 oAddr.put(DB.id_message, sMsgId); 103 oAddr.put(DB.tx_email, sTxEMail); 104 oAddr.put(DB.tp_recipient, sTpRecipient); 105 106 if (null!=sTxPersonal) oAddr.put(DB.tx_personal, sTxPersonal); 107 if (null!=sGuUser) oAddr.put(DB.gu_user, sGuUser); 108 if (null!=sGuContact) oAddr.put(DB.gu_contact, sGuContact); 109 if (null!=sGuCompany) oAddr.put(DB.gu_company, sGuCompany); 110 } 111 112 114 118 public String getAddress () { return oAddr.getStringNull (DB.tx_email, null); } 119 120 122 public String getString (String sKey) { return oAddr.getString (sKey); } 123 124 126 public String getStringNull (String sKey, String sDefault) { return oAddr.getStringNull (sKey, sDefault); } 127 128 130 136 public boolean equals (Object oOtherAddr) 137 throws ClassCastException { 138 DBInetAddr oAddr2 = (DBInetAddr) oOtherAddr; 139 140 if (getAddress()==null || oAddr2.getAddress()==null) 141 return false; 142 else 143 return getAddress().equalsIgnoreCase(oAddr2.getAddress()); 144 } 145 146 148 152 public String getType () { return "rfc822"; } 153 154 156 160 public String getPersonal () { return oAddr.getStringNull (DB.tx_personal, null); } 161 162 163 165 169 public boolean store (JDCConnection oConn) throws UnsupportedOperationException { 170 if (true) throw new UnsupportedOperationException ("Method InetAddrDB.store() is not supported"); 171 return false; 172 } 173 174 176 193 public static boolean write (JDCConnection oConn, int iDomainId, String sWorkAreaId, 194 String sGuMimeMsg, String sIdMimeMsg, String sTxEMail, 195 String sTpRecipient, String sTxPersonal) 196 throws SQLException { 197 if (DebugFile.trace) { 198 DebugFile.writeln("Begin DBInetAddr.write([Connection], " + String.valueOf(iDomainId) + "," + sWorkAreaId + "," + 199 sGuMimeMsg + "," + sIdMimeMsg + "," + sTxEMail + "," + sTpRecipient + "," + sTxPersonal + ")"); 200 DebugFile.incIdent(); 201 } 202 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) { 203 if (DebugFile.trace) DebugFile.writeln("SELECT k_sp_write_inet_addr ("+String.valueOf(iDomainId)+",'"+sWorkAreaId+"',"+(sGuMimeMsg==null ? "null" : "'"+sGuMimeMsg+"'")+",'"+sIdMimeMsg+"','"+sTxEMail+"','"+sTpRecipient+"','"+(sTxPersonal==null ? "null" : "'"+sTxPersonal+"'")+"')"); 204 PreparedStatement oStmt = oConn.prepareStatement("SELECT k_sp_write_inet_addr (?,?,?,?,?,?,?)"); 205 oStmt.setInt (1, iDomainId); 206 oStmt.setString(2, sWorkAreaId); 207 oStmt.setString(3, sGuMimeMsg); 208 oStmt.setString(4, sIdMimeMsg); 209 oStmt.setString(5, sTxEMail); 210 oStmt.setString(6, sTpRecipient); 211 if (sTxPersonal!=null) 212 oStmt.setString(7, sTxPersonal); 213 else 214 oStmt.setNull(7, Types.VARCHAR); 215 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()"); 216 oStmt.executeQuery(); 217 oStmt.close(); 218 } 219 else if (oConn.getDataBaseProduct()==JDCConnection.DBMS_ORACLE) { 220 if (DebugFile.trace) DebugFile.writeln("{call K_SP_WRITE_INET_ADDR (?,?,?,?,?,?,?)}"); 221 CallableStatement oCall = oConn.prepareCall("{call K_SP_WRITE_INET_ADDR (?,?,?,?,?,?,?)}"); 222 oCall.setBigDecimal(1 , new java.math.BigDecimal (iDomainId)); 223 oCall.setString(2, sWorkAreaId); 224 oCall.setString(3, sGuMimeMsg); 225 oCall.setString(4, sIdMimeMsg); 226 if (sTpRecipient!=null) 227 oCall.setString(5, sTpRecipient); 228 else 229 oCall.setNull(5, Types.VARCHAR); 230 oCall.setString(6, sTxEMail); 231 if (sTxPersonal!=null) 232 oCall.setString(7, sTxPersonal); 233 else 234 oCall.setNull(7, Types.VARCHAR); 235 if (DebugFile.trace) DebugFile.writeln("CallableStatement.execute()"); 236 oCall.execute(); 237 oCall.close(); 238 } 239 else { 240 if (DebugFile.trace) DebugFile.writeln("{call k_sp_write_inet_addr (?,?,?,?,?,?,?)}"); 241 CallableStatement oCall = oConn.prepareCall("{call k_sp_write_inet_addr (?,?,?,?,?,?,?)}"); 242 oCall.setInt (1, iDomainId); 243 oCall.setString(2, sWorkAreaId); 244 oCall.setString(3, sGuMimeMsg); 245 oCall.setString(4, sIdMimeMsg); 246 oCall.setString(5, sTpRecipient); 247 oCall.setString(6, sTxEMail); 248 if (sTxPersonal!=null) 249 oCall.setString(7, sTxPersonal); 250 else 251 oCall.setNull(7, Types.VARCHAR); 252 if (DebugFile.trace) DebugFile.writeln("CallableStatement.execute()"); 253 oCall.execute(); 254 oCall.close(); 255 } 256 if (DebugFile.trace) { 257 DebugFile.decIdent(); 258 DebugFile.writeln("End DBInetAddr.write()"); 259 } 260 return true; 261 } 263 265 273 public boolean write (JDCConnection oConn, int iDomainId, String sWorkAreaId) throws SQLException { 274 if (DebugFile.trace) { 275 DebugFile.writeln("Begin DBInetAddrDB.write([Connection], " + String.valueOf(iDomainId) + "," + sWorkAreaId + ")"); 276 DebugFile.incIdent(); 277 } 278 boolean bRetVal = 279 DBInetAddr.write(oConn,iDomainId,sWorkAreaId,oAddr.getString(DB.gu_mimemsg), 280 oAddr.getStringNull(DB.id_message,null), 281 oAddr.getString(DB.tx_email),oAddr.getString(DB.tp_recipient), 282 oAddr.getStringNull(DB.tx_personal,null)); 283 if (DebugFile.trace) { 284 DebugFile.decIdent(); 285 DebugFile.writeln("End InetAddrDB.write()"); 286 } 287 return bRetVal; 288 } 290 292 302 public static InternetAddress parseAddress(String sNamePlusEMail) 303 throws AddressException ,NullPointerException ,UnsupportedEncodingException { 304 InternetAddress oRetAdr = null; 305 306 String sAddr = sNamePlusEMail.trim(); 307 308 int iLeftAng = sAddr.indexOf('<'); 309 int iRightAng= sAddr.indexOf('>'); 310 int iLeftPar = sAddr.indexOf('('); 311 int iRightPar= sAddr.indexOf(')'); 312 int iLeftQuo = sAddr.indexOf('"'); 313 int iRightQuo; 314 if (iLeftQuo>=0) iRightQuo = sAddr.indexOf('"',iLeftQuo+1); else iRightQuo = -1; 315 316 if (iRightAng<iLeftAng) throw new AddressException ("Misplaced right angle"); 317 if (iLeftAng<0 && iRightAng>=0) throw new AddressException ("Missing left angle"); 318 if (iLeftAng>=0 && iRightAng<0) throw new AddressException ("Missing right angle"); 319 if (iLeftPar<0 && iRightPar>=0) throw new AddressException ("Missing left parenthesis"); 320 if (iLeftPar>=0 && iRightPar<0) throw new AddressException ("Missing right parenthesis"); 321 if (iRightPar<iLeftPar) throw new AddressException ("Misplaced right parenthesis"); 322 if (iLeftQuo>=0 && iRightQuo<0) throw new AddressException ("Unclosed quote"); 323 324 if (iLeftAng>=0 && iRightAng>=0 && iLeftPar>=0 && iRightPar>=0) { 325 oRetAdr = new InternetAddress (sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(iLeftPar+1,iRightPar)); 327 } else if (iLeftAng>=0 && iRightAng>=0 && iLeftQuo>=0 && iRightQuo>=0) { 328 oRetAdr = new InternetAddress (sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(iLeftQuo+1,iRightQuo)); 330 } else if (iLeftAng>=0 && iRightAng>=0) { 331 if (0==iLeftAng) 333 oRetAdr = new InternetAddress (sAddr.substring(1,iRightAng),sAddr.substring(iRightAng+1)); 334 else 335 oRetAdr = new InternetAddress (sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(0,iLeftAng)); 336 } else { 337 oRetAdr = new InternetAddress (sAddr); 338 } 339 return oRetAdr; 340 } 341 342 344 348 public String toString() { 349 return getPersonal()+" <"+getAddress()+">"; 350 } 351 } 352 | Popular Tags |