1 32 33 package com.knowgate.hipermail; 34 35 import java.lang.UnsupportedOperationException ; 36 37 import java.io.File ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.FileNotFoundException ; 41 42 import java.sql.DriverManager ; 43 import java.sql.Connection ; 44 import java.sql.SQLException ; 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 48 import java.net.ProtocolException ; 49 50 import javax.mail.Store ; 51 import javax.mail.Session ; 52 import javax.mail.URLName ; 53 import javax.mail.Folder ; 54 import javax.mail.MessagingException ; 55 import javax.mail.StoreClosedException ; 56 import javax.mail.AuthenticationFailedException ; 57 import javax.mail.FolderNotFoundException ; 58 import javax.mail.internet.MimeBodyPart ; 59 import javax.mail.internet.MimeMessage ; 60 61 import com.knowgate.debug.DebugFile; 62 import com.knowgate.jdc.JDCConnection; 63 import com.knowgate.dataobjs.DB; 64 import com.knowgate.dataobjs.DBSubset; 65 import com.knowgate.misc.Environment; 66 import com.knowgate.misc.Gadgets; 67 import com.knowgate.hipergate.Category; 68 import com.knowgate.acl.*; 69 import com.knowgate.dfs.FileSystem; 70 71 import javax.activation.CommandInfo ; 72 import javax.activation.CommandMap ; 73 import javax.activation.MailcapCommandMap ; 74 75 80 81 public class DBStore extends javax.mail.Store { 82 83 private JDCConnection oConn; 84 private ACLUser oUser; 85 private URLName oURL; 86 87 public DBStore (javax.mail.Session session, URLName url) 88 throws MessagingException { 89 super(session, url); 90 91 Class cStore = null; 92 93 try { 94 cStore = Class.forName("javax.mail.Store"); 95 } catch (ClassNotFoundException cnf) {} 96 97 oURL = new URLName (url.getProtocol(), url.getHost(), url.getPort(), url.getFile(), url.getUsername(), url.getPassword()); 98 99 if (null!=url.getFile()) { 100 File oDir = new File (url.getFile()); 101 102 if (!oDir.exists()) { 103 FileSystem oFS = new FileSystem(); 104 try { 105 oFS.mkdirs(url.getFile()); 106 } catch (Exception e) { 107 if (DebugFile.trace) DebugFile.writeln(e.getClass().getName() + " " + e.getMessage()); 108 throw new MessagingException (e.getMessage(), e); 109 } 110 } 111 } 112 113 oConn = null; 114 oUser = null; 115 } 116 117 119 129 public static DBStore open (Session oMailSession, String sProfile, 130 String sMBoxDir, String sGuUser, String sPwd) 131 throws MessagingException { 132 DBStore oNewInstance = new DBStore (oMailSession, new URLName ("jdbc://", sProfile, -1, sMBoxDir, sGuUser, sPwd)); 133 oNewInstance.connect(sProfile, sGuUser, sPwd); 134 return oNewInstance; 135 } 137 139 public JDCConnection getConnection() { 140 return oConn; 141 } 142 143 145 public Session getSession() { 146 return session; 147 } 148 149 151 public boolean isConnected() { 152 return (oConn!=null); 153 } 154 155 157 166 protected boolean protocolConnect(String host, int port, String user, String password) 167 throws AuthenticationFailedException , MessagingException { 168 169 if (DebugFile.trace) { 170 DebugFile.writeln("Begin DBStore.protocolConnect("+host+", "+user+", ...)"); 171 DebugFile.incIdent(); 172 } 173 174 if (oConn!=null || isConnected()) { 175 if (DebugFile.trace) DebugFile.decIdent(); 176 throw new MessagingException ("DBStore ia already connected"); 177 } 178 179 String dburl = Environment.getProfileVar(host, "dburl"); 180 String dbusr = Environment.getProfileVar(host, "dbuser"); 181 String dbpwd = Environment.getProfileVar(host, "dbpassword"); 182 String schema = Environment.getProfileVar(host, "schema", ""); 183 184 try { 185 if (DebugFile.trace) 186 DebugFile.writeln("DriverManager.getConnection("+dburl+", "+dbusr+", ...)"); 187 188 if (schema.length()>0) 189 oConn = new JDCConnection(DriverManager.getConnection(dburl, dbusr, dbpwd), null, schema); 190 else 191 oConn = new JDCConnection(DriverManager.getConnection(dburl, dbusr, dbpwd), null); 192 193 oConn.setAutoCommit(false); 194 195 short iAuth = ACL.autenticate(oConn, user, password, ACL.PWD_CLEAR_TEXT); 196 197 if (iAuth<0) { 198 oConn.close(); 199 oConn = null; 200 if (DebugFile.trace) DebugFile.decIdent(); 201 throw new AuthenticationFailedException (ACL.getErrorMessage(iAuth) + " (" + user + ")"); 202 } 203 else { 204 oUser = new ACLUser(oConn, user); 205 setConnected(true); 206 } 207 } 208 catch (SQLException sqle) { 209 if (DebugFile.trace) DebugFile.decIdent(); 210 throw new MessagingException (sqle.getMessage(), sqle); 211 } 212 213 if (DebugFile.trace) { 214 DebugFile.decIdent(); 215 DebugFile.writeln("End DBStore.protocolConnect()"); 216 } 217 218 return true; 219 } 220 221 223 public void connect (String host, String user, String password) 224 throws MessagingException { 225 226 protocolConnect (host, -1, user, password); 227 } 228 229 231 public void connect() throws MessagingException { 232 URLName oURLName = getURLName(); 233 234 protocolConnect (oURLName.getHost(), oURLName.getPort(), oURLName.getUsername(), oURLName.getPassword()); 235 } 236 237 239 public void close() throws MessagingException { 240 if (DebugFile.trace) { 241 DebugFile.writeln("Begin DBStore.close()"); 242 DebugFile.incIdent(); 243 } 244 if (null!=oConn && isConnected()) { 245 try { 246 oConn.close(); 247 oConn=null; 248 oUser=null; 249 setConnected(false); 250 } 251 catch (SQLException sqle) { 252 throw new MessagingException (sqle.getMessage(), sqle); 253 } 254 } 255 else { 256 throw new StoreClosedException (this, "Store already closed"); 257 } 258 if (DebugFile.trace) { 259 DebugFile.decIdent(); 260 DebugFile.writeln("End DBStore.close()"); 261 } 262 } 263 264 266 274 public Folder getFolder(URLName oURL) 275 throws StoreClosedException ,FolderNotFoundException ,MessagingException { 276 277 return getFolder(oURL.getFile()); 278 } 279 280 282 291 public Folder getFolder(String sFolderName) 292 throws StoreClosedException ,FolderNotFoundException ,MessagingException { 293 294 if (DebugFile.trace) { 295 DebugFile.writeln("Begin DBStore.getFolder("+sFolderName+")"); 296 DebugFile.incIdent(); 297 } 298 299 if (sFolderName==null) { 300 if (DebugFile.trace) DebugFile.decIdent(); 301 throw new NullPointerException ("DBStore.getFolder() folder name cannot be null"); 302 } 303 if (sFolderName.length()==0) { 304 if (DebugFile.trace) DebugFile.decIdent(); 305 throw new NullPointerException ("DBStore.getFolder() folder name cannot be an empty string"); 306 } 307 308 if (!isConnected()) { 309 if (DebugFile.trace) DebugFile.decIdent(); 310 throw new StoreClosedException (this, "Store is closed"); 311 } 312 313 DBFolder oRetVal = new DBFolder(this, sFolderName); 314 boolean bExistsGuid = false; 315 PreparedStatement oStmt = null; 316 ResultSet oRSet = null; 317 318 try { 319 if (sFolderName.length()==32) { 320 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT NULL FROM "+DB.k_categories+" WHERE "+DB.gu_category+"='"+sFolderName+"')"); 321 322 oStmt = this.getConnection().prepareStatement("SELECT NULL FROM "+DB.k_categories+" WHERE "+DB.gu_category+"=?"); 323 oStmt.setString(1, sFolderName); 324 oRSet = oStmt.executeQuery(); 325 bExistsGuid = oRSet.next(); 326 oRSet.close(); 327 oStmt.close(); 328 } 329 else 330 bExistsGuid = false; 331 332 if (bExistsGuid) { 333 oRetVal.getCategory().load(oConn, new Object [] {sFolderName}); 334 } 335 else { 336 String sGuid = oUser.getMailFolder(oConn, sFolderName); 337 338 if (null==sGuid) { 339 if (DebugFile.trace) DebugFile.decIdent(); 340 throw new FolderNotFoundException (oRetVal, sFolderName); 341 } 342 oRetVal.getCategory().load(oConn, new Object [] {sGuid}); 343 } 344 } 345 catch (SQLException sqle) { 346 if (DebugFile.trace) DebugFile.decIdent(); 347 throw new MessagingException (sqle.getMessage(), sqle); 348 } 349 350 if (DebugFile.trace) { 351 DebugFile.decIdent(); 352 DebugFile.writeln("End DBStore.getFolder("+sFolderName+")"); 353 } 354 355 return oRetVal; 356 } 358 360 368 public DBFolder getDBFolder(String sFolderName) 369 throws StoreClosedException ,FolderNotFoundException ,MessagingException { 370 return (DBFolder) getFolder(sFolderName); 371 } 372 373 375 384 public DBFolder openDBFolder(String sFolderName, int iMode) 385 throws StoreClosedException ,FolderNotFoundException ,MessagingException { 386 DBFolder oFldr = (DBFolder) getFolder(sFolderName); 387 oFldr.open(iMode); 388 return oFldr; 389 } 390 391 393 400 public Folder getDefaultFolder() 401 throws StoreClosedException ,FolderNotFoundException ,MessagingException { 402 403 return getFolder("inbox"); 404 } 406 408 public Folder [] getPersonalNamespaces() 409 throws StoreClosedException ,FolderNotFoundException ,MessagingException { 410 411 DBFolder[] aRetVal; 412 413 if (!isConnected()) 414 throw new StoreClosedException (this, "Store is closed"); 415 416 try { 417 418 String sGuid = oUser.getMailRoot(oConn); 419 420 if (null == sGuid) 421 throw new FolderNotFoundException (new DBFolder(this, "mailroot"), "mailroot"); 422 423 Category oMailRoot = new Category(sGuid); 424 DBSubset oChilds = oMailRoot.getChilds(oConn); 425 int iFolders = oChilds.getRowCount(); 426 427 if (0==iFolders) 428 aRetVal = null; 429 else { 430 Object [] oPK = new Object []{null}; 431 DBFolder oFR; 432 aRetVal = new DBFolder[iFolders]; 433 for (int f=0; f<iFolders; f++) { 434 oPK[0] = oChilds.get(0,f); 435 oFR = new DBFolder(this, null); 436 oFR.getCategory().load(oConn, oPK); 437 aRetVal[f] = oFR; 438 } } } 441 catch (SQLException sqle) { 442 throw new MessagingException (sqle.getMessage(), sqle); 443 } 444 return aRetVal; 445 } 446 447 449 public Folder [] getSharedNamespaces() { 450 return null; 451 } 452 453 455 public Folder [] getUserNamespaces(String sUserId) 456 throws StoreClosedException ,FolderNotFoundException ,MessagingException { 457 DBFolder[] aRetVal; 458 459 if (!isConnected()) 460 throw new StoreClosedException (this, "Store is closed"); 461 462 try { 463 464 ACLUser oUsr = new ACLUser(sUserId); 465 466 String sGuid = oUsr.getMailRoot(oConn); 467 468 if (null == sGuid) 469 throw new FolderNotFoundException (new DBFolder(this, "mailroot"), "mailroot"); 470 471 Category oMailRoot = new Category(sGuid); 472 DBSubset oChilds = oMailRoot.getChilds(oConn); 473 int iFolders = oChilds.getRowCount(); 474 475 if (0==iFolders) 476 aRetVal = null; 477 else { 478 Object [] oPK = new Object []{null}; 479 DBFolder oFR; 480 aRetVal = new DBFolder[iFolders]; 481 for (int f=0; f<iFolders; f++) { 482 oPK[0] = oChilds.get(0,f); 483 oFR = new DBFolder(this, null); 484 oFR.getCategory().load(oConn, oPK); 485 aRetVal[f] = oFR; 486 } } } 489 catch (SQLException sqle) { 490 throw new MessagingException (sqle.getMessage(), sqle); 491 } 492 return aRetVal; 493 } 495 497 public URLName getURLName() { 498 return oURL; 499 } 500 501 503 public ACLUser getUser() { 504 return oUser; 505 } 506 507 509 516 public DBMimeMessage preFetchMessage(Folder oIncomingFldr, int iMsgNum) 517 throws MessagingException { 518 519 if (DebugFile.trace) { 520 DebugFile.writeln("Begin DBStore.preFetchMessage([Folder],"+String.valueOf(iMsgNum)+")"); 521 DebugFile.incIdent(); 522 } 523 524 if (null==oIncomingFldr) 525 throw new MessagingException ("Unable to open inbox folder", 526 new NullPointerException ("DBStore.preFetchMessage() Folder is null")); 527 528 boolean bWasConnected = isConnected(); 529 if (!bWasConnected) connect(); 530 DBFolder oInboxFldr = (DBFolder) getDefaultFolder(); 531 oInboxFldr.open(Folder.READ_WRITE); 532 boolean bWasOpen = oIncomingFldr.isOpen(); 533 if (!bWasOpen) oIncomingFldr.open(Folder.READ_ONLY); 534 DBMimeMessage oMimeMsg = new DBMimeMessage ((MimeMessage ) oIncomingFldr.getMessage(iMsgNum)); 535 oInboxFldr.appendMessage(oMimeMsg); 536 if (!bWasOpen) oIncomingFldr.close(false); 537 if (!bWasConnected) close(); 538 539 if (DebugFile.trace) { 540 DebugFile.decIdent(); 541 DebugFile.writeln("End DBStore.preFetchMessage() : " + oMimeMsg.getMessageGuid()); 542 } 543 544 return oMimeMsg; 545 } 547 549 public static String MBoxDirectory(String sProfile, int iDomainId, String sWorkAreaGu) 550 throws ProtocolException { 551 String sSep = System.getProperty("file.separator"); 552 String sFileProtocol = Environment.getProfileVar(sProfile, "fileprotocol", "file://"); 553 String sMBoxDir; 554 if (sFileProtocol.equals("file://")) 555 sMBoxDir = sFileProtocol + Environment.getProfilePath(sProfile, "storage") + "domains" + sSep + String.valueOf(iDomainId) + sSep + "workareas" + sSep + sWorkAreaGu; 556 else 557 throw new java.net.ProtocolException (sFileProtocol); 558 return sMBoxDir; 559 } } 561 | Popular Tags |