Author: mgrigorov Date: Mon Oct 3 14:39:41 2011 New Revision: 1178419 URL: http://svn.apache.org/viewvc?rev=1178419&view=rev Log: WICKET-4095 Adding wicket-example for rendering a Page in the current requestCycle
Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.html wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.java wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplateApplication.java wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.html wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.java wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/resource.txt wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/asemail/ wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/asemail/mail-template.tmpl Modified: wicket/trunk/wicket-examples/pom.xml wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml Modified: wicket/trunk/wicket-examples/pom.xml URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/pom.xml?rev=1178419&r1=1178418&r2=1178419&view=diff ============================================================================== --- wicket/trunk/wicket-examples/pom.xml (original) +++ wicket/trunk/wicket-examples/pom.xml Mon Oct 3 14:39:41 2011 @@ -141,6 +141,7 @@ <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.vm</include> + <include>**/*.tmpl</include> </includes> </resource> </resources> Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.html URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.html?rev=1178419&view=auto ============================================================================== --- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.html (added) +++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.html Mon Oct 3 14:39:41 2011 @@ -0,0 +1,27 @@ +<html xmlns:wicket="http://wicket.apache.org"> +<head> + <title>Wicket Examples - Render mail templates</title> + <link rel="stylesheet" type="text/css" href="style.css"/> +</head> +<body> + <span wicket:id="mainNavigation"/> + + <div wicket:id="feedback"></div> + + <form wicket:id="form"> + <label wicket:for="name"><wicket:label>Enter your name:</wicket:label></label><input wicket:id="name"/> + </form> + + Using Wicket page as template: <a wicket:id="pageBasedLink">generate</a>. + <br/> + Using Wicket TextTemplate as template: <a wicket:id="textTemplateBasedLink">generate</a>. + + <br/><br/> + + <h3>Here is the generated markup:</h3> + <div wicket:id="result"> + + </div> + +</body> +</html> \ No newline at end of file Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.java?rev=1178419&view=auto ============================================================================== --- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.java (added) +++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplate.java Mon Oct 3 14:39:41 2011 @@ -0,0 +1,199 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.asemail; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.Component; +import org.apache.wicket.Page; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink; +import org.apache.wicket.examples.WicketExamplePage; +import org.apache.wicket.markup.html.basic.MultiLineLabel; +import org.apache.wicket.markup.html.form.Form; +import org.apache.wicket.markup.html.form.TextField; +import org.apache.wicket.markup.html.panel.FeedbackPanel; +import org.apache.wicket.model.Model; +import org.apache.wicket.model.PropertyModel; +import org.apache.wicket.protocol.http.BufferedWebResponse; +import org.apache.wicket.request.Response; +import org.apache.wicket.request.Url; +import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.request.handler.PageProvider; +import org.apache.wicket.request.handler.RenderPageRequestHandler; +import org.apache.wicket.request.handler.RenderPageRequestHandler.RedirectPolicy; +import org.apache.wicket.request.handler.render.PageRenderer; +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.request.resource.PackageResourceReference; +import org.apache.wicket.util.template.PackageTextTemplate; +import org.apache.wicket.util.template.TextTemplate; + +/** + * An example page that shows how to generate email templates from Wicket {@link Page} or + * {@link TextTemplate} + */ +public class MailTemplate extends WicketExamplePage +{ + + /** + * The name of the customer for which a mail body will be generated. + */ + private String name = ""; + + /** + * Constructor. + * + * @param parameters + * the current page parameters + */ + public MailTemplate(final PageParameters parameters) + { + super(parameters); + + final FeedbackPanel feedback = new FeedbackPanel("feedback"); + feedback.setOutputMarkupId(true); + add(feedback); + + final Form<Void> form = new Form<Void>("form"); + add(form); + + TextField<String> nameTextField = new TextField<String>("name", new PropertyModel<String>( + MailTemplate.this, "name")); + nameTextField.setOutputMarkupId(true); + form.add(nameTextField); + + final MultiLineLabel result = new MultiLineLabel("result", new Model<String>()); + result.setOutputMarkupId(true); + add(result); + + AjaxSubmitLink basedOnPageLink = new AjaxSubmitLink("pageBasedLink", form) + { + @Override + protected void onSubmit(AjaxRequestTarget target, Form<?> form) + { + CharSequence pageHtml = renderPage(TemplateBasedOnPage.class); + + updateResult(result, pageHtml, target); + target.add(feedback); + } + + @Override + protected void onError(AjaxRequestTarget target, Form<?> form) + { + target.add(feedback); + } + }; + + AjaxSubmitLink basedOnTextTemplateLink = new AjaxSubmitLink("textTemplateBasedLink", form) + { + @Override + protected void onSubmit(AjaxRequestTarget target, Form<?> form) + { + + CharSequence templateHtml = renderTemplate(new PackageTextTemplate( + MailTemplate.class, "mail-template.tmpl")); + updateResult(result, templateHtml, target); + target.add(feedback); + } + + @Override + protected void onError(AjaxRequestTarget target, Form<?> form) + { + target.add(feedback); + } + }; + + add(basedOnPageLink, basedOnTextTemplateLink); + } + + /** + * Loads the mail template from a file with placeholders, populates them and returns the text + * that can be used as mail body. + * + * @param template + * the {@link TextTemplate} to use for the final result + * @return the fully populated template + */ + private CharSequence renderTemplate(final TextTemplate template) + { + Map<String, Object> variables = new HashMap<String, Object>(); + variables.put("name", name); + + CharSequence relativeUrl = urlFor(new PackageResourceReference(MailTemplate.class, + "resource.txt"), null); + String href = getRequestCycle().getUrlRenderer().renderFullUrl( + Url.parse(relativeUrl.toString())); + variables.put("downloadLink", href); + + String html = template.asString(variables); + return html; + } + + /** + * Collects the html generated by the rendering of a page. + * + * @param pageClass + * the class of the page which should be rendered. + * @return the html rendered by a page + */ + private CharSequence renderPage(final Class<? extends Page> pageClass) + { + PageParameters parameters = new PageParameters(); + parameters.set("name", name); + + final RenderPageRequestHandler handler = new RenderPageRequestHandler(new PageProvider( + pageClass, parameters), RedirectPolicy.NEVER_REDIRECT); + + final PageRenderer pageRenderer = getApplication().getPageRendererProvider().get(handler); + + RequestCycle requestCycle = getRequestCycle(); + + final Response oldResponse = requestCycle.getResponse(); + BufferedWebResponse tempResponse = new BufferedWebResponse(null); + + try + { + requestCycle.setResponse(tempResponse); + pageRenderer.respond(requestCycle); + } + finally + { + requestCycle.setResponse(oldResponse); + } + + return tempResponse.getText(); + } + + /** + * Updates the component that is used to show the generated mail body for this example. + * + * @param result + * the component that shows the mail body + * @param mailBody + * the text for the mail body + * @param target + * the current Ajax request handler + */ + private void updateResult(final Component result, final CharSequence mailBody, + final AjaxRequestTarget target) + { + result.setDefaultModelObject(mailBody); + target.add(result); + } + +} Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplateApplication.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplateApplication.java?rev=1178419&view=auto ============================================================================== --- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplateApplication.java (added) +++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/MailTemplateApplication.java Mon Oct 3 14:39:41 2011 @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.asemail; + +import org.apache.wicket.Page; +import org.apache.wicket.protocol.http.WebApplication; + +/** + * + */ +public class MailTemplateApplication extends WebApplication +{ + + @Override + public Class<? extends Page> getHomePage() + { + return MailTemplate.class; + } + +} Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.html URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.html?rev=1178419&view=auto ============================================================================== --- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.html (added) +++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.html Mon Oct 3 14:39:41 2011 @@ -0,0 +1,23 @@ +<!DOCTYPE html> +<html xmlns:wicket="http://wicket.apache.org"> + + <head> + <title>A template based on a page</title> + </head> + + <body> + <!-- The next is dynamically generated --> + Hello, <span wicket:id="name"></span> + + You receive this email because you are subscribed for our products. + We just released a new version of product X. + + <!-- This link is also dynamically generated --> + Please download it <a wicket:id="downloadLink">here</a>! + + <br/><br/> + Sincerely, + The Marketing team + </body> + +</html> \ No newline at end of file Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.java?rev=1178419&view=auto ============================================================================== --- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.java (added) +++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/TemplateBasedOnPage.java Mon Oct 3 14:39:41 2011 @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.asemail; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.link.ExternalLink; +import org.apache.wicket.request.Url; +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.request.resource.PackageResourceReference; + +/** + * A page which acts a template for mails which should be send to the customers. + */ +public class TemplateBasedOnPage extends WebPage +{ + /** + * Constructor. + * + * @param parameters + * the current page parameters + */ + public TemplateBasedOnPage(final PageParameters parameters) + { + super(parameters); + + add(new Label("name", parameters.get("name").toString("Unknown"))); + + CharSequence relativeUrl = urlFor(new PackageResourceReference(MailTemplate.class, + "resource.txt"), null); + String href = getRequestCycle().getUrlRenderer().renderFullUrl( + Url.parse(relativeUrl.toString())); + ExternalLink downloadLink = new ExternalLink("downloadLink", href); + add(downloadLink); + } +} Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/resource.txt URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/resource.txt?rev=1178419&view=auto ============================================================================== --- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/resource.txt (added) +++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/asemail/resource.txt Mon Oct 3 14:39:41 2011 @@ -0,0 +1,2 @@ +This is the new version of product X. +Enjoy! \ No newline at end of file Added: wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/asemail/mail-template.tmpl URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/asemail/mail-template.tmpl?rev=1178419&view=auto ============================================================================== --- wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/asemail/mail-template.tmpl (added) +++ wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/asemail/mail-template.tmpl Mon Oct 3 14:39:41 2011 @@ -0,0 +1,9 @@ +Hello, ${name} + +You receive this email because you are subscribed for our products. +We just released a new version of product X. + +Please download it <a href="${downloadLink}">here</a> + +Sincerely, +The Marketing team \ No newline at end of file Modified: wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html?rev=1178419&r1=1178418&r2=1178419&view=diff ============================================================================== --- wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html (original) +++ wicket/trunk/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html Mon Oct 3 14:39:41 2011 @@ -39,6 +39,7 @@ <tr><td align="right"><a href="upload/single">upload</a></td><td> - Single file upload.</td></tr> <tr><td align="right"><a href="upload/multi">upload</a></td><td> - Multiple file upload.</td></tr> <tr><td align="right"><a href="template">template</a></td><td> - Templating example.</td></tr> + <tr><td align="right"><a href="mailtemplate">mail template</a></td><td> - Generate mail templates out of Page or TextTemplate.</td></tr> <tr><td align="right"><a href="stateless">stateless</a></td><td> - Demonstrates stateless pages/sessions.</td></tr> <tr><td align="right"><a href="hellobrowser">hellobrowser</a></td><td> - Browser snooper.</td></tr> <tr><td align="right"><a href="frames">frames</a></td><td> - Example demonstrating HTML frames.</td></tr> @@ -68,4 +69,4 @@ </div> </body> -</html> \ No newline at end of file +</html> Modified: wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml?rev=1178419&r1=1178418&r2=1178419&view=diff ============================================================================== --- wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml (original) +++ wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml Mon Oct 3 14:39:41 2011 @@ -432,6 +432,14 @@ </init-param> </filter> + <filter> + <filter-name>wicket.mail-template</filter-name> + <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> + <init-param> + <param-name>applicationClassName</param-name> + <param-value>org.apache.wicket.examples.asemail.MailTemplateApplication</param-value> + </init-param> + </filter> <!-- couple the session filter to the helloworld servlet --> @@ -741,6 +749,12 @@ <dispatcher>REQUEST</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping> + + <filter-mapping> + <filter-name>wicket.mail-template</filter-name> + <url-pattern>/mailtemplate/*</url-pattern> + </filter-mapping> + <!-- LISTENER --> <listener> @@ -754,9 +768,9 @@ </servlet> <servlet-mapping> - <servlet-name>HelloWorldServlet</servlet-name> - <url-pattern>/helloworldservlet/*</url-pattern> - </servlet-mapping> + <servlet-name>HelloWorldServlet</servlet-name> + <url-pattern>/helloworldservlet/*</url-pattern> + </servlet-mapping> <servlet> <servlet-name>ServletTest</servlet-name>