Integrating Tapestry with Gravatar

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.

Tagged: , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: