View Javadoc
1   package com.foxinmy.weixin4j.util;
2   
3   import io.netty.util.AttributeKey;
4   
5   import java.lang.reflect.Constructor;
6   import java.lang.reflect.Modifier;
7   import java.nio.charset.Charset;
8   import java.security.MessageDigest;
9   import java.security.NoSuchAlgorithmException;
10  import java.util.Random;
11  
12  import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
13  
14  /**
15   * 工具包
16   * 
17   * @className ServerToolkits
18   * @author jinyu(foxinmy@gmail.com)
19   * @date 2015年12月26日
20   * @since JDK 1.7
21   * @see
22   */
23  public final class ServerToolkits {
24  	private static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
25  	public static final Charset UTF_8 = Charset.forName("UTF-8");
26  	public static final String AES = "AES";
27  	public static final String SHA1 = "SHA-1";
28  	public static final String PROTOCOL_FILE = "file";
29  	public static final String PROTOCOL_JAR = "jar";
30  	public static final String CONTENTTYPE$APPLICATION_XML = "application/xml";
31  	public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain";
32  
33  	public static final AttributeKey<WeixinMessageTransfer> MESSAGE_TRANSFER_KEY = AttributeKey
34  			.valueOf("attr:MESSAGETRANSFER");
35  
36  	/**
37  	 * 返回一个定长的随机字符串(包含数字和大小写字母)
38  	 * 
39  	 * @param length
40  	 *            随机数的长度
41  	 * @return
42  	 */
43  	public static String generateRandomString(int length) {
44  		StringBuilder sb = new StringBuilder(length);
45  		Random random = new Random();
46  		for (int i = 0; i < length; i++) {
47  			sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
48  		}
49  		return sb.toString();
50  	}
51  
52  	/**
53  	 * 构造器设置为可见
54  	 * 
55  	 * @param ctor
56  	 */
57  	public static void makeConstructorAccessible(Constructor<?> ctor) {
58  		if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor
59  				.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) {
60  			ctor.setAccessible(true);
61  		}
62  	}
63  
64  	/**
65  	 * SHA1签名
66  	 * 
67  	 * @param content
68  	 *            待签名字符串
69  	 * @return 签名后的字符串
70  	 */
71  	public static String digestSHA1(String content) {
72  		byte[] data = ServerToolkits.getBytesUtf8(content);
73  		try {
74  			return HexUtil.encodeHexString(MessageDigest.getInstance(SHA1)
75  					.digest(data));
76  		} catch (NoSuchAlgorithmException e) {
77  			return null;
78  		}
79  	}
80  
81  	private static String newString(final byte[] bytes, final Charset charset) {
82  		return bytes == null ? null : new String(bytes, charset);
83  	}
84  
85  	public static byte[] getBytesUtf8(final String content) {
86  		return content != null ? content.getBytes(UTF_8) : null;
87  	}
88  
89  	public static String newStringUtf8(final byte[] bytes) {
90  		return newString(bytes, UTF_8);
91  	}
92  
93  	/**
94  	 * 判断字符串是否为空
95  	 * 
96  	 * @param cs
97  	 * @return
98  	 */
99  	public static boolean isBlank(final CharSequence cs) {
100 		int strLen;
101 		if (cs == null || (strLen = cs.length()) == 0) {
102 			return true;
103 		}
104 		for (int i = 0; i < strLen; i++) {
105 			if (Character.isWhitespace(cs.charAt(i)) == false) {
106 				return false;
107 			}
108 		}
109 		return true;
110 	}
111 }