| 1 53 package com.ivata.groupware.business.mail.struts; 54 55 import java.io.File ; 56 import java.util.Arrays ; 57 import java.util.List ; 58 import java.util.Vector ; 59 60 import javax.servlet.http.HttpServletRequest ; 61 import javax.servlet.http.HttpSession ; 62 63 import jp.ne.so_net.ga2.no_ji.jcom.IDispatch; 64 import jp.ne.so_net.ga2.no_ji.jcom.JComException; 65 66 import org.apache.log4j.Logger; 67 import org.apache.struts.action.ActionMapping; 68 import org.picocontainer.PicoContainer; 69 70 import com.ivata.groupware.admin.security.server.SecuritySession; 71 import com.ivata.groupware.admin.setting.Settings; 72 import com.ivata.groupware.admin.struts.HibernateSetupForm; 73 import com.ivata.groupware.business.mail.server.HMailServer; 74 import com.ivata.groupware.container.PicoContainerFactory; 75 import com.ivata.mask.util.StringHandling; 76 import com.ivata.mask.util.SystemException; 77 import com.ivata.mask.validation.ValidationError; 78 import com.ivata.mask.validation.ValidationErrors; 79 80 89 public class MailSetupForm extends HibernateSetupForm { 90 93 private static final long serialVersionUID = 1L; 94 97 private static final Logger logger = Logger.getLogger(MailSetupForm.class); 98 101 private String mailDomain; 102 105 private List mailDomains = null; 106 109 private String mailHostIMAP; 110 113 private String mailHostSMTP; 114 117 private String scriptsPath; 118 121 private boolean windows = false; 122 127 protected void clear() { 128 if (logger.isDebugEnabled()) { 129 logger.debug("clear() - start"); 130 } 131 132 if (logger.isDebugEnabled()) { 133 logger.debug("clear() - end"); 134 } 135 } 136 148 public String getMailDomain() { 149 if (logger.isDebugEnabled()) { 150 logger.debug("getMailDomain() - start"); 151 } 152 153 if (logger.isDebugEnabled()) { 154 logger 155 .debug("getMailDomain() - end - return value = " 156 + mailDomain); 157 } 158 return mailDomain; 159 } 160 167 public List getMailDomains() { 168 if (logger.isDebugEnabled()) { 169 logger.debug("getMailDomains() - start"); 170 } 171 172 if (logger.isDebugEnabled()) { 173 logger.debug("getMailDomains() - end - return value = " 174 + mailDomains); 175 } 176 return mailDomains; 177 } 178 184 public String getMailHostIMAP() { 185 if (logger.isDebugEnabled()) { 186 logger.debug("getMailHostIMAP() - start"); 187 } 188 189 if (logger.isDebugEnabled()) { 190 logger.debug("getMailHostIMAP() - end - return value = " 191 + mailHostIMAP); 192 } 193 return mailHostIMAP; 194 } 195 200 public String getMailHostSMTP() { 201 if (logger.isDebugEnabled()) { 202 logger.debug("getMailHostSMTP() - start"); 203 } 204 205 if (logger.isDebugEnabled()) { 206 logger.debug("getMailHostSMTP() - end - return value = " 207 + mailHostSMTP); 208 } 209 return mailHostSMTP; 210 } 211 219 public String getScriptsPath() { 220 if (logger.isDebugEnabled()) { 221 logger.debug("getScriptsPath() - start"); 222 } 223 224 if (logger.isDebugEnabled()) { 225 logger.debug("getScriptsPath() - end - return value = " 226 + scriptsPath); 227 } 228 return scriptsPath; 229 } 230 236 public boolean isWindows() { 237 if (logger.isDebugEnabled()) { 238 logger.debug("isWindows() - start"); 239 } 240 241 if (logger.isDebugEnabled()) { 242 logger.debug("isWindows() - end - return value = " + windows); 243 } 244 return windows; 245 } 246 247 253 public void reset( 254 final ActionMapping mappingParam, 255 final HttpServletRequest requestParam) { 256 if (logger.isDebugEnabled()) { 257 logger.debug("reset(ActionMapping mappingParam = " + mappingParam 258 + ", HttpServletRequest requestParam = " + requestParam 259 + ") - start"); 260 } 261 262 super.reset(mappingParam, requestParam); 263 SecuritySession securitySession = (SecuritySession) 264 requestParam.getSession().getAttribute("securitySession"); 265 PicoContainerFactory factory; 267 try { 268 factory = PicoContainerFactory.getInstance(); 269 } catch (SystemException e) { 270 logger.error("reset(ActionMapping, HttpServletRequest)", e); 271 272 throw new RuntimeException (e); 273 } 274 assert (factory != null); 275 PicoContainer container = factory.getGlobalContainer(); 276 assert (container != null); 277 Settings settings = (Settings) 278 container.getComponentInstance(Settings.class); 279 assert (settings != null); 280 windows = System.getProperty("os.name") 281 .toLowerCase().startsWith("windows"); 282 283 try { 284 mailDomain = 285 settings.getStringSetting(securitySession, 286 "emailAddressHost", 287 null); 288 if (windows) { 290 mailDomains = new Vector (); 291 try { 292 IDispatch domains = HMailServer.getDomains(); 293 int count; 294 if (domains == null) { 295 logger.error("Null domains object returned from" 296 + " HMailServer. Could not connect to " 297 + "hMailServer via COM."); 298 count = 0; 299 } else { 300 count = ((Integer ) domains.get("Count")).intValue(); 301 } 302 for (int i = 0; i < count; ++i) { 303 IDispatch domain = (IDispatch) domains.get("Item", 304 new Object [] { 305 new Integer (i) 306 }); 307 mailDomains.add(domain.get("Name")); 308 } 309 } catch (JComException e) { 310 logger.error(e.getClass().getName() 311 + ": getting Windows domains.", 312 e); 313 throw new RuntimeException (e); 314 } 315 } else if (StringHandling.isNullOrEmpty(mailDomain)) { 316 mailDomain = MailSetupConstants.DEFAULT_MAIL_DOMAIN; 319 mailDomains = null; 320 } 321 } catch (SystemException e) { 322 logger.error("reset(ActionMapping, HttpServletRequest)", e); 323 324 throw new RuntimeException (e); 325 } 326 try { 327 mailHostIMAP = 328 settings.getStringSetting(securitySession, "emailHost", 329 null); 330 } catch (SystemException e) { 331 logger.error("reset(ActionMapping, HttpServletRequest)", e); 332 333 throw new RuntimeException (e); 334 } 335 try { 336 mailHostSMTP = 337 settings.getStringSetting(securitySession, "emailHostSmtp", 338 null); 339 } catch (SystemException e) { 340 logger.error("reset(ActionMapping, HttpServletRequest)", e); 341 342 throw new RuntimeException (e); 343 } 344 try { 346 String pathScriptMailServer = 347 settings.getStringSetting(securitySession, 348 "pathScriptMailServer", 349 null); 350 int pos; 351 if (StringHandling.isNullOrEmpty(pathScriptMailServer) 354 || ((pos = pathScriptMailServer 355 .indexOf(MailSetupConstants 356 .SCRIPT_PATH_SUDO)) == -1)) { 357 scriptsPath = MailSetupConstants.DEFAULT_SCRIPTS_PATH; 358 } else { 359 scriptsPath = pathScriptMailServer.substring(0, pos); 360 } 361 } catch (SystemException e) { 362 logger.error("reset(ActionMapping, HttpServletRequest)", e); 363 364 throw new RuntimeException (e); 365 } 366 367 if (logger.isDebugEnabled()) { 368 logger.debug("reset(ActionMapping, HttpServletRequest) - end"); 369 } 370 } 371 372 377 public void setMailDomain(final String mailDomainParam) { 378 if (logger.isDebugEnabled()) { 379 logger.debug("Setting mailDomain. Before '" + mailDomain 380 + "', after '" + mailDomainParam + "'"); 381 } 382 mailDomain = mailDomainParam; 383 384 if (logger.isDebugEnabled()) { 385 logger.debug("setMailDomain(String) - end"); 386 } 387 } 388 393 public void setMailDomains(final List mailDomainsParam) { 394 if (logger.isDebugEnabled()) { 395 logger.debug("Setting mailDomains. Before '" + mailDomains 396 + "', after '" + mailDomainsParam + "'"); 397 } 398 mailDomains = mailDomainsParam; 399 400 if (logger.isDebugEnabled()) { 401 logger.debug("setMailDomains(List) - end"); 402 } 403 } 404 409 public void setMailHostIMAP(final String mailHostIMAPParam) { 410 if (logger.isDebugEnabled()) { 411 logger.debug("Setting mailHostIMAP. Before '" + mailHostIMAP 412 + "', after '" + mailHostIMAPParam + "'"); 413 } 414 mailHostIMAP = mailHostIMAPParam; 415 416 if (logger.isDebugEnabled()) { 417 logger.debug("setMailHostIMAP(String) - end"); 418 } 419 } 420 425 public void setMailHostSMTP(final String mailHostSMTPParam) { 426 if (logger.isDebugEnabled()) { 427 logger.debug("Setting mailHostSMTP. Before '" + mailHostSMTP 428 + "', after '" + mailHostSMTPParam + "'"); 429 } 430 mailHostSMTP = mailHostSMTPParam; 431 432 if (logger.isDebugEnabled()) { 433 logger.debug("setMailHostSMTP(String) - end"); 434 } 435 } 436 437 442 public void setScriptsPath(final String mailServerScriptsPathParam) { 443 if (logger.isDebugEnabled()) { 444 logger.debug("Setting mailServerScriptsPath. Before '" 445 + scriptsPath + "', after '" 446 + mailServerScriptsPathParam + "'"); 447 } 448 scriptsPath = mailServerScriptsPathParam; 449 450 if (logger.isDebugEnabled()) { 451 logger.debug("setScriptsPath(String) - end"); 452 } 453 } 454 458 public void setWindows(final boolean windowsParam) { 459 if (logger.isDebugEnabled()) { 460 logger.debug("Setting windows. Before '" + windows + "', after '" 461 + windowsParam + "'"); 462 } 463 windows = windowsParam; 464 465 if (logger.isDebugEnabled()) { 466 logger.debug("setWindows(boolean) - end"); 467 } 468 } 469 477 public ValidationErrors validate( 478 final HttpServletRequest requestParam, 479 final HttpSession sessionParam) { 480 if (logger.isDebugEnabled()) { 481 logger.debug("validate(HttpServletRequest requestParam = " 482 + requestParam + ", HttpSession sessionParam = " 483 + sessionParam + ") - start"); 484 } 485 486 ValidationErrors errors = super.validate(requestParam, sessionParam); 487 488 boolean mailDomainEmpty = StringHandling.isNullOrEmpty(mailDomain); 489 boolean mailHostSMTPEmpty = StringHandling.isNullOrEmpty(mailHostSMTP); 490 boolean mailHostIMAPEmpty = StringHandling.isNullOrEmpty(mailHostIMAP); 491 if (!(mailDomainEmpty 492 && mailHostIMAPEmpty 493 && mailHostSMTPEmpty) 494 && (mailDomainEmpty 495 || mailHostIMAPEmpty 496 || mailHostSMTPEmpty)) { 497 errors.add( 498 new ValidationError("errors.setup.mailParameters", 499 Arrays.asList(new String [] {}))); 500 } 501 if (!(StringHandling.isNullOrEmpty(scriptsPath) 503 || isWindows())) { 504 if (scriptsPath.endsWith("/")) { 506 scriptsPath = scriptsPath.substring(0, 507 scriptsPath.length() - 1); 508 } 509 File sudoScriptMailServerDir = new File (scriptsPath 510 + MailSetupConstants.SCRIPT_PATH_SUDO); 511 File eximScriptMailServerDir = new File (scriptsPath 512 + MailSetupConstants.SCRIPT_PATH_EXIM); 513 if (!sudoScriptMailServerDir.exists()) { 514 errors.add( 515 new ValidationError("errors.setup.sudoPath", 516 Arrays.asList(new String [] { 517 MailSetupConstants.SCRIPT_PATH_SUDO, 518 scriptsPath 519 }))); 520 } else if (!sudoScriptMailServerDir.isDirectory()) { 521 errors.add( 522 new ValidationError("errors.setup.sudoPathDirectory", 523 Arrays.asList(new String [] { 524 MailSetupConstants.SCRIPT_PATH_SUDO, 525 scriptsPath 526 }))); 527 } else if (!eximScriptMailServerDir.exists()) { 528 errors.add( 531 new ValidationError("errors.setup.eximPath", 532 Arrays.asList(new String [] { 533 MailSetupConstants.SCRIPT_PATH_EXIM, 534 scriptsPath 535 }))); 536 } else if (!eximScriptMailServerDir.isDirectory()) { 537 errors.add( 538 new ValidationError("errors.setup.eximPathDirectory", 539 Arrays.asList(new String [] { 540 MailSetupConstants.SCRIPT_PATH_EXIM, 541 scriptsPath 542 }))); 543 } 544 } 545 546 547 if (logger.isDebugEnabled()) { 548 logger.debug("validate - end - return value = " 549 + errors); 550 } 551 return errors; 552 } 553 } 554 | Popular Tags |