Over a million developers have joined DZone.

Integrating Tapestry with Gravatar

DZone's Guide to

Integrating Tapestry with Gravatar

· Java Zone ·
Free Resource

Start coding something amazing with our library of open source Cloud code patterns. Content provided by IBM.

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/

Code something amazing with the IBM library of open source blockchain patterns.  Content provided by IBM.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}