From: David G. Q. <dga...@us...> - 2008-07-24 07:55:44
|
Update of /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv18385/modules/core/src/com/babeldoc/core/pipeline/stage Modified Files: SmtpWriterPipelineStage.java DecompressionPipelineStage.java Log Message: SMTP authentification USER and PASSWORD Fixes at Decompress pipeline Index: SmtpWriterPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/SmtpWriterPipelineStage.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** SmtpWriterPipelineStage.java 4 Aug 2007 11:37:14 -0000 1.15 --- SmtpWriterPipelineStage.java 24 Jul 2008 07:55:51 -0000 1.16 *************** *** 92,95 **** --- 92,97 ---- import javax.mail.internet.MimeMultipart; + import org.apache.commons.vfs.auth.StaticUserAuthenticator; + /** *************** *** 126,129 **** --- 128,135 ---- /** Constants. */ public static final String PLAIN_FORMAT = "plain"; + /** Constants. */ + public static final String USER = "user"; + /** Constants. */ + public static final String PASSWORD = "password"; /** Constructor with this stages info. */ *************** *** 162,165 **** --- 168,176 ---- false, I18n.get("100151"))); + // Adding USER and PASSWORD for SMTP authentification + options.add(new ConfigOption(USER, IConfigOptionType.STRING, null, false, I18n.get("core.pipeline.stage.smtp.user"))); + options.add(new ConfigOption(PASSWORD, IConfigOptionType.STRING, null, false, I18n.get("core.pipeline.stage.smtp.password"))); + + return options; } *************** *** 228,231 **** --- 239,307 ---- /** + * Send the smtp message. + * + * @param smtpHost SMTP host + * @param smtpFrom email from + * @param smtpTo email to + * @param smtpSubject subject line + * @param smtpMsg email message + * @param files files! + * @param docToAttach attache document + * @param documentFileName attachment name + * @param contentType type of attachment. + * @throws PipelineException + */ + public void sendSmtpMessageWithAuthentification(String smtpHost, String smtpFrom, + String smtpTo, String smtpSubject, String smtpMsg, Collection files, + PipelineDocument docToAttach, String documentFileName, String contentType, String user, String password) + throws PipelineException { + //Set the host smtp address + SmtpAuthenticator auth = new SmtpAuthenticator(user, password); + //SmtpAuthenticator auth = new SmtpAuthenticator (); + + Properties props = new Properties(); + props.put("mail.smtp.host", smtpHost); + props.put("mail.smtp.auth", "true"); + + // create some properties and getChild the default Session + //Session session = Session.getInstance(props, null); + Session session = Session.getInstance(props, auth); + + // create a message + Message msg = new MimeMessage(session); + + try { + // set the from and to address + InternetAddress addressFrom = new InternetAddress(smtpFrom); + msg.setFrom(addressFrom); + + StringTokenizer st = new StringTokenizer(smtpTo, " ,"); + InternetAddress[] addressTo = new InternetAddress[st.countTokens()]; + + for (int i = 0; i < addressTo.length; i++) { + addressTo[i] = new InternetAddress(st.nextToken()); + } + + msg.setRecipients(Message.RecipientType.TO, addressTo); + + // Optional : You can also set your custom headers in the Email if you Want + // msg.addHeader("MyHeaderName", "myHeaderValue"); + // Setting the Subject and Content Type + msg.setSubject(smtpSubject); + + if (((files != null) && !files.isEmpty()) || (docToAttach != null)) { + msg.setContent(createMultipart(smtpMsg, contentType, files, + docToAttach, documentFileName)); + } else { + msg.setContent(smtpMsg, contentType); + } + + Transport.send(msg); + } catch (MessagingException messagex) { + throw new PipelineException("[SmtpWriterPipelineStage.process]", messagex); + } + } + + /** * pipeline stage - send an email message. * *************** *** 260,263 **** --- 336,342 ---- String documentFileName = getOptions(DOC_FILE_NAME); String format = getOptions(MAIL_FORMAT); + String user = getOptions(USER); + String password = getOptions(PASSWORD); + if ((format == null) || format.equals("")) { *************** *** 282,288 **** //send the actual message ! sendSmtpMessage(smtpHost, smtpFrom, smtpTo, smtpSubject, smtpMsg, ! filesToAttach, (attachDoc == true) ? this.getDocument() : null, ! documentFileName, contentType); return super.processHelper(); --- 361,375 ---- //send the actual message ! if (user == null || user == "") { ! sendSmtpMessage(smtpHost, smtpFrom, smtpTo, smtpSubject, smtpMsg, ! filesToAttach, (attachDoc == true) ? this.getDocument() : null, ! documentFileName, contentType); ! ! } else { ! sendSmtpMessageWithAuthentification(smtpHost, smtpFrom, smtpTo, smtpSubject, smtpMsg, ! filesToAttach, (attachDoc == true) ? this.getDocument() : null, ! documentFileName, contentType, user, password); ! ! } return super.processHelper(); *************** *** 383,385 **** --- 470,499 ---- return multipart; } + + private class SmtpAuthenticator extends javax.mail.Authenticator { + + String pass = ""; + String login = ""; + + public SmtpAuthenticator() { + super(); + } + + public SmtpAuthenticator(String login, String pass) { + super(); + + this.login = login; + this.pass = pass; + } + + public PasswordAuthentication getPasswordAuthentication() { + if (pass.equals("")) + return null; + else + return new PasswordAuthentication (login, pass); + } + + } } + + Index: DecompressionPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/DecompressionPipelineStage.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** DecompressionPipelineStage.java 20 Jun 2008 11:04:53 -0000 1.8 --- DecompressionPipelineStage.java 24 Jul 2008 07:55:51 -0000 1.9 *************** *** 68,71 **** --- 68,72 ---- import com.babeldoc.core.I18n; import com.babeldoc.core.LogService; + import com.babeldoc.core.Pair; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigInfo; *************** *** 208,214 **** for (int i = 0; i < results.size() && i < entryNames.size(); i++) { ! ArrayList newAtribute = new ArrayList(); ! psr[i] = processHelper((PipelineDocument)results.get(i))[0]; ! psr[i].getDocument().put("entryName", entryNames.get(i)); } --- 209,218 ---- for (int i = 0; i < results.size() && i < entryNames.size(); i++) { ! ArrayList newAtributeList = new ArrayList(); ! Pair newAtribute = new Pair("entryName", entryNames.get(i)); ! newAtributeList.add(newAtribute); ! psr[i] = processHelper((PipelineDocument)results.get(i),newAtributeList)[0]; ! ! //psr[i].getDocument().put("entryName", entryNames.get(i)); } |