View | Details | Raw Unified | Return to bug 343602 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mylyn/commons/identity/gravatar/GravatarUtils.java (+108 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2011 GitHub Inc.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 *  Contributors:
9
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.commons.identity.gravatar;
13
14
import java.math.BigInteger;
15
import java.security.MessageDigest;
16
import java.security.NoSuchAlgorithmException;
17
import java.util.Arrays;
18
import java.util.Locale;
19
20
import org.eclipse.core.runtime.IAdaptable;
21
22
/**
23
 * Gravatar utililites.
24
 * 
25
 * @author Kevin Sawicki (kevin@github.com)
26
 */
27
public abstract class GravatarUtils {
28
29
	private static String digest(String value) {
30
		String hashed = null;
31
		try {
32
			byte[] input = value.getBytes(IGravatarConstants.CHARSET);
33
			byte[] digested = MessageDigest.getInstance(IGravatarConstants.HASH_ALGORITHM).digest(input);
34
			hashed = new BigInteger(1, digested).toString(16);
35
			int padding = IGravatarConstants.HASH_LENGTH - hashed.length();
36
			if (padding > 0) {
37
				char[] zeros = new char[padding];
38
				Arrays.fill(zeros, '0');
39
				hashed = new String(zeros) + hashed;
40
			}
41
		} catch (NoSuchAlgorithmException e) {
42
			hashed = null;
43
		}
44
		return hashed;
45
	}
46
47
	/**
48
	 * Get hash for object by attempting to adapt it to an {@link IGravatarHashProvider} and fall back on
49
	 * {@link Object#toString()} value if adaptation fails and {@link Object#toString()} is or can be transformed into a
50
	 * valid hash.
51
	 * 
52
	 * @param element
53
	 * @return hash
54
	 */
55
	public String getAdaptedHash(Object element) {
56
		if (element == null) {
57
			return null;
58
		}
59
60
		String hash = null;
61
		IGravatarHashProvider provider = null;
62
		if (element instanceof IGravatarHashProvider) {
63
			provider = (IGravatarHashProvider) element;
64
		} else if (element instanceof IAdaptable) {
65
			provider = (IGravatarHashProvider) ((IAdaptable) element).getAdapter(IGravatarHashProvider.class);
66
		}
67
		if (provider != null) {
68
			hash = provider.getGravatarHash();
69
		} else {
70
			String potentialHash = element.toString();
71
			if (isValidHash(potentialHash)) {
72
				hash = potentialHash;
73
			} else {
74
				hash = getHash(potentialHash);
75
			}
76
		}
77
		return hash;
78
	}
79
80
	/**
81
	 * Is the specified string a valid graavatar hash?
82
	 * 
83
	 * @param hash
84
	 * @return true if valid hash, false otherwise
85
	 */
86
	public static boolean isValidHash(String hash) {
87
		return hash != null && hash.length() == IGravatarConstants.HASH_LENGTH
88
				&& IGravatarConstants.HASH_PATTERN.matcher(hash).matches();
89
	}
90
91
	/**
92
	 * Get gravatar hash for specified e-mail address
93
	 * 
94
	 * @param email
95
	 * @return hash
96
	 */
97
	public static String getHash(String email) {
98
		String hash = null;
99
		if (email != null) {
100
			email = email.trim().toLowerCase(Locale.US);
101
			if (email.length() > 0) {
102
				hash = digest(email);
103
			}
104
		}
105
		return hash;
106
	}
107
108
}
(-)src/org/eclipse/mylyn/commons/identity/gravatar/IGravatarConstants.java (+54 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2011 GitHub Inc.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 *  Contributors:
9
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.commons.identity.gravatar;
13
14
import java.nio.charset.Charset;
15
import java.util.regex.Pattern;
16
17
/**
18
 * Gravatar constants.
19
 * 
20
 * @author Kevin Sawicki (kevin@github.com)
21
 */
22
public interface IGravatarConstants {
23
24
	/**
25
	 * URL
26
	 */
27
	String URL = "http://www.gravatar.com/avatar/"; //$NON-NLS-1$
28
29
	/**
30
	 * HASH_REGEX
31
	 */
32
	String HASH_REGEX = "[0-9a-f]{32}"; //$NON-NLS-1$
33
34
	/**
35
	 * HASH_PATTERN
36
	 */
37
	Pattern HASH_PATTERN = Pattern.compile(HASH_REGEX);
38
39
	/**
40
	 * HASH_LENGTH
41
	 */
42
	int HASH_LENGTH = 32;
43
44
	/**
45
	 * HASH_ALGORITHM
46
	 */
47
	String HASH_ALGORITHM = "MD5"; //$NON-NLS-1$
48
49
	/**
50
	 * Charset used for hashing
51
	 */
52
	Charset CHARSET = Charset.forName("CP1252"); //$NON-NLS-1$
53
54
}

Return to bug 343602