Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Integrating Tapestry with Gravatar

DZone's Guide to

Integrating Tapestry with Gravatar

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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/

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}