Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.blossompone.utils;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class GravatarUtil {
- //@formatter:off
- boolean reqExtension = false;
- public GravatarUtil setReturnExtension(boolean flag) { reqExtension = flag; return this; }
- Integer size = 80;
- public GravatarUtil setImgSize(Integer x) { size = x; return this; }
- DefaultOptions defOpts = null;
- public GravatarUtil setDefaultOption(DefaultOptions opts) { defOpts = opts; return this; }
- String defImg = null;
- @SuppressWarnings("deprecation")
- public GravatarUtil setDefaultImageURL(String url) { defImg = url == null ? null : URLEncoder.encode(url); return this; }
- boolean forceDefaultImg = false;
- public GravatarUtil setForceDefault(boolean flag) { forceDefaultImg = flag; return this; }
- Rating rating = Rating.G;
- public GravatarUtil setRating(Rating r) { rating = r; return this; }
- boolean secure = false;
- public GravatarUtil setSecure(boolean flag) { secure = flag; return this; }
- public String getUrl(String email) throws Exception{
- StringBuilder sb = new StringBuilder();
- sb.append((secure ? "https://secure" : "http://www") + ".gravatar.com/avatar/");
- email = new String(new MD5Util().md5Hex(email.toLowerCase().trim()));
- sb.append(email + (reqExtension ? ".jpg" : ""));
- boolean firstParam = true;
- if(size != 80) { sb.append((firstParam ? "?" : "&") + "s=" + size); firstParam = false; }
- if(defOpts != null) { sb.append((firstParam ? "?" : "&") + "d="+defOpts); firstParam = false; }
- else if(defImg!=null && !defImg.equals("")) { sb.append((firstParam ? "?" : "&") + "d=" + defImg); firstParam = false; }
- if(forceDefaultImg) { sb.append((firstParam ? "?" : "&") + "f=y"); firstParam = false; }
- if(rating != null && rating != Rating.G) { sb.append((firstParam ? "?" : "&") + "rating=" + rating); firstParam = false; }
- return sb.toString();
- }
- //@formatter:on
- public GravatarUtil() {}
- public static enum DefaultOptions {
- _404("404"), MYSTERY_MAN("mm"), IDENTICON("identicon"), MONSTERID("monsterid"), WAVATAR("wavatar"), RETRO("retro"), BLANK("blank");
- private final String st;
- DefaultOptions(String st) {
- this.st = st;
- }
- public String getString() {
- return st;
- }
- public String toString() {
- return getString();
- }
- }
- public static enum Rating {
- G("g"), PG("pg"), R("r"), X("x");
- private final String st;
- Rating(String st) {
- this.st = st;
- }
- public String getString() {
- return st;
- }
- public String toString() {
- return getString();
- }
- }
- public class MD5Util {
- public 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();
- }
- public String md5Hex(String message) {
- try {
- MessageDigest md = MessageDigest.getInstance("MD5");
- return hex(md.digest(message.getBytes("CP1252")));
- } catch (NoSuchAlgorithmException e) {} catch (UnsupportedEncodingException e) {}
- return null;
- }
- }
- }
Advertisement