You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Bala Sambandam <ba...@idanim.com> on 2001/04/18 00:53:49 UTC

mail/sendmail processors code submission

Hello,

I've written a web frontend for IMAP/Sendmail using the mail.xsl and 
sendmail.xsl processors, and I'm including the modifications that I had 
to make to these processors to get as far as I've gotten with the 
application.

mail.xsl.diff is diff -w -u against version 1.6
sendmail.xsl.diff is diff -w -u against version 1.9

If any additional info is needed for either file, then let me know by 
sending mail to me directly, as I will likely unsub from the list.

The modifications that I made to mail.xsl require HTML Tidy for the 
interpretation of badly formed HTML messages as XML fragments; 
otherwise, badly formed HTML messages are interpretted as plain text.  
Well formed HTML messages are interpreted as XML fragments without the 
need of HTML Tidy.

thanks,
Bala

Re: mail/sendmail processors code submission

Posted by Davanum Srinivas <di...@yahoo.com>.
Bala,

Can you send us some samples used to test both mail.xsl and sendmail.xsl? Then i will be able to
test it before checking it in.

Thanks,
dims

--- Bala Sambandam <ba...@idanim.com> wrote:
> Hello,
> 
> I've written a web frontend for IMAP/Sendmail using the mail.xsl and 
> sendmail.xsl processors, and I'm including the modifications that I had 
> to make to these processors to get as far as I've gotten with the 
> application.
> 
> mail.xsl.diff is diff -w -u against version 1.6
> sendmail.xsl.diff is diff -w -u against version 1.9
> 
> If any additional info is needed for either file, then let me know by 
> sending mail to me directly, as I will likely unsub from the list.
> 
> The modifications that I made to mail.xsl require HTML Tidy for the 
> interpretation of badly formed HTML messages as XML fragments; 
> otherwise, badly formed HTML messages are interpretted as plain text.  
> Well formed HTML messages are interpreted as XML fragments without the 
> need of HTML Tidy.
> 
> thanks,
> Bala
> > --- mail.xsl	Tue Apr 17 17:25:12 2001
> +++ idanim/intranet/src/org/apache/cocoon/processor/xsp/library/mail/mail.xsl	Tue Apr 17
> 15:41:21 2001
> @@ -55,6 +55,7 @@
>  
>   <author>Donald A. Ball Jr.</author>
>   <author>Ugo Cei</author>
> + <contributor>Bala Sambandam, Idanim Solutions Inc.</contributor>
>   <version>1.2</version>
>  -->
>  <xsl:stylesheet
> @@ -68,6 +69,7 @@
>   <xsl:copy>
>    <xsl:apply-templates select="@*"/>
>    <xsp:structure>
> +				<xsp:include>java.lang.reflect.Method</xsp:include>
>     <xsp:include>javax.mail.Session</xsp:include>
>     <xsp:include>javax.mail.Store</xsp:include>
>     <xsp:include>javax.mail.Folder</xsp:include>
> @@ -76,13 +78,32 @@
>     <xsp:include>javax.mail.Part</xsp:include>
>     <xsp:include>javax.mail.Multipart</xsp:include>
>     <xsp:include>javax.mail.Flags</xsp:include>
> +				<xsp:include>javax.mail.MessagingException</xsp:include>
>     <xsp:include>javax.mail.internet.MimeMessage</xsp:include>
>     <xsp:include>javax.mail.internet.MimePart</xsp:include>
>     <xsp:include>javax.mail.internet.InternetAddress</xsp:include>
> +				<xsp:include>javax.mail.search.MessageIDTerm</xsp:include>
> +				<xsp:include>javax.mail.search.FromStringTerm</xsp:include>
> +				<xsp:include>javax.mail.search.SearchTerm</xsp:include>
> +				<xsp:include>javax.mail.search.OrTerm</xsp:include>
>     <xsp:include>java.text.DateFormat</xsp:include>
>    </xsp:structure>
>    <xsl:apply-templates/>
> +
>    <xsp:logic>
> +
> +				/**
> +				 * This method process the message content.
> +				 *
> +				 * @param request The servlet request.
> +				 * @param response The servlet response.
> +				 * @param document The xsp document.
> +				 * @param xspParentNode The xsp parent node pass through.
> +				 * @param xspCurrentNode The xsp current node pass through.
> +				 * @param xspNodeStack The xsp node stack pass through.
> +				 * @param session The servlet session.
> +				 * @param part The the message or submessage (on recursion).
> +				 */
>     void _mail_process_content(
>      HttpServletRequest request,
>      HttpServletResponse response,
> @@ -91,23 +112,199 @@
>      Node xspCurrentNode,
>      Stack xspNodeStack,
>      HttpSession session,
> -    MimePart part) throws Exception {
> +						MimePart part) throws MessagingException, IOException {
> +	
>    if (part.isMimeType("text/plain")) {
> -   <xsp:content><xsp:expr>(String)part.getContent()</xsp:expr></xsp:content>
> +
> +						// just return the plain text
> +						<xsp:content>
> +							<xsp:expr>(String)part.getContent()</xsp:expr>
> +						</xsp:content>
> +
> +					} else if (part.isMimeType("text/html")) {
> +
> +						try {
> +
> +							<xsp:content>
> +								<xsp:expr>
> +									// try to parse the html as xml
> +									// who knows it might be well formed
> +									xspParser.parse(new InputSource(new StringReader(
> +										(String) part.getContent()))).getDocumentElement()
> +								</xsp:expr>
> +							</xsp:content>
> +
> +						} catch (Throwable e) {
> +
> +							// more than likely something will go wrong
> +
> +							e.printStackTrace();
> +
> +							try {
> +
> +								// try to load the HTML Tidy class to
> +								// clean the HTML up a bit
> +								Class _mail_tidy_class =
> +									Class.forName("org.w3c.tidy.Tidy");
> +								Object _mail_tidy = _mail_tidy_class.newInstance();
> +
> +								// find the right method
> +								Class[] _mail_params = {
> +									InputStream.class, OutputStream.class
> +								};
> +								Method _mail_method =
> +									_mail_tidy_class.getMethod("parseDOM", _mail_params);
> +
> +								// invoke the method
> +								Object[] _mail_args = {
> +									new ByteArrayInputStream(
> +										((String) part.getContent()).getBytes()),
> +									null
> +								};
> +								Document _mail_html_doc = (Document)
> +										_mail_method.invoke(
> +											_mail_tidy, _mail_args);
> +
> +								// return the HTML as an XML fragment
> +								<xsp:content>
> +									<xsp:expr>
> +										_mail_html_doc.getDocumentElement()
> +									</xsp:expr>
> +								</xsp:content>
> +
> +							} catch (Throwable e1) {
> +
> +								// if all else fails then just return it
> +								// as if it was plain text
> +								e1.printStackTrace();
> +								<xsp:content>
> +									<xsp:expr>(String) part.getContent()</xsp:expr>
> +								</xsp:content>
> +
> +							}
> +						}
> +
>    } else if (part.isMimeType("message/rfc822")) {
> -   <xsp:content><rfc822><xsp:logic>
> -  
>
_mail_process_content(request,response,document,xspParentNode,xspCurrentNode,xspNodeStack,session,(MimePart)part.getContent());
> -   </xsp:logic></rfc822></xsp:content>
> +
> +						<xsp:content>
> +							<rfc822>
> +								<xsp:logic>
> +									_mail_process_content(
> +										request,
> +										response,
> +										document,
> +										xspParentNode,
> +										xspCurrentNode,
> +										xspNodeStack,
> +										session,
> +										(MimePart)part.getContent());
> +								</xsp:logic>
> +							</rfc822>
> +						</xsp:content>
> +
>    } else if (part.isMimeType("multipart/*")) {
> +
> +						// recursively process each subpart
> +
>     Multipart multipart = (Multipart)part.getContent();
>     int count = multipart.getCount();
>     for (int i=0; i&lt;count; i++) {
> -    <xsp:content><part><xsp:logic>
> -   
>
_mail_process_content(request,response,document,xspParentNode,xspCurrentNode,xspNodeStack,session,(MimePart)multipart.getBodyPart(i));
> -    </xsp:logic></part></xsp:content>
> +							<xsp:content>
> +								<part>
> +									<xsp:logic>
> +										_mail_process_content(
> +											request,
> +											response,
> +											document,
> +											xspParentNode,
> +											xspCurrentNode,
> +											xspNodeStack,
> +											session,
> +											(MimePart)multipart.getBodyPart(i));
> +									</xsp:logic>
> +								</part>
> +							</xsp:content>
> +						}
> +
> +					}
> +				}
> +
> +				/**
> +				 * This method is used to recursively process a list of folders.
> +				 *
> +				 * @param prefix The string denoting the full name of the parent
> +				 *               folder.
> +				 * @param sep The folder separator.
> +				 * @param folders The list of folders to process.
> +				 * @param document The xsp document.
> +				 * @param xspParentNode The xsp parent node pass through.
> +				 * @param xspCurrentNode The xsp current node pass through.
> +				 * @param xspNodeStack The xsp node stack pass through.
> +				 */
> +				void _mail_process_folders(
> +						String prefix,
> +						String sep,
> +						Folder[] folders,
> +						Document document,
> +						Node xspParentNode,
> 
=== message truncated ===> --- sendmail.xsl	Tue Apr 17 17:25:26 2001
> +++ idanim/intranet/src/org/apache/cocoon/processor/xsp/library/mail/sendmail.xsl	Tue Apr 17
> 16:04:03 2001
> @@ -4,7 +4,7 @@
>                     The Apache Software License, Version 1.1
>   ============================================================================
>  
> - Copyright (C) 1999-2001 The Apache Software Foundation. All rights reserved.
> + Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
>  
>   Redistribution and use in source and binary forms, with or without modifica-
>   tion, are permitted provided that the following conditions are met:
> @@ -53,13 +53,114 @@
>   </description>
>  
>   <author>Donald A. Ball Jr.</author>
> + <contributor>Bala Sambandam, Idanim Solutions Inc.</contributor>
>   <version>1.0</version>
> - <release version="1.1" author="Drasko Kokic"/>
>  -->
> -<xsl:stylesheet version="1.0"
> +<xsl:stylesheet
>                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                  xmlns:xsp="http://www.apache.org/1999/XSP/Core"
> -                xmlns:sendmail="http://apache.org/cocoon/sendmail/v1">
> + xmlns:sendmail="http://apache.org/cocoon/sendmail/v1"
> + version="1.0"
> +>
> +
> +	<xsl:param name="XSP-ENVIRONMENT"/>
> +	<xsl:param name="XSP-VERSION"/>
> +	<xsl:param name="filename"/>
> +	<xsl:param name="language"/>
> +
> +	<xsl:variable name="cocoon1-environment">Cocoon 1</xsl:variable>
> +	<xsl:variable name="cocoon2-environment">Cocoon 2</xsl:variable>
> +
> +	<xsl:variable
> name="cocoon1-xsp-namespace-uri">http://www.apache.org/1999/XSP/Core</xsl:variable>
> +	<xsl:variable name="cocoon2-xsp-namespace-uri">http://apache.org/xsp</xsl:variable>
> +
> +	<xsl:variable name="environment">
> +		<xsl:choose>
> +			<xsl:when test="starts-with($XSP-ENVIRONMENT,$cocoon1-environment)">
> +				<xsl:text>cocoon1</xsl:text>
> +			</xsl:when>
> +			<xsl:when test="starts-with($XSP-ENVIRONMENT,$cocoon2-environment)">
> +				<xsl:text>cocoon2</xsl:text>
> +			</xsl:when>
> +			<xsl:otherwise>
> +				<xsl:text>cocoon2</xsl:text>
> +			</xsl:otherwise>
> +		</xsl:choose>
> +	</xsl:variable>
> +	
> +	<xsl:variable name="xsp-namespace-uri">
> +		<xsl:choose>
> +			<xsl:when test="$environment = 'cocoon1'">
> +				<xsl:value-of select="$cocoon1-xsp-namespace-uri"/>
> +			</xsl:when>
> +			<xsl:when test="$environment = 'cocoon2'">
> +				<xsl:value-of select="$cocoon2-xsp-namespace-uri"/>
> +			</xsl:when>
> +		</xsl:choose>
> +	</xsl:variable>
> +	
> +	<xsl:template name="get-nested-content">
> +		<xsl:param name="content"/>
> +		<xsl:choose>
> +			<xsl:when test="$content/*">
> +				<xsl:apply-templates select="$content/*"/>
> +			</xsl:when>
> +			<xsl:otherwise>
> +				<xsl:value-of select="$content"/>
> +			</xsl:otherwise>
> +		</xsl:choose>
> +	</xsl:template>
> +	
> +	<xsl:template name="get-nested-string">
> +		<xsl:param name="content"/>
> +		<xsl:choose>
> +			<xsl:when test="$environment = 'cocoon1'">
> +				<xsl:choose>
> +					<xsl:when test="$content/*">
> +						""
> +						<xsl:for-each select="$content/node()">
> +							<xsl:choose>
> +								<xsl:when test="name(.)">
> +									+ <xsl:apply-templates select="."/>
> +								</xsl:when>
> +								<xsl:otherwise>
> +									+ "<xsl:value-of select="translate(.,'	

','   ')"/>"
> +								</xsl:otherwise>
> +							</xsl:choose>
> +						</xsl:for-each>
> +					</xsl:when>
> +					<xsl:otherwise>
> +						"<xsl:value-of select="normalize-space($content)"/>"
> +					</xsl:otherwise>
> +				</xsl:choose>
> +			</xsl:when>
> +			<xsl:when test="$environment = 'cocoon2'">
> +				<xsl:choose>
> +					<xsl:when test="$content/*">
> +						""
> +						<xsl:for-each select="$content/node()">
> +							<xsl:choose>
> +								<xsl:when test="name(.)">
> +									<xsl:choose>
> +										<xsl:when test="namespace-uri(.)='http://apache.org/xsp' and local-name(.)='text'">
> +											+ "<xsl:value-of select="."/>"
> +										</xsl:when>
> +										<xsl:otherwise>
> +											+ <xsl:apply-templates select="."/>
> +										</xsl:otherwise>
> +									</xsl:choose>
> +								</xsl:when>
> +								<xsl:otherwise>
> +									+ "<xsl:value-of select="translate(.,'	

','   ')"/>"
> +								</xsl:otherwise>
> +							</xsl:choose>
> +						</xsl:for-each>
> +					</xsl:when>
> +					<xsl:otherwise>"<xsl:value-of select="normalize-space($content)"/>"</xsl:otherwise>
> +				</xsl:choose>
> +			</xsl:when>
> +		</xsl:choose>
> +	</xsl:template>
>  
>    <xsl:template match="xsp:page">
>      <xsl:copy>
> @@ -77,8 +178,7 @@
>        </xsp:structure>
>        <xsp:logic>
>          static Properties _sendmail_properties;
> -        static
> -         {
> +				static {
>            _sendmail_properties = new Properties();
>            _sendmail_properties.put ("mail.smtp.host", "127.0.0.1");
>           }
> @@ -87,126 +187,109 @@
>      </xsl:copy>
>    </xsl:template>
>  
> -
>    <xsl:template match="sendmail:send-mail">
> -    <xsl:variable name="subject"><xsl:call-template name="get-nested-string"><xsl:with-param
> name="content" select="sendmail:subject"/></xsl:call-template></xsl:variable>
> -    <xsl:variable name="body"><xsl:call-template name="get-nested-string"><xsl:with-param
> name="content" select="sendmail:body"/></xsl:call-template></xsl:variable>
> -    <xsl:variable name="smtphost"><xsl:call-template name="get-nested-string"><xsl:with-param
> name="content" select="sendmail:smtphost"/></xsl:call-template></xsl:variable>
> +		<xsl:variable name="from">
> +			<xsl:call-template name="get-nested-string">
> +				<xsl:with-param name="content" select="sendmail:from"/>
> +			</xsl:call-template>
> +		</xsl:variable>
> +		<xsl:variable name="from-personal">
> +			<xsl:call-template name="get-nested-string">
> +				<xsl:with-param name="content" select="sendmail:from-personal"/>
> +			</xsl:call-template>
> +		</xsl:variable>
> +		<xsl:variable name="to">
> +			<xsl:call-template name="get-nested-string">
> +				<xsl:with-param name="content" select="sendmail:to"/>
> +			</xsl:call-template>
> +		</xsl:variable>
> +		<xsl:variable name="cc">
> +			<xsl:call-template name="get-nested-string">
> +				<xsl:with-param name="content" select="sendmail:cc"/>
> +			</xsl:call-template>
> +		</xsl:variable>
> +		<xsl:variable name="subject">
> +			<xsl:call-template name="get-nested-string">
> +				<xsl:with-param name="content" select="sendmail:subject"/>
> +			</xsl:call-template>
> +		</xsl:variable>
> +		<xsl:variable name="body">
> +			<xsl:call-template name="get-nested-string">
> +				<xsl:with-param name="content" select="sendmail:body"/>
> +			</xsl:call-template>
> +		</xsl:variable>
> +		<xsl:variable name="smtphost">
> +			<xsl:call-template name="get-nested-string">
> +				<xsl:with-param name="content" select="sendmail:smtphost"/>
> +			</xsl:call-template>
> +		</xsl:variable>
>      <xsp:logic>
> -      try
> -       {
> -        Properties _sendmail_properties = new Properties (this._sendmail_properties);
> -        if (!"null".equals (String.valueOf (<xsl:copy-of select="$smtphost"/>)))
> -         {
> -          _sendmail_properties.put ("mail.smtp.host", String.valueOf (<xsl:copy-of
> select="$smtphost"/>));
> -         }
> -        Session _sendmail_session = Session.getDefaultInstance (_sendmail_properties,null);
> -        Message _sendmail_message = new MimeMessage (_sendmail_session);
> -        _sendmail_message.setFrom (new InternetAddress (String.valueOf (<xsl:call-template
> name="get-nested-string"><xsl:with-param name="content"
> select="sendmail:from"/></xsl:call-template>)));
> 
=== message truncated ===> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> For additional commands, email: cocoon-dev-help@xml.apache.org


=====
Davanum Srinivas, JNI-FAQ Manager
http://www.jGuru.com/faq/JNI

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org