Showing
4 changed files
with
92 additions
and
73 deletions
1 | +package com.monitor.cmdb.ctrl; | ||
2 | + | ||
3 | +import com.monitor.cmdb.service.IHostInfoService; | ||
4 | +import org.apache.commons.lang.StringUtils; | ||
5 | +import org.slf4j.Logger; | ||
6 | +import org.slf4j.LoggerFactory; | ||
7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
8 | +import org.springframework.stereotype.Controller; | ||
9 | +import org.springframework.web.bind.annotation.RequestBody; | ||
10 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
11 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
12 | + | ||
13 | +import javax.servlet.http.HttpServletRequest; | ||
14 | + | ||
15 | +/** | ||
16 | + * Created by craig.qin on 2017/11/1. | ||
17 | + */ | ||
18 | +@Controller | ||
19 | +@RequestMapping("/cmdb") | ||
20 | +public class CmdbApiCtrl { | ||
21 | + | ||
22 | + Logger log = LoggerFactory.getLogger(CmdbApiCtrl.class); | ||
23 | + | ||
24 | + @Autowired | ||
25 | + IHostInfoService hostInfoService; | ||
26 | + | ||
27 | + /** | ||
28 | + * 根据提供的tags标签查询host。返回json串 | ||
29 | + */ | ||
30 | + @RequestMapping("/api") | ||
31 | + @ResponseBody | ||
32 | + public String api(HttpServletRequest request,@RequestBody String content) { | ||
33 | + String ip=getIpAddr(request); | ||
34 | + return hostInfoService.getHostInfoJson(ip, content); | ||
35 | + } | ||
36 | + | ||
37 | + | ||
38 | + | ||
39 | + /** | ||
40 | + * 获取访问者IP | ||
41 | + * | ||
42 | + * 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。 | ||
43 | + * | ||
44 | + * 本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割), | ||
45 | + * 如果还不存在则调用Request .getRemoteAddr()。 | ||
46 | + * | ||
47 | + * @param request | ||
48 | + * @return | ||
49 | + */ | ||
50 | + private String getIpAddr(HttpServletRequest request) { | ||
51 | + String ip = request.getHeader("X-Real-IP"); | ||
52 | + if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { | ||
53 | + return ip; | ||
54 | + } | ||
55 | + ip = request.getHeader("X-Forwarded-For"); | ||
56 | + if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { | ||
57 | + // 多次反向代理后会有多个IP值,第一个为真实IP。 | ||
58 | + int index = ip.indexOf(','); | ||
59 | + if (index != -1) { | ||
60 | + return ip.substring(0, index); | ||
61 | + } else { | ||
62 | + return ip; | ||
63 | + } | ||
64 | + } else { | ||
65 | + return request.getRemoteAddr(); | ||
66 | + } | ||
67 | + } | ||
68 | +} |
@@ -37,76 +37,7 @@ public class HostInfoCtrl { | @@ -37,76 +37,7 @@ public class HostInfoCtrl { | ||
37 | @Autowired | 37 | @Autowired |
38 | IHostInfoService hostInfoService; | 38 | IHostInfoService hostInfoService; |
39 | 39 | ||
40 | - /** | ||
41 | - * 根据提供的tags标签查询host。返回json串 | ||
42 | - * encodeTags 加密串需要解密之后再用 | ||
43 | - * @return | ||
44 | - */ | ||
45 | - @RequestMapping("/getHostInfoJson") | ||
46 | - @ResponseBody | ||
47 | - public String getHostInfoJson(@RequestBody CmdbApiReq apiReq) { | ||
48 | - log.info("cmdbapi param {}", apiReq); | ||
49 | - return hostInfoService.getHostInfoJson(apiReq.getSourceIp(), apiReq.getContent()); | ||
50 | - } | ||
51 | - | ||
52 | - /** | ||
53 | - * 根据提供的tags标签查询host。返回json串 | ||
54 | - */ | ||
55 | - @RequestMapping("/api") | ||
56 | - @ResponseBody | ||
57 | - public String api(HttpServletRequest request,@RequestBody String content) { | ||
58 | - String ip=getIpAddr(request); | ||
59 | - return hostInfoService.getHostInfoJson(ip, content); | ||
60 | - } | ||
61 | - | ||
62 | - /** | ||
63 | - * 根据提供的tags标签查询host。返回json串 | ||
64 | - */ | ||
65 | - @RequestMapping("/api2") | ||
66 | - @ResponseBody | ||
67 | - public String api2(HttpServletRequest request) { | ||
68 | - String ip=getIpAddr(request); | ||
69 | - return ip; | ||
70 | - } | ||
71 | - | ||
72 | - /** | ||
73 | - * 根据提供的tags标签查询host。返回json串 | ||
74 | - */ | ||
75 | - @RequestMapping("/api3") | ||
76 | - @ResponseBody | ||
77 | - public String api3(HttpServletRequest request,@RequestBody String content) { | ||
78 | - return content; | ||
79 | - } | ||
80 | 40 | ||
81 | - /** | ||
82 | - * 获取访问者IP | ||
83 | - * | ||
84 | - * 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。 | ||
85 | - * | ||
86 | - * 本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割), | ||
87 | - * 如果还不存在则调用Request .getRemoteAddr()。 | ||
88 | - * | ||
89 | - * @param request | ||
90 | - * @return | ||
91 | - */ | ||
92 | - private String getIpAddr(HttpServletRequest request) { | ||
93 | - String ip = request.getHeader("X-Real-IP"); | ||
94 | - if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { | ||
95 | - return ip; | ||
96 | - } | ||
97 | - ip = request.getHeader("X-Forwarded-For"); | ||
98 | - if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { | ||
99 | - // 多次反向代理后会有多个IP值,第一个为真实IP。 | ||
100 | - int index = ip.indexOf(','); | ||
101 | - if (index != -1) { | ||
102 | - return ip.substring(0, index); | ||
103 | - } else { | ||
104 | - return ip; | ||
105 | - } | ||
106 | - } else { | ||
107 | - return request.getRemoteAddr(); | ||
108 | - } | ||
109 | - } | ||
110 | 41 | ||
111 | 42 | ||
112 | /** | 43 | /** |
@@ -22,7 +22,9 @@ import org.springframework.stereotype.Service; | @@ -22,7 +22,9 @@ import org.springframework.stereotype.Service; | ||
22 | import org.springframework.util.CollectionUtils; | 22 | import org.springframework.util.CollectionUtils; |
23 | 23 | ||
24 | import java.util.ArrayList; | 24 | import java.util.ArrayList; |
25 | +import java.util.HashMap; | ||
25 | import java.util.List; | 26 | import java.util.List; |
27 | +import java.util.Map; | ||
26 | 28 | ||
27 | /** | 29 | /** |
28 | * Created by yoho on 2016/6/14. | 30 | * Created by yoho on 2016/6/14. |
@@ -40,7 +42,6 @@ public class HostInfoServiceImpl implements IHostInfoService { | @@ -40,7 +42,6 @@ public class HostInfoServiceImpl implements IHostInfoService { | ||
40 | 42 | ||
41 | @Override | 43 | @Override |
42 | public String getHostInfoJson(String sourceIp ,String content){ | 44 | public String getHostInfoJson(String sourceIp ,String content){ |
43 | - logger.info("begin getHostInfoJson"); | ||
44 | String backJson=""; | 45 | String backJson=""; |
45 | if(StringUtils.isNotBlank(content)&&sourceIp.startsWith("172")){ | 46 | if(StringUtils.isNotBlank(content)&&sourceIp.startsWith("172")){ |
46 | JSONObject jo=JSON.parseObject(content); | 47 | JSONObject jo=JSON.parseObject(content); |
@@ -68,11 +69,20 @@ public class HostInfoServiceImpl implements IHostInfoService { | @@ -68,11 +69,20 @@ public class HostInfoServiceImpl implements IHostInfoService { | ||
68 | } | 69 | } |
69 | } | 70 | } |
70 | //执行查询,至少要有一个tag | 71 | //执行查询,至少要有一个tag |
71 | - List<HostInfo> hostInfos=new ArrayList<HostInfo>(); | 72 | + List<Map<String,Object>> hostInfosMap=new ArrayList<Map<String,Object>>(); |
72 | if(tagsList.size()>0){ | 73 | if(tagsList.size()>0){ |
73 | - hostInfos=hostInfoMapper.selectHostInfosByTagListAndCloudTypeList(tagsList,cloudTypeList); | 74 | + List<HostInfo> hostInfos =hostInfoMapper.selectHostInfosByTagListAndCloudTypeList(tagsList,cloudTypeList); |
75 | + if(hostInfos!=null&&hostInfos.size()>0){ | ||
76 | + for(HostInfo host:hostInfos){ | ||
77 | + Map<String,Object> tmp=new HashMap<String,Object>(); | ||
78 | + tmp.put("hostIp",host.getHostIp()); | ||
79 | + tmp.put("tags",host.getTags()); | ||
80 | + tmp.put("cloudType",host.getCloudType()); | ||
81 | + hostInfosMap.add(tmp); | ||
82 | + } | ||
83 | + } | ||
74 | } | 84 | } |
75 | - backJson=JSON.toJSONString(hostInfos); | 85 | + backJson=JSON.toJSONString(hostInfosMap); |
76 | }else{ | 86 | }else{ |
77 | backJson="bad request param ! please check content:"+content+" sourceIP "+sourceIp; | 87 | backJson="bad request param ! please check content:"+content+" sourceIP "+sourceIp; |
78 | } | 88 | } |
@@ -73,4 +73,14 @@ public class CmdbApiPostDemo { | @@ -73,4 +73,14 @@ public class CmdbApiPostDemo { | ||
73 | 73 | ||
74 | } | 74 | } |
75 | 75 | ||
76 | + public static void main(String[] arg){ | ||
77 | + Map<String ,Object> contentList=new HashMap<String,Object>(); | ||
78 | + contentList.put("cloudType","1,2"); | ||
79 | + contentList.put("tags",Arrays.asList(new String[]{"java","redis"})); | ||
80 | + String content = JSON.toJSONString(contentList); | ||
81 | + System.out.println(content); | ||
82 | + //{"cloudType":"1,2","tags":["java"]} | ||
83 | + //{"cloudType":"1,2","tags":["java","redis"]} | ||
84 | + } | ||
85 | + | ||
76 | } | 86 | } |
-
Please register or login to post a comment