001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * ---------------------------------------------------------
005: * ivata groupware may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * ---------------------------------------------------------
030: * $Log: MailSetupForm.java,v $
031: * Revision 1.7 2005/10/10 16:06:39 colinmacleod
032: * Merged in changes from releases 0.11.2 and 0.11.3.
033: *
034: * Revision 1.6 2005/10/09 09:39:27 colinmacleod
035: * Merged changes from ivata groupware v0.11.2 back into main trunk.
036: *
037: * Revision 1.5 2005/10/03 10:21:16 colinmacleod
038: * Fixed some style and javadoc issues.
039: *
040: * Revision 1.4 2005/10/02 14:08:59 colinmacleod
041: * Added/improved log4j logging.
042: *
043: * Revision 1.3 2005/09/14 16:15:35 colinmacleod
044: * Added constructor.
045: * Added mailDomains.
046: *
047: * Revision 1.2.2.1 2005/10/08 17:35:43 colinmacleod
048: * Extended for hMailServer v4.x
049: * Now uses JDBC to set settings (rather than attempting to re-initializse).
050: *
051: * Revision 1.2 2005/04/27 15:20:40 colinmacleod
052: * Fix for (no) scripts path on Windows.
053: *
054: * Revision 1.1 2005/04/11 10:03:43 colinmacleod
055: * Added setup feature.
056: *
057: * ---------------------------------------------------------
058: */
059: package com.ivata.groupware.business.mail.struts;
060:
061: import java.io.File;
062: import java.util.Arrays;
063: import java.util.List;
064: import java.util.Vector;
065:
066: import javax.servlet.http.HttpServletRequest;
067: import javax.servlet.http.HttpSession;
068:
069: import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
070: import jp.ne.so_net.ga2.no_ji.jcom.JComException;
071:
072: import org.apache.log4j.Logger;
073: import org.apache.struts.action.ActionMapping;
074: import org.picocontainer.PicoContainer;
075:
076: import com.ivata.groupware.admin.security.server.SecuritySession;
077: import com.ivata.groupware.admin.setting.Settings;
078: import com.ivata.groupware.admin.struts.HibernateSetupForm;
079: import com.ivata.groupware.business.mail.server.HMailServer;
080: import com.ivata.groupware.container.PicoContainerFactory;
081: import com.ivata.mask.util.StringHandling;
082: import com.ivata.mask.util.SystemException;
083: import com.ivata.mask.validation.ValidationError;
084: import com.ivata.mask.validation.ValidationErrors;
085:
086: /**
087: * Contains all of the values you need to setup a basic <strong>ivata
088: * groupware</strong> installation.
089: *
090: * @since ivata groupware 0.11 (2005-03-27)
091: * @author Colin MacLeod
092: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
093: * @version $Revision: 1.7 $
094: */
095: public class MailSetupForm extends HibernateSetupForm {
096: /**
097: * Serialization version (for <code>Serializable</code> interface).
098: */
099: private static final long serialVersionUID = 1L;
100: /**
101: * Logger for this class.
102: */
103: private static final Logger logger = Logger
104: .getLogger(MailSetupForm.class);
105: /**
106: * <copyDoc>Refer to {@link #getMailDomain}.</copyDoc>
107: */
108: private String mailDomain;
109: /**
110: * <copyDoc>Refer to {@link #getMailDomains}.</copyDoc>
111: */
112: private List mailDomains = null;
113: /**
114: * <copyDoc>Refer to {@link #getMailHostIMAP}.</copyDoc>
115: */
116: private String mailHostIMAP;
117: /**
118: * <copyDoc>Refer to {@link #getMailHostSMTP}.</copyDoc>
119: */
120: private String mailHostSMTP;
121: /**
122: * <copyDoc>Refer to {@link #getMailServerScriptsPath}.</copyDoc>
123: */
124: private String scriptsPath;
125: /**
126: * <copyDoc>Refer to {@link #isWindows}.</copyDoc>
127: */
128: private boolean windows = false;
129:
130: /**
131: * Construct the mail setup form.
132: *
133: * @param settingsParam {@inheritDoc}
134: */
135: public MailSetupForm(final Settings settingsParam) {
136: super (settingsParam);
137: }
138:
139: /**
140: * Prepare the form.
141: *
142: * @see com.ivata.mask.web.struts.DialogForm#clear()
143: */
144: protected void clear() {
145: if (logger.isDebugEnabled()) {
146: logger.debug("clear() - start");
147: }
148:
149: if (logger.isDebugEnabled()) {
150: logger.debug("clear() - end");
151: }
152: }
153:
154: /**
155: * <p>
156: * Get the domain name of the email address for users of this system.
157: * The domain name is that part which comes afer the 'at symbol' (@) in
158: * their email addresses.
159: * </p>
160: * <p>
161: * On Windows, this must match the domain name set up in
162: * <strong>hMailServer</strong>.
163: * </p>
164: * @return Returns the mail domain.
165: */
166: public String getMailDomain() {
167: if (logger.isDebugEnabled()) {
168: logger.debug("getMailDomain() - start");
169: }
170:
171: if (logger.isDebugEnabled()) {
172: logger.debug("getMailDomain() - end - return value = "
173: + mailDomain);
174: }
175: return mailDomain;
176: }
177:
178: /**
179: * On <strong>Windows</strong>, this returns a list of all email domains
180: * as strings. On other platforms, it just returns <code>null</code>.
181: *
182: * @return Returns the mail domains for <strong>hMailServer</strong> on
183: * <strong>Windows</strong>
184: */
185: public List getMailDomains() {
186: if (logger.isDebugEnabled()) {
187: logger.debug("getMailDomains() - start");
188: }
189:
190: if (logger.isDebugEnabled()) {
191: logger.debug("getMailDomains() - end - return value = "
192: + mailDomains);
193: }
194: return mailDomains;
195: }
196:
197: /**
198: * Name or IP address of the host used to receive <strong>IMAP</strong>
199: * messages.
200: *
201: * @return Returns the mail host used for IMAP.
202: */
203: public String getMailHostIMAP() {
204: if (logger.isDebugEnabled()) {
205: logger.debug("getMailHostIMAP() - start");
206: }
207:
208: if (logger.isDebugEnabled()) {
209: logger.debug("getMailHostIMAP() - end - return value = "
210: + mailHostIMAP);
211: }
212: return mailHostIMAP;
213: }
214:
215: /**
216: * Name or IP address of the host used to send mail messages.
217: *
218: * @return Returns the mail host used for SMTP.
219: */
220: public String getMailHostSMTP() {
221: if (logger.isDebugEnabled()) {
222: logger.debug("getMailHostSMTP() - start");
223: }
224:
225: if (logger.isDebugEnabled()) {
226: logger.debug("getMailHostSMTP() - end - return value = "
227: + mailHostSMTP);
228: }
229: return mailHostSMTP;
230: }
231:
232: /**
233: * On <strong>UNIX-style</strong> systems, this is the path to scripts which
234: * are used to add/remove users from the mail system. (The implementation
235: * for <strong>Windows</strong> uses <strong>COM</strong> to communicate
236: * with <strong>hMailServer</strong> directly.
237: *
238: * @return Returns the mail server scripts path.
239: */
240: public String getScriptsPath() {
241: if (logger.isDebugEnabled()) {
242: logger.debug("getScriptsPath() - start");
243: }
244:
245: if (logger.isDebugEnabled()) {
246: logger.debug("getScriptsPath() - end - return value = "
247: + scriptsPath);
248: }
249: return scriptsPath;
250: }
251:
252: /**
253: * Returns <code>true</code> if this is a <strong>Microsoft Windows</strong>
254: * installation.
255: * @return Returns <code>true</code> if this is a <strong>Microsoft
256: * Windows</strong> installation.
257: */
258: public boolean isWindows() {
259: if (logger.isDebugEnabled()) {
260: logger.debug("isWindows() - start");
261: }
262:
263: if (logger.isDebugEnabled()) {
264: logger.debug("isWindows() - end - return value = "
265: + windows);
266: }
267: return windows;
268: }
269:
270: /**
271: * {@inheritDoc}
272: *
273: * @param mappingParam {@inheritDoc}
274: * @param requestParam {@inheritDoc}
275: */
276: public void reset(final ActionMapping mappingParam,
277: final HttpServletRequest requestParam) {
278: if (logger.isDebugEnabled()) {
279: logger.debug("reset(ActionMapping mappingParam = "
280: + mappingParam
281: + ", HttpServletRequest requestParam = "
282: + requestParam + ") - start");
283: }
284:
285: super .reset(mappingParam, requestParam);
286: SecuritySession securitySession = (SecuritySession) requestParam
287: .getSession().getAttribute("securitySession");
288: // don't use constructor injection as the factory may reset
289: PicoContainerFactory factory;
290: try {
291: factory = PicoContainerFactory.getInstance();
292: } catch (SystemException e) {
293: logger.error("reset(ActionMapping, HttpServletRequest)", e);
294:
295: throw new RuntimeException(e);
296: }
297: assert (factory != null);
298: PicoContainer container = factory.getGlobalContainer();
299: assert (container != null);
300: Settings settings = (Settings) container
301: .getComponentInstance(Settings.class);
302: assert (settings != null);
303: windows = System.getProperty("os.name").toLowerCase()
304: .startsWith("windows");
305:
306: try {
307: mailDomain = settings.getStringSetting(securitySession,
308: "emailAddressHost", null);
309: // on windows, get all the domains from hMailServer
310: if (windows) {
311: mailDomains = new Vector();
312: try {
313: IDispatch domains = HMailServer.getDomains();
314: int count;
315: if (domains == null) {
316: logger
317: .error("Null domains object returned from"
318: + " HMailServer. Could not connect to "
319: + "hMailServer via COM.");
320: count = 0;
321: } else {
322: count = ((Integer) domains.get("Count"))
323: .intValue();
324: }
325: for (int i = 0; i < count; ++i) {
326: IDispatch domain = (IDispatch) domains
327: .get("Item",
328: new Object[] { new Integer(i) });
329: mailDomains.add(domain.get("Name"));
330: }
331: } catch (JComException e) {
332: logger.error(e.getClass().getName()
333: + ": getting Windows domains.", e);
334: throw new RuntimeException(e);
335: }
336: } else if (StringHandling.isNullOrEmpty(mailDomain)) {
337: // only default the mail domain in UNIX; on windows it will
338: // default to the first domain found in hMailServer
339: mailDomain = MailSetupConstants.DEFAULT_MAIL_DOMAIN;
340: mailDomains = null;
341: }
342: } catch (SystemException e) {
343: logger.error("reset(ActionMapping, HttpServletRequest)", e);
344:
345: throw new RuntimeException(e);
346: }
347: try {
348: mailHostIMAP = settings.getStringSetting(securitySession,
349: "emailHost", null);
350: } catch (SystemException e) {
351: logger.error("reset(ActionMapping, HttpServletRequest)", e);
352:
353: throw new RuntimeException(e);
354: }
355: try {
356: mailHostSMTP = settings.getStringSetting(securitySession,
357: "emailHostSmtp", null);
358: } catch (SystemException e) {
359: logger.error("reset(ActionMapping, HttpServletRequest)", e);
360:
361: throw new RuntimeException(e);
362: }
363: // script mail server is not for windows - only UNIX style machines
364: try {
365: String pathScriptMailServer = settings.getStringSetting(
366: securitySession, "pathScriptMailServer", null);
367: int pos;
368: // normall the script mail server path contains the sudo subpath
369: // if not, or the setting is empty, default
370: if (StringHandling.isNullOrEmpty(pathScriptMailServer)
371: || ((pos = pathScriptMailServer
372: .indexOf(MailSetupConstants.SCRIPT_PATH_SUDO)) == -1)) {
373: scriptsPath = MailSetupConstants.DEFAULT_SCRIPTS_PATH;
374: } else {
375: scriptsPath = pathScriptMailServer.substring(0, pos);
376: }
377: } catch (SystemException e) {
378: logger.error("reset(ActionMapping, HttpServletRequest)", e);
379:
380: throw new RuntimeException(e);
381: }
382:
383: if (logger.isDebugEnabled()) {
384: logger
385: .debug("reset(ActionMapping, HttpServletRequest) - end");
386: }
387: }
388:
389: /**
390: * <copyDoc>Refer to {@link #getMailDomain}.</copyDoc>
391: * @param mailDomainParam
392: * <copyDoc>Refer to {@link #getMailDomain}.</copyDoc>
393: */
394: public void setMailDomain(final String mailDomainParam) {
395: if (logger.isDebugEnabled()) {
396: logger.debug("Setting mailDomain. Before '" + mailDomain
397: + "', after '" + mailDomainParam + "'");
398: }
399: mailDomain = mailDomainParam;
400:
401: if (logger.isDebugEnabled()) {
402: logger.debug("setMailDomain(String) - end");
403: }
404: }
405:
406: /**
407: * <copyDoc>Refer to {@link #getMailDomains}.</copyDoc>
408: * @param mailDomainsParam
409: * <copyDoc>Refer to {@link #getMailDomains}.</copyDoc>
410: */
411: public void setMailDomains(final List mailDomainsParam) {
412: if (logger.isDebugEnabled()) {
413: logger.debug("Setting mailDomains. Before '" + mailDomains
414: + "', after '" + mailDomainsParam + "'");
415: }
416: mailDomains = mailDomainsParam;
417:
418: if (logger.isDebugEnabled()) {
419: logger.debug("setMailDomains(List) - end");
420: }
421: }
422:
423: /**
424: * <copyDoc>Refer to {@link #getMailHostIMAP}.</copyDoc>
425: * @param mailHostIMAPParam
426: * <copyDoc>Refer to {@link #getMailHostIMAP}.</copyDoc>
427: */
428: public void setMailHostIMAP(final String mailHostIMAPParam) {
429: if (logger.isDebugEnabled()) {
430: logger.debug("Setting mailHostIMAP. Before '"
431: + mailHostIMAP + "', after '" + mailHostIMAPParam
432: + "'");
433: }
434: mailHostIMAP = mailHostIMAPParam;
435:
436: if (logger.isDebugEnabled()) {
437: logger.debug("setMailHostIMAP(String) - end");
438: }
439: }
440:
441: /**
442: * <copyDoc>Refer to {@link #getMailHostSMTP}.</copyDoc>
443: * @param mailHostSMTPParam
444: * <copyDoc>Refer to {@link #getMailHostSMTP}.</copyDoc>
445: */
446: public void setMailHostSMTP(final String mailHostSMTPParam) {
447: if (logger.isDebugEnabled()) {
448: logger.debug("Setting mailHostSMTP. Before '"
449: + mailHostSMTP + "', after '" + mailHostSMTPParam
450: + "'");
451: }
452: mailHostSMTP = mailHostSMTPParam;
453:
454: if (logger.isDebugEnabled()) {
455: logger.debug("setMailHostSMTP(String) - end");
456: }
457: }
458:
459: /**
460: * <copyDoc>Refer to {@link #getMailServerScriptsPath}.</copyDoc>
461: * @param mailServerScriptsPathParam <copyDoc>Refer to
462: * {@link #getMailServerScriptsPath}.</copyDoc>
463: */
464: public void setScriptsPath(final String mailServerScriptsPathParam) {
465: if (logger.isDebugEnabled()) {
466: logger.debug("Setting mailServerScriptsPath. Before '"
467: + scriptsPath + "', after '"
468: + mailServerScriptsPathParam + "'");
469: }
470: scriptsPath = mailServerScriptsPathParam;
471:
472: if (logger.isDebugEnabled()) {
473: logger.debug("setScriptsPath(String) - end");
474: }
475: }
476:
477: /**
478: * <copyDoc>Refer to {@link #isWindows}.</copyDoc>
479: * @param windowsParam <copyDoc>Refer to {@link #isWindows}.</copyDoc>
480: */
481: public void setWindows(final boolean windowsParam) {
482: if (logger.isDebugEnabled()) {
483: logger.debug("Setting windows. Before '" + windows
484: + "', after '" + windowsParam + "'");
485: }
486: windows = windowsParam;
487:
488: if (logger.isDebugEnabled()) {
489: logger.debug("setWindows(boolean) - end");
490: }
491: }
492:
493: /**
494: * This method has been implemented to check that the mail server fields
495: * are either all blank, or all preasent.
496: * @param requestParam current request we are processing.
497: * @param sessionParam current HTTP session.
498: * @return a validation errors collection containing any errors which
499: * would prevent the form from being successfully processed.
500: */
501: public ValidationErrors validate(
502: final HttpServletRequest requestParam,
503: final HttpSession sessionParam) {
504: if (logger.isDebugEnabled()) {
505: logger.debug("validate(HttpServletRequest requestParam = "
506: + requestParam + ", HttpSession sessionParam = "
507: + sessionParam + ") - start");
508: }
509:
510: ValidationErrors errors = super .validate(requestParam,
511: sessionParam);
512:
513: boolean mailDomainEmpty = StringHandling
514: .isNullOrEmpty(mailDomain);
515: boolean mailHostSMTPEmpty = StringHandling
516: .isNullOrEmpty(mailHostSMTP);
517: boolean mailHostIMAPEmpty = StringHandling
518: .isNullOrEmpty(mailHostIMAP);
519: if (!(mailDomainEmpty && mailHostIMAPEmpty && mailHostSMTPEmpty)
520: && (mailDomainEmpty || mailHostIMAPEmpty || mailHostSMTPEmpty)) {
521: errors.add(new ValidationError(
522: "errors.setup.mailParameters", Arrays
523: .asList(new String[] {})));
524: }
525: // now check the scripts path, if a scripts path was specified
526: if (!(StringHandling.isNullOrEmpty(scriptsPath) || isWindows())) {
527: // strip off trailing slash
528: if (scriptsPath.endsWith("/")) {
529: scriptsPath = scriptsPath.substring(0, scriptsPath
530: .length() - 1);
531: }
532: File sudoScriptMailServerDir = new File(scriptsPath
533: + MailSetupConstants.SCRIPT_PATH_SUDO);
534: File eximScriptMailServerDir = new File(scriptsPath
535: + MailSetupConstants.SCRIPT_PATH_EXIM);
536: if (!sudoScriptMailServerDir.exists()) {
537: errors.add(new ValidationError("errors.setup.sudoPath",
538: Arrays.asList(new String[] {
539: MailSetupConstants.SCRIPT_PATH_SUDO,
540: scriptsPath })));
541: } else if (!sudoScriptMailServerDir.isDirectory()) {
542: errors.add(new ValidationError(
543: "errors.setup.sudoPathDirectory",
544: Arrays.asList(new String[] {
545: MailSetupConstants.SCRIPT_PATH_SUDO,
546: scriptsPath })));
547: } else if (!eximScriptMailServerDir.exists()) {
548: // note - if one is missing they likely both are - I used an
549: // else here so only one rror would show
550: errors.add(new ValidationError("errors.setup.eximPath",
551: Arrays.asList(new String[] {
552: MailSetupConstants.SCRIPT_PATH_EXIM,
553: scriptsPath })));
554: } else if (!eximScriptMailServerDir.isDirectory()) {
555: errors.add(new ValidationError(
556: "errors.setup.eximPathDirectory",
557: Arrays.asList(new String[] {
558: MailSetupConstants.SCRIPT_PATH_EXIM,
559: scriptsPath })));
560: }
561: }
562:
563: if (logger.isDebugEnabled()) {
564: logger.debug("validate - end - return value = " + errors);
565: }
566: return errors;
567: }
568: }
|