0
点赞
收藏
分享

微信扫一扫

海带工具类

罗子僧 02-16 15:00 阅读 4

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * <p></p>
 *
 * 海带工具类
 * 
 */
public class HaidaiApiUtils {

    public static String getApiUrl(String haidaiApiUrl, Map<String, String> params, String haidaiAppKey, String haidaiAppSecret) throws Exception {
        long timestamp = System.currentTimeMillis();

        if (params == null) {
            params = Maps.newHashMap();
        }

        params.put("appkey", haidaiAppKey);
        params.put("timestamp", String.valueOf(timestamp));

        Map<String, String> sortedMap = sortMap(params);
        StringBuilder sb = new StringBuilder();
        for (Entry<String, String> entry : sortedMap.entrySet()) {
            if (StringUtils.isBlank(entry.getValue())) {
                continue;
            }
            sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8")).append("&");
        }
        String resultStr = haidaiAppSecret + StringUtils.removeEnd(sb.toString(), "&") + haidaiAppSecret;
        String topSign = HaidaiApiUtils.SHA1(resultStr).toUpperCase();
        sortedMap.put("topSign", topSign);

        List<NameValuePair> pairs = Lists.newArrayList();
        for (Entry<String, String> entry : sortedMap.entrySet()) {
            if(StringUtils.isNotBlank(entry.getValue())){
                pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }

        return String.format("%s?%s", haidaiApiUrl,
                URLEncodedUtils.format(pairs, "UTF-8"));
    }


    private static Map<String, String> sortMap(Map<String, String> paramsMap) {
        Map<String, String> treeMap = new TreeMap<String, String>(new Comparator<String>() {
            @Override
            public int compare(String obj1, String obj2) { //升序排序
                return obj1.compareTo(obj2);
            }
        });
        treeMap.putAll(paramsMap);
        return treeMap;
    }

    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String removeEnd(final String str, final String remove) {
        if (isEmpty(str) || isEmpty(remove)) {
            return str;
        }
        if (str.endsWith(remove)) {
            return str.substring(0, str.length() - remove.length());
        }
        return str;
    }
}

举报

相关推荐

0 条评论