Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import java.util.Locale;
import java.util.Map;

public class Main {
    public static HashMap getParamMap(Map map) {
        HashMap hashmap = new HashMap();
        hashmap.put(encodeStr("appid"), encodeStr("1uMqYWpHo3MoLH"));
        hashmap.put(encodeStr("callid"),
                encodeStr((new StringBuilder()).append("").append(generateCallId()).toString()));
        hashmap.put(encodeStr("v"), encodeStr("1.0"));
        hashmap.put(encodeStr("lang"), encodeStr(Locale.getDefault().getLanguage()));
        if (map != null) {
            hashmap.putAll(map);
        }
        hashmap.put("bd_sig", generateBgsid(hashmap));
        return hashmap;
    }

    public static String encodeStr(String s) {
        String s1;
        try {
            s1 = URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException unsupportedencodingexception) {
            unsupportedencodingexception.printStackTrace();
            return null;
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
        return s1;
    }

    public static long generateCallId() {
        return (new Date()).getTime();
    }

    public static String generateBgsid(HashMap hashmap) {
        StringBuffer stringbuffer = new StringBuffer();
        ArrayList arraylist = new ArrayList();
        for (Iterator iterator = hashmap.entrySet().iterator(); iterator.hasNext(); arraylist
                .add(((java.util.Map.Entry) iterator.next()).getKey())) {
        }
        Collections.sort(arraylist, String.CASE_INSENSITIVE_ORDER);
        for (Iterator iterator1 = arraylist.iterator(); iterator1.hasNext(); stringbuffer
                .append((String) hashmap.get((String) iterator1.next()))) {
        }
        stringbuffer.append("c18c24046606b2e084edd37f9fe9f94d");
        return md5(stringbuffer.toString());
    }

    public static String md5(String s) {
        byte abyte0[];
        StringBuilder stringbuilder;
        int i;
        try {
            abyte0 = MessageDigest.getInstance("MD5").digest(s.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException nosuchalgorithmexception) {
            throw new RuntimeException("Huh, MD5 should be supported?", nosuchalgorithmexception);
        } catch (UnsupportedEncodingException unsupportedencodingexception) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", unsupportedencodingexception);
        }
        stringbuilder = new StringBuilder(2 * abyte0.length);
        i = abyte0.length;
        for (int j = 0; j < i; j++) {
            byte byte0 = abyte0[j];
            if ((byte0 & 0xff) < 16) {
                stringbuilder.append("0");
            }
            stringbuilder.append(Integer.toHexString(byte0 & 0xff));
        }

        return stringbuilder.toString();
    }
}