Commit 58685b41 authored by caotao's avatar caotao

新增锦浪云数据采集公共处理

parent 35458a49
package com.yeejoin.amos.api.householdapi.Utils;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
//锦浪云 header处理类
public class GoLangHeaderUtils {
// public static void main(String[] args) {
// try {
// String key = "";
// String keySecret = "";
// Map<String,Object> map = new HashMap();
// map.put("pageNo",1);
// map.put("pageSize",10);
// String body = JSON.toJSONString(map);
// String ContentMd5 = getDigest(body);
// String Date = getGMTTime();
// String path = "/v1/api/userStationList";
// String param = "POST" + "\n" + ContentMd5 + "\n" + "application/json" + "\n" + Date + "\n" + path;
// String sign = HmacSHA1Encrypt(param, keySecret);
// String url = "url" + path ;
// OkHttpClient client = new OkHttpClient();
// MediaType xmlType = MediaType.parse("application/json;charset=UTF-8");
// okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(xmlType,body);
// okhttp3.Request request = new okhttp3.Request.Builder()
// .url(url)
// .addHeader("Content-type", "application/json;charset=UTF-8")
// .addHeader("Authorization","API "+key+":"+sign)
// .addHeader("Content-MD5",ContentMd5)
// .addHeader("Date",Date)
// .post(requestBody)
// .build();
// Response response = client.newCall(request).execute();
// String string = response.body().string();
// System.out.println(string);
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public static String HmacSHA1Encrypt(String encryptText, String KeySecret) throws Exception
{
byte[] data=KeySecret.getBytes("UTF-8");
SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);
byte[] text = encryptText.getBytes("UTF-8");
byte[] result = mac.doFinal(text);
return Base64.encodeBase64String(result);
}
public static String getGMTTime(){
Calendar cd = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String str = sdf.format(cd.getTime());
return str;
}
//获取header中的 Content-MD5
public static String getDigest(String test) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(test.getBytes());
byte[] b = md.digest();
result = java.util.Base64.getEncoder().encodeToString(b);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
}
......@@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.HashMap;
@Component
......@@ -54,6 +55,13 @@ public class HouseholdPvUtils {
JSONObject jsonObject = JSON.parseObject(response);
hashMaphead.put("Authorization", (String) jsonObject.get("data"));
hashMap.put("header",hashMaphead);
}else if(PVProducerInfoEnum.JLY.getCode().equals(code)){
hashMaphead.put("Content-type","application/json;charset=UTF-8");
hashMaphead.put("Authorization","API "+householdPvProducerInfo.getProdcerAppid());
hashMaphead.put("Content-MD5",null);
hashMaphead.put("Date", GoLangHeaderUtils.getGMTTime());
hashMap.put("header",hashMaphead);
hashMap.put("appsecret", householdPvProducerInfo.getProdcerAppsecret());
}
return hashMap;
}
......@@ -61,9 +69,21 @@ public class HouseholdPvUtils {
HouseholdPvApiDto householdPvApiDto = this.householdPvApiServiceImpl.queryBySeq(seq);
HashMap<String,Object> producerInfo = this.getHeaderByProducerCode(householdPvApiDto.getProducerId());
HashMap<String,String> headMap = (HashMap<String, String>) producerInfo.get("header");
if(PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())){
String contentMD5 = GoLangHeaderUtils.getDigest(householdPvApiDto.getParamInfo());
String param = "POST"+"\n"+contentMD5+"\n"+"application/json"+"\n"+headMap.get("Date")+"\n"+householdPvApiDto.getApiUrl();
String sign="";
try {
sign = GoLangHeaderUtils.HmacSHA1Encrypt(param, (String) producerInfo.get("appsecret"));
} catch (Exception e) {
throw new RuntimeException(e);
}
headMap.put("Content-MD5",contentMD5);
headMap.put("Authorization",headMap.get("Authorization")+":"+sign);
}
String respone="";
if(householdPvApiDto.getRequestMethod().equals("POST")){
respone = HttpUtil.createPost(producerInfo.get("apiurl")+householdPvApiDto.getApiUrl()).headerMap(headMap,true).
respone = HttpUtil.createPost(producerInfo.get("apiurl")+householdPvApiDto.getApiUrl()).headerMap(headMap,false).
body(householdPvApiDto.getParamInfo()).execute().body();
}
if(householdPvApiDto.getRequestMethod().equals("GET")){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment