<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>Dan Walmsley &#187; Programming</title>
	<atom:link href="http://www.danwalmsley.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danwalmsley.com</link>
	<description>Putting the nerd in comnerdedy</description>
	<lastBuildDate>Thu, 03 Jun 2010 23:38:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<!-- podcast_generator="podPress/8.8" - maintenance_release="8.8.4" -->
		<copyright>2006-2007 </copyright>
		<managingEditor>dan@danwalmsley.com (Dan Walmsley)</managingEditor>
		<webMaster>dan@danwalmsley.com (Dan Walmsley)</webMaster>
		<category>posts</category>
		<itunes:keywords></itunes:keywords>
		<itunes:subtitle></itunes:subtitle>
		<itunes:summary>Putting the nerd in comnerdedy</itunes:summary>
		<itunes:author>Dan Walmsley</itunes:author>
		<itunes:category text="Society &amp; Culture"/>
		<itunes:owner>
			<itunes:name>Dan Walmsley</itunes:name>
			<itunes:email>dan@danwalmsley.com</itunes:email>
		</itunes:owner>
		<itunes:block>No</itunes:block>
		<itunes:explicit>no</itunes:explicit>
		<itunes:image href="http://www.danwalmsley.com/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<image>
			<url>http://www.danwalmsley.com/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
			<title>Dan Walmsley</title>
			<link>http://www.danwalmsley.com</link>
			<width>144</width>
			<height>144</height>
		</image>
		<item>
		<title>Render a Wicket page to a string for HTML email</title>
		<link>http://www.danwalmsley.com/2008/10/21/render-a-wicket-page-to-a-string-for-html-email/</link>
		<comments>http://www.danwalmsley.com/2008/10/21/render-a-wicket-page-to-a-string-for-html-email/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 01:13:43 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[wicket]]></category>

		<guid isPermaLink="false">http://www.danwalmsley.com/?p=171</guid>
		<description><![CDATA[Something that&#8217;s very desirable to do in Apache Wicket is create HTML emails using Wicket&#8217;s brilliant component-oriented markup.
I&#8217;ve been working on this problem on and off for ages &#8212; it&#8217;s tricky because of teh way that markup rendering is so deeply tied to the requestcycle, which in turn is deeply dependent on the httpservletrequest &#8212; [...]]]></description>
			<content:encoded><![CDATA[<p>Something that&#8217;s very desirable to do in <a href="http://wicket.apache.org">Apache Wicket</a> is create HTML emails using Wicket&#8217;s brilliant component-oriented markup.</p>
<p>I&#8217;ve been working on this problem on and off for ages &#8212; it&#8217;s tricky because of teh way that markup rendering is so deeply tied to the requestcycle, which in turn is deeply dependent on the httpservletrequest &#8212; with good reason, too. That&#8217;s where Wicket gets its autoconfiguring magic from!</p>
<p>So in order to use Wicket to create HTML emails, we need to fake the request/response cycle. I wrote this convenient method that renders a bookmarkable page (pageclass + pageparameters) to a string:</p>
<pre>protected String renderPage(Class&lt;? extends Page&gt; pageClass, PageParameters pageParameters) {

		//get the servlet context
		WebApplication application = (WebApplication) WebApplication.get();

		ServletContext context = application.getServletContext();

		//fake a request/response cycle
		MockHttpSession servletSession = new MockHttpSession(context);
		servletSession.setTemporary(true);

		MockHttpServletRequest servletRequest = new MockHttpServletRequest(
				application, servletSession, context);
		MockHttpServletResponse servletResponse = new MockHttpServletResponse(
				servletRequest);

		//initialize request and response
		servletRequest.initialize();
		servletResponse.initialize();

		WebRequest webRequest = new WebRequest(servletRequest);

		BufferedWebResponse webResponse = new BufferedWebResponse(servletResponse);
		webResponse.setAjax(true);

		WebRequestCycle requestCycle = new WebRequestCycle(
				application, webRequest, webResponse);

		requestCycle.setRequestTarget(new BookmarkablePageRequestTarget(pageClass, pageParameters));

		try {
			requestCycle.request();

			log.warn("Response after request: "+webResponse.toString());

			if (requestCycle.wasHandled() == false) {
				requestCycle.setRequestTarget(new WebErrorCodeResponseTarget(
						HttpServletResponse.SC_NOT_FOUND));
			}
			requestCycle.detach();

		} finally {
			requestCycle.getResponse().close();
		}

		return webResponse.toString();
	}</pre>
<p>One other thing that&#8217;s desirable to do is change all relative links in the email to absolute URLs &#8212; something that Wicket makes super-easy, if you know how. That will be the subject of my next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danwalmsley.com/2008/10/21/render-a-wicket-page-to-a-string-for-html-email/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
