Over a million developers have joined DZone.
Silver Partner

Integrating Tapestry with Gravatar

· Java Zone

The Java Zone is brought to you by Stormpath—offering a pre-built identity API for developers. Easily build powerful user management, authentication, and authorization into your web and mobile applications. Check out this tutorial to build a simple web app with Spring Boot and Spring Security in 15 minutes.

Integration with Gravatar‘s avatar is all about embedding an img tag into your page. To create a gravatar image link, the steps involved are :

  1. Create a hash from email id
  2. Append a type to get image in a particular format. e.g .jpg, .png etc
  3. Set query parameters. The currently supported are s(size), d(default), f(force default), r(rating)

For more information about integration, read this
In Tapestry, we can create a component which will embed this link.


public class Gravatar {

@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL, allowNull = false)
private String email;

@Parameter(defaultPrefix = BindingConstants.LITERAL)
private Integer size;

@Parameter(defaultPrefix = BindingConstants.LITERAL)
private String _default;

@Parameter(defaultPrefix = BindingConstants.LITERAL)
private String rating;

@Parameter(defaultPrefix = BindingConstants.LITERAL)
private String type;

@Parameter(defaultPrefix = BindingConstants.LITERAL, value = "false")
private boolean forceDefault;

private ComponentResources resources;

private GravatarURLCreator urlCreator;

void beginRender(MarkupWriter writer){
Map<String, String> parameters = getParams();
writer.element("img", "src", urlCreator.createAvatarURL(email, type, parameters));

private Map<String, String> getParams(){
Map<String, String> parameters = new HashMap<String, String>();

if(size != null){
parameters.put("s", String.valueOf(size));

if(_default != null){
parameters.put("d", _default);

if(rating != null){
parameters.put("r", rating);

if(type != null){
parameters.put("t", type);

parameters.put("f", "y");

return parameters;


This component exposes email, type and query parameters as component parameters and in beginRender phase writes the img tag. The URL is created using GravatarURLCreator service. The service has one method, createAvatarURL() which implements the steps mentioned above.


public interface GravatarURLCreator {
String createAvatarURL(String email, String type, Map<String, String> parameters);

public class GravatarURLCreatorImpl implements GravatarURLCreator{
private String baseURL;

public String createAvatarURL(String email, String type, Map<String, String> parameters) {
StringBuilder sb = new StringBuilder();
sb.append(md5(email)); //hash
if(type != null){

for(String key: parameters.keySet()){

return sb.toString();

private String encode(String string) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Could not encode : " + string, e);

private String hex(byte[] array) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
return sb.toString();

private String md5(String message) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return hex(md.digest(message.getBytes("CP1252")));
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {
return null;


We can also access profiles from gravatar. Will blog about that next.

 From http://tawus.wordpress.com/2011/05/17/integrating-tapestry-with-gravatar/

The Java Zone is brought to you by Stormpath—offering a pre-built, streamlined user management API for building web and mobile applications. Learn how token authentication with JWTs (JSON Web Tokens) helps secure websites and avoids the pitfalls of using cookies and sessions.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}