2018-05-21 09:46:49 +08:00
|
|
|
|
package com.nis.util;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
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-21 09:46:49 +08:00
|
|
|
|
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
|
|
|
|
|
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从后台服务获取compileid,groupid,regionid
|
|
|
|
|
|
* @param type 1是compileid,2是groupid,3是regionid
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getId(int type,int num) throws Exception {
|
|
|
|
|
|
String result = null;
|
|
|
|
|
|
String url = "";
|
|
|
|
|
|
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);
|
|
|
|
|
|
}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-21 14:10:11 +08:00
|
|
|
|
return result;
|
2018-05-21 09:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* MAAT配置提交
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String postMaatCfg(String params) throws Exception{
|
|
|
|
|
|
String result = 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);
|
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);
|
|
|
|
|
|
}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
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 回调配置提交
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String postCallbackCfg(String params) throws Exception{
|
|
|
|
|
|
String result = 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);
|
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);
|
|
|
|
|
|
}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
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件配置提交
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2018-05-21 11:22:51 +08:00
|
|
|
|
public static String postFileCfg(String params,File file,JSONObject fileDesc) throws Exception{
|
2018-05-21 09:46:49 +08:00
|
|
|
|
String result = 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);
|
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);
|
|
|
|
|
|
}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
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 配置删除
|
|
|
|
|
|
* @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-21 10:07:00 +08:00
|
|
|
|
public static String delete(String params,Integer type){
|
|
|
|
|
|
String result = 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);
|
|
|
|
|
|
}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-21 09:46:49 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
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-21 10:07:00 +08:00
|
|
|
|
public static String put(String params,Integer type){
|
|
|
|
|
|
String result = 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);
|
|
|
|
|
|
}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-21 09:46:49 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 信息获取
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getFileDigest(String params,File file,Map<String, Object> fileDesc) throws Exception{
|
|
|
|
|
|
String result = 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",ClientUtil.formatFileDesc(fileDesc) );
|
|
|
|
|
|
Response response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
|
|
|
|
|
|
if( response.getStatus() == 200){
|
|
|
|
|
|
result= response.readEntity(String.class);
|
|
|
|
|
|
}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
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
//创建连接
|
|
|
|
|
|
WebTarget wt = ClientUtil.getWebTarger("http://10.0.6.30:8080/maatRest/service/cfg/v1/getCompileId?sourceName=CONFIG_COMPILE&num=1");
|
|
|
|
|
|
//获取响应结果
|
|
|
|
|
|
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
|
|
|
|
|
Response response= header.get();
|
|
|
|
|
|
if( response.getStatus() == 200){
|
|
|
|
|
|
String result= response.readEntity(String.class);
|
|
|
|
|
|
}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
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|