J2EE developer with over 7 years of experience in designing and implementing enterprise j2ee solutions based on open source technologies like Tapestry, Hibernate, Spring. Current interests include Tapestry, Plastic, Spock, Scala. Taha is a DZone MVB and is not an employee of DZone and has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

Integrating Tapestry with Gravatar

05.26.2011
| 2785 views |
  • submit to reddit

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;

@Inject
private ComponentResources resources;

@Inject
private GravatarURLCreator urlCreator;

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

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);
}

if(forceDefault){
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.

 

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

//Implementation
public class GravatarURLCreatorImpl implements GravatarURLCreator{
@Symbol("gravatar-base-url")
@Inject
@Property
private String baseURL;

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

sb.append('?');
for(String key: parameters.keySet()){
sb.append('&');
sb.append(key);
sb.append('=');
sb.append(encode(parameters.get(key)));
}

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/

Published at DZone with permission of Taha Siddiqi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: