350 lines
13 KiB
Java
350 lines
13 KiB
Java
package com.nis.util;
|
||
|
||
import java.io.File;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
import javax.ws.rs.client.Entity;
|
||
import javax.ws.rs.client.Invocation.Builder;
|
||
import javax.ws.rs.client.WebTarget;
|
||
import javax.ws.rs.core.MediaType;
|
||
import javax.ws.rs.core.Response;
|
||
|
||
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
||
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import com.google.common.reflect.TypeToken;
|
||
import com.google.gson.Gson;
|
||
import com.nis.domain.log.ReportLog;
|
||
import com.nis.domain.log.ReportResultLog;
|
||
import com.nis.domain.maat.ToMaatResult;
|
||
import com.nis.exceptions.MaatConvertException;
|
||
import com.nis.util.httpclient.ClientUtil;
|
||
|
||
import net.sf.json.JSONObject;
|
||
import net.sf.json.JsonConfig;
|
||
|
||
public class ConfigServiceUtil {
|
||
private static Logger logger = LoggerFactory.getLogger(ConfigServiceUtil.class);
|
||
/*public static class MapTypeAdapter extends TypeAdapter<Object> {
|
||
@Override
|
||
public Object read(JsonReader in) throws IOException {
|
||
JsonToken token = in.peek();
|
||
switch (token) {
|
||
case BEGIN_ARRAY:
|
||
List<Object> list = new ArrayList<Object>();
|
||
in.beginArray();
|
||
while (in.hasNext()) {
|
||
list.add(read(in));
|
||
}
|
||
in.endArray();
|
||
return list;
|
||
|
||
case BEGIN_OBJECT:
|
||
Map<String, Object> map = new LinkedTreeMap<String, Object>();
|
||
in.beginObject();
|
||
while (in.hasNext()) {
|
||
map.put(in.nextName(), read(in));
|
||
}
|
||
in.endObject();
|
||
return map;
|
||
|
||
case STRING:
|
||
return in.nextString();
|
||
|
||
case NUMBER:
|
||
String temp = in.nextString();
|
||
BigDecimal dbNum = new BigDecimal(temp);
|
||
BigDecimal maxLong = new BigDecimal(Long.MAX_VALUE);
|
||
BigDecimal maxInteger = new BigDecimal(Integer.MAX_VALUE);
|
||
// 数字超过long的最大值,返回BigDecimal类型
|
||
if (dbNum.compareTo(maxLong)==1) {
|
||
return dbNum;
|
||
}else if(dbNum.compareTo(maxInteger)==1){
|
||
long lngNum = Long.parseLong(temp);
|
||
return lngNum;
|
||
}else{
|
||
int lngNum = Integer.parseInt(temp);
|
||
return lngNum;
|
||
}
|
||
|
||
case BOOLEAN:
|
||
return in.nextBoolean();
|
||
|
||
case NULL:
|
||
in.nextNull();
|
||
return null;
|
||
|
||
default:
|
||
throw new IllegalStateException();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void write(JsonWriter out, Object value) throws IOException {
|
||
// 序列化无需实现
|
||
}
|
||
|
||
}*/
|
||
/**
|
||
* 从后台服务获取compileid,groupid,regionid
|
||
* @param type 1是compileid,2是groupid,3是regionid
|
||
* @return
|
||
*/
|
||
public static List<Integer> getId(int type,int num) throws MaatConvertException {
|
||
String result = null;
|
||
String url = "";
|
||
List<Integer> list = new ArrayList();
|
||
if (type == 1) {
|
||
url = Constants.SERVICE_URL+Constants.CONFIG_ID_SOURCES+"?sourceName=CONFIG_COMPILE&num="+num;
|
||
} else if (type == 2) {
|
||
url = Constants.SERVICE_URL+Constants.CONFIG_ID_SOURCES+"?sourceName=CONFIG_GROUP&num="+num;
|
||
} else if (type == 3) {
|
||
url = Constants.SERVICE_URL+Constants.CONFIG_ID_SOURCES+"?sourceName=CONFIG_REGION&num="+num;
|
||
}
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
//获取响应结果
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||
Response response= header.get();
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
// result = "{\"status\":200,\"businessCode\":2000,\"reason\":\"数据获取操作成功\","
|
||
// + "\"msg\":\"配置ID获取成功\",\"fromuri\":\"/galaxy/service/cfg/v1/configPzIdSources\","
|
||
// + "\"traceCode\":\"2018052409232108368751\",\"data\":{\"sourceName\":\"CONFIG_COMPILE\",\"num\":1,\"pzIdList\":[22]}}";
|
||
JSONObject resObject = JSONObject.fromObject(result);
|
||
ToMaatResult bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
||
if(!StringUtil.isEmpty(bean)){
|
||
if(!StringUtil.isEmpty(bean.getData())){
|
||
list = bean.getData().getPzIdList();
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return list;
|
||
}
|
||
/**
|
||
* MAAT配置提交
|
||
* @param params
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static ToMaatResult postMaatCfg(String params) throws MaatConvertException{
|
||
String result = null;
|
||
ToMaatResult bean = null;
|
||
String url = Constants.SERVICE_URL+Constants.MAAT_CFG;
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
//获取响应结果
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||
Response response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
JsonConfig config=new JsonConfig();
|
||
config.setExcludes(new String[]{"configCompileList"});
|
||
JSONObject resObject = JSONObject.fromObject(result,config);
|
||
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return bean;
|
||
}
|
||
/**
|
||
* 回调配置提交
|
||
* @param params
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static ToMaatResult postCallbackCfg(String params) throws MaatConvertException{
|
||
String result = null;
|
||
ToMaatResult bean = null;
|
||
String url = Constants.SERVICE_URL+Constants.CALLBACK_CFG;
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
//获取响应结果
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||
Response response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
JSONObject resObject = JSONObject.fromObject(result);
|
||
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return bean;
|
||
|
||
}
|
||
/**
|
||
* 文件配置提交
|
||
* @param params
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static ToMaatResult postFileCfg(String params,File file,String fileDesc) throws MaatConvertException{
|
||
String result = null;
|
||
ToMaatResult bean = null;
|
||
String url = Constants.SERVICE_URL+Constants.FILE_UPLOAD_CFG;
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
FormDataMultiPart formDataMultiPart=new FormDataMultiPart();
|
||
FileDataBodyPart bodyPart=new FileDataBodyPart("file",file);
|
||
formDataMultiPart.bodyPart(bodyPart);
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("File-Desc",fileDesc);
|
||
Response response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
JSONObject resObject = JSONObject.fromObject(result);
|
||
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return bean;
|
||
|
||
}
|
||
/**
|
||
* 配置删除
|
||
* @param params
|
||
* @param type,1表示maat配置取消,2表示回调配置取消
|
||
* @return
|
||
*/
|
||
public static ToMaatResult delete(String params,Integer type) throws MaatConvertException{
|
||
String result = null;
|
||
ToMaatResult bean = null;
|
||
String url = Constants.SERVICE_URL;
|
||
if(type==1){
|
||
url = url+Constants.MAAT_CFG;
|
||
}else if(type==2){
|
||
url = url+Constants.CALLBACK_CFG;
|
||
}
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
//获取响应结果
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||
Response response= header.delete();
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
JSONObject resObject = JSONObject.fromObject(result);
|
||
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return bean;
|
||
|
||
}
|
||
/**
|
||
* 配置状态修改
|
||
* @param params
|
||
* @param type,1表示maat配置取消,2表示回调配置取消
|
||
* @return
|
||
*/
|
||
public static ToMaatResult put(String params,Integer type) throws MaatConvertException{
|
||
String result = null;
|
||
ToMaatResult bean = null;
|
||
String url = Constants.SERVICE_URL;
|
||
if(type==1){
|
||
url = url+Constants.MAAT_CFG;
|
||
}else if(type==2){
|
||
url = url+Constants.CALLBACK_CFG;
|
||
}
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
//获取响应结果
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||
Response response= header.put(Entity.entity(params, MediaType.APPLICATION_JSON));
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
JSONObject resObject = JSONObject.fromObject(result);
|
||
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return bean;
|
||
}
|
||
/**
|
||
* 信息获取
|
||
* @param params
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static ToMaatResult getFileDigest(String params,File file,String fileDesc) throws MaatConvertException{
|
||
String result = null;
|
||
ToMaatResult bean = null;
|
||
String url = Constants.SERVICE_URL+Constants.FILE_DIGEST_CFG;
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||
FormDataMultiPart formDataMultiPart=new FormDataMultiPart();
|
||
FileDataBodyPart bodyPart=new FileDataBodyPart("file",file);
|
||
formDataMultiPart.bodyPart(bodyPart);
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("File-Desc",fileDesc);
|
||
Response response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
logger.info("获取文件摘要响应结果"+result);
|
||
JSONObject resObject = JSONObject.fromObject(result);
|
||
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return bean;
|
||
}
|
||
/**
|
||
*
|
||
* getReport(配置日志总量统计查询)
|
||
* (这里描述这个方法适用条件 – 可选)
|
||
* @param type 1- 配置命中总量业务2- 配置报表业务
|
||
* @param ids
|
||
* @param serviceIds
|
||
* @param searchReportStartTime
|
||
* @param searchReportEndTime
|
||
* @return
|
||
* @throws MaatConvertException
|
||
*ReportResultLog
|
||
* @exception
|
||
* @since 1.0.0
|
||
*/
|
||
public static ReportResultLog<ReportLog> getReport(int type,String ids,String serviceIds,String searchReportStartTime,String searchReportEndTime) throws MaatConvertException{
|
||
if(StringUtils.isBlank(ids)||StringUtils.isBlank(serviceIds)){
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
|
||
}
|
||
String result = null;
|
||
ReportResultLog<ReportLog> bean = null;
|
||
StringBuffer url=new StringBuffer(Constants.SERVICE_URL+Constants.NTC_PZ_REPORT+"?");
|
||
url.append("searchBusinessType=").append(type)
|
||
.append("&searchService=").append(serviceIds).append("&searchCfgId=").append(ids);
|
||
if(StringUtils.isNotBlank(searchReportStartTime)){
|
||
url.append("&searchReportStartTime=").append(searchReportStartTime);
|
||
}
|
||
if(StringUtils.isNotBlank(searchReportEndTime)){
|
||
url.append("&searchReportEndTime=").append(searchReportEndTime);
|
||
}
|
||
//创建连接
|
||
WebTarget wt = ClientUtil.getWebTarger(url.toString());
|
||
Builder header = wt.request(MediaType.APPLICATION_JSON);
|
||
Response response= header.get();
|
||
if( response.getStatus() == 200){
|
||
result= response.readEntity(String.class);
|
||
Gson gson=Constants.gsonbuilder.create();
|
||
bean = gson.fromJson(result, new TypeToken<ReportResultLog<ReportLog>>(){
|
||
private static final long serialVersionUID = 7963055764864141968L;}.getType());
|
||
}else{
|
||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||
}
|
||
return bean;
|
||
}
|
||
public static void main(String[] args) {
|
||
try {
|
||
// getId(1,1);
|
||
System.out.println(IpUtil.convertMask(4));
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|