1 48 package com.ivata.groupware.business.mail.struts; 49 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.net.Socket ; 53 import java.net.UnknownHostException ; 54 import java.sql.Connection ; 55 import java.sql.DriverManager ; 56 import java.sql.PreparedStatement ; 57 import java.sql.SQLException ; 58 import java.sql.Statement ; 59 import java.util.List ; 60 61 import javax.servlet.http.HttpServletRequest ; 62 import javax.servlet.http.HttpServletResponse ; 63 import javax.servlet.http.HttpSession ; 64 65 import org.apache.log4j.Logger; 66 import org.apache.struts.action.ActionErrors; 67 import org.apache.struts.action.ActionForm; 68 import org.apache.struts.action.ActionMapping; 69 import org.apache.struts.action.ActionMessage; 70 import org.picocontainer.PicoContainer; 71 72 import com.ivata.groupware.admin.security.Security; 73 import com.ivata.groupware.admin.security.server.SecuritySession; 74 import com.ivata.groupware.admin.setting.Settings; 75 import com.ivata.groupware.admin.struts.HibernateSetupAction; 76 import com.ivata.groupware.admin.struts.HibernateSetupForm; 77 import com.ivata.groupware.container.PicoContainerFactory; 78 import com.ivata.mask.MaskFactory; 79 import com.ivata.mask.util.SystemException; 80 import com.ivata.mask.web.struts.MaskAction; 81 import com.ivata.mask.web.struts.MaskAuthenticator; 82 83 91 public class MailSetupAction extends HibernateSetupAction { 92 95 private static final Logger logger = 96 Logger.getLogger(MailSetupAction.class); 97 100 private static final String SETTINGS_STATEMENT = 101 "UPDATE SETTING SET value = ? WHERE name = ? AND person_user IS NULL"; 102 107 private String mailFolderNamespace = ""; 108 117 public MailSetupAction(final Security securityParam, 118 final Settings settingsParam, 119 final MaskFactory maskFactoryParam, 120 final MaskAuthenticator authenticatorParam) { 121 super(securityParam, settingsParam, maskFactoryParam, 122 authenticatorParam); 123 } 124 135 private StringBuffer checkSocket(final String host, final int port, 136 final ActionErrors errorMessages, final String errorString) { 137 Socket socket = null; 138 StringBuffer socketOutput = new StringBuffer (); 139 try { 140 socket = new Socket (host, port); 141 logger.info("Connected with server " 142 + socket.getInetAddress() 143 + ":" 144 + socket.getPort()); 145 InputStream socketInput = socket.getInputStream(); 146 byte [] buffer = new byte[1024]; 147 int bytesRead = 0; 148 do { 149 synchronized(this) { 152 for (int waitCount = 0; 153 (waitCount < MailSetupConstants.SOCKET_WAIT_NUMBER) 154 && (socketInput.available() == 0); 155 ++waitCount) { 156 try { 157 wait(MailSetupConstants.SOCKET_WAIT_INTERVAL); 158 } catch (InterruptedException e1) { 159 logger.warn("Socket wait interrupted.", e1); 160 break; 161 } 162 } 163 } 164 socketOutput.append(new String (buffer, 0, bytesRead)); 165 } while ((socketInput.available() != 0) 166 && ((bytesRead = socketInput.read( 167 buffer, 0, buffer.length)) != -1)); 168 if (logger.isDebugEnabled()) { 169 logger.debug("Read the following string from " 170 + socket.getInetAddress() 171 + ":" 172 + socket.getPort()); 173 logger.debug(socketOutput.toString()); 174 } 175 } catch (UnknownHostException e) { 176 errorMessages.add(null, new ActionMessage( 177 errorString, 178 e.getClass().getName(), 179 host, 180 e.getMessage())); 181 } catch (IOException e) { 182 errorMessages.add(null, new ActionMessage( 183 errorString, 184 e.getClass().getName(), 185 host, 186 e.getMessage())); 187 } finally { 188 if ((socket != null) 189 && !socket.isClosed()) { 190 try { 191 socket.close(); 192 } catch (IOException e) { 193 logger.error(e.getClass().getName() 194 + ": closing socket: " 195 + e.getMessage(), 196 e); 197 } 198 } 199 } 200 return socketOutput; 201 } 202 213 public String execute(ActionMapping mappingParam, ActionErrors errorsParam, 214 ActionForm formParam, HttpServletRequest requestParam, 215 HttpServletResponse responseParam, HttpSession sessionParam) 216 throws SystemException { 217 MailSetupForm setupForm = (MailSetupForm) formParam; 218 List mailDomains = setupForm.getMailDomains(); 219 if (setupForm.isWindows() 220 && ((mailDomains == null) 221 || mailDomains.isEmpty())) { 222 errorsParam.add(null, new ActionMessage( 223 "errors.setup.noMailDomains")); 224 } 225 return super.execute(mappingParam, errorsParam, formParam, 226 requestParam, responseParam, sessionParam); 227 } 228 243 public String onConfirm(ActionMapping mappingParam, 244 ActionErrors errorMessages, ActionForm formParam, 245 HttpServletRequest request, HttpServletResponse responseParam, 246 HttpSession session, String defaultForwardParam) 247 throws SystemException { 248 MailSetupForm setupForm = (MailSetupForm)formParam; 249 250 String mailHostIMAP = setupForm.getMailHostIMAP(); 252 String mailHostSMTP = setupForm.getMailHostSMTP(); 253 254 StringBuffer socketOutput = checkSocket(mailHostIMAP, 255 MailSetupConstants.IMAP_PORT, 256 errorMessages, "errors.setup.hostIMAP"); 257 if (socketOutput.indexOf(MailSetupConstants.CYRUS_SIGNATURE) != -1) { 260 logger.info("IMAP server identified as Cyrus - setting folder " 261 + "namespace to '" 262 + MailSetupConstants.CYRUS_FOLDER_NAMESPACE 263 + "'."); 264 mailFolderNamespace = MailSetupConstants.CYRUS_FOLDER_NAMESPACE; 265 } else if (socketOutput.indexOf(MailSetupConstants.COURIER_SIGNATURE) != -1) { 266 logger.info("IMAP server identified as Courier - setting folder " 267 + "namespace to '" 268 + MailSetupConstants.COURIER_FOLDER_NAMESPACE 269 + "'."); 270 mailFolderNamespace = MailSetupConstants.COURIER_FOLDER_NAMESPACE; 271 } else { 272 logger.info("IMAP server neither Cyrus nor Courier - using " 273 + "default folder namespace."); 274 mailFolderNamespace = ""; 275 } 276 277 checkSocket(mailHostSMTP, MailSetupConstants.SMTP_PORT, 278 errorMessages, "errors.setup.hostSMTP"); 279 280 String superReturn = 281 super.onConfirm(mappingParam, errorMessages, formParam, request, 282 responseParam, session, defaultForwardParam); 283 if (!errorMessages.isEmpty()) { 285 return null; 286 } 287 288 if (superReturn != null) { 290 return superReturn; 291 } 292 return defaultForwardParam; 294 } 295 304 private void setOneSystemSetting( 305 final PreparedStatement preparedStatement, 306 final String settingName, 307 final String settingValue) 308 throws SystemException { 309 if (logger.isDebugEnabled()) { 310 logger.debug("setOneSystemSetting(" 311 + "PreparedStatement preparedStatement = " 312 + preparedStatement 313 + ", String settingName = " 314 + settingName 315 + ", String settingValue = " 316 + settingValue + ") - start"); 317 } 318 try { 319 preparedStatement.setString(1, settingValue); 320 } catch (SQLException e) { 321 logger.error ("setOneSystemSetting - Setting setting value to '" 322 + settingValue 323 + "'", e); 324 throw new SystemException(e); 325 } 326 try { 327 preparedStatement.setString(2, settingName); 328 } catch (SQLException e) { 329 logger.error ("setOneSystemSetting - Setting setting name to '" 330 + settingName 331 + "'", e); 332 throw new SystemException(e); 333 } 334 int numberOfRows; 335 try { 336 numberOfRows = preparedStatement.executeUpdate(); 337 } catch (SQLException e) { 338 logger.error("setOneSystemSetting - Executing statement '" 339 + SETTINGS_STATEMENT 340 + "'", e); 341 throw new SystemException(e); 342 } 343 if (logger.isDebugEnabled()) { 344 logger.debug("setOneSystemSetting -" 345 + "Number of rows returned: " 346 + numberOfRows); 347 } 348 if (numberOfRows != 1) { 349 String message = "setOneSystemSetting - More than 1 row returned " 350 + "for system setting '" 351 + settingName 352 + "'"; 353 logger.error(message); 354 throw new SystemException(message); 355 } 356 if (logger.isDebugEnabled()) { 357 logger.debug("setOneSystemSetting - end"); 358 } 359 } 360 369 protected void resetFactoryUpdateSettings( 370 final HibernateSetupForm hibernateSetupForm, 371 final SecuritySession securitySession, 372 final HttpSession session) 373 throws SystemException { 374 if (logger.isDebugEnabled()) { 375 logger.debug("resetFactoryUpdateSettings(" 376 + "HibernateSetupForm hibernateSetupForm = " 377 + hibernateSetupForm 378 + ", SecuritySession securitySession = " 379 + securitySession 380 + ", HttpSession session = " 381 + session + ") - start"); 382 } 383 384 MailSetupForm setupForm = (MailSetupForm) hibernateSetupForm; 385 String uRL = hibernateSetupForm.getDatabaseURL(); 386 String userName = hibernateSetupForm.getDatabaseUserName(); 387 String password = hibernateSetupForm.getDatabasePassword(); 388 389 if (logger.isDebugEnabled()) { 390 logger.debug("resetFactoryUpdateSettings - " 391 + "Connecting to new DB with parameters: " 392 + "uRL = " + uRL + ", " 393 + "userName = " + userName + ", " 394 + "password = ******)"); 395 } 396 Connection connection; 397 try { 398 connection = DriverManager.getConnection(uRL, userName, 399 password); 400 } catch (SQLException e) { 401 logger.error("resetFactoryUpdateSettings - " 402 + "Connecting to new DB with paramteters: " 403 + "uRL = " + uRL + ", " 404 + "userName = " + userName + ", " 405 + "password = ******)", e); 406 throw new SystemException(e); 407 } 408 PreparedStatement preparedStatement; 409 try { 410 preparedStatement = connection.prepareStatement( 411 SETTINGS_STATEMENT); 412 } catch (SQLException e) { 413 logger.error("resetFactoryUpdateSettings - Preparing statement '" 414 + SETTINGS_STATEMENT 415 + "'", e); 416 throw new SystemException(e); 417 } 418 try { 419 setOneSystemSetting(preparedStatement, 420 "emailAddressHost", 421 setupForm.getMailDomain()); 422 setOneSystemSetting(preparedStatement, 423 "emailFolderNamespace", 424 mailFolderNamespace); 425 setOneSystemSetting(preparedStatement, 426 "emailHost", 427 setupForm.getMailHostIMAP()); 428 setOneSystemSetting(preparedStatement, 429 "emailHostSmtp", 430 setupForm.getMailHostSMTP()); 431 setOneSystemSetting(preparedStatement, 432 "emailScriptServerEnvironment", 433 "SITE_ID=www\nSUDO_USER=root\nSUDO_PATH=" 434 + setupForm.getScriptsPath() 435 + MailSetupConstants.SCRIPT_PATH_EXIM); 436 setOneSystemSetting(preparedStatement, 437 "pathScriptMailServer", 438 setupForm.getScriptsPath() 439 + MailSetupConstants.SCRIPT_PATH_SUDO); 440 if (setupForm.isWindows()) { 442 setOneSystemSetting(preparedStatement, 443 "securitySessionServer", 444 "com.ivata.groupware.business.mail.server.HMailServer"); 445 setOneSystemSetting(preparedStatement, 447 "emailFolderInbox", 448 "INBOX"); 449 } else { 450 setOneSystemSetting(preparedStatement, 452 "securitySessionServer", 453 "com.ivata.groupware.business.mail.server." 454 + "ScriptMailServer"); 455 } 456 } catch (Exception e) { 457 logger.error("resetFactoryUpdateSettings - Exception, so rolling " 458 + "back transation", e); 459 try { 460 preparedStatement.close(); 461 } catch (SQLException e2) { 462 logger.error ("resetFactoryUpdateSettings - Errror rolling " 463 + "back statement.", e2); 464 } 465 throw new SystemException(e); 466 467 } finally { 468 try { 469 preparedStatement.close(); 470 } catch (SQLException e) { 471 logger.error ("resetFactoryUpdateSettings - Error closing " 472 + "prepared statement.", e); 473 throw new SystemException(e); 474 } 475 if ((connection != null) 477 && (uRL.indexOf("hsqldb:file") != -1)) { 478 logger.info("resetFactoryUpdateSettings - shutting down " 479 + "HSQLDB file."); 480 Statement statement = null; 481 try { 482 statement = connection.createStatement(); 483 boolean success = statement.execute("SHUTDOWN"); 484 if (!success) { 485 logger.error("resetFactoryUpdateSettings - shutting " 486 + "down HSQLDB file returned FALSE."); 487 } 488 } catch (SQLException e) { 489 logger.error ("resetFactoryUpdateSettings - Error closing " 490 + " shutdown statement.", e); 491 throw new SystemException(e); 492 } finally { 493 if (statement != null) { 494 try { 495 statement.close(); 496 } catch (SQLException e) { 497 logger.error ("resetFactoryUpdateSettings - Error " 498 + "closing shutdown statement.", e); 499 throw new SystemException(e); 500 } 501 } 502 } 503 } 504 } 505 if (logger.isDebugEnabled()) { 506 logger.debug("resetFactoryUpdateSettings() - end"); 507 } 508 } 509 } 510 | Popular Tags |