Skip to content

Instantly share code, notes, and snippets.

@dashorst
Created December 30, 2009 22:10
Show Gist options
  • Save dashorst/266444 to your computer and use it in GitHub Desktop.
Save dashorst/266444 to your computer and use it in GitHub Desktop.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wicketstuff.minis.gravatar;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
/**
* Creates an image that is rendered by the <a
* href="http://gravatar.com">Gravatar</a> online service. Gravatars are user
* icons that are registered by a central server and that can be used across the
* internet with blogs, forums and other profile based websites.
* <p>
* The <tt>Gravatar</tt> requires the email address for which the gravatar image
* needs to be rendered (each gravatar account can have multiple email addresses
* registered).
* <p>
* Available options:
* <ul>
* <li>you can set the {@linkplain #setSize(int) size of the rendered image}</li>
* <li>you can pick a {@linkplain #setRating(String) rating} for the intended
* audience</li>
* <li>you can set the {@linkplain #setDefaultImage(String) default image} that
* is rendered when no gravatar was found with the provided email address</li>
* <li>you can choose to use a {@linkplain setSecure secure https protocol or
* plain http}</li>
* <li>the gravatar supports both the {@link #setAlt(IModel) alt} and
* {@link #setTitle(IModel) title} attributes</li>
* </ul>
* <p>
* Set the static fields if you want to modify the defaults for your whole
* application.
*
* @author dashorst
*/
public class Gravatar extends WebComponent {
private static final long serialVersionUID = 1L;
/** The non-secure URL for the gravatar service. */
public static String URL_HTTP = "http://www.gravatar.com/avatar/";
/** The secure URL for the gravatar service. */
public static String URL_HTTPS = "https://secure.gravatar.com/avatar";
/** Default value for the defaultImage property. */
public static String DEFAULT_IMAGE = "wavatar";
/** Default rating. */
public static String DEFAULT_RATING = "G";
/** Default setting for picking the protocol to retrieve gravatars with. */
public static boolean DEFAULT_SECURE = false;
/** Default size of the rendered gravatar images. */
public static int DEFAULT_SIZE = 80;
private String defaultCharset = Charset.defaultCharset().name();
private String defaultImage = DEFAULT_IMAGE;
private String rating = DEFAULT_RATING;
private boolean secure = DEFAULT_SECURE;
private int size = DEFAULT_SIZE;
private IModel<String> title = new Model<String>("");
private IModel<String> alt = new Model<String>("");
/**
* Constructs the gravatar image. The <tt>email</tt> is used to construct
* the 'secret' gravatar URL.
*/
public Gravatar(String id, String email) {
this(id, new Model<String>(email));
}
/**
* Constructs the gravatar image. The <tt>email</tt> is used to construct
* the 'secret' gravatar URL.
*/
public Gravatar(String id, IModel<String> email) {
super(id, email);
}
/**
* Sets the value for the <tt>title</tt> attribute of the image. Defaults to
* empty string.
*/
public Gravatar setTitle(IModel<String> title) {
this.title = title;
return this;
}
/**
* Sets the value for the <tt>alt</tt> attribute of the image. Defaults to
* empty string.
*/
public Gravatar setAlt(IModel<String> alt) {
this.alt = alt;
return this;
}
/**
* Sets the rating for the gravatar: G, PG, R and X are supported by the
* gravatar service. Defaults to G.
*/
public Gravatar setRating(String rating) {
this.rating = rating;
return this;
}
/**
* Sets the default image that is generated when no gravatar was available
* for the account. Defaults to <tt>identicon</tt>. The gravatar service
* supports <tt>identicon</tt>, <tt>monsterid</tt> and <tt>wavatar</tt>. You
* can also link to a different image by providing the fully qualified URL,
* for example: <tt>http://example.com/images/example.jpg</tt>
*/
public Gravatar setDefaultImage(String defaultImage) {
try {
this.defaultImage = URLEncoder.encode(defaultImage, defaultCharset);
} catch (UnsupportedEncodingException e) {
}
return this;
}
/**
* Sets the charset that is used to encode the default image with, defaults
* to the platform's default charset.
*
* @throws UnsupportedEncodingException
*/
public void setDefaultCharset(String defaultCharset)
throws UnsupportedEncodingException {
URLEncoder.encode(URL_HTTP, defaultCharset);
this.defaultCharset = defaultCharset;
}
/**
* Sets the render size of the gravatar, sizes between 1 and 512 are
* supported. Defaults to 80 pixels. The size will also be set on the
* <tt>width</tt> and <tt>height</tt> attributes of this tag.
*/
public Gravatar setSize(int size) {
this.size = size;
return this;
}
/**
* Sets whether to use the secure gravatar URL links or normal http links.
*/
public Gravatar setSecure(boolean secure) {
this.secure = secure;
return this;
}
@Override
protected void onComponentTag(ComponentTag tag) {
tag.setName("img");
tag.put("src", getGravatarUrl());
if (title != null) {
tag.put("title", getDefaultModelObjectAsString(title.getObject()));
}
if (alt != null) {
tag.put("alt", getDefaultModelObjectAsString(alt.getObject()));
}
tag.put("width", size);
tag.put("height", size);
super.onComponentTag(tag);
}
private String getGravatarUrl() {
return (secure ? URL_HTTPS : URL_HTTP)
+ md5Hex(getDefaultModelObjectAsString()) + "?r=" + rating
+ "&amp;s=" + size + "&amp;d=" + defaultImage;
}
private String md5Hex(String message) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return hex(md.digest(message.getBytes("CP1252")));
} catch (Exception e) {
}
return null;
}
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment