2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package com.landtool.lanbase.modules.terra;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.landtool.lanbase.common.utils.HttpUtils;
import com.landtool.lanbase.common.utils.JpinyinUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
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.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
 
public class GisServiceListener extends AnalysisEventListener<GisService> {
 
    private String proxyHost = "http://192.168.20.106:8066";
 
 
    public CookieStore store;
    public SSLConnectionSocketFactory sslsf;
    public String tgc;
    private String regurl = "http://192.168.20.106:8082/res/ziYuanInsert";
 
    private String casurl = "https://192.168.20.106:8088/cas/login";
 
    private String muluUrl = "http://192.168.20.106:8082/res/selectMuLu?id=0";
 
    private String updateUrl = "http://192.168.20.106:8082/res/resExtBaseMap/insertSelectiveAndUpdate";
 
    private String themeMapUrl = "http://192.168.20.106:8082/res/ResExtThemeMap/insertSelectiveAndUpdate";
 
 
    private String cookieurl = "http://192.168.20.106:8082/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());
                }
 
                /* 循环构造子图层并
                 * 调用注册接口
                 */
                if (layers == null || layers.isEmpty()) {
                    if (regService(t)) {
                        if ("基础底图".equals(t.getResourceclass())) {
                            registerMapUrlService(t);
                        } else {
                            registerthemeMap(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)) {
                                if ("基础底图".equals(t.getResourceclass())) {
                                    registerMapUrlService(cloneObj);
                                } else {
                                    registerthemeMap(cloneObj);
                                }
 
                            } 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());
 
        if (t.getRemark().indexOf("esri/wmts") > -1) {
            map.put("publishsoft", "天地图");
        } else {
            map.put("publishsoft", "Arcgis");
        }
        map.put("basemaptype", "影像");
        map.put("extMapUrlStr", "DYNAMIC," + t.getProxyUrl() + "," + t.getProxyid() + "," + t.getRemark() + "|");
        map.put("xianshibiliStart", "");
        map.put("xianshibiliEnd", "");
        map.put("resourceid", Integer.valueOf(t.getResourceid()));
 
        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 boolean registerthemeMap(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("legendurl", "");
        map.put("extMapUrlStr", "DYNAMIC," + t.getProxyUrl() + "," + t.getProxyid() + "," + t.getRemark() + "|");
        map.put("resourceid", Integer.valueOf(t.getResourceid()));
        map.put("type", "ArcMap");
        map.put("oldserverurl", t.getRemark());
        map.put("serverurl", t.getProxyUrl());
        map.put("espproxy", 1);
        String result = "";
        try {
            result = HttpUtils.post(themeMapUrl, map);
 
 
        } catch (Exception e) {
            log.error("注册专题地图扩展服务失败");
            e.printStackTrace();
        }
        return Integer.valueOf(result) == 1 ? true : false;
        //调用更新接口,写入扩展资源表
 
 
    }
 
 
    public List<Map> getSubLayerInfo(String gisUrl) {
        List<Map> list = new ArrayList<>();
        List<Map> finallist = new ArrayList<>();
        try {
            String res = HttpUtils.get(gisUrl + "/layers?f=json");
            JSONObject json = JSONObject.parseObject(res);
            list = (List<Map>) json.get("layers");
            list.forEach(new Consumer<Map>() {
                @Override
                public void accept(Map t) {
                    if ("Feature Layer".equals(t.get("type"))) {
                        finallist.add(t);
                    }
 
                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return finallist;
 
    }
 
    public Map getMulu(int id, String catlogtitle) {
 
 
        List<Map> list = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
 
        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) {
 
                res = EntityUtils.toString(entity);
                System.out.println(res);
            }
 
            //    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", JpinyinUtils.changeToGetShortPinYin((String) 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);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("pubdate", sdf.format(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 {
 
            CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultCookieStore(store).setSSLSocketFactory(sslsf).build();
 
            HttpPost post = new HttpPost(regurl);
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            map.forEach(new BiConsumer<String, Object>() {
 
                @Override
                public void accept(String t, Object u) {
                    parameters.add(new BasicNameValuePair(t, String.valueOf(u)));
 
                }
            });
            UrlEncodedFormEntity entit = new UrlEncodedFormEntity(parameters, "utf-8");
            post.setEntity(entit);
            CloseableHttpResponse httprep = httpclient.execute(post);
            HttpEntity entity = httprep.getEntity();
            System.out.println("----------------------------------------------------");
            System.out.println(httprep.getStatusLine());
            String res = "";
            if (entity != null) {
 
                res = EntityUtils.toString(entity);
                System.out.println(res);
            }
 
            //    String  res=HttpUtils.get(muluUrl);
 
 
            json = JSONObject.parseObject(res);
            t.setResourceid(json.getString("ziyuanId"));
        } 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());
 
        JSONObject json = getProxyUrl(map);
        t.setProxyUrl(json.getString("proxyurl"));
        t.setProxyid(json.getInteger("id"));
 
    }
 
    public JSONObject getProxyUrl(Map<String, Object> map) {
        JSONObject json = new JSONObject();
        try {
            String res = com.landtool.lanbase.common.utils.HttpUtils.get(proxyHost + "/serverRegister/addArcGisServer", map);
            json = JSONObject.parseObject(res);
 
        } catch (Exception e) {
 
            e.printStackTrace();
        }
        return json;
 
    }
}