Authored by qinchao

-cmdb api功能

package com.monitor.cmdb.ctrl;
import com.monitor.cmdb.service.IHostInfoService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
* Created by craig.qin on 2017/11/1.
*/
@Controller
@RequestMapping("/cmdb")
public class CmdbApiCtrl {
Logger log = LoggerFactory.getLogger(CmdbApiCtrl.class);
@Autowired
IHostInfoService hostInfoService;
/**
* 根据提供的tags标签查询host。返回json串
*/
@RequestMapping("/api")
@ResponseBody
public String api(HttpServletRequest request,@RequestBody String content) {
String ip=getIpAddr(request);
return hostInfoService.getHostInfoJson(ip, content);
}
/**
* 获取访问者IP
*
* 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。
*
* 本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割),
* 如果还不存在则调用Request .getRemoteAddr()。
*
* @param request
* @return
*/
private String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Real-IP");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
ip = request.getHeader("X-Forwarded-For");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个IP值,第一个为真实IP。
int index = ip.indexOf(',');
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
} else {
return request.getRemoteAddr();
}
}
}
... ...
... ... @@ -37,76 +37,7 @@ public class HostInfoCtrl {
@Autowired
IHostInfoService hostInfoService;
/**
* 根据提供的tags标签查询host。返回json串
* encodeTags 加密串需要解密之后再用
* @return
*/
@RequestMapping("/getHostInfoJson")
@ResponseBody
public String getHostInfoJson(@RequestBody CmdbApiReq apiReq) {
log.info("cmdbapi param {}", apiReq);
return hostInfoService.getHostInfoJson(apiReq.getSourceIp(), apiReq.getContent());
}
/**
* 根据提供的tags标签查询host。返回json串
*/
@RequestMapping("/api")
@ResponseBody
public String api(HttpServletRequest request,@RequestBody String content) {
String ip=getIpAddr(request);
return hostInfoService.getHostInfoJson(ip, content);
}
/**
* 根据提供的tags标签查询host。返回json串
*/
@RequestMapping("/api2")
@ResponseBody
public String api2(HttpServletRequest request) {
String ip=getIpAddr(request);
return ip;
}
/**
* 根据提供的tags标签查询host。返回json串
*/
@RequestMapping("/api3")
@ResponseBody
public String api3(HttpServletRequest request,@RequestBody String content) {
return content;
}
/**
* 获取访问者IP
*
* 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。
*
* 本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割),
* 如果还不存在则调用Request .getRemoteAddr()。
*
* @param request
* @return
*/
private String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Real-IP");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
ip = request.getHeader("X-Forwarded-For");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个IP值,第一个为真实IP。
int index = ip.indexOf(',');
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
} else {
return request.getRemoteAddr();
}
}
/**
... ...
... ... @@ -22,7 +22,9 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by yoho on 2016/6/14.
... ... @@ -40,7 +42,6 @@ public class HostInfoServiceImpl implements IHostInfoService {
@Override
public String getHostInfoJson(String sourceIp ,String content){
logger.info("begin getHostInfoJson");
String backJson="";
if(StringUtils.isNotBlank(content)&&sourceIp.startsWith("172")){
JSONObject jo=JSON.parseObject(content);
... ... @@ -68,11 +69,20 @@ public class HostInfoServiceImpl implements IHostInfoService {
}
}
//执行查询,至少要有一个tag
List<HostInfo> hostInfos=new ArrayList<HostInfo>();
List<Map<String,Object>> hostInfosMap=new ArrayList<Map<String,Object>>();
if(tagsList.size()>0){
hostInfos=hostInfoMapper.selectHostInfosByTagListAndCloudTypeList(tagsList,cloudTypeList);
List<HostInfo> hostInfos =hostInfoMapper.selectHostInfosByTagListAndCloudTypeList(tagsList,cloudTypeList);
if(hostInfos!=null&&hostInfos.size()>0){
for(HostInfo host:hostInfos){
Map<String,Object> tmp=new HashMap<String,Object>();
tmp.put("hostIp",host.getHostIp());
tmp.put("tags",host.getTags());
tmp.put("cloudType",host.getCloudType());
hostInfosMap.add(tmp);
}
}
}
backJson=JSON.toJSONString(hostInfos);
backJson=JSON.toJSONString(hostInfosMap);
}else{
backJson="bad request param ! please check content:"+content+" sourceIP "+sourceIp;
}
... ...
... ... @@ -73,4 +73,14 @@ public class CmdbApiPostDemo {
}
public static void main(String[] arg){
Map<String ,Object> contentList=new HashMap<String,Object>();
contentList.put("cloudType","1,2");
contentList.put("tags",Arrays.asList(new String[]{"java","redis"}));
String content = JSON.toJSONString(contentList);
System.out.println(content);
//{"cloudType":"1,2","tags":["java"]}
//{"cloudType":"1,2","tags":["java","redis"]}
}
}
... ...