package org.flume.alarm.restful; import com.google.gson.GsonBuilder; import net.sf.json.JSONObject; import org.flume.alarm.core.util.MD5Utils; import org.flume.alarm.core.util.StringUtils; import org.flume.alarm.util.Configure; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class AlarmUtilDTO { public static Map<String, String> devTypeCodeMap = new HashMap<String, String>(); private String otherSystem; private String id; private String externalEventId; private String reportName; private String reportPhone; private String eventLv; private String reportSex; private String isSecret; private String isOpen; private String eventClassify; private int eventSource; private String keyword; private String remark; private String longitude; private String latitude; private String eventContent; private String processInstName; private String eventCode; private String eventTypeTreeName; private String accessToken; public static Map<String, String> getDevTypeCodeMap() { return devTypeCodeMap; } public static void setDevTypeCodeMap(Map<String, String> devTypeCodeMap) { AlarmUtilDTO.devTypeCodeMap = devTypeCodeMap; } public String getOtherSystem() { return otherSystem; } public void setOtherSystem(String otherSystem) { this.otherSystem = otherSystem; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getExternalEventId() { return externalEventId; } public void setExternalEventId(String externalEventId) { this.externalEventId = externalEventId; } public String getReportName() { return reportName; } public void setReportName(String reportName) { this.reportName = reportName; } public String getReportPhone() { return reportPhone; } public void setReportPhone(String reportPhone) { this.reportPhone = reportPhone; } public String getEventLv() { return eventLv; } public void setEventLv(String eventLv) { this.eventLv = eventLv; } public String getReportSex() { return reportSex; } public void setReportSex(String reportSex) { this.reportSex = reportSex; } public String getIsSecret() { return isSecret; } public void setIsSecret(String isSecret) { this.isSecret = isSecret; } public String getIsOpen() { return isOpen; } public void setIsOpen(String isOpen) { this.isOpen = isOpen; } public String getEventClassify() { return eventClassify; } public void setEventClassify(String eventClassify) { this.eventClassify = eventClassify; } public int getEventSource() { return eventSource; } public void setEventSource(int eventSource) { this.eventSource = eventSource; } public String getKeyword() { return keyword; } public void setKeyword(String keyword) { this.keyword = keyword; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getLongitude() { return longitude; } public void setLongitude(String longitude) { this.longitude = longitude; } public String getLatitude() { return latitude; } public void setLatitude(String latitude) { this.latitude = latitude; } public String getEventContent() { return eventContent; } public void setEventContent(String eventContent) { this.eventContent = eventContent; } public String getProcessInstName() { return processInstName; } public void setProcessInstName(String processInstName) { this.processInstName = processInstName; } public String getEventCode() { return eventCode; } public void setEventCode(String eventCode) { this.eventCode = eventCode; } public String getEventTypeTreeName() { return eventTypeTreeName; } public void setEventTypeTreeName(String eventTypeTreeName) { this.eventTypeTreeName = eventTypeTreeName; } public String getAccessToken() { return accessToken; } public void setAccessToken(String accessToken) { this.accessToken = accessToken; } public static String getUUID() { UUID uuid = UUID.randomUUID(); String randomString = uuid.toString().replace("-", "").toUpperCase(); return randomString; } public AlarmUtilDTO() { } public AlarmUtilDTO(String accessToken) { this.accessToken = accessToken; } public AlarmUtilDTO(String otherSystem, String id, String externalEventId, String reportName, String reportPhone, String eventLv, String reportSex, String isSecret, String isOpen, String eventClassify, String eventSource, String keyword, String remark, String longitude, String latitude, String eventContent, String processInstName, String eventCode, String eventTypeTreeName) { this.otherSystem = "RongJiang"; this.id = id; this.externalEventId = externalEventId; this.reportName = reportName; this.reportPhone = reportPhone; this.eventLv = eventLv; this.reportSex = reportSex; this.isSecret = isSecret; this.isOpen = isOpen; this.eventClassify = "0"; this.eventSource = 10; this.keyword = keyword; this.remark = remark; this.longitude = longitude; this.latitude = latitude; this.eventContent = eventContent; this.processInstName = processInstName; this.eventCode = eventCode; this.eventTypeTreeName = eventTypeTreeName; } public static void sendMsg(String url, AlarmUtilDTO msg) { String ss= new GsonBuilder().create().toJson(msg); try { //获取token getToken(); if (StringUtils.isBlank(Configure.getProperty("ZZToken"))) return; // String body = new GsonBuilder().create().toJson(msg); // String str = url + body.substring(1, body.length() - 1) + Configure.getProperty("Secret"); // str = MD5Utils.MD5Encode(str, "utf8"); Map<String, Object> jsonMap = new HashMap<String, Object>(); jsonMap.put("data", msg); // jsonMap.put("header", new AlarmUtilDTO(Configure.getProperty("ZZToken"))); String responseMsg = HttpClientUtils.post(url + "?accessToken=" + Configure.getProperty("ZZToken"), new GsonBuilder().create().toJson(msg)); System.out.println("****************************" + responseMsg); } catch (Exception e) { e.printStackTrace(); } } public static boolean validateSession() { try { String token = Configure.getProperty("ZZToken"); if (StringUtils.isBlank(token)) { getToken(); } if (StringUtils.isNotBlank(Configure.getProperty("ZZToken"))) return true; } catch (Exception e) { e.printStackTrace(); System.out.println("********获取token失败"); } return false; } public static void getToken() { Configure.setProperty("ZZToken", ""); try { Map<String, Object> paraMap = new HashMap<String, Object>(); paraMap.put("clientId", Configure.getProperty("clientId")); String salt = (Math.random() * 9 + 1) * 1000 + ""; String clientSecret = Configure.getProperty("clientSecret"); String responseMsg = HttpClientUtils.post(Configure.getProperty("ZZTokenURL") + "?clientId=" + Configure.getProperty("clientId") + "&clientSecret=" + MD5Utils.MD5Encode(clientSecret + salt, "utf8") + "&salt=" + salt, ""); if (StringUtils.isNotBlank(responseMsg)) { JSONObject json = JSONObject.fromObject(responseMsg); if ("200".equals(json.get("code").toString())) { JSONObject jsonObject = (JSONObject) json.get("data"); Configure.setProperty("ZZToken", jsonObject.get("appToken").toString()); } } } catch (Exception e) { e.printStackTrace(); System.out.println("获取token失败-----------"); } } }