SuperMap-iServer过滤请求返回值

目的:

iServer发布的arcgis地图服务中,由于tileinfo参数为null,导致用arcgis-ios客户端开发的APP闪退。通过过滤器将get请求的返回值修改

代码:

 1 package com.channel.filter;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.IOException;
 5 
 6 import javax.servlet.ServletOutputStream;
 7 import javax.servlet.WriteListener;
 8 import javax.servlet.http.HttpServletResponse;
 9 import javax.servlet.http.HttpServletResponseWrapper;
10 
11 /**
12  * 返回值输出代理类
13  * 
14  * @Title: ResponseWrapper
15  * @Description:
16  * @author kokJuis
17  * @date 上午9:52:11
18  */
19 public class ResponseWrapper  extends HttpServletResponseWrapper {
20 
21     private ByteArrayOutputStream buffer;
22 
23     private ServletOutputStream out;
24 
25     public ResponseWrapper(HttpServletResponse httpServletResponse) {
26         super(httpServletResponse);
27         buffer = new ByteArrayOutputStream();
28         out = new WrapperOutputStream(buffer);
29     }
30 
31     @Override
32     public ServletOutputStream getOutputStream() throws IOException {
33         return out;
34     }
35 
36     @Override
37     public void flushBuffer() throws IOException {
38         if (out != null) {
39             out.flush();
40         }
41     }
42 
43     public byte[] getContent() throws IOException {
44         flushBuffer();
45         return buffer.toByteArray();
46     }
47 
48     class WrapperOutputStream extends ServletOutputStream {
49         private ByteArrayOutputStream bos;
50 
51         public WrapperOutputStream(ByteArrayOutputStream bos) {
52             this.bos = bos;
53         }
54 
55         @Override
56         public void write(int b) throws IOException {
57             bos.write(b);
58         }
59 
60         @Override
61         public boolean isReady() {
62 
63             // TODO Auto-generated method stub
64             return false;
65 
66         }
67 
68         @Override
69         public void setWriteListener(WriteListener arg0) {
70 
71             // TODO Auto-generated method stub
72 
73         }
74     }
75 
76 }
View Code

在dofilter中实现返回值的替换

 1 package com.channel.filter;
 2 
 3 import java.io.IOException;
 4 import java.io.StringReader;
 5 
 6 import javax.servlet.Filter;
 7 import javax.servlet.FilterChain;
 8 import javax.servlet.FilterConfig;
 9 import javax.servlet.ServletException;
10 import javax.servlet.ServletOutputStream;
11 import javax.servlet.ServletRequest;
12 import javax.servlet.ServletResponse;
13 import javax.servlet.http.HttpServletResponse;
14 
15 import com.alibaba.fastjson.JSON;
16 import com.alibaba.fastjson.JSONObject;
17 
18 /**
19  * 返回值输出过滤器,这里用来加密返回值
20  * 
21  * @Title: ResponseFilter
22  * @Description:
23  * @author kokJuis
24  * @date 上午9:52:42
25  */
26 public class ResponseFilter implements Filter {
27 
28 
29     public void doFilter(ServletRequest request, ServletResponse response,
30             FilterChain filterChain) throws IOException, ServletException {
31         ResponseWrapper wrapperResponse = new ResponseWrapper(
32                 (HttpServletResponse) response);// 转换成代理类
33         // 这里只拦截返回,直接让请求过去,如果在请求前有处理,可以在这里处理
34         filterChain.doFilter(request, wrapperResponse);
35         byte[] content = wrapperResponse.getContent();// 获取返回值
36         // 判断是否有值
37         if (content.length > 0) {
38 
39             String str = new String(content, "UTF-8");
40             System.out.println(str);
41             String ciphertext = null;
42 
43             // 判断下代码中是否包含tileinfo,包含则进行过滤,否则直接跳过响应体
44             if (str.contains("tileInfo")) {
45                 try {
46                     // 获取返回值中tileinfo值,将null替换成{}测试
47                     ciphertext = str.replaceFirst("null", "{}");
48                     System.out.println("修改对应的参数!");
49                 } catch (Exception e) {
50                     e.printStackTrace();
51                 }
52             }
53 
54             // 把返回值输出到客户端
55             ServletOutputStream out = response.getOutputStream();
56             out.write(ciphertext.getBytes());
57             out.flush();
58         }
59     }
60 
61     public void init(FilterConfig arg0) throws ServletException {
62 
63     }
64 
65     public void destroy() {
66 
67     }
68 }
View Code

配置说明:
1、jar包复制到iserver安装目录\webapps\iserver\WEB-INF\lib下
2、修改iserver安装目录\webapps\iserver\WEB-INF的web.xml
3、在web.xml中新增如下配置:



ResponseFilter
com.channel.filter.ResponseFilter


ResponseFilter
/services/map-test2/arcgisrest/test/MapServer
注:换成对应的的服务地址,只过滤指定的服务


4、使用客户端动态出图,得到的返回值中tileinfo有null变为{}

SuperMap-iServer过滤请求返回值_第1张图片

 

转载于:https://www.cnblogs.com/yaohuimo/p/10269821.html

你可能感兴趣的:(SuperMap-iServer过滤请求返回值)