Wednesday, August 20, 2014

omg wicket finally added the best feature ever!

well it was finally added a long time ago, i just never bothered to check. but its now possible and extremely simple to use wicket for email templates. no more ant-style variables with archaic templating engines like velocity and freemarker. here's a quick example:

in some arbitrary class i have...

public class EmailSendingClass {
public void sendWelcomeEmail(User user) {
log.warn("send welcome message for user: {}", user.getId());

Component component = new WelcomeEmail("email", new UserModel(user));
CharSequence body = ComponentRenderer.renderComponent(component);
log.warn(body.toString());
}
}

then i have a WelcomeEmail class...

public class WelcomeEmail extends Panel {
private static final long serialVersionUID = 1L;

public WelcomeEmail(String id, IModel<User> user) {
super(id, new CompoundPropertyModel<User>(user));

add(new Label("email"));
}
}

and finally the corresponding markup...

<wicket:panel>
<html>
<body>

<p>You have successfully created an account.</p>
<p>Use your email address <span wicket:id="email">user@example.com</span> to login.</p>
<p>Thanks!</p>

</body>
</html>
</wicket:panel>

the magic part here is of course the org.apache.wicket.core.util.string.ComponentRenderer class that renders a wicket component as a string. you can now take that text and print it to a log file or perhaps something more interesting like sending an email. for the plain text version, don't forget to add .setRenderBodyOnly(true) to each Label, etc.

Wednesday, February 05, 2014

wicket session cookie path and nginx

i found that wicket was setting cookies something like this:

Set-Cookie:JSESSIONID=CK_iXXR-Sj11WxeJjPz2R8VI; path=

where the path wasn't set. if the page that set the cookie was in /foo/bar/MyPage then resources at /myservlet were not getting the cookie passed. this caused a problem when i deep linked users and the cookie was being set from a deeper path and not the home page. it looked like others had similar problems and couldn't find a setting in wicket so i looked to nginx to deal with it. the fix was quite simple:

 proxy_cookie_path '' /;

here's the page i used for reference: