| | |
| | | |
| | | import androidx.annotation.NonNull; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.google.gson.JsonElement; |
| | | import com.google.gson.JsonObject; |
| | | import com.google.gson.JsonParser; |
| | |
| | | import com.hdl.hdlhttp.utils.GsonConvert; |
| | | import com.hdl.linkpm.sdk.HDLLinkPMSdk; |
| | | import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader; |
| | | import com.hdl.linkpm.sdk.template.GsonUtils; |
| | | import com.hdl.linkpm.sdk.utils.HDLMD5Utils; |
| | | import com.hdl.linkpm.sdk.utils.HDLSDKLog; |
| | | |
| | |
| | | |
| | | |
| | | private String jsonToUrlParameter(JsonObject object) { |
| | | final Map<String, String> map = GsonConvert.getGson().fromJson(object, new TypeToken<Map<String, String>>() { |
| | | }.getType()); |
| | | // final Map<String, String> map = GsonUtils.getGson().fromJson(object, new TypeToken<Map<String, String>>() { |
| | | // }.getType()); |
| | | // final Uri.Builder builder = new Uri.Builder(); |
| | | // List<String> list = new ArrayList<>(map.keySet()); |
| | | // Collections.sort(list); |
| | | // for (String key : list) { |
| | | // String value = map.get(key); |
| | | // if (valueNeedSign(value)) { |
| | | // builder.appendQueryParameter(key, value); |
| | | // } |
| | | // } |
| | | // return builder.build().getQuery(); |
| | | final Uri.Builder builder = new Uri.Builder(); |
| | | List<String> list = new ArrayList<>(map.keySet()); |
| | | Collections.sort(list); |
| | | for (String key : list) { |
| | | //判断当前值是否需要参与签名,保持跟云端一致 |
| | | if (IfValueNeedSign(map.get(key))) { |
| | | builder.appendQueryParameter(key, map.get(key)); |
| | | // HDLSDKLog.e("要签名:" + key + " :" + map.get(key)); |
| | | } else { |
| | | // HDLSDKLog.e("不需要签名:" + key + " :" + map.get(key)); |
| | | try { |
| | | JSONObject jsonobject = JSON.parseObject(object.toString()); |
| | | List<String> list = new ArrayList<>(jsonobject.keySet()); |
| | | Collections.sort(list); |
| | | for (String key : list) { |
| | | Object obj = jsonobject.get(key); |
| | | if (isSignValue(obj)) |
| | | builder.appendQueryParameter(key, obj.toString()); |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return builder.build().getQuery(); |
| | | } |
| | | |
| | | private static boolean isSignValue(Object value) { |
| | | if (value == null) return false; |
| | | if (value instanceof Number) return true; |
| | | String className = value.getClass().getSimpleName(); |
| | | switch (className) { |
| | | case "String": |
| | | case "Integer": |
| | | case "Boolean": |
| | | case "Long": |
| | | case "Short": |
| | | case "Float": |
| | | case "Double": |
| | | case "Character": |
| | | case "Byte": |
| | | if (TextUtils.isEmpty(value.toString())) return false; |
| | | return true; |
| | | default: |
| | | return false; |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 基本类型加密 |
| | | * <p> |
| | | * 判断当前值是否需要参与签名,保持跟云端一致 |
| | | * 空字符串不参与 |
| | | * 数组,集合,对象不参与 |
| | | * 数组,集合不参与 |
| | | * |
| | | * @param valueStr |
| | | * @return |
| | | * @return ture 需要加密 |
| | | */ |
| | | private static boolean IfValueNeedSign(String valueStr) { |
| | | if (TextUtils.isEmpty(valueStr)) |
| | | return false; |
| | | final char[] strChar = valueStr.substring(0, 1).toCharArray(); |
| | | final char firstChar = strChar[0]; |
| | | //System.out.println("getJSONType firstChar = "+firstChar); |
| | | if (firstChar != '{' && firstChar != '[') |
| | | return true; |
| | | |
| | | private static boolean valueNeedSign(String valueStr) { |
| | | try { |
| | | if (!TextUtils.isEmpty(valueStr)) { |
| | | JsonElement json = JsonParser.parseString(valueStr); |
| | | if (json != null && (json.isJsonPrimitive() || json.getAsJsonPrimitive().isString())) { |
| | | return true; |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | // /** |
| | | // * 判断当前值是否需要参与签名,保持跟云端一致 |
| | | // * 空字符串不参与 |
| | | // * 数组,集合,对象不参与 |
| | | // * |
| | | // * @param inputValueStr |
| | | // * @return |
| | | // */ |
| | | // private static boolean IfValueNeedSign(String inputValueStr) { |
| | | // if (TextUtils.isEmpty(inputValueStr)) |
| | | // return false; |
| | | // try { |
| | | // //先解码 |
| | | // String valueStr = URLDecoder.decode(inputValueStr, "utf-8"); |
| | | // final char[] strChar = valueStr.substring(0, 1).toCharArray(); |
| | | // final char firstChar = strChar[0]; |
| | | // //System.out.println("getJSONType firstChar = "+firstChar); |
| | | // if (firstChar != '{' && firstChar != '[') { |
| | | // return true; |
| | | // } |
| | | // return false; |
| | | // } catch (UnsupportedEncodingException e) { |
| | | // e.printStackTrace(); |
| | | // return false; |
| | | // } |
| | | // |
| | | // } |
| | | |
| | | public static boolean isPrimitive(String type) { |
| | | return (type.equals("char") |
| | | || type.equals("double") || type.equals("float") |
| | | || type.equals("integer") || type.equals("string") |
| | | || type.equals("boolean")) || (type.equals("Char") |
| | | || type.equals("Double") || type.equals("Float") |
| | | || type.equals("Integer") || type.equals("String") |
| | | || type.equals("Boolean") || type.equals("int") |
| | | || type.equals("long") || type.equals("Int") || type |
| | | .equals("Long")); |
| | | } |
| | | |
| | | } |