本文使用ssm框架。springmvc,spring,mybatis
配置文件
#-----REST接口-----------------------------
rs_username=ad
rs_password=ad
rs_url=http://192.168.42.147:。。。。
rs_url2=http://192.168.42.147:。。。。
rs_url3=http\://192.168.42.147\。。。。
代码
package com.cn.cncc.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.cncc.util.PropertiesUtils;
@Controller
public class NoteController {
final static String CONTENT_TYPE_TEXT_JSON = "text/json";
@RequestMapping(value="/getDevAndLog.do",produces={"application/json; charset=UTF-8"})
@ResponseBody
public Object getDevAndLog(HttpServletRequest request){
HttpClient client = null;
HttpPost httppost = null;
HttpGet httpget = null;
StringEntity se = null;
HttpResponse response = null;
HttpEntity entity = null;
Map<String,Object> result = new HashMap<String, Object>();
List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
try {
PropertiesUtils.readProperties("default.properties");
String username = PropertiesUtils.getProperty("rs_username");
String password = PropertiesUtils.getProperty("rs_password");
String rs_url = PropertiesUtils.getProperty("rs_url");
String rs_url2 = PropertiesUtils.getProperty("rs_url2");
String rs_url3 = PropertiesUtils.getProperty("rs_url3");
/*-----第一次请求,获取token--------------------------------------------------------*/
// 创建默认的httpClient实例.
client = new DefaultHttpClient();
// 创建httppost
httppost = new HttpPost(rs_url);
// 设置请求头
httppost.setHeader("Content-Type", "application/json;charset=UTF-8");
// 创建参数队列
String js = "{\"username\":\""+ username +"\",\"password\":\""+password+"\"}";
se = new StringEntity(js);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
httppost.setEntity(se);
response = client.execute(httppost);
entity = response.getEntity();
String strEntity = EntityUtils.toString(entity, "UTF-8");
JSONObject json = new JSONObject(strEntity);
//获取到token
String token = (String)json.get("authToken");
System.out.println("token: " + token);
System.out.println("__________________________________________________________________");
/*------第二次请求,获取searchJobId---------------------------------------------------------------*/
httppost = new HttpPost(rs_url2);
// 设置请求头
httppost.setHeader("Content-Type", "application/json;charset=UTF-8");
httppost.setHeader("Authorization", token);
//配置查询参数
js = "{\"searchValue\":\"* | filter isNotNull(ApplicationContext) | fields ApplicationContext, ServiceName | group ApplicationContext ServiceName\",\"searchType\":\"queryString\"," +
"\"startTime\":\"now-60m\",\"endTime\":\"now\"" +
",\"resultCountReq\":\"Yes\",\"pageSize\":\"100\",\"pageNumber\":\"1\"}";
//System.out.println(js);
se = new StringEntity(js);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
httppost.setEntity(se);
response = client.execute(httppost);
entity = response.getEntity();
String strEntity2 = EntityUtils.toString(entity, "UTF-8");
JSONObject json2 = new JSONObject(strEntity2);
if(json2.has("id")){
//获取到token
String searchJobId = (String)json2.get("id");
System.out.println("searchJobId: "+searchJobId);
System.out.println("__________________________________________________________________");
/*------第三次请求,获取查询结果---------------------------------------------------------------*/
rs_url3 = rs_url3+searchJobId;
StringBuffer sb = new StringBuffer(rs_url3);
//StringBuffer sbs = new StringBuffer();
sb.append("&searchValue=" + "* | filter isNotNull(ApplicationContext) | fields ApplicationContext, ServiceName | group ApplicationContext ServiceName");
sb.append("&searchType=" + "queryString");
sb.append("&startTime=" + "now-60m");
sb.append("&endTime=" + "now");
sb.append("&resultCountReq=" + "Yes");
sb.append("&pageSize=" + 100);
sb.append("&pageNumber=" + 1);
String str = sb.toString().replaceAll(" ", "+");
str = str.replaceAll("\\|", "%7C");
//String str = URLEncoder.encode(sb.toString().replaceAll(" ", "+"));
// 创建请求对象,url中不可以有空格,转意成“+”号
System.out.println(str);
httpget = new HttpGet(str);
httpget.setHeader("Content-Type", "application/json;charset=UTF-8");
httpget.setHeader("Authorization", token);
// 设置参数
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
// 执行请求的对象
HttpClient clientget = new DefaultHttpClient(params);
HttpResponse resp = clientget.execute(httpget);
// 判断是否响应成功
if (resp.getStatusLine().getStatusCode() == 200) {
// 获取响应 的数据
entity = resp.getEntity();
String strEntity3 = EntityUtils.toString(entity, "utf-8");
org.json.JSONObject json3 = new org.json.JSONObject(strEntity3);
org.json.JSONObject searchResults = ( org.json.JSONObject) json3.get("searchResults");
org.json.JSONArray results = searchResults.getJSONArray("results");
for(int i=0;i<results.length();i++){
Map<String,Object> resultMap = new HashMap<String, Object>();
org.json.JSONObject jsonobject = results.getJSONObject(i);
org.json.JSONObject fieldValues = ( org.json.JSONObject)jsonobject.get("fieldValues");
org.json.JSONObject applicationContext = ( org.json.JSONObject)fieldValues.get("ApplicationContext");
org.json.JSONArray appValues = applicationContext.getJSONArray("values");
for(int j=0;j<appValues.length();j++){
org.json.JSONObject jsonobject2 = appValues.getJSONObject(j);
//设备名
String value = (String) jsonobject2.get("value");
System.out.println("设备名:" + value);
resultMap.put("ApplicationContext", value);
}
org.json.JSONObject serviceName = (org.json.JSONObject)fieldValues.get("ServiceName");
org.json.JSONArray snValues = serviceName.getJSONArray("values");
for(int j=0;j<snValues.length();j++){
org.json.JSONObject jsonobject2 = snValues.getJSONObject(j);
//日志名
String value = (String) jsonobject2.get("value");
System.out.println("日志名: " + value);
resultMap.put("ServiceName", value);
}
resultList.add(resultMap);
}
System.out.println("resultList:" + resultList);
}
result.put("data", resultList);
}else{
result.put("data", "");
//查询无结果,没有searchId
result.put("error", "1");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(resultList.size());
return result;
}
}