1
13693261870
2022-09-16 21ddee1a38257297288b88bc6c0476ec96a5f867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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"); 
             
        }
}