commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gabriel Belingueres <belingue...@gmail.com>
Subject Re: [email] embed/include parsed JSP page file in email HTML body
Date Tue, 10 Aug 2010 12:06:37 GMT
Hi,

It is indeed possible to generate HTML code from a JSP and send it as
an email. I've been doing it for a long time, however I tested the
code only on Tomcat 4.x and 5.x, beware that I don't know if it is
portable across other app servers. The basic idea is to wrap the
Response object to a new class, and get the email HTML code after
forward to the JSP page that generate it:

public class CapturedServletOutputStream extends ServletOutputStream {
  /** The response buffer */
  java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();

  /**
   * Implement the only requried method.
   * @param b An integer to write to the stream.
   * @throws java.io.IOException Thrown if there is an exception
writing the message.
   */
  public void write(int b) throws java.io.IOException {
    buffer.write(b);
  }

  /**
   * Gets the bytes from the contained buffer.
   * @return The byte array captured.
   */
  public byte[] getBytes() {
    return buffer.toByteArray();
  }
}

public class EmailResponseWrapper extends HttpServletResponseWrapper {

  String contentType = null;

  String characterEncoding = null;

  CapturedServletOutputStream outputStream = null;

  java.io.PrintWriter outputWriter = null;

  /**
   * Creates a new instance of EmailResponseWrapper
   * @param response The response object to contain.
   */
  public EmailResponseWrapper(javax.servlet.http.HttpServletResponse response) {
    super(response);
    outputStream = new CapturedServletOutputStream();
    outputWriter = new java.io.PrintWriter(outputStream);
  }

  /**
   * Gets a java.io.PrintWriter object for output.
   * @return An implementation java.io.PrintWriter.
   */
  public java.io.PrintWriter getWriter() {
    return outputWriter;
  }

  /**
   * Sets the content length of the response. This value is prevented
from being passed
   * to the contained wrapper. It is discarded.
   * @param param The content length.
   */
  public void setContentLength(int param) {
  }

  /**
   * Sets the content type of the response. This value is prevented
from being passed
   * to the contained wrapper.  It is recorded within this instance.
   * @param str The content type.
   */
  public void setContentType(String str) {
    contentType = str;
  }

  /**
   * Gets the content type from the member attribute.
   * @return The content type of the response.
   */
  public String getContentType() {
    return contentType;
  }

  /**
   * Gets the character encoding of the response from the internal
member attribute.
   * @return The character encoding of the response.
   */
  public String getCharacterEncoding() {
    return characterEncoding;
  }

  /**
   * Sets the character encoding of this response. This value is
prevented from being passed
   * to the contained wrapper.  It is recorded in a member attribute.
   * @param str The content encoding.
   */
  public void setCharacterEncoding(String str) {
    characterEncoding = str;
  }

  /**
   * Gets a servlet output stream.
   * @throws IOException Thrown if there is an exception creating the stream.
   * @return The output stream.
   */
  public javax.servlet.ServletOutputStream getOutputStream() throws java.io.
      IOException {
    return outputStream;
  }

  /**
   * The internally captured buffer.
   * @return The output of the JSP operation.
   */
  public java.lang.String getOutput() {
    String output = new String(outputStream.getBytes());
    return output;
  }

  /**
   * Sets the status of the response.  This value is discarded.
   * @param param The status code.
   * @param str The status message.
   */
  public void setStatus(int param, String str) {
  }

  /**
   * Sets a date header.  This value is discarded.
   * @param str The name of the header.
   * @param param The value of the header.
   */
  public void setDateHeader(String str, long param) {
  }

  /**
   * Sets a string header. This value is discarded.
   * @param str The name of the header.
   * @param str1 The value of the header.
   */
  public void setHeader(String str, String str1) {
  }

  /**
   * Sets the buffer size. This parameter is discarded.
   * @param param the buffer size.
   */
  public void setBufferSize(int param) {
  }

  /**
   * Sets an int header.  This value is discarded.
   * @param str The name of the header.
   * @param param The value of the header.
   */
  public void setIntHeader(String str, int param) {
  }

  /**
   * Sets the locale of the response.  this value is discarded.
   * @param locale The locale of the response.
   */
  public void setLocale(java.util.Locale locale) {
  }

  /**
   * Sets the status of the response. This value is discarded.
   * @param param The status code.
   */
  public void setStatus(int param) {
  }

  /**
   * Adds a date header. This value is discarded.
   * @param str The name of the header.
   * @param param The value of the header.
   */
  public void addDateHeader(String str, long param) {
  }

  /**
   * Adds an arbitrary string header. This value is discarded.
   * @param str The name of the header.
   * @param str1 The value of the header.
   */
  public void addHeader(String str, String str1) {
  }

  /**
       * Adds a cookie to the response. This implementation will
discard this cookie.
   * @param cookie The cookie to add.
   */
  public void addCookie(javax.servlet.http.Cookie cookie) {
  }

  /**
   * Adds an integer header. This value is discarded.
   * @param str The header name.
   * @param param The header value.
   */
  public void addIntHeader(String str, int param) {
  }

  /**
   * Resets the response. This implementation does nothing.
   */
  public void reset() {
  }

  /**
   * Resets the response buffer. This implementation does nothing.
   */
  public void resetBuffer() {
  }

  /**
   * override this method to never commit the answer
   * @return allways false
   */
  public boolean isCommitted() {
    return false;
  }

  public void flushBuffer() throws java.io.IOException {
    //outputWriter.flush();
  }
}


Use it this way:

    final RequestDispatcher rd =
        request.getSession().getServletContext().getRequestDispatcher(template);
    final EmailResponseWrapper responseWrapper = new
EmailResponseWrapper(response);
    rd.forward(request, responseWrapper);

    // Generate Message
    final Message message = new MimeMessage(session);

    final String messageBody = responseWrapper.getOutput().trim();
    message.setContent(messageBody, "text/html");


2010/8/9 Siegfried Goeschl <siegfried.goeschl@it20one.at>:
> Hi Ricardo,
>
> that was pretty much my understanding ... :-) ... as I mentioned before in
> the commons-email trunk there is some new code which allows to pass a HTML
> string and a base url to resolve the referenced images within the HTML
> document. The images are than fetched from the server/filesystem and added
> as embedded images (ImageHmtlEmail).
>
> Cheers,
>
> Siegfried Goeschl
>
> On 09.08.10 19:18, Ricardo Duval wrote:
>>
>> As far as I understand he wants the HTML content generated by JSP parsing.
>>
>> Sorry, but I don't think that'll work because you don't have a servlet
>> engine running when you try to attach the JSP, so obviously the servlet
>> won't be generate and all you gonna have is the original JSP script. I
>> recommend using a template engine, like velocity or freemarker to generate
>> an HTML content as a String.
>>
>> 2010/8/8 Siegfried Goeschl<siegfried.goeschl@it20one.at>
>>
>>> Hi Ferindo,
>>>
>>> I'm not sure if I understand what you are doing ... :-)
>>>
>>> What is a "parsed JSP page"? Recently there was some work on adding HTML
>>> content and automatically adding embedding images.
>>>
>>> Cheers,
>>>
>>> Siegfried Goeschl
>>>
>>>
>>> On 08.08.10 23:28, Ferindo Middleton wrote:
>>>
>>>> I'm using Class HtmlEmail.  Is there a way to embed a parsed JSP page in
>>>> the
>>>> email message body. I use Tomcat and Java Server Pages. In my JSP pages
>>>> I
>>>> can do something like:<%@ include file="ticket_email_response_body.jsp"
>>>> %>
>>>> and want to be able to included this processed .jsp file in the body of
>>>> the
>>>> text message.
>>>>
>>>> I've looked at the examples in the documentation for embedding Files and
>>>> images and I've tried the following example code but it doesn't embedded
>>>> the
>>>> actual parsed .jsp into the image. It includes the file as an attachment
>>>> and
>>>> puts the text string of the quoted text in the body of the message:
>>>>
>>>>  File ticketEmailResponseBody = new
>>>>
>>>>
>>>> File("webapps/utrad/web/radtickets/ticket_email_response/ticket_email_response_dispatcher_page.jsp");
>>>>  StringBuffer msg = new StringBuffer();
>>>>  msg.append("<%@ include file=\"webapps/utrad/web/header.jsp\" %>");
>>>>  msg.append("<%@ include
>>>> file=").append(email.embed(ticketEmailResponseBody)).append("
>>>> %><BR><BR>");
>>>>
>>>>
>>>>  msg.append("+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+");
>>>>
>>>>  URL emailBodyURL = new URL("
>>>>
>>>>
>>>> http://192.168.1.2:8080/utrad/web/radtickets/ticket_email_response/ticket_email_response_dispatcher_page.jsp
>>>> ");
>>>>  String pid = email.embed(emailBodyURL,
>>>> "2nd_ticket_email_response_dispatcher_EMBEDDED_page.jsp");
>>>>  msg.append("<BR><BR>try the above code again a different way:<BR>
  <%@
>>>> include file=\"" + pid + "\" %><BR>- Ferindo<BR><BR>");
>>>>
>>>> email.setHtmlMsg(msg.toString());
>>>> email.send();
>>>>
>>>> none of the "appends" I use above end up including the .jsp page as
>>>> parsed
>>>> data in the body of the email/not even the raw unparsed text is included
>>>> in
>>>> the message body. Looking at the logs, it looks like it's included the
>>>> raw
>>>> text content of the JSP file (unparsed) but it doesn't include the text
>>>> in
>>>> the body of the email, just the following:
>>>> <%@ include file="webapps/utrad/web/header.jsp" %><%@ include
>>>> file=ekzvyazwil %>
>>>>
>>>> ...and it included two of the .jsps in the message above as attachments
>>>> instead, not exactly what I was trying to do.
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>>> For additional commands, e-mail: user-help@commons.apache.org
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message