1 56 package org.opencrx.mail.servlet; 57 58 import java.util.Iterator ; 59 60 import javax.servlet.ServletContext ; 61 62 import org.opencrx.kernel.workflow1.cci.ExternalTask; 63 import org.openmdx.application.log.AppLog; 64 import org.openmdx.base.accessor.jmi.cci.JmiServiceException; 65 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0; 66 import org.openmdx.compatibility.base.naming.Path; 67 import org.openmdx.kernel.exception.BasicException; 68 69 76 public class MailServerConfig { 77 78 89 public MailServerConfig( 90 RefPackage_1_0 rootPkg, 91 String providerName, 92 String segmentName, 93 ServletContext context 94 ) { 95 String server = null; 96 String account = null; 97 String password = null; 98 Integer port = null; 99 String protocol = null; 100 String mailbox = null; 101 Boolean sslRequired = null; 102 Boolean deleteImportedMessages = null; 103 Boolean debug = null; 104 try { 105 Path taskIdentity = new Path( 106 "xri:@openmdx:org.opencrx.kernel.workflow1/provider/" + providerName + "/segment/" + segmentName + "/wfProcess/" + TASK_ID 107 ); 108 ExternalTask task = null; 109 try { 110 task = (ExternalTask)rootPkg.refObject(taskIdentity.toXri()); 111 } 112 catch(JmiServiceException e) { 113 if(e.getExceptionCode() != BasicException.Code.NOT_FOUND) { 115 throw e; 116 } 117 try { 118 org.opencrx.kernel.workflow1.cci.workflow1Package workflowPkg = getWorkflowPkg(rootPkg); 119 org.opencrx.kernel.base.cci.basePackage basePkg = getBasePkg(rootPkg); 120 org.opencrx.kernel.workflow1.cci.Segment workflowSegment = 121 (org.opencrx.kernel.workflow1.cci.Segment)rootPkg.refObject( 122 taskIdentity.getPrefix(taskIdentity.size() - 2).toXri() 123 ); 124 task = workflowPkg.getExternalTaskClass().createExternalTask(); 126 task.setName(MailImporterServlet.class.getName()); 127 task.setDescription("Mail importer. To override the default property create a new property without suffix .Default"); 128 workflowPkg.refBegin(); 129 workflowSegment.addWfProcess( 130 TASK_ID, 131 task 132 ); 133 org.opencrx.kernel.base.cci.StringProperty sp = basePkg.getStringPropertyClass().createStringProperty(); 135 sp.setName(MailServerConfig.OPTION_MAIL_SERVER + ".Default"); 136 sp.setDescription("Incoming mail server"); 137 sp.setStringValue("mail.changeme.com"); 138 task.addProperty(sp); 139 sp = basePkg.getStringPropertyClass().createStringProperty(); 141 sp.setName(MailServerConfig.OPTION_MAIL_ACCOUNT + ".Default"); 142 sp.setDescription("Account name"); 143 sp.setStringValue("account"); 144 task.addProperty(sp); 145 sp = basePkg.getStringPropertyClass().createStringProperty(); 147 sp.setName(MailServerConfig.OPTION_MAIL_PASSWORD + ".Default"); 148 sp.setDescription("Account password"); 149 sp.setStringValue("password"); 150 task.addProperty(sp); 151 org.opencrx.kernel.base.cci.IntegerProperty ip = basePkg.getIntegerPropertyClass().createIntegerProperty(); 153 ip.setName(MailServerConfig.OPTION_MAIL_SERVER_PORT + ".Default"); 154 ip.setDescription("Server port number"); 155 ip.setIntegerValue(110); 156 task.addProperty(ip); 157 sp = basePkg.getStringPropertyClass().createStringProperty(); 159 sp.setName(MailServerConfig.OPTION_MAIL_PROTOCOL + ".Default"); 160 sp.setDescription("Mail protocol"); 161 sp.setStringValue("pop3"); 162 task.addProperty(sp); 163 sp = basePkg.getStringPropertyClass().createStringProperty(); 165 sp.setName(MailServerConfig.OPTION_MAIL_BOX + ".Default"); 166 sp.setDescription("Mailbox name"); 167 sp.setStringValue("INBOX"); 168 task.addProperty(sp); 169 org.opencrx.kernel.base.cci.BooleanProperty bp = basePkg.getBooleanPropertyClass().createBooleanProperty(); 171 bp.setName(MailServerConfig.OPTION_MAIL_SSL_REQUIRED + ".Default"); 172 bp.setDescription("Server requires SSL"); 173 bp.setBooleanValue(false); 174 task.addProperty(bp); 175 bp = basePkg.getBooleanPropertyClass().createBooleanProperty(); 177 bp.setName(MailServerConfig.OPTION_MAIL_DELETE_IMPORTED_MESSAGES + ".Default"); 178 bp.setDescription("Delete imported messages"); 179 bp.setBooleanValue(false); 180 task.addProperty(bp); 181 bp = basePkg.getBooleanPropertyClass().createBooleanProperty(); 183 bp.setName(MailServerConfig.OPTION_MAIL_DEBUG + ".Default"); 184 bp.setDescription("Enable debugging output"); 185 bp.setBooleanValue(true); 186 task.addProperty(bp); 187 workflowPkg.refCommit(); 188 } 189 catch(JmiServiceException e0) { 190 AppLog.info("Can not create default configuration", e0.getMessage()); 191 throw e0; 192 } 193 } 194 for( 196 int i = 0; 197 i < 2; 198 i++ 199 ) { 200 String suffix = new String []{".Default", ""}[i]; 201 for( 202 Iterator j = task.getProperty().iterator(); 203 j.hasNext(); 204 ) { 205 org.opencrx.kernel.base.cci.Property property = (org.opencrx.kernel.base.cci.Property)j.next(); 206 if((MailServerConfig.OPTION_MAIL_SERVER + suffix).equals(property.getName())) { 207 server = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue(); 208 } 209 else if((MailServerConfig.OPTION_MAIL_ACCOUNT + suffix).equals(property.getName())) { 210 account = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue(); 211 } 212 else if((MailServerConfig.OPTION_MAIL_PASSWORD + suffix).equals(property.getName())) { 213 password = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue(); 214 } 215 else if((MailServerConfig.OPTION_MAIL_SERVER_PORT + suffix).equals(property.getName())) { 216 port = new Integer (((org.opencrx.kernel.base.cci.IntegerProperty)property).getIntegerValue()); 217 } 218 else if((MailServerConfig.OPTION_MAIL_PROTOCOL + suffix).equals(property.getName())) { 219 protocol = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue(); 220 } 221 else if((MailServerConfig.OPTION_MAIL_BOX + suffix).equals(property.getName())) { 222 mailbox = ((org.opencrx.kernel.base.cci.StringProperty)property).getStringValue(); 223 } 224 else if((MailServerConfig.OPTION_MAIL_SSL_REQUIRED + suffix).equals(property.getName())) { 225 sslRequired = new Boolean (((org.opencrx.kernel.base.cci.BooleanProperty)property).isBooleanValue()); 226 } 227 else if((MailServerConfig.OPTION_MAIL_DELETE_IMPORTED_MESSAGES + suffix).equals(property.getName())) { 228 deleteImportedMessages = new Boolean (((org.opencrx.kernel.base.cci.BooleanProperty)property).isBooleanValue()); 229 } 230 else if((MailServerConfig.OPTION_MAIL_DEBUG + suffix).equals(property.getName())) { 231 debug = new Boolean (((org.opencrx.kernel.base.cci.BooleanProperty)property).isBooleanValue()); 232 } 233 } 234 } 235 } 236 catch(Exception e) { 237 AppLog.info("Can not retrieve config from external task configuration", e.getMessage()); 238 } 239 this.mailServer = server == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_SERVER) : server; 241 this.mailAccount = account == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_ACCOUNT) : account; 242 this.mailPassword = password == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_PASSWORD) : password; 243 this.mailPort = port == null ? Integer.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_SERVER_PORT)) : port; 244 protocol = protocol == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_PROTOCOL) : protocol; 245 if( 246 (protocol == null) || 247 (!MailStore.POP3_MODE.equalsIgnoreCase(protocol) && !MailStore.IMAP_MODE.equalsIgnoreCase(protocol)) 248 ) { 249 this.mailProtocol = MailStore.POP3_MODE; 250 } 251 else { 252 this.mailProtocol = protocol; 253 } 254 this.mailbox = mailbox == null ? context.getInitParameter(MailServerConfig.OPTION_MAIL_BOX) : mailbox; 255 this.isSslRequired = sslRequired == null ? Boolean.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_SSL_REQUIRED)).booleanValue() : sslRequired.booleanValue(); 256 this.deleteImportedMessages = deleteImportedMessages == null ? Boolean.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_DELETE_IMPORTED_MESSAGES)).booleanValue() : deleteImportedMessages.booleanValue(); 257 this.debugMode = debug == null ? Boolean.valueOf(context.getInitParameter(MailServerConfig.OPTION_MAIL_DEBUG)).booleanValue() : debug.booleanValue(); 258 } 259 260 protected static org.opencrx.kernel.base.cci.basePackage getBasePkg( 262 RefPackage_1_0 rootPkg 263 ) { 264 return (org.opencrx.kernel.base.cci.basePackage)rootPkg.refPackage( 265 org.opencrx.kernel.base.cci.basePackage.class.getName() 266 ); 267 } 268 269 protected static org.opencrx.kernel.workflow1.cci.workflow1Package getWorkflowPkg( 271 RefPackage_1_0 rootPkg 272 ) { 273 return (org.opencrx.kernel.workflow1.cci.workflow1Package)rootPkg.refPackage( 274 org.opencrx.kernel.workflow1.cci.workflow1Package.class.getName() 275 ); 276 } 277 278 282 public String toString( 283 ) { 284 StringBuffer tmp = new StringBuffer (); 285 tmp.append("EMailServerConfiguration{"); 286 tmp.append("mailServer='").append(mailServer).append("', "); 287 tmp.append("mailProtocol='").append(mailProtocol).append("', "); 288 tmp.append("mailPort='").append(mailPort).append("', "); 289 tmp.append("debugMode='").append(debugMode).append("', "); 290 tmp.append("isSslRequired='").append(isSslRequired).append("', "); 291 tmp.append("deleteImportedMessages='").append(this.deleteImportedMessages).append("', "); 292 tmp.append("mailAccount='").append(mailAccount).append("'}"); 293 return tmp.toString(); 294 } 295 296 public String getMailServer( 298 ) { 299 return this.mailServer; 300 } 301 302 public String getMailAccount( 304 ) { 305 return this.mailAccount; 306 } 307 308 public String getMailPassword( 310 ) { 311 return this.mailPassword; 312 } 313 314 public Integer getMailPort( 316 ) { 317 return this.mailPort; 318 } 319 320 public String getMailbox( 322 ) { 323 return this.mailbox; 324 } 325 326 public boolean deleteImportedMessages( 328 ) { 329 return this.deleteImportedMessages; 330 } 331 332 private static final String TASK_ID = "MailImporter"; 334 private static final String OPTION_MAIL_DEBUG = "debug"; 335 private static final String OPTION_MAIL_SSL_REQUIRED = "sslRequired"; 336 private static final String OPTION_MAIL_BOX = "mailbox"; 337 private static final String OPTION_MAIL_PROTOCOL = "protocol"; 338 private static final String OPTION_MAIL_SERVER_PORT = "port"; 339 private static final String OPTION_MAIL_PASSWORD = "password"; 340 private static final String OPTION_MAIL_ACCOUNT = "account"; 341 private static final String OPTION_MAIL_SERVER = "server"; 342 private static final String OPTION_MAIL_DELETE_IMPORTED_MESSAGES = "deleteImportedMessages"; 343 344 345 protected final String mailServer; 346 347 protected final String mailAccount; 348 349 protected final String mailPassword; 350 351 protected final Integer mailPort; 352 353 protected final String mailProtocol; 354 355 protected final String mailbox; 356 357 protected final boolean debugMode; 358 359 protected final boolean isSslRequired; 360 361 protected final boolean deleteImportedMessages; 362 363 } 364 | Popular Tags |