1 package com.quikj.server.framework; 2 3 import java.io.*; 4 5 import javax.mail.internet.*; 6 7 import javax.xml.parsers.*; 9 import org.xml.sax.*; 10 import org.w3c.dom.*; 11 12 13 public class AceMailConfiguration 14 { 15 public static final String ROOT_NODE_NAME = "mail-configuration"; 16 public static final String EL_SENDMAIL_SERVER = "send-mail-server"; 17 public static final String ATT_SERVER_ADDRESS = "address"; 18 public static final String EL_AUTHENTICATION = "authentication"; 19 public static final String ATT_USER_NAME = "username"; 20 public static final String ATT_PASSWORD = "password"; 21 public static final String EL_OVERRIDE_FROM = "override-from"; 22 public static final String ATT_FROM = "from"; 23 public static final String EL_SAVE_PENDING_MAIL = "save-pending-mail"; 24 public static final String ATT_DIRECTORY = "dir"; 25 public static final String ATT_FILE = "savefile"; 26 27 public AceMailConfiguration(String path) 28 throws ArrayIndexOutOfBoundsException , FileNotFoundException, 29 IOException, AceException, SAXException, ParserConfigurationException 30 { 31 absPath = path; 32 loadConfigurationFile(); 33 instance = this; 34 } 35 36 public AceMailConfiguration(String dir, String file) 37 throws ArrayIndexOutOfBoundsException , FileNotFoundException, 38 IOException, AceException, SAXException, ParserConfigurationException 39 { 40 this(AceConfigTableFileParser.getAcePath(AceConfigTableFileParser.LOCAL_DATA, 41 dir, file)); 42 } 43 44 public static AceMailConfiguration getInstance() 45 { 46 return instance; 47 } 48 49 public static void dispose() 50 { 51 instance = null; 52 } 53 54 public String getConfigFileFullPathName() 55 { 56 return absPath; 57 } 58 59 public boolean isModified() 60 { 61 File file = new File(absPath); 62 long mod_time = file.lastModified(); 63 64 if (mod_time > lastModTime) 65 { 66 return true; 67 } 68 else 69 { 70 return false; 71 } 72 } 73 74 public void loadConfigurationFile() 75 throws FileNotFoundException, IOException, SAXException, AceException, 76 ParserConfigurationException 77 { 78 File file = new File(absPath); 79 lastModTime = file.lastModified(); 80 81 FileInputStream fis = new FileInputStream(file); 82 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 83 84 dbf.setValidating(false); 86 dbf.setIgnoringComments(true); 87 dbf.setIgnoringElementContentWhitespace(true); 88 dbf.setCoalescing(true); 89 DocumentBuilder doc_builder = dbf.newDocumentBuilder(); 90 91 Document doc = doc_builder.parse(fis); 92 93 processDoc(doc); 94 95 fis.close(); 96 } 97 98 private void processDoc(Document doc) 99 throws AceException 100 { 101 if (doc.getNodeType() != Node.DOCUMENT_NODE) 102 { 103 throw new AceException("Document does not begin with an XML node"); 105 } 106 107 Node root_element; 108 boolean root_found = false; 109 for (root_element = doc.getFirstChild(); 110 root_element != null; 111 root_element = root_element.getNextSibling()) 112 { 113 if (root_element.getNodeType() == Node.ELEMENT_NODE) 114 { 115 if (root_element.getNodeName().equals(ROOT_NODE_NAME) == false) 116 { 117 throw new AceException("Root node name " 118 + root_element.getNodeName() 119 + " must be " 120 + ROOT_NODE_NAME); 121 } 122 root_found = true; 123 break; 124 125 } 126 } 128 129 if (root_found == false) 130 { 131 throw new AceException("Root node " 132 + ROOT_NODE_NAME 133 + " not found"); 134 } 135 136 Node ele_node; 138 for (ele_node = root_element.getFirstChild(); 139 ele_node != null; 140 ele_node = ele_node.getNextSibling()) 141 { 142 if (ele_node.getNodeType() == Node.ELEMENT_NODE) 143 { 144 String node_name = ele_node.getNodeName(); 145 146 if (node_name.equals(EL_SENDMAIL_SERVER) == true) 147 { 148 processSendmailServer(ele_node); 149 } 150 else if (node_name.equals(EL_AUTHENTICATION) == true) 151 { 152 processAuthenticationParms(ele_node); 153 } 154 else if (node_name.equals(EL_OVERRIDE_FROM) == true) 155 { 156 processOverrideFrom(ele_node); 157 } 158 else if (node_name.equals(EL_SAVE_PENDING_MAIL) == true) 159 { 160 processSavePendingParms(ele_node); 161 } 162 else 163 { 164 throw new AceException("Unknown element " 165 + node_name); 166 } 167 } 168 } 170 171 if (checkParams() == false) 173 { 174 throw new AceException("Mandatory parameter(s) missing"); 175 } 176 } 177 178 private void processSendmailServer(Node node) 179 throws AceException 180 { 181 sendMailServer = AceXMLHelper.getXMLAttribute(node, ATT_SERVER_ADDRESS); 182 183 if (sendMailServer == null) 184 { 185 throw new AceException("The syntax for " 186 + EL_SENDMAIL_SERVER 187 + " is incorrect"); 188 } 189 } 190 191 private void processAuthenticationParms(Node node) 192 throws AceException 193 { 194 String [] attributes = 195 { ATT_USER_NAME, ATT_PASSWORD }; 196 197 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 198 199 if ((attrib_values[0] == null) || (attrib_values[1] == null)) 200 { 201 throw new AceException("One or more options from " 202 + ATT_USER_NAME 203 + ", " 204 + ATT_PASSWORD 205 + " is not specified in " 206 + EL_AUTHENTICATION); 207 } 208 209 userName = attrib_values[0]; 210 password = attrib_values[1]; 211 } 212 213 private void processOverrideFrom(Node node) 214 throws AceException 215 { 216 overrideFrom = AceXMLHelper.getXMLAttribute(node, ATT_FROM); 217 218 if (overrideFrom == null) 219 { 220 throw new AceException("The syntax for " 221 + EL_OVERRIDE_FROM 222 + " is incorrect"); 223 } 224 225 try 226 { 227 InternetAddress addr = new InternetAddress(overrideFrom); 228 } 229 catch (AddressException ex) 230 { 231 throw new AceException("The format of " 232 + EL_OVERRIDE_FROM 233 + " is not a valid internet email address"); 234 } 235 236 if (AceMailMessage.addressValid(overrideFrom) == false) 237 { 238 throw new AceException("The composition of " 239 + EL_OVERRIDE_FROM 240 + " is not a valid internet email address"); 241 } 242 } 243 244 private void processSavePendingParms(Node node) 245 throws AceException 246 { 247 String [] attributes = 248 { ATT_DIRECTORY, ATT_FILE }; 249 250 String [] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes); 251 252 String dir = null; 253 if (attrib_values[0] == null) { 255 File file = new File(absPath); 257 pendingDir = file.getParent(); 258 } 259 else { 261 try 262 { 263 pendingDir = AceConfigTableFileParser.getAcePath(AceConfigTableFileParser.LOCAL_DATA, attrib_values[0]); 264 } 265 catch (ArrayIndexOutOfBoundsException ex) 266 { 267 throw new AceException("Internal error (AceMailConfiguration.processSavePendingParms() - ArrayIndexOutOfBoundsException"); 269 } 270 } 271 272 if (attrib_values[1] == null) 273 { 274 throw new AceException(ATT_FILE 275 + " is not specified in " 276 + EL_SAVE_PENDING_MAIL); 277 } 278 279 File file = new File(pendingDir); 280 281 if ((file.isDirectory() == false) || 282 (file.exists() == false) || 283 (file.canWrite() == false)) 284 { 285 throw new AceException("The save pending mail directory " 286 + pendingDir 287 + " either does not exist or is not a directory or there is no write permission"); 288 } 289 290 pendingFile = attrib_values[1]; 291 292 } 293 294 private boolean checkParams() 295 { 296 if (sendMailServer == null) 297 { 298 return false; 299 } 300 301 if (pendingDir == null) 302 { 303 return false; 304 } 305 306 if (pendingFile == null) 307 { 308 return false; 309 } 310 311 return true; 312 } 313 314 317 public long getLastModTime() 318 { 319 return lastModTime; 320 } 321 322 325 public java.lang.String getOverrideFrom() 326 { 327 return overrideFrom; 328 } 329 330 333 public java.lang.String getPassword() 334 { 335 return password; 336 } 337 338 341 public java.lang.String getPendingDir() 342 { 343 return pendingDir; 344 } 345 346 349 public java.lang.String getPendingFile() 350 { 351 return pendingFile; 352 } 353 354 357 public java.lang.String getSendMailServer() 358 { 359 return sendMailServer; 360 } 361 362 365 public java.lang.String getUserName() 366 { 367 return userName; 368 } 369 370 374 public String getPendingPath() 375 { 376 return this.pendingPath; 377 } 378 379 private String absPath; 380 private long lastModTime; 381 382 private String sendMailServer = null; 383 private String userName = null; 384 private String password = null; 385 private String overrideFrom = null; 386 private String pendingDir = null; 387 private String pendingFile = null; 388 389 private static AceMailConfiguration instance = null; 390 391 392 private String pendingPath; 393 394 } 395 396 397 398 399 | Popular Tags |