| @@ -5,6 +5,7 @@ import org.apache.commons.fileupload.FileItem; | |||
| import org.apache.commons.fileupload.FileItemFactory; | |||
| import org.apache.commons.fileupload.disk.DiskFileItemFactory; | |||
| import org.apache.commons.fileupload.servlet.ServletFileUpload; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.http.Header; | |||
| import org.apache.http.HttpEntity; | |||
| import org.apache.http.auth.AuthScope; | |||
| @@ -29,8 +30,11 @@ import org.springframework.web.bind.annotation.RequestMapping; | |||
| import org.springframework.web.bind.annotation.RequestMethod; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject; | |||
| import com.iformall.annotation.AuthIgnore; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.haikang.HKHttpClientInstanceFactory; | |||
| import com.iformall.haikang.HKPeopleNumberFactory; | |||
| import javax.servlet.ServletException; | |||
| import javax.servlet.ServletOutputStream; | |||
| @@ -59,6 +63,16 @@ public class HaiKangController { | |||
| @Autowired | |||
| HKHttpClientInstanceFactory httpFactory; | |||
| @Autowired | |||
| HKPeopleNumberFactory peopleNumberFactory; | |||
| @AuthIgnore | |||
| @GetMapping("/getPeopleNumber") | |||
| public ResultData getPeopleNumber() { | |||
| return new ResultData(peopleNumberFactory.getPeopleNumber()); | |||
| } | |||
| /** | |||
| * 监听接口事件解析,设备监听页面设置URL为“/notify” | |||
| * @param request | |||
| @@ -80,38 +94,60 @@ public class HaiKangController { | |||
| for (FileItem fi : formData) { | |||
| if (fi.isFormField()) { | |||
| //解析报文中json | |||
| System.out.println("field_name:" + fi.getFieldName()); | |||
| logger.info("field_name:" + fi.getFieldName()); | |||
| //打印json格式上传的事件报文 | |||
| System.out.println(fi.getString("UTF-8")); | |||
| String baowen = fi.getString("UTF-8"); | |||
| //通过表单name属性进行解析报文 | |||
| switch (fi.getFieldName()) { | |||
| case "event_log"://门禁、身份证事件解析 | |||
| System.out.println("receive Acs event"); | |||
| logger.info("receive Acs event"); | |||
| break; | |||
| case "personQueueRealTime": | |||
| if (StringUtils.isNotBlank(baowen)) { | |||
| JSONObject bobj = JSONObject.parseObject(baowen); | |||
| if (null != bobj) { | |||
| JSONObject rtd = bobj.getJSONObject("PersonQueueRealTimeData"); | |||
| String ip = bobj.getString("ipAddress"); | |||
| if (null != rtd) { | |||
| Integer peopleNum = rtd.getInteger("peopleNum"); | |||
| if (null != peopleNum) { | |||
| peopleNumberFactory.setPeopleNumber(ip, peopleNum); | |||
| }else { | |||
| logger.info("unValid response:"+baowen); | |||
| } | |||
| }else { | |||
| logger.info("unValid response:"+baowen); | |||
| } | |||
| }else { | |||
| logger.info("unValid response:"+baowen); | |||
| } | |||
| }else { | |||
| logger.info("unValid response:"+baowen); | |||
| } | |||
| default: | |||
| System.out.println("unknow data"); | |||
| logger.info("unknow data"); | |||
| } | |||
| } else { | |||
| //保存报文中的图片数据 | |||
| String image_name = fi.getName(); | |||
| logger.info("image_name:" + image_name); | |||
| if (image_name != "") { | |||
| //图片保存路径 | |||
| String image_dir_path = "C:\\image\\"; | |||
| File image_dir = new File(image_dir_path); | |||
| if (!image_dir.exists()) { | |||
| image_dir.mkdir(); | |||
| } | |||
| String file_name = UUID.randomUUID().toString(); | |||
| String suffix = image_name.substring(fi.getName().lastIndexOf(".")); | |||
| System.out.println("图片报文路径:" + image_dir_path); | |||
| System.out.println("图片名称:" + file_name); | |||
| System.out.println("图片格式:" + suffix); | |||
| fi.write(new File(image_dir_path, file_name + suffix)); | |||
| } else { | |||
| throw new Exception("no file receive"); | |||
| } | |||
| // String image_name = fi.getName(); | |||
| // logger.info("image_name:" + image_name); | |||
| // if (image_name != "") { | |||
| // //图片保存路径 | |||
| // String image_dir_path = "C:\\image\\"; | |||
| // | |||
| // File image_dir = new File(image_dir_path); | |||
| // if (!image_dir.exists()) { | |||
| // image_dir.mkdir(); | |||
| // } | |||
| // String file_name = UUID.randomUUID().toString(); | |||
| // String suffix = image_name.substring(fi.getName().lastIndexOf(".")); | |||
| // System.out.println("图片报文路径:" + image_dir_path); | |||
| // System.out.println("图片名称:" + file_name); | |||
| // System.out.println("图片格式:" + suffix); | |||
| // fi.write(new File(image_dir_path, file_name + suffix)); | |||
| // } else { | |||
| // throw new Exception("no file receive"); | |||
| // } | |||
| } | |||
| } | |||
| //正常接收平台响应回复 | |||
| @@ -43,72 +43,73 @@ public class RequestInterceptor extends HandlerInterceptorAdapter { | |||
| @Override | |||
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | |||
| logger.debug("preHandle start"); | |||
| if ("GET".equalsIgnoreCase(request.getMethod())) { | |||
| // 获取不检查幂等 | |||
| logger.debug("preHandle start 1"); | |||
| return true; | |||
| } | |||
| String ipaddress = IPUtil.getIpAddr(request); | |||
| String url = request.getRequestURL().toString(); | |||
| if (UrlCheck.checkUrl(url)) { | |||
| // pvlog不检查幂等 | |||
| // awsFileUpload不检查幂等 | |||
| // stopFee | |||
| return true; | |||
| } | |||
| StringBuilder sb = new StringBuilder(); | |||
| sb.append(url); | |||
| sb.append("method=").append(request.getMethod()).append("&"); | |||
| sb.append("ip=").append(ipaddress).append("&"); | |||
| final Enumeration parameterNames = request.getParameterNames(); | |||
| while (parameterNames.hasMoreElements()) { | |||
| String key = (String) parameterNames.nextElement(); | |||
| if(key.equalsIgnoreCase("ran")) // 跳过ran | |||
| continue; | |||
| String parameter = request.getParameter(key); | |||
| sb.append(key).append("=").append(parameter).append("&"); | |||
| } | |||
| InputStream inStream = request.getInputStream(); | |||
| ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); | |||
| byte[] buffer = new byte[1024]; | |||
| int len = 0; | |||
| while ((len = inStream.read(buffer)) != -1) { | |||
| outSteam.write(buffer, 0, len); | |||
| } | |||
| String resultBody = new String(outSteam.toByteArray(), Charset.forName("UTF-8")); | |||
| inStream.close(); | |||
| outSteam.close(); | |||
| sb.append(resultBody); | |||
| String key = "request:C:" + HashUtil.md5(sb.toString()); | |||
| Boolean isAbsent = redisTemplate.<Boolean>execute(new RedisCallback<Boolean>() { | |||
| @Override | |||
| public Boolean doInRedis(RedisConnection connection) throws DataAccessException { | |||
| RedisSerializer valueSerializer = redisTemplate.getValueSerializer(); | |||
| RedisSerializer keySerializer = redisTemplate.getKeySerializer(); | |||
| Object obj = connection.execute("set", keySerializer.serialize(key), | |||
| valueSerializer.serialize(key), | |||
| SafeEncoder.encode("NX"), | |||
| SafeEncoder.encode("EX"), | |||
| Protocol.toByteArray(3)); // 3s | |||
| return obj != null; | |||
| } | |||
| }); | |||
| if (isAbsent) { | |||
| logger.info(key + ": 第一次提交"); | |||
| logger.debug("preHandle start 2"); | |||
| return true; | |||
| } | |||
| logger.info(key + ": 第二次提交"); | |||
| logger.debug("preHandle start 3"); | |||
| throw new MallinkException(ErrorCode.SYS_REPEAT_SUBMIT_EXCEPTION); | |||
| // logger.debug("preHandle start"); | |||
| // if ("GET".equalsIgnoreCase(request.getMethod())) { | |||
| // // 获取不检查幂等 | |||
| // logger.debug("preHandle start 1"); | |||
| // return true; | |||
| // } | |||
| // String ipaddress = IPUtil.getIpAddr(request); | |||
| // String url = request.getRequestURL().toString(); | |||
| // if (UrlCheck.checkUrl(url)) { | |||
| // // pvlog不检查幂等 | |||
| // // awsFileUpload不检查幂等 | |||
| // // stopFee | |||
| // return true; | |||
| // } | |||
| // StringBuilder sb = new StringBuilder(); | |||
| // | |||
| // sb.append(url); | |||
| // | |||
| // sb.append("method=").append(request.getMethod()).append("&"); | |||
| // | |||
| // sb.append("ip=").append(ipaddress).append("&"); | |||
| // | |||
| // final Enumeration parameterNames = request.getParameterNames(); | |||
| // while (parameterNames.hasMoreElements()) { | |||
| // String key = (String) parameterNames.nextElement(); | |||
| // if(key.equalsIgnoreCase("ran")) // 跳过ran | |||
| // continue; | |||
| // String parameter = request.getParameter(key); | |||
| // sb.append(key).append("=").append(parameter).append("&"); | |||
| // } | |||
| // | |||
| // InputStream inStream = request.getInputStream(); | |||
| // ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); | |||
| // byte[] buffer = new byte[1024]; | |||
| // int len = 0; | |||
| // while ((len = inStream.read(buffer)) != -1) { | |||
| // outSteam.write(buffer, 0, len); | |||
| // } | |||
| // String resultBody = new String(outSteam.toByteArray(), Charset.forName("UTF-8")); | |||
| // inStream.close(); | |||
| // outSteam.close(); | |||
| // | |||
| // sb.append(resultBody); | |||
| // | |||
| // String key = "request:C:" + HashUtil.md5(sb.toString()); | |||
| // Boolean isAbsent = redisTemplate.<Boolean>execute(new RedisCallback<Boolean>() { | |||
| // @Override | |||
| // public Boolean doInRedis(RedisConnection connection) throws DataAccessException { | |||
| // RedisSerializer valueSerializer = redisTemplate.getValueSerializer(); | |||
| // RedisSerializer keySerializer = redisTemplate.getKeySerializer(); | |||
| // Object obj = connection.execute("set", keySerializer.serialize(key), | |||
| // valueSerializer.serialize(key), | |||
| // SafeEncoder.encode("NX"), | |||
| // SafeEncoder.encode("EX"), | |||
| // Protocol.toByteArray(3)); // 3s | |||
| // return obj != null; | |||
| // } | |||
| // }); | |||
| // if (isAbsent) { | |||
| // logger.info(key + ": 第一次提交"); | |||
| // logger.debug("preHandle start 2"); | |||
| // return true; | |||
| // } | |||
| // logger.info(key + ": 第二次提交"); | |||
| // logger.debug("preHandle start 3"); | |||
| // throw new MallinkException(ErrorCode.SYS_REPEAT_SUBMIT_EXCEPTION); | |||
| return true; | |||
| } | |||
| @Override | |||
| @@ -27,7 +27,7 @@ spring: | |||
| # REDIS | |||
| redis: | |||
| host: 101.200.130.134 | |||
| host: 101.200.130.135 | |||
| port: 6379 | |||
| password: iF0rm@2l2ol9 | |||
| timeout: 3600 | |||
| @@ -124,7 +124,7 @@ wechat: | |||
| componentToken: ss | |||
| componentAesKey: ss | |||
| redis: | |||
| host: 101.200.130.134 | |||
| host: 101.200.130.135 | |||
| port: 6379 | |||
| password: iF0rm@2l2ol9 | |||
| timeout: 3600 | |||
| @@ -0,0 +1,44 @@ | |||
| package com.iformall.haikang; | |||
| import java.util.Iterator; | |||
| import java.util.Map; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import org.apache.http.auth.AuthScope; | |||
| import org.apache.http.auth.UsernamePasswordCredentials; | |||
| import org.apache.http.client.CredentialsProvider; | |||
| import org.apache.http.impl.client.BasicCredentialsProvider; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.apache.http.impl.client.HttpClients; | |||
| import org.springframework.stereotype.Component; | |||
| @Component | |||
| public class HKPeopleNumberFactory { | |||
| private Map<String,Integer> peopleCountMap; | |||
| public void setPeopleNumber(String ip,Integer count) { | |||
| synchronized (ip) { | |||
| if (null == peopleCountMap) { | |||
| peopleCountMap = new ConcurrentHashMap<String,Integer>(); | |||
| } | |||
| peopleCountMap.put(ip, count); | |||
| } | |||
| } | |||
| public Integer getPeopleNumber() { | |||
| if (null == peopleCountMap) { | |||
| return 0; | |||
| } | |||
| Integer all = 0; | |||
| for (Iterator<String> it = peopleCountMap.keySet().iterator();it.hasNext();) { | |||
| String ip = it.next(); | |||
| Integer count = peopleCountMap.get(ip); | |||
| if (null != count ) { | |||
| all = all + count; | |||
| } | |||
| } | |||
| return all; | |||
| } | |||
| } | |||