This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/util/ConfigServiceUtil.java

288 lines
9.6 KiB
Java
Raw Normal View History

2018-05-21 09:46:49 +08:00
package com.nis.util;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
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;
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;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.nis.exceptions.MaatConvertException;
2018-05-21 09:46:49 +08:00
import com.nis.util.httpclient.ClientUtil;
public class ConfigServiceUtil {
private static Gson gson = new GsonBuilder()
.registerTypeAdapter(new TypeToken<Map>() {}.getType(), new MapTypeAdapter())
.registerTypeAdapter(new TypeToken<List>() {}.getType(), new MapTypeAdapter())
.create();
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 {
// 序列化无需实现
}
}
2018-05-21 09:46:49 +08:00
/**
* 从后台服务获取compileid,groupid,regionid
* @param type 1是compileid,2是groupid,3是regionid
* @return
*/
public static List<Integer> getId(int type,int num) throws Exception {
2018-05-21 09:46:49 +08:00
String result = null;
String url = "";
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);
Map<String, Object> resMap = gson.fromJson(result,Map.class);
if(!StringUtil.isEmpty(resMap)){
Map<String, Object> dataMap = (Map<String, Object>)resMap.get("data");
if(!StringUtil.isEmpty(dataMap)){
list = (List<Integer>)dataMap.get("pzIdList");
}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{
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
2018-05-21 09:46:49 +08:00
}
return list;
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);
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{
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);
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{
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 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);
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{
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
* @param type,1表示maat配置取消2表示回调配置取消
2018-05-21 09:46:49 +08:00
* @return
*/
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{
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
2018-05-21 09:46:49 +08:00
return result;
}
/**
* 配置状态修改
2018-05-21 09:46:49 +08:00
* @param params
* @param type,1表示maat配置取消2表示回调配置取消
2018-05-21 09:46:49 +08:00
* @return
*/
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{
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 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{
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 {
getId(1,1);
2018-05-21 09:46:49 +08:00
} catch (Exception e) {
e.printStackTrace();
}
}
}