2018-05-21 09:46:49 +08:00
|
|
|
|
package com.nis.util;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2018-05-23 19:01:57 +08:00
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.math.BigDecimal;
|
2018-05-23 14:31:57 +08:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.List;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.ws.rs.client.Invocation.Builder;
|
|
|
|
|
|
import javax.ws.rs.client.Entity;
|
|
|
|
|
|
import javax.ws.rs.client.WebTarget;
|
|
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
|
|
2018-05-21 11:22:51 +08:00
|
|
|
|
import net.sf.json.JSONObject;
|
2018-05-29 18:51:56 +08:00
|
|
|
|
import net.sf.json.JsonConfig;
|
2018-05-21 11:22:51 +08:00
|
|
|
|
|
2018-05-21 09:46:49 +08:00
|
|
|
|
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
|
|
|
|
|
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
|
|
|
|
|
|
|
2018-05-24 10:01:24 +08:00
|
|
|
|
import com.jcraft.jsch.Logger;
|
|
|
|
|
|
import com.nis.domain.maat.ToMaatResult;
|
|
|
|
|
|
import com.nis.domain.maat.ToMaatResult.ResponseData;
|
2018-05-21 14:10:11 +08:00
|
|
|
|
import com.nis.exceptions.MaatConvertException;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
import com.nis.util.httpclient.ClientUtil;
|
|
|
|
|
|
|
|
|
|
|
|
public class ConfigServiceUtil {
|
2018-05-24 10:01:24 +08:00
|
|
|
|
/*public static class MapTypeAdapter extends TypeAdapter<Object> {
|
2018-05-23 19:01:57 +08:00
|
|
|
|
@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 {
|
|
|
|
|
|
// 序列化无需实现
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-24 10:01:24 +08:00
|
|
|
|
}*/
|
2018-05-21 09:46:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从后台服务获取compileid,groupid,regionid
|
|
|
|
|
|
* @param type 1是compileid,2是groupid,3是regionid
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2018-05-28 11:40:58 +08:00
|
|
|
|
public static List<Integer> getId(int type,int num) throws MaatConvertException {
|
2018-05-21 09:46:49 +08:00
|
|
|
|
String result = null;
|
|
|
|
|
|
String url = "";
|
2018-05-23 14:31:57 +08:00
|
|
|
|
List<Integer> list = new ArrayList();
|
2018-05-21 09:46:49 +08:00
|
|
|
|
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);
|
2018-05-24 10:01:24 +08:00
|
|
|
|
// 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();
|
2018-05-23 14:31:57 +08:00
|
|
|
|
}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));
|
|
|
|
|
|
}
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}else{
|
2018-05-21 14:10:11 +08:00
|
|
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
2018-05-23 14:31:57 +08:00
|
|
|
|
return list;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* MAAT配置提交
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2018-05-28 11:40:58 +08:00
|
|
|
|
public static ToMaatResult postMaatCfg(String params) throws MaatConvertException{
|
2018-05-21 09:46:49 +08:00
|
|
|
|
String result = null;
|
2018-05-24 10:01:24 +08:00
|
|
|
|
ToMaatResult bean = null;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
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);
|
2018-05-21 10:07:00 +08:00
|
|
|
|
Response response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
|
2018-05-21 09:46:49 +08:00
|
|
|
|
if( response.getStatus() == 200){
|
|
|
|
|
|
result= response.readEntity(String.class);
|
2018-05-29 18:51:56 +08:00
|
|
|
|
JsonConfig config=new JsonConfig();
|
|
|
|
|
|
config.setExcludes(new String[]{"configCompileList"});
|
|
|
|
|
|
JSONObject resObject = JSONObject.fromObject(result,config);
|
2018-05-24 10:01:24 +08:00
|
|
|
|
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}else{
|
2018-05-21 14:10:11 +08:00
|
|
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
2018-05-24 10:01:24 +08:00
|
|
|
|
return bean;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 回调配置提交
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2018-05-28 11:40:58 +08:00
|
|
|
|
public static ToMaatResult postCallbackCfg(String params) throws MaatConvertException{
|
2018-05-21 09:46:49 +08:00
|
|
|
|
String result = null;
|
2018-05-24 10:01:24 +08:00
|
|
|
|
ToMaatResult bean = null;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
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);
|
2018-05-21 10:07:00 +08:00
|
|
|
|
Response response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
|
2018-05-21 09:46:49 +08:00
|
|
|
|
if( response.getStatus() == 200){
|
|
|
|
|
|
result= response.readEntity(String.class);
|
2018-05-24 10:01:24 +08:00
|
|
|
|
JSONObject resObject = JSONObject.fromObject(result);
|
|
|
|
|
|
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}else{
|
2018-05-21 14:10:11 +08:00
|
|
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
2018-05-24 10:01:24 +08:00
|
|
|
|
return bean;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件配置提交
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2018-05-28 11:40:58 +08:00
|
|
|
|
public static ToMaatResult postFileCfg(String params,File file,JSONObject fileDesc) throws MaatConvertException{
|
2018-05-21 09:46:49 +08:00
|
|
|
|
String result = null;
|
2018-05-24 10:01:24 +08:00
|
|
|
|
ToMaatResult bean = null;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
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);
|
2018-05-21 11:22:51 +08:00
|
|
|
|
Builder header = wt.request(MediaType.APPLICATION_JSON).header("File-Desc",fileDesc);
|
2018-05-21 09:46:49 +08:00
|
|
|
|
Response response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
|
|
|
|
|
|
if( response.getStatus() == 200){
|
|
|
|
|
|
result= response.readEntity(String.class);
|
2018-05-24 10:01:24 +08:00
|
|
|
|
JSONObject resObject = JSONObject.fromObject(result);
|
|
|
|
|
|
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}else{
|
2018-05-21 14:10:11 +08:00
|
|
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
2018-05-24 10:01:24 +08:00
|
|
|
|
return bean;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 配置删除
|
|
|
|
|
|
* @param params
|
2018-05-21 10:07:00 +08:00
|
|
|
|
* @param type,1表示maat配置取消,2表示回调配置取消
|
2018-05-21 09:46:49 +08:00
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2018-05-28 11:40:58 +08:00
|
|
|
|
public static ToMaatResult delete(String params,Integer type) throws MaatConvertException{
|
2018-05-21 10:07:00 +08:00
|
|
|
|
String result = null;
|
2018-05-24 10:01:24 +08:00
|
|
|
|
ToMaatResult bean = null;
|
2018-05-21 10:07:00 +08:00
|
|
|
|
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);
|
2018-05-24 10:01:24 +08:00
|
|
|
|
JSONObject resObject = JSONObject.fromObject(result);
|
|
|
|
|
|
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
2018-05-21 10:07:00 +08:00
|
|
|
|
}else{
|
2018-05-21 14:10:11 +08:00
|
|
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
2018-05-21 10:07:00 +08:00
|
|
|
|
}
|
2018-05-24 10:01:24 +08:00
|
|
|
|
return bean;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
2018-05-21 10:07:00 +08:00
|
|
|
|
* 配置状态修改
|
2018-05-21 09:46:49 +08:00
|
|
|
|
* @param params
|
2018-05-21 10:07:00 +08:00
|
|
|
|
* @param type,1表示maat配置取消,2表示回调配置取消
|
2018-05-21 09:46:49 +08:00
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2018-05-28 11:40:58 +08:00
|
|
|
|
public static ToMaatResult put(String params,Integer type) throws MaatConvertException{
|
2018-05-21 10:07:00 +08:00
|
|
|
|
String result = null;
|
2018-05-24 10:01:24 +08:00
|
|
|
|
ToMaatResult bean = null;
|
2018-05-21 10:07:00 +08:00
|
|
|
|
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);
|
2018-05-29 15:37:48 +08:00
|
|
|
|
Response response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
|
2018-05-21 10:07:00 +08:00
|
|
|
|
if( response.getStatus() == 200){
|
|
|
|
|
|
result= response.readEntity(String.class);
|
2018-05-24 10:01:24 +08:00
|
|
|
|
JSONObject resObject = JSONObject.fromObject(result);
|
|
|
|
|
|
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
2018-05-21 10:07:00 +08:00
|
|
|
|
}else{
|
2018-05-21 14:10:11 +08:00
|
|
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
2018-05-21 10:07:00 +08:00
|
|
|
|
}
|
2018-05-24 10:01:24 +08:00
|
|
|
|
return bean;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 信息获取
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2018-05-28 11:40:58 +08:00
|
|
|
|
public static ToMaatResult getFileDigest(String params,File file,Map<String, Object> fileDesc) throws MaatConvertException{
|
2018-05-24 10:01:24 +08:00
|
|
|
|
String result = null;
|
|
|
|
|
|
ToMaatResult bean = null;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
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",ClientUtil.formatFileDesc(fileDesc) );
|
|
|
|
|
|
Response response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
|
|
|
|
|
|
if( response.getStatus() == 200){
|
|
|
|
|
|
result= response.readEntity(String.class);
|
2018-05-24 10:01:24 +08:00
|
|
|
|
JSONObject resObject = JSONObject.fromObject(result);
|
|
|
|
|
|
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}else{
|
2018-05-21 14:10:11 +08:00
|
|
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
2018-05-24 10:01:24 +08:00
|
|
|
|
return bean;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
try {
|
2018-05-23 19:01:57 +08:00
|
|
|
|
getId(1,1);
|
2018-05-21 09:46:49 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|