package com.terra.proxymanager.listener;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.alibaba.fastjson.JSONObject;
|
import com.terra.proxymanager.bean.MyCookieStore;
|
import com.terra.proxymanager.controller.GisService;
|
import com.terra.proxymanager.util.HttpUtils;
|
import com.terra.proxymanager.util.PinyinUtils;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.CookieStore;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
import org.apache.http.conn.ssl.SSLContexts;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.util.EntityUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.web.util.CookieGenerator;
|
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.security.KeyManagementException;
|
import java.security.KeyStore;
|
import java.security.KeyStoreException;
|
import java.security.NoSuchAlgorithmException;
|
import java.security.cert.CertificateException;
|
import java.util.*;
|
import java.util.function.Consumer;
|
|
public class GisServiceListener extends AnalysisEventListener<GisService> {
|
@Value("${proxyHost}")
|
private String proxyHost;
|
@Autowired
|
public MyCookieStore myCookieStore;
|
|
public CookieStore store;
|
public SSLConnectionSocketFactory sslsf;
|
public String tgc;
|
private String regurl="http://192.168.20.106:8082/res/ziYuanInsert";
|
|
private String casurl="https://cas.nmsmp.com/cas/login";
|
|
private String muluUrl="http://www.nmsmp.com:8082/res/selectMuLu?id=0";
|
|
private String updateUrl="http://192.168.20.106:8082/res/resExtBaseMap/insertSelectiveAndUpdate";
|
|
private String cookieurl="http://192.168.20.106:8070/manage/ok";
|
|
private String st ="";
|
//不包含子图层的注册服务list
|
public List<GisService> list=new ArrayList<>();
|
//包含子图层的注册服务list
|
public List<GisService> finallist=new ArrayList<>();
|
|
public Map<String,Object> resourceclass=new HashMap<>();
|
|
public GisServiceListener(String t,String jsession) throws ClientProtocolException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException{
|
tgc=t;
|
resourceclass.put("接口服务", "JKFW");
|
resourceclass.put("数据文件","SJWJ" );
|
resourceclass.put("业务集成", "YWJC");
|
resourceclass.put("基础底图", "KJ_JCDT");
|
resourceclass.put("专题地图", "KJ_ZTDT");
|
resourceclass.put("空间分析", "KJ_KJFX");
|
resourceclass.put("业务图层", "KJ_YWTC");
|
resourceclass.put("三维模型", "KJ_SWMX");
|
resourceclass.put("三维地形", "KJ_SWDX");
|
resourceclass.put("三维影像", "KJ_SWYX");
|
DefaultHttpClient cl=new DefaultHttpClient();
|
|
HttpGet httpget=new HttpGet(cookieurl+"?tgc="+tgc+"&jsession="+jsession);
|
|
HttpResponse httprep= cl.execute(httpget);
|
this.store=cl.getCookieStore();
|
KeyStore trustStore=KeyStore.getInstance(KeyStore.getDefaultType());
|
FileInputStream ins=new FileInputStream(new ClassPathResource("cas.keystore").getFile());
|
trustStore.load(ins, "123456".toCharArray());
|
javax.net.ssl.SSLContext sslContext= SSLContexts.custom().loadTrustMaterial(trustStore).build();
|
sslsf=new SSLConnectionSocketFactory(sslContext,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
|
|
}
|
|
|
private Logger log=LoggerFactory.getLogger(GisServiceListener.class);
|
@Override
|
public void invoke(GisService data, AnalysisContext context) {
|
list.add(data);
|
log.info(data.toString());
|
}
|
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext context) {
|
// TODO 调用接口保存数据
|
log.info("调用接口保存数据");
|
registerService(list);
|
}
|
|
/**
|
* 注册服务,保存返回资源id
|
*/
|
public void registerService( List<GisService> list){
|
list.forEach(new Consumer<GisService>(){
|
|
@Override
|
public void accept(GisService t) {
|
List<Map> layers=new ArrayList<>();
|
if(t.getRemark().indexOf("arcgis")!=-1&&!"基础底图".equals(t.getResourceclass())){
|
layers=getSubLayerInfo(t.getRemark());
|
t.setLayers(getSubLayerInfo(t.getRemark()));
|
}
|
|
/* 循环构造子图层并
|
* 调用注册接口
|
*/
|
if(layers.isEmpty()){
|
if(regService(t)){
|
registerMapUrlService(t);
|
}else{
|
log.error("注册服务错误");
|
}
|
}else{
|
layers.forEach(new Consumer<Map>() {
|
|
@Override
|
public void accept(Map map) {
|
GisService cloneObj=new GisService();
|
BeanUtils.copyProperties(t, cloneObj);
|
cloneObj.setTitle(map.get("name").toString());
|
cloneObj.setRemark(t.getRemark()+"/"+map.get("id"));
|
finallist.add(cloneObj);
|
if(regService(cloneObj)){
|
registerMapUrlService(t);
|
}else{
|
log.error("注册服务错误");
|
}
|
}
|
});
|
|
|
}
|
|
|
}
|
|
});
|
|
}
|
|
|
/**
|
* 代理地址,注册入扩展资源
|
*/
|
public boolean registerMapUrlService( GisService t ){
|
//生成代理服务地址,构造扩展对象
|
layersProxy(t);
|
List<Map> list=new ArrayList<>();
|
Map<String,Object> map=new HashMap<>();
|
map.put("typeandurl", "DYNAMIC");
|
map.put("serverurl", t.getProxyUrl());
|
map.put("publishsoft", "Arcgis");
|
map.put("basemaptype","矢量");
|
map.put("extMapUrlStr", "DYNAMIC,"+t.getProxyUrl()+",1|");
|
map.put("ticket", st);
|
JSONObject json=null;
|
try {
|
String res=HttpUtils.post(updateUrl, map);
|
json=JSONObject.parseObject(res);
|
|
} catch (Exception e) {
|
log.error("注册扩展服务失败");
|
e.printStackTrace();
|
}
|
return json.getInteger("result")==1?true:false;
|
//调用更新接口,写入扩展资源表
|
|
|
}
|
|
public List<Map> getSubLayerInfo(String gisUrl){
|
List<Map> list=new ArrayList<>();
|
try {
|
String res=HttpUtils.get(gisUrl+"/layers?f=json");
|
JSONObject json=JSONObject.parseObject(res);
|
list=(List<Map>) json.get("layers");
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return list;
|
|
}
|
|
public Map getMulu(int id,String catlogtitle){
|
|
|
List<Map> list=new ArrayList<>();
|
Map<String,Object> params=new HashMap<>();
|
params.put("id", id);
|
params.put("ticket", st);
|
try {
|
CookieGenerator cg=new CookieGenerator();
|
|
|
|
CloseableHttpClient httpclient=HttpClientBuilder.create().setDefaultCookieStore(store).setSSLSocketFactory(sslsf).build();
|
|
HttpGet httpget=new HttpGet(muluUrl);
|
|
CloseableHttpResponse httprep= httpclient.execute(httpget);
|
HttpEntity entity=httprep.getEntity();
|
System.out.println("----------------------------------------------------");
|
System.out.println(httprep.getStatusLine());
|
String res="";
|
if(entity!=null){
|
System.out.println(EntityUtils.toString(entity));
|
res=EntityUtils.toString(entity);
|
}
|
|
// String res=HttpUtils.get(muluUrl);
|
|
|
list=JSONObject.parseArray(res,Map.class);
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
}
|
for(Map t : list){
|
if(catlogtitle.equals(t.get("name"))){
|
return t;
|
}
|
}
|
return null;
|
|
|
|
}
|
|
//http://www.nmsmp.com:8082/res/selectMuLu
|
public boolean regService(GisService t){
|
List<Map> list=new ArrayList<>();
|
Map<String,Object> map=new HashMap<>();
|
map.put("title", t.getTitle());
|
map.put("abbreviation",PinyinUtils.toHanyuPinyinUpperCase(t.getTitle()) );
|
map.put("resourceclass", resourceclass.get(t.getResourceclass()));
|
Map<String,Object> catalog=getMulu(0,t.getCatalog());
|
map.put("catlogid",catalog.get("id"));
|
map.put("catlogcode", catalog.get("code"));
|
map.put("catlogtitle", catalog.get("name"));
|
map.put("imgurl", "");
|
map.put("datasources", "个人发布");
|
map.put("productiontime", null);
|
map.put("pubunitid", 4);
|
map.put("sharprotocol", "单位内公开");
|
map.put("administrativeid", null);
|
map.put("administrativename", null);
|
map.put("updatetimes", null);
|
map.put("createuserid", 1939);
|
map.put("pubdate", new Date());
|
map.put("txtTags", null);
|
map.put("keywords", t.getKeywords());
|
map.put("securitylev", null);
|
map.put("remark", t.getRemark());
|
map.put("description", t.getDescription());
|
map.put("ticket", st);
|
JSONObject json=null;
|
try {
|
String res=HttpUtils.post(regurl, map);
|
json=JSONObject.parseObject(res);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
}
|
return json.getInteger("result")==1?true:false;
|
|
}
|
|
|
|
|
public void layersProxy(GisService t){
|
Map<String,Object> map=new HashMap<>();
|
map.put("zyid", Integer.valueOf(t.getResourceid()));
|
map.put("serverUrl", t.getRemark());
|
map.put("contextPath", "");
|
map.put("urlid", null);
|
map.put("isPublic", 1);
|
map.put("suffix", 0);
|
map.put("fromsys", "HYJGPT");
|
map.put("servername", t.getTitle());
|
|
String proxyurl= getProxyUrl(map);
|
t.setProxyUrl(proxyurl);
|
|
}
|
|
public String getProxyUrl(Map<String,Object> map){
|
JSONObject json=new JSONObject();
|
try {
|
String res=HttpUtils.get(proxyHost+"/serverRegister/addArcGisServer");
|
json=JSONObject.parseObject(res);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
}
|
return (String) json.get("proxyurl");
|
|
}
|
}
|